feat: 新老部门对应关系导入
This commit is contained in:
+3
-8
@@ -43,14 +43,9 @@ public class OldSystemController {
|
||||
@ApiOperation(value = "老法规系统-老部门现部门关系导入", notes = "老法规系统-老部门现部门关系导入")
|
||||
@ApiOperationSupport(order = 1)
|
||||
@AutoLog(value = "老法规系统-老部门现部门关系导入")
|
||||
public Result<?> importExcel(@ApiParam(value = "要上传的文件", required = true) @RequestParam("file") MultipartFile file) {
|
||||
Map<String, Object> resultMap = oldSystemService.importData(file);
|
||||
Object flag = resultMap.get("flag");
|
||||
if (flag == YesOrNoEnum.YES.getValue()) {
|
||||
return Result.OK(resultMap.get("dataList"));
|
||||
} else {
|
||||
return Result.error("导入失败", resultMap.get("msg"));
|
||||
}
|
||||
public void importExcel(@ApiParam(value = "要上传的文件", required = true) @RequestParam("file") MultipartFile file,
|
||||
HttpServletResponse response) {
|
||||
oldSystemService.importData(file, response);
|
||||
}
|
||||
|
||||
@Resource
|
||||
|
||||
+7
@@ -75,6 +75,13 @@ public class OldSystemDepartRelation implements Serializable {
|
||||
@ApiModelProperty(value = "现上级组织Id")
|
||||
private String curOrgSuperiorId;
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
@Excel(name = "错误信息", width = 35)
|
||||
@ApiModelProperty(value = "错误信息")
|
||||
private String errorInfo;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ import com.jero.modules.laws.oldSystem.entity.OldSystemDepartRelation;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -13,5 +14,5 @@ import java.util.Map;
|
||||
*/
|
||||
public interface OldSystemDepartRelationService extends IService<OldSystemDepartRelation> {
|
||||
|
||||
Map<String, Object> importData(MultipartFile file);
|
||||
void importData(MultipartFile file, HttpServletResponse response);
|
||||
}
|
||||
|
||||
+91
-13
@@ -2,49 +2,59 @@ package com.jero.modules.laws.oldSystem.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.jero.common.api.vo.ResultCommon;
|
||||
import com.jero.common.constant.CommonConstant;
|
||||
import com.jero.common.constant.enums.YesOrNoEnum;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.system.vo.SysDictItemCore;
|
||||
import com.jero.common.util.MessageUtils;
|
||||
import com.jero.modules.activiti.process.esAnnualInit.entity.ProcessEsAnnualInit;
|
||||
import com.jero.modules.activiti.util.ExcelUtils;
|
||||
import com.jero.modules.laws.oldSystem.entity.ESAuthDept;
|
||||
import com.jero.modules.laws.oldSystem.entity.OldSystemDepartRelation;
|
||||
import com.jero.modules.laws.oldSystem.service.OldSystemDepartRelationService;
|
||||
import com.jero.modules.laws.oldSystem.mapper.OldSystemDepartRelationMapper;
|
||||
import com.jero.modules.laws.standard.service.ILawsEnterpriseStandardService;
|
||||
import com.jero.modules.system.entity.SysDepart;
|
||||
import com.jero.modules.system.service.ISysDepartService;
|
||||
import lombok.Cleanup;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.jeecgframework.poi.excel.ExcelExportUtil;
|
||||
import org.jeecgframework.poi.excel.ExcelImportUtil;
|
||||
import org.jeecgframework.poi.excel.entity.ExportParams;
|
||||
import org.jeecgframework.poi.excel.entity.ImportParams;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author ThinkBook
|
||||
* @description 针对表【old_system_depart_relation】的数据库操作Service实现
|
||||
* @createDate 2023-11-10 14:17:20
|
||||
*/
|
||||
* @author ThinkBook
|
||||
* @description 针对表【old_system_depart_relation】的数据库操作Service实现
|
||||
* @createDate 2023-11-10 14:17:20
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional(rollbackFor = JeroBootException.class)
|
||||
public class OldSystemDepartRelationServiceImpl extends ServiceImpl<OldSystemDepartRelationMapper, OldSystemDepartRelation>
|
||||
implements OldSystemDepartRelationService {
|
||||
implements OldSystemDepartRelationService {
|
||||
|
||||
@Resource
|
||||
private ISysDepartService sysDepartService;
|
||||
|
||||
|
||||
@Resource
|
||||
private ILawsEnterpriseStandardService enterpriseStandardService;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> importData(MultipartFile file) {
|
||||
Map<String, Object> resultMap = new HashMap<>();
|
||||
List<String> errorMsgs = new ArrayList<>();
|
||||
public void importData(MultipartFile file, HttpServletResponse response) {
|
||||
List<OldSystemDepartRelation> errImportList = new ArrayList<>();
|
||||
|
||||
ImportParams params = new ImportParams();
|
||||
params.setTitleRows(0);
|
||||
@@ -55,15 +65,83 @@ public class OldSystemDepartRelationServiceImpl extends ServiceImpl<OldSystemDep
|
||||
@Cleanup
|
||||
InputStream inputStream = file.getInputStream();
|
||||
List<OldSystemDepartRelation> list = ExcelImportUtil.importExcel(inputStream, OldSystemDepartRelation.class, params);
|
||||
|
||||
// 删除空行
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
OldSystemDepartRelation data = list.get(i);
|
||||
if (data.isEmptyRow()) {
|
||||
list.remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有有关联的新部门
|
||||
LambdaQueryWrapper<SysDepart> sysDeptWrapper = new LambdaQueryWrapper<>();
|
||||
sysDeptWrapper.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
|
||||
sysDeptWrapper.eq(SysDepart::getStatus, 1);
|
||||
List<SysDepart> curDeptList = sysDepartService.list(sysDeptWrapper);
|
||||
Map<String, List<SysDepart>> curDeptNameMap = curDeptList.stream().collect(Collectors.groupingBy(SysDepart::getDepartName));
|
||||
Map<String, SysDepart> curDeptIdMap = curDeptList.stream().collect(Collectors.toMap(SysDepart::getId, o -> o));
|
||||
|
||||
// 每个部门名称都查一下是否和现部门匹配
|
||||
list = list.stream().filter(dr -> {
|
||||
List<SysDepart> sysDeparts = curDeptNameMap.get(dr.getCurOrgName());
|
||||
if (sysDeparts == null || sysDeparts.isEmpty()) {
|
||||
dr.setErrorInfo("系统中未找到同名的现部门");
|
||||
errImportList.add(dr);
|
||||
return false;
|
||||
} else {
|
||||
if (sysDeparts.size() > 1) {
|
||||
// 根据上级部门名称再次匹配
|
||||
for (SysDepart sysDepart : sysDeparts) {
|
||||
String parentDeptName = curDeptIdMap.get(sysDepart.getParentId()).getDepartName();
|
||||
if (parentDeptName.equals(dr.getCurOrgSuperior())) {
|
||||
dr.setCurOrgId(sysDepart.getId());
|
||||
dr.setCurOrgSuperiorId(sysDepart.getParentId());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
dr.setErrorInfo("匹配到多个同名部门,但无法通过上级部门名称确定具体部门");
|
||||
errImportList.add(dr);
|
||||
return false;
|
||||
} else {
|
||||
SysDepart sysDepart = sysDeparts.get(0);
|
||||
dr.setCurOrgId(sysDepart.getId());
|
||||
dr.setCurOrgSuperiorId(sysDepart.getParentId());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
saveBatch(list, list.size());
|
||||
} catch (Exception e) {
|
||||
errorMsgs.add("文件导入异常:" + e.getMessage());
|
||||
resultMap.put("msg", errorMsgs);
|
||||
resultMap.put("flag", YesOrNoEnum.NO.getValue());
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return resultMap;
|
||||
if (!errImportList.isEmpty()) {
|
||||
log.info("导入失败数据条数:{}", errImportList.size());
|
||||
this.exportWithExcel(response, errImportList, "DeptRelationErrorData.xls");
|
||||
}
|
||||
log.info("企标新老部门对应关系导入结束");
|
||||
}
|
||||
|
||||
public <T> void exportWithExcel(HttpServletResponse response, List<T> dtoList, String fileName) {
|
||||
try {
|
||||
// 设置导出参数
|
||||
ExportParams exportParams = new ExportParams(null, fileName); // 可以为标题添加更多的配置
|
||||
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, dtoList.get(0).getClass(), dtoList);
|
||||
|
||||
// 设置响应头和内容类型
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
||||
|
||||
// 写出到响应流
|
||||
workbook.write(response.getOutputStream());
|
||||
workbook.close();
|
||||
} catch (Exception e) {
|
||||
log.error("导出excel异常", e);
|
||||
throw new JeroBootException(MessageUtils.getMessage(ResultCommon.EXPORT_ERROR), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
+44
-36
@@ -117,6 +117,9 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
|
||||
|
||||
@Resource
|
||||
private ISysDepartService sysDepartService;
|
||||
|
||||
@Resource
|
||||
private OldSystemDepartRelationService oldSystemDepartRelationService;
|
||||
|
||||
@Override
|
||||
public LawsEnterpriseStandard queryByStandardNumber(String standardNumber) {
|
||||
@@ -393,6 +396,7 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
|
||||
|
||||
@Override
|
||||
public void importOldESAuthDeptExcel(MultipartFile file, HttpServletResponse response) {
|
||||
List<ESAuthDept> errImportList = new ArrayList<>();
|
||||
// 验证文件是否为压缩格式file.getContentType()).contains("xls")
|
||||
if (!Objects.requireNonNull(file.getOriginalFilename()).contains("xls")) {
|
||||
throw new JeroBootException("文件格式不正确,请上传Excel。");
|
||||
@@ -417,7 +421,12 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
|
||||
i--;
|
||||
}
|
||||
}
|
||||
log.info("获取数据成功,数据条数:{}", datalist.size());
|
||||
// 区分数据
|
||||
log.info("获取数据成功,总数据条数:{}", datalist.size());
|
||||
// 删选标准分类不为空且是企标的数据
|
||||
datalist = datalist.stream().filter(data -> StrUtil.isNotBlank(data.getStandardType()))
|
||||
.filter(data -> data.getStandardType().startsWith("企业标准")).collect(Collectors.toList());
|
||||
log.info("企标相关数据条数:{}", datalist.size());
|
||||
// 获取当前时间戳(毫秒)
|
||||
long endTime = System.currentTimeMillis();
|
||||
// 计算执行时间(毫秒)
|
||||
@@ -428,52 +437,46 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
|
||||
LambdaQueryWrapper<LawsEnterpriseStandard> esWrapper = new LambdaQueryWrapper<>();
|
||||
List<LawsEnterpriseStandard> esList = this.list(esWrapper.isNotNull(LawsEnterpriseStandard::getOldSystemId));
|
||||
Map<String, LawsEnterpriseStandard> esMap = esList.stream().collect(Collectors.toMap(LawsEnterpriseStandard::getOldSystemId, o -> o));
|
||||
Map<String, List<ESAuthDept>> auMap = datalist.stream().collect(Collectors.groupingBy(ESAuthDept::getStandardId));
|
||||
|
||||
// 将Excel中涉及到的老部门名称和老上级部门名称找出来
|
||||
Set<String> oldDeptNameList = new HashSet<>();
|
||||
for (ESAuthDept ad : datalist) {
|
||||
oldDeptNameList.add(ad.getDeptName());
|
||||
oldDeptNameList.add(ad.getHigherDeptName());
|
||||
}
|
||||
|
||||
// 根据老部门名称获取新部门Id
|
||||
LambdaQueryWrapper<OldSystemDepartRelation> oldDeptWrapper = new LambdaQueryWrapper<>();
|
||||
oldDeptWrapper.in(OldSystemDepartRelation::getOldOrgName, oldDeptNameList);
|
||||
List<OldSystemDepartRelation> oldDeptList = oldDeptService.list(oldDeptWrapper);
|
||||
|
||||
|
||||
// 获取所有有关联的新部门
|
||||
LambdaQueryWrapper<SysDepart> sysDeptWrapper = new LambdaQueryWrapper<>();
|
||||
sysDeptWrapper.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
|
||||
sysDeptWrapper.eq(SysDepart::getStatus, 1);
|
||||
List<SysDepart> curDeptList = sysDepartService.list(sysDeptWrapper);
|
||||
Map<String, List<SysDepart>> curDeptNameMap = curDeptList.stream().collect(Collectors.groupingBy(SysDepart::getDepartName));
|
||||
Map<String, SysDepart> curDeptIdMap = curDeptList.stream().collect(Collectors.toMap(SysDepart::getId, o -> o));
|
||||
curDeptList.add(sysDepartService.getById("0"));
|
||||
Map<String, SysDepart> allCurDeptMap = curDeptList.stream().collect(Collectors.toMap(SysDepart::getId, o -> o));
|
||||
|
||||
Map<String, SysDepart> curDeptNameMap = new HashMap<>();
|
||||
for (SysDepart d : curDeptList) {
|
||||
if (StrUtil.isNotBlank(d.getParentId())) {
|
||||
String departName = d.getDepartName();
|
||||
SysDepart sysDepart = allCurDeptMap.get(d.getParentId());
|
||||
if (sysDepart != null) {
|
||||
departName = departName + "-" + sysDepart.getDepartName();
|
||||
}
|
||||
curDeptNameMap.put(departName, d);
|
||||
}
|
||||
}
|
||||
List<OldSystemDepartRelation> drList = oldSystemDepartRelationService.list();
|
||||
Map<String, OldSystemDepartRelation> drMap = drList.stream().collect(Collectors.toMap(o -> o.getOldOrgName() + "-" + o.getOldOrgSuperior(), o -> o));
|
||||
|
||||
for (ESAuthDept ad : datalist) {
|
||||
try {
|
||||
EnterpriseStandardAuthDept authDept = new EnterpriseStandardAuthDept();
|
||||
authDept.setStandardId(esMap.get(ad.getStandardId()).getId());
|
||||
String curDeptId = curDeptNameMap.get(ad.getDeptName() + "-" + ad.getHigherDeptName()).getId();
|
||||
if (StrUtil.isBlank(curDeptId)) {
|
||||
log.error("导入部门对应出现错误:{}", "找不到对应的新部门");
|
||||
log.error("错误数据:{}", ad.toString());
|
||||
continue;
|
||||
String oldDeptName = ad.getDeptName();
|
||||
String oldHigherDeptName = ad.getHigherDeptName();
|
||||
// 根据老部门名称从关系表中找到新部门名称
|
||||
OldSystemDepartRelation oldSystemDepartRelation = drMap.get(oldDeptName + "-" + oldHigherDeptName);
|
||||
if (oldSystemDepartRelation == null) {
|
||||
throw new JeroBootException("未找到该部门的现部门名称:" + oldDeptName + ",上级部门名称:" + oldHigherDeptName + ",标准编号:" + ad.getStandardNumber());
|
||||
} else {
|
||||
// 通过现部门名称找到对应的新部门
|
||||
List<SysDepart> curDepts = curDeptNameMap.get(oldSystemDepartRelation.getCurOrgName());
|
||||
if (curDepts == null || curDepts.isEmpty()) {
|
||||
throw new JeroBootException("未找到该部门,部门名称:" + oldDeptName + ",上级部门名称:" + oldHigherDeptName + ",标准编号:" + ad.getStandardNumber());
|
||||
}
|
||||
}
|
||||
authDept.setAuthDept(curDeptId);
|
||||
esAuthDeptService.save(authDept);
|
||||
// 如果只有一个则找到
|
||||
// List<String> deptNameList = "";
|
||||
// String curDeptId = curDeptNameMap.get(ad.getDeptName() + "-" + ad.getHigherDeptName()).getId();
|
||||
// if (StrUtil.isBlank(curDeptId)) {
|
||||
// log.error("导入部门对应出现错误:{}", "找不到对应的新部门");
|
||||
// log.error("错误数据:{}", ad.toString());
|
||||
// continue;
|
||||
// }
|
||||
// EnterpriseStandardAuthDept authDept = new EnterpriseStandardAuthDept();
|
||||
// authDept.setStandardId(esMap.get(ad.getStandardId()).getId());
|
||||
// authDept.setAuthDept(curDeptId);
|
||||
// esAuthDeptService.save(authDept);
|
||||
} catch (Exception e) {
|
||||
log.error("导入部门对应出现错误:{}", e.getMessage());
|
||||
log.error("错误数据:{}", ad.toString());
|
||||
@@ -482,6 +485,11 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
|
||||
} catch (Exception e) {
|
||||
log.error("导入失败;{}", e.getMessage());
|
||||
}
|
||||
if (!errImportList.isEmpty()) {
|
||||
log.info("导入失败数据条数:{}", errImportList.size());
|
||||
this.exportWithExcel(response, errImportList, "ESAuthDeptErrorData.xls");
|
||||
}
|
||||
log.info("企标部门权限导入结束");
|
||||
}
|
||||
|
||||
public <T> void exportWithExcel(HttpServletResponse response, List<T> dtoList, String fileName) {
|
||||
|
||||
Reference in New Issue
Block a user