“djh”
3 天以前 a0469a082320c33fc19d4e7239b7ddde67945227
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.gkhy.exam.system.service.impl;
 
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.domain.model.LoginUserDetails;
import com.gkhy.exam.common.exception.ApiException;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.common.utils.StringUtils;
import com.gkhy.exam.system.domain.*;
import com.gkhy.exam.system.mapper.CompanyIndustryTemplateMapper;
import com.gkhy.exam.system.mapper.CompanyRosterMapper;
import com.gkhy.exam.system.mapper.SysIndustryTypeMapper;
import com.gkhy.exam.system.service.CompanyIndustryTemplateService;
import com.gkhy.exam.system.service.SysCompanyService;
import com.gkhy.exam.system.service.SysIndustryTypeService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import static cn.hutool.poi.excel.cell.CellUtil.getCellValue;
 
@Service
public class CompanyIndustryTemplateServiceImpl extends ServiceImpl<CompanyIndustryTemplateMapper, CompanyIndustryTemplate> implements CompanyIndustryTemplateService {
 
    @Autowired
    private CompanyIndustryTemplateMapper companyIndustryTemplateMapper;
    @Autowired
    private SysCompanyService sysCompanyService;
 
    @Autowired
    private SysIndustryTypeMapper sysIndustryTypeMapper;
 
 
 
    @Override
    public CommonPage selectCompanyIndustryTemplateList(CompanyIndustryTemplate companyId) {
        boolean admin = SecurityUtils.adminUser();
        if (!admin){
            if (companyId==null){
                throw new ApiException("非管理员操作,查询条件不可为空");
            }
        }
        PageUtils.startPage();
        List<CompanyIndustryTemplate> companyIndustryTemplates = companyIndustryTemplateMapper.selectCompanyIndustryTemplateList(companyId);
        return CommonPage.restPage(companyIndustryTemplates);
    }
 
    @Override
    public CommonResult insertCompanyIndustryTemplate(CompanyIndustryTemplate companyIndustryTemplate) {
        LoginUserDetails loginUser = SecurityUtils.getLoginUser();
 
        SysCompany sysCompany = sysCompanyService.selectCompanyById(companyIndustryTemplate.getCompanyId()==null?SecurityUtils.getCompanyId():companyIndustryTemplate.getCompanyId());
        companyIndustryTemplate.setCompanyName(sysCompany.getName());
        companyIndustryTemplate.setCreateBy(loginUser.getUsername());
        companyIndustryTemplate.setCreateTime(LocalDateTime.now());
        int insert = companyIndustryTemplateMapper.insert(companyIndustryTemplate);
        if (insert>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
 
    @Override
    public CommonResult updateCompanyIndustryTemplate(CompanyIndustryTemplate companyIndustryTemplate) {
        LoginUserDetails loginUser = SecurityUtils.getLoginUser();
        SysCompany sysCompany = sysCompanyService.selectCompanyById(companyIndustryTemplate.getCompanyId()==null?SecurityUtils.getCompanyId():companyIndustryTemplate.getCompanyId());
        companyIndustryTemplate.setCompanyName(sysCompany.getName());
        companyIndustryTemplate.setUpdateBy(loginUser.getUsername());
        companyIndustryTemplate.setUpdateTime(LocalDateTime.now());
        int update = companyIndustryTemplateMapper.updateCompanyIndustryTemplateById(companyIndustryTemplate);
        if (update>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
 
    @Override
    public CommonResult deletedCompanyIndustryTemplate(Integer companyIndustryTemplateId) {
        CompanyIndustryTemplate industryTemplate = new CompanyIndustryTemplate();
        LoginUserDetails loginUser = SecurityUtils.getLoginUser();
        industryTemplate.setUpdateBy(loginUser.getUsername());
        industryTemplate.setUpdateTime(LocalDateTime.now());
        industryTemplate.setDelFlag(1);
        industryTemplate.setId(companyIndustryTemplateId);
        int i = companyIndustryTemplateMapper.updateById(industryTemplate);
        if (i>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
 
    @Override
    public CommonResult uploadQuestion(MultipartFile file) throws IOException {
        List<CompanyIndustryType> companyIndustryTypes = sysIndustryTypeMapper.selectIndustryTypeList();
        List<SysCompany> list = sysCompanyService.selectCompanyLists();
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
 
// 1. 创建列名-索引映射表
        Map<String, Integer> columnIndexMap = new HashMap<>();
        Row headerRow = sheet.getRow(0);
        for (Cell cell : headerRow) {
            String headerName = cell.getStringCellValue().trim();
            columnIndexMap.put(headerName, cell.getColumnIndex());
        }
 
// 验证必要列是否存在
        String[] requiredHeaders = {"章节", "文件记录", "分类", "行业", "企业名称"};
        List<String> missingHeaders = new ArrayList<>();
        for (String header : requiredHeaders) {
            if (!columnIndexMap.containsKey(header)) {
                missingHeaders.add(header);
            }
        }
        if (!missingHeaders.isEmpty()) {
            workbook.close();
            return CommonResult.failed("缺少必要表头: " + String.join(", ", missingHeaders));
        }
        List<CompanyIndustryTemplate> companyIndustryTemplates = new ArrayList<>();
        LoginUserDetails loginUser = SecurityUtils.getLoginUser();
        DataFormatter dataFormatter = new DataFormatter();
        StringBuilder errorBuilder = new StringBuilder();
        // 2. 获取各列的索引
        int chapterIndex = columnIndexMap.get("章节");
        int templateNameIndex = columnIndexMap.get("文件记录");
        int typeIndex = columnIndexMap.get("分类");
        int industryTypeIndex = columnIndexMap.get("行业");
        int companyNameIndex = columnIndexMap.get("企业名称");
        // 3. 从第二行开始处理数据
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (row == null) continue;
 
            CompanyIndustryTemplate template = new CompanyIndustryTemplate();
            template.setCreateBy(loginUser.getUsername());
            template.setCreateTime(LocalDateTime.now());
 
            // 4. 使用动态索引获取单元格
            template.setChapter(getCellValue(row, chapterIndex, dataFormatter));
            template.setTemplateName(getCellValue(row, templateNameIndex, dataFormatter));
            template.setType(getCellValue(row, typeIndex, dataFormatter));
 
            // 5. 处理行业类型映射
            String industryName = getCellValue(row, industryTypeIndex, dataFormatter);
            boolean industryFound = false;
            for (CompanyIndustryType type : companyIndustryTypes) {
                if (industryName.equals(type.getName())) {
                    template.setIndustryType(type.getId());
                    industryFound = true;
                    break;
                }
            }
            if (!industryFound) {
                errorBuilder.append("第").append(i + 1).append("行: 未找到行业[").append(industryName).append("]; ");
            }
 
            // 6. 处理企业映射
            String companyName = getCellValue(row, companyNameIndex, dataFormatter);
            boolean companyFound = false;
            for (SysCompany company : list) {
                if (companyName.equals(company.getName())) {
                    template.setCompanyId(company.getId());
                    companyFound = true;
                    break;
                }
            }
            if (!companyFound) {
                errorBuilder.append("第").append(i + 1).append("行: 未找到企业[").append(companyName).append("]; ");
            }
            if (SecurityUtils.getCompanyId()!=null){
                template.setCompanyId(SecurityUtils.getCompanyId());
            }
 
            companyIndustryTemplates.add(template);
        }
 
        // 7. 错误处理
        if (errorBuilder.length() > 0) {
            workbook.close();
            return CommonResult.failed(errorBuilder.toString());
        }
 
        // 8. 批量插入
        int affectedRows = companyIndustryTemplateMapper.insertIndustrys(companyIndustryTemplates);
        workbook.close();
 
        if (affectedRows < 1) {
            throw new ApiException("导入行业模版失败");
        }
        return CommonResult.success();
    }
        // 安全获取单元格值
        private String getCellValue(Row row, int columnIndex, DataFormatter formatter) {
            if (columnIndex < 0) return "";
            Cell cell = row.getCell(columnIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
            return formatter.formatCellValue(cell).trim();
        }
 
//    List<CompanyIndustryType> companyIndustryTypes = sysIndustryTypeMapper.selectIndustryTypeList();
//    List<SysCompany> list = sysCompanyService.selectCompanyLists();
//    Workbook workbook = WorkbookFactory.create(file.getInputStream());
//    Sheet sheetAt = workbook.getSheetAt(0);
//    List<CompanyIndustryTemplate> companyIndustryTemplates = new ArrayList<>();
//    LoginUserDetails loginUser = SecurityUtils.getLoginUser();
//    DataFormatter dataFormatter = new DataFormatter();
//    StringBuilder stringBuilder = new StringBuilder();
//        for (int i = 0; i <sheetAt.getLastRowNum(); i++) {
//        Row row = sheetAt.getRow(i + 1);
//        CompanyIndustryTemplate companyIndustryTemplate = new CompanyIndustryTemplate();
//        if (row!=null){
//            companyIndustryTemplate.setChapter(dataFormatter.formatCellValue(row.getCell(0)));
//            companyIndustryTemplate.setTemplateName(dataFormatter.formatCellValue(row.getCell(1)));
//            companyIndustryTemplate.setType(dataFormatter.formatCellValue(row.getCell(2)));
//            for (CompanyIndustryType companyIndustryType : companyIndustryTypes) {
//                if (dataFormatter.formatCellValue(row.getCell(3)).equals(companyIndustryType.getName())){
//                    companyIndustryTemplate.setIndustryType(companyIndustryType.getId());
//                }
//            }
//            if (companyIndustryTemplate.getIndustryType()==null){
//                stringBuilder.append("未找到对应行业类型:["+dataFormatter.formatCellValue(row.getCell(3))+"]   ,");
//            }
//            companyIndustryTemplate.setCreateBy(loginUser.getUsername());
//            companyIndustryTemplate.setCreateTime(LocalDateTime.now());
//            for (SysCompany sysCompany : list) {
//                if (dataFormatter.formatCellValue(row.getCell(4)).equals(sysCompany.getName())){
//                    companyIndustryTemplate.setCompanyId(sysCompany.getId());
//                }
//            }
//            if (companyIndustryTemplate.getCompanyId()==null){
//                stringBuilder.append("未找到对应企业:["+dataFormatter.formatCellValue(row.getCell(4))+"]");
//            }
//            companyIndustryTemplates.add(companyIndustryTemplate);
//        }
//    }
//        if (StringUtils.isNotBlank(stringBuilder)){
//        workbook.close();
//        return CommonResult.failed(stringBuilder.toString());
//    }
//    int i = companyIndustryTemplateMapper.insertIndustrys(companyIndustryTemplates);
//        if (i<1){
//        throw new ApiException("导入行业模版失败");
//    }
//        workbook.close();
//        return CommonResult.success();
 
 
}