郑永安
2023-06-19 451e341df739f387201914b67323efa1c4f4c8d3
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
package com.ruoyi.system.web;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.company.AreaInfo;
import com.ruoyi.system.domain.company.CompanyInfo;
import com.ruoyi.system.domain.dto.CompanyInfoDto;
import com.ruoyi.system.domain.vo.CompanyInfoVO;
import com.ruoyi.system.service.company.IAreaInfoService;
import com.ruoyi.system.service.company.ICompanyInfoService;
import com.ruoyi.system.utils.MyServerConfig;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
@RestController
public class CompanyInfoController extends BaseController {
 
    @Autowired
    private ICompanyInfoService companyInfoService;
 
    @Autowired
    private MyServerConfig serverConfig;
 
    @Autowired
    private IAreaInfoService areaInfoService;
 
 
    @RequestMapping("/companyInfo/svarInfo")
    public AjaxResult savaInfo(CompanyInfoVO companyInfo, MultipartFile companyImageFile,
                               MultipartFile businessLicenseFile, MultipartFile securityCertificateFile) {
        if (!checkIsExit(Collections.singletonList(companyInfo.getCertificateNum()), null)) {
            return AjaxResult.error("数据已存在,请检查证书编号是否填写正确!");
        }
        initFileInfo(companyInfo, companyImageFile, businessLicenseFile, securityCertificateFile);
        companyInfo.setCreateTime(new Date());
        companyInfo.setCreateBy(getUsername());
        companyInfoService.save(companyInfo);
        return AjaxResult.success("添加成功!");
    }
 
    @RequestMapping("/companyInfo/updateInfo")
    public AjaxResult updateInfo(CompanyInfoVO companyInfo, MultipartFile companyImageFile,
                                 MultipartFile businessLicenseFile, MultipartFile securityCertificateFile) {
 
        if (!checkIsExit(Collections.singletonList(companyInfo.getCertificateNum()), Collections.singletonList(companyInfo.getId()))) {
            return AjaxResult.error("数据已存在,请检查证书编号是否填写正确!");
        }
        initFileInfo(companyInfo, companyImageFile, businessLicenseFile, securityCertificateFile);
        companyInfo.setUpdateTime(new Date());
        companyInfo.setUpdateBy(getUsername());
        companyInfoService.updateById(companyInfo);
        return AjaxResult.success("修改成功!");
    }
 
    @RequestMapping("/companyInfo/deleteById")
    public AjaxResult deleteById(@RequestBody CompanyInfo companyInfo) {
        companyInfo.setId(Long.valueOf(companyInfo.getIdStr()));
        companyInfo.setIsDelete(1);
        companyInfo.setUpdateTime(new Date());
        companyInfo.setUpdateBy(getUsername());
        companyInfoService.updateById(companyInfo);
        return AjaxResult.success("删除成功!");
    }
 
 
    @RequestMapping("/companyInfo/findCompanyInfo")
    public AjaxResult findCompanyInfo(@RequestBody CompanyInfoVO companyInfoVO) {
        return companyInfoService.findCompanyInfoPage(companyInfoVO);
    }
 
    @RequestMapping("/companyInfo/getOne")
    public AjaxResult getOne(@RequestBody CompanyInfoVO companyInfoVO) {
        if (companyInfoVO.getId() != null) {
            CompanyInfo companyInfo = companyInfoService.getById(companyInfoVO.getId());
            if(companyInfo!=null){
                List<CompanyInfoDto> companyInfoDtoList =  companyInfoService.initCompanyCity(Collections.singletonList(companyInfo));
                return AjaxResult.success(companyInfoDtoList.get(0));
            }else{
                return AjaxResult.error("数据不存在!");
            }
 
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq(CompanyInfo.IS_DELETE, 0);
            queryWrapper.orderByDesc(CompanyInfo.CREATE_TIME);
            queryWrapper.last("limit 1");
            return AjaxResult.success(companyInfoService.getOne(queryWrapper));
        }
    }
 
    @PostMapping("/companyInfo/export")
    public void export(HttpServletResponse response, @RequestBody CompanyInfoVO companyInfoVO) {
        List<CompanyInfoDto> list = companyInfoService.findExportData(companyInfoVO);
        ExcelUtil<CompanyInfoDto> util = new ExcelUtil<>(CompanyInfoDto.class);
        util.exportExcel(response, list, "企业数据");
    }
 
    @PostMapping("/companyInfo/downLoadTemplate")
    public void downLoadTemplate(HttpServletResponse response, @RequestBody CompanyInfoVO companyInfoVO) {
        List<CompanyInfoDto> list = new ArrayList<>();
        ExcelUtil<CompanyInfoDto> util = new ExcelUtil<>(CompanyInfoDto.class);
        util.exportExcel(response, list, "导入模板");
    }
 
    @RequestMapping("/companyInfo/importData")
    public AjaxResult importData(MultipartFile file) {
        List<AreaInfo> areaInfoList =  areaInfoService.list();
        Map<String,List<AreaInfo>> areaMap =  areaInfoList.stream().collect(Collectors.groupingBy(o->o.getCity()+o.getArea()));
        ExcelUtil<CompanyInfo> util = new ExcelUtil<CompanyInfo>(CompanyInfo.class);
        List<CompanyInfo> companyInfos = null;
        List<String> importFlaseData = new ArrayList<>();
        try {
            Date time = new Date();
            companyInfos = util.importExcel(file.getInputStream());
            final Integer[] companyCityIsEmpty = {0};
            companyInfos.forEach(companyInfo -> {
                companyInfo.setCreateTime(time);
                companyInfo.setCreateBy(getUsername());
                if(CollectionUtils.isNotEmpty(areaMap.get(companyInfo.getCity()+companyInfo.getArea()))){
                    companyInfo.setCompanyCity(areaMap.get(companyInfo.getCity()+companyInfo.getArea()).get(0).getId());
                }else{
                    companyCityIsEmpty[0] = 1;
                }
            });
            if(companyCityIsEmpty[0] == 1){
                return AjaxResult.error("导入失败!所在地州市有误,请检查修改后重试!");
            }
            //获取证书编号集合
            List<String> companyNumList = companyInfos.stream()
                    .filter(companyInfo -> StringUtils.isNotEmpty(companyInfo.getCertificateNum()))
                    .map(CompanyInfo::getCertificateNum).collect(Collectors.toList());
            //去重判断是否重复
            Set<String> companySet = companyNumList.stream().collect(Collectors.toSet());
 
            if (companySet.size() != companyNumList.size() || !checkIsExit(companyNumList, null)) {
                return AjaxResult.error("导入失败!数据重复,请检查导入数据!");
            }
 
            //过滤出不完整的数据
            Iterator<CompanyInfo> iterator = companyInfos.iterator();
            while (iterator.hasNext()) {
                CompanyInfo companyInfo = iterator.next();
                if (StringUtils.isEmpty(companyInfo.getCompanyName()) || StringUtils.isEmpty(companyInfo.getCompanyDirector())
                        || StringUtils.isEmpty(companyInfo.getCompanyDirector()) || StringUtils.isEmpty(companyInfo.getCertificateNum())
                        || StringUtils.isEmpty(companyInfo.getCompanyCity())) {
                    importFlaseData.add(companyInfo.getCompanyName());
                    iterator.remove();
                }
            }
 
            if (StringUtils.isNotEmpty(companyInfos)) {
                companyInfoService.saveBatch(companyInfos);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("导入失败,请检查导入文件格式是否正确");
        }
        List<String> successIds = companyInfos.stream()
                .map(CompanyInfo::getIdStr).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(importFlaseData)) {
            return AjaxResult.success("导入成功,成功插入" + companyInfos.size() + "条数据", successIds);
        } else {
            return AjaxResult.success("导入成功,成功插入" + companyInfos.size() + "条数据," + importFlaseData + "数据不完整,请修改后重新导入!", successIds);
        }
    }
 
    public boolean checkIsExit(List<String> companyNum, List<Long> idList) {
        if (CollectionUtils.isNotEmpty(companyNum)) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in(CompanyInfo.CERTIFICATE_NUM, companyNum);
            queryWrapper.eq(CompanyInfo.IS_DELETE, 0);
            //如果idList不为空,则是修改方法,判断重复去除本事数据
            if (CollectionUtils.isNotEmpty(idList)) {
                queryWrapper.notIn(CompanyInfo.ID, idList);
            }
            List<CompanyInfo> companyInfoList = companyInfoService.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(companyInfoList)) {
                return false;
            }
        }
        return true;
    }
 
    public void initFileInfo(CompanyInfoVO companyInfo, MultipartFile companyImage,
                             MultipartFile businessLicense, MultipartFile securityCertificate) {
 
        if (BooleanUtils.isTrue(companyInfo.getDeleteCompanyImage())) {
            companyInfo.setCompanyImage("");
        }
        if (BooleanUtils.isTrue(companyInfo.getDeleteBusinessLicense())) {
            companyInfo.setBusinessLicense("");
        }
        if (BooleanUtils.isTrue(companyInfo.getDeleteSecurityCertificate())) {
            companyInfo.setSecurityCertificate("");
        }
 
        if (companyImage != null) {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = null;
            try {
                fileName = FileUploadUtils.upload(filePath, companyImage);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            companyInfo.setCompanyImage(fileName);
        }
 
        if (businessLicense != null) {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = null;
            try {
                fileName = FileUploadUtils.upload(filePath, businessLicense);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            companyInfo.setBusinessLicense(fileName);
        }
 
        if (securityCertificate != null) {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = null;
            try {
                fileName = FileUploadUtils.upload(filePath, securityCertificate);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            companyInfo.setSecurityCertificate(fileName);
        }
    }
}