|
|
@@ -1,184 +1,114 @@
|
|
|
package org.sky.scientific.excel.listener;
|
|
|
|
|
|
import com.alibaba.excel.context.AnalysisContext;
|
|
|
-import com.alibaba.excel.read.listener.ReadListener;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.sky.core.tool.utils.DateUtil;
|
|
|
+import org.sky.core.tool.utils.Func;
|
|
|
import org.sky.scientific.excel.TechnicianExcel;
|
|
|
+import org.sky.scientific.utils.ImportResult;
|
|
|
+import org.sky.scientific.utils.ValidationUtils;
|
|
|
+import org.sky.system.cache.DictCache;
|
|
|
+import org.sky.system.pojo.entity.Dict;
|
|
|
+import org.sky.system.pojo.enums.DictEnum;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
-public class TechnicianImportListener implements ReadListener<Map<Integer, String>> {
|
|
|
-
|
|
|
-// private static final int BATCH_COUNT = 100;
|
|
|
-// private List<TechnicianEntity> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
|
|
|
-// private List<ImportError> errorList = new ArrayList<>();
|
|
|
-// private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
|
|
-//
|
|
|
-// List<String> identifierList = null;
|
|
|
-//
|
|
|
-// private final ITechnicianService technicianService;
|
|
|
-// private final TechnicianMapper technicianMapper;
|
|
|
-// private final DepartmentMapper departmentMapper;
|
|
|
-// private final String yearAndMonth;
|
|
|
-
|
|
|
-// public TechnicianImportListener(ITechnicianService technicianService, TechnicianMapper technicianMapper, String yearAndMonth, DepartmentMapper departmentMapper) {
|
|
|
-// this.technicianService = technicianService;
|
|
|
-// this.technicianMapper = technicianMapper;
|
|
|
-// this.yearAndMonth = yearAndMonth;
|
|
|
-// this.departmentMapper = departmentMapper;
|
|
|
-//
|
|
|
-// List<TechnicianEntity> dbTechnicianList = technicianMapper.selectList(Wrappers.<TechnicianEntity>lambdaQuery().eq(TechnicianEntity::getYearAndMonth, yearAndMonth));
|
|
|
-// identifierList = new ArrayList<>(dbTechnicianList.stream().map(tmp -> tmp.getNumber() + ":" + tmp.getIdCard()).toList());
|
|
|
-// }
|
|
|
+public class TechnicianImportListener extends BaseReadListener {
|
|
|
|
|
|
@Override
|
|
|
- public void invoke(Map<Integer, String> data, AnalysisContext context) {
|
|
|
- log.info("解析到一条数据:{}", data);
|
|
|
- // 获取当前行号
|
|
|
-// Integer rowIndex = context.readRowHolder().getRowIndex() + 1;
|
|
|
-// // 数据验证
|
|
|
-// Set<ConstraintViolation<TechnicianExcel>> violations = validator.validate(data);
|
|
|
-// if (!violations.isEmpty()) {
|
|
|
-// // 收集错误信息
|
|
|
-// for (ConstraintViolation<TechnicianExcel> violation : violations) {
|
|
|
-// errorList.add(new ImportError(rowIndex, violation.getMessage()));
|
|
|
-// }
|
|
|
-// return;
|
|
|
-// }
|
|
|
-//
|
|
|
-// validateBusiness(data, rowIndex);
|
|
|
-//
|
|
|
-//// cachedDataList.add(data);
|
|
|
-//
|
|
|
-// // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
|
|
|
-// if (cachedDataList.size() >= BATCH_COUNT) {
|
|
|
-// technicianService.saveBatch(cachedDataList);
|
|
|
-// cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
|
|
|
-// }
|
|
|
- }
|
|
|
+ public void invoke(Map<Integer, String> map, AnalysisContext context) {
|
|
|
+ int rowIndex = context.readRowHolder().getRowIndex() + 1;
|
|
|
|
|
|
- private void validateBusiness(TechnicianExcel excel, Integer rowIndex) {
|
|
|
- // 示例:验证部门是否存在
|
|
|
-// if (!importService.isDepartmentValid(data.getDepartment())) {
|
|
|
-// errorList.add(new ImportError(rowIndex, "部门不存在: " + data.getDepartment()));
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 示例:验证工资范围
|
|
|
-// if (data.getSalary().compareTo(new BigDecimal("1000000")) > 0) {
|
|
|
-// errorList.add(new ImportError(rowIndex, "工资不能超过1000000"));
|
|
|
-// }
|
|
|
-
|
|
|
-// TechnicianEntity technician = Objects.requireNonNull(BeanUtil.copyProperties(excel, TechnicianDTO.class));
|
|
|
-//
|
|
|
-// String identifier = technician.getNumber() + ":" + technician.getIdCard();
|
|
|
-// if (identifierList.contains(identifier)) {
|
|
|
-// throw new ServiceException("数据已存在,导入失败!");
|
|
|
-// }
|
|
|
-// identifierList.add(identifier);
|
|
|
-// technician.setNumber(Func.isBlank(technician.getNumber()) ? null : technician.getNumber().trim());
|
|
|
-// technician.setIdCard(Func.isBlank(technician.getIdCard()) ? null : technician.getIdCard().trim());
|
|
|
-// //转换 性别字典
|
|
|
-// technician.setGender(Integer.valueOf(DictCache.getKey(DictEnum.SEX, excel.getGenderName())));
|
|
|
-// technician.setYearAndMonth(yearAndMonth);
|
|
|
-//
|
|
|
-// if (Func.isNotBlank(excel.getDepartment())) {
|
|
|
-// List<String> tmpList = Arrays.asList(excel.getDepartment().split("/"));
|
|
|
-// String department = null;
|
|
|
-// if (tmpList.size() == 1 || tmpList.size() == 2) {
|
|
|
-// department = tmpList.get(tmpList.size() - 1);
|
|
|
-// } else {
|
|
|
-// throw new ServiceException("所属二级单位/部门最多只能有两级,用'/'分隔");
|
|
|
-// }
|
|
|
-// LambdaQueryWrapper<DepartmentEntity> eq = Wrappers.<DepartmentEntity>lambdaQuery()
|
|
|
-// .eq(DepartmentEntity::getName, department)
|
|
|
-// .eq(DepartmentEntity::getYearAndMonth, StringUtil.sub(yearAndMonth, 0, 4));
|
|
|
-// DepartmentEntity dbDepartment = departmentMapper.selectOne(eq);
|
|
|
-// if (Func.isNull(dbDepartment)) {
|
|
|
-// throw new ServiceException("序号为[" + excel.getXh() + "]的部门/科室/项目部[" + excel.getDepartment() + "]不存在");
|
|
|
-// }
|
|
|
-// technician.setDepartmentId(dbDepartment.getId());
|
|
|
-// }
|
|
|
-// String unicode = technicianMapper.getUnicodeByNumberAndIdentityCard(technician.getNumber(), technician.getIdCard());
|
|
|
-// if (unicode == null) {
|
|
|
-// unicode = technicianService.generateUnicode();
|
|
|
-// }
|
|
|
-// technician.setUnicode(unicode);
|
|
|
-//
|
|
|
-// cachedDataList.add(technician);
|
|
|
+ ImportResult<Object> result = convertAndValidate(map, rowIndex);
|
|
|
+ resultList.add(result);
|
|
|
+ if (result.isSuccess()) {
|
|
|
+ dataList.add(result.getData());
|
|
|
+ incrementSuccessCount();
|
|
|
+ } else {
|
|
|
+ incrementErrorCount();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-// public List<ImportError> getErrorList() {
|
|
|
-// return errorList;
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 是否有错误
|
|
|
-// */
|
|
|
-// public boolean hasErrors() {
|
|
|
-// return !errorList.isEmpty();
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
- // private void saveData() {
|
|
|
-//
|
|
|
-// List<TechnicianEntity> dbTechnicianList = technicianMapper.selectList(Wrappers.<TechnicianEntity>lambdaQuery().eq(TechnicianEntity::getYearAndMonth, yearAndMonth));
|
|
|
-// List<String> identifierList = new ArrayList<>(dbTechnicianList.stream().map(tmp -> tmp.getNumber() + ":" + tmp.getIdCard()).toList());
|
|
|
-//
|
|
|
-// List<TechnicianEntity> insertList = new ArrayList<>();
|
|
|
-//
|
|
|
-// for (TechnicianExcel excel : cachedDataList) {
|
|
|
-// TechnicianEntity technician = Objects.requireNonNull(BeanUtil.copyProperties(excel, TechnicianDTO.class));
|
|
|
-//
|
|
|
-// String identifier = technician.getNumber() + ":" + technician.getIdCard();
|
|
|
-// if(identifierList.contains(identifier)){
|
|
|
-// throw new ServiceException("数据已存在,导入失败!");
|
|
|
-// }
|
|
|
-// identifierList.add(identifier);
|
|
|
-// technician.setNumber(Func.isBlank(technician.getNumber()) ? null : technician.getNumber().trim());
|
|
|
-// technician.setIdCard(Func.isBlank(technician.getIdCard()) ? null : technician.getIdCard().trim());
|
|
|
-// //转换 性别字典
|
|
|
-// technician.setGender(Integer.valueOf(DictCache.getKey(DictEnum.SEX, excel.getGenderName())));
|
|
|
-// technician.setYearAndMonth(yearAndMonth);
|
|
|
-//
|
|
|
-// if (Func.isNotBlank(excel.getDepartment())) {
|
|
|
-// List<String> tmpList = Arrays.asList(excel.getDepartment().split("/"));
|
|
|
-// String department = null;
|
|
|
-// if (tmpList.size() == 1 || tmpList.size() == 2) {
|
|
|
-// department = tmpList.get(tmpList.size() - 1);
|
|
|
-// } else {
|
|
|
-// throw new ServiceException("所属二级单位/部门最多只能有两级,用'/'分隔");
|
|
|
-// }
|
|
|
-// LambdaQueryWrapper<DepartmentEntity> eq = Wrappers.<DepartmentEntity>lambdaQuery()
|
|
|
-// .eq(DepartmentEntity::getName, department)
|
|
|
-// .eq(DepartmentEntity::getYearAndMonth, StringUtil.sub(yearAndMonth, 0, 4));
|
|
|
-// DepartmentEntity dbDepartment = departmentMapper.selectOne(eq);
|
|
|
-// if (Func.isNull(dbDepartment)) {
|
|
|
-// throw new ServiceException("序号为[" + excel.getXh() + "]的部门/科室/项目部[" + excel.getDepartment() + "]不存在");
|
|
|
-// }
|
|
|
-// technician.setDepartmentId(dbDepartment.getId());
|
|
|
-// }
|
|
|
-// String unicode = technicianMapper.getUnicodeByNumberAndIdentityCard(technician.getNumber(), technician.getIdCard());
|
|
|
-// if (unicode == null) {
|
|
|
-// unicode = technicianService.generateUnicode();
|
|
|
-// }
|
|
|
-// technician.setUnicode(unicode);
|
|
|
-//
|
|
|
-// insertList.add(technician);
|
|
|
-// if (insertList.size() % 20 == 0) {
|
|
|
-// technicianService.saveBatch(insertList);
|
|
|
-// insertList.clear();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// if (Func.isNotEmpty(insertList)) {
|
|
|
-// this.saveBatch(insertList);
|
|
|
-// }
|
|
|
-// }
|
|
|
- @Override
|
|
|
- public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
|
-// if (Func.isNotEmpty(cachedDataList)) {
|
|
|
-// technicianService.saveBatch(cachedDataList);
|
|
|
-// }
|
|
|
+ private ImportResult<Object> convertAndValidate(Map<Integer, String> data, int rowIndex) {
|
|
|
+
|
|
|
+ List<String> errors = new ArrayList<>();
|
|
|
+
|
|
|
+ TechnicianExcel excel = new TechnicianExcel();
|
|
|
+ excel.setXh(data.get(0));
|
|
|
+ excel.setName(data.get(1));
|
|
|
+ excel.setNumber(data.get(2));
|
|
|
+ excel.setIdCard(data.get(3));
|
|
|
+ String sexName = data.get(4);
|
|
|
+ List<String> sexList = DictCache.getList(DictEnum.SEX.getName()).stream().map(Dict::getDictValue).toList();
|
|
|
+ if(Func.isNotEmpty(sexList) && !sexList.contains(sexName)){
|
|
|
+ errors.add("性别错误,请重新选择");
|
|
|
+ }
|
|
|
+ excel.setGender(Integer.valueOf(DictCache.getKey(DictEnum.SEX, sexName)));
|
|
|
+
|
|
|
+ if (!ValidationUtils.isValidDateFormat(data.get(5), DateUtil.PATTERN_DATE)) {
|
|
|
+ errors.add("出生日期为空或格式错误,yyyy-MM-dd");
|
|
|
+ }
|
|
|
+ excel.setBirthday(data.get(5));
|
|
|
+ excel.setEthnic(data.get(6));
|
|
|
+ excel.setDepartment(data.get(7));
|
|
|
+ excel.setPost(data.get(8));
|
|
|
+ excel.setPostLocation(data.get(9));
|
|
|
+ String nature = data.get(10);
|
|
|
+ List<String> natureList = DictCache.getList("yonggongxingzhi").stream().map(Dict::getDictValue).toList();
|
|
|
+ if(Func.isNotEmpty(natureList) && !natureList.contains(nature)){
|
|
|
+ errors.add("用工性质错误,请重新选择");
|
|
|
+ }
|
|
|
+ excel.setNature(nature);
|
|
|
+ if (!ValidationUtils.isValidDateFormat(data.get(11), DateUtil.PATTERN_DATE)) {
|
|
|
+ errors.add("入职时间为空或格式错误,yyyy-MM-dd");
|
|
|
+ }
|
|
|
+ excel.setHireDate(data.get(11));
|
|
|
+
|
|
|
+ String education = data.get(12);
|
|
|
+ List<String> educationList = DictCache.getList("xueli").stream().map(Dict::getDictValue).toList();
|
|
|
+ if(Func.isNotEmpty(educationList) && !educationList.contains(education)){
|
|
|
+ errors.add("最高学历错误,请重新选择");
|
|
|
+ }
|
|
|
+ excel.setEducation(education);
|
|
|
+
|
|
|
+ excel.setDegree(data.get(13));
|
|
|
+ excel.setSchoolName(data.get(14));
|
|
|
+ excel.setMajor(data.get(15));
|
|
|
+
|
|
|
+ String rank = data.get(16);
|
|
|
+ if(Func.isNotBlank(rank)){
|
|
|
+ List<String> rankList = DictCache.getList("zhicheng").stream().map(Dict::getDictValue).toList();
|
|
|
+ if(Func.isNotEmpty(rankList) && !rankList.contains(rank.trim())){
|
|
|
+ errors.add("职称错误,请重新选择");
|
|
|
+ }
|
|
|
+ excel.setRank(rank);
|
|
|
+ }
|
|
|
+
|
|
|
+ excel.setPracticingRequirement(data.get(17));
|
|
|
+ excel.setRemark(data.get(18));
|
|
|
+
|
|
|
+ String situation = data.get(19);
|
|
|
+ List<String> situationList = DictCache.getList("canyanqingkuang").stream().map(Dict::getDictValue).toList();
|
|
|
+ if(Func.isNotEmpty(situationList) && !situationList.contains(situation)){
|
|
|
+ errors.add("参研情况错误,请重新选择");
|
|
|
+ }
|
|
|
+ excel.setSituation(situation);
|
|
|
+
|
|
|
+ String personnelType = data.get(20);
|
|
|
+ List<String> personnelTypeList = DictCache.getList("renyuanleixing").stream().map(Dict::getDictValue).toList();
|
|
|
+ if(Func.isNotEmpty(personnelTypeList) && !personnelTypeList.contains(personnelType)){
|
|
|
+ errors.add("人员类型错误,请重新选择");
|
|
|
+ }
|
|
|
+ excel.setPersonnelType(personnelType);
|
|
|
+
|
|
|
+ errors.addAll(ValidationUtils.validate(excel));
|
|
|
+ if (!errors.isEmpty()) {
|
|
|
+ return ImportResult.error(rowIndex, errors, data.toString());
|
|
|
+ }
|
|
|
+ return ImportResult.success(rowIndex, excel);
|
|
|
}
|
|
|
}
|