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);
|
}
|
}
|
}
|