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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user