|
|
@@ -1,148 +1,86 @@
|
|
|
package org.sky.scientific.excel.listener;
|
|
|
|
|
|
import com.alibaba.excel.context.AnalysisContext;
|
|
|
-import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
-import jakarta.validation.ConstraintViolation;
|
|
|
-import jakarta.validation.Validation;
|
|
|
-import jakarta.validation.Validator;
|
|
|
-import jakarta.validation.ValidatorFactory;
|
|
|
import lombok.Data;
|
|
|
import lombok.EqualsAndHashCode;
|
|
|
+import org.sky.core.tool.utils.Func;
|
|
|
import org.sky.scientific.excel.YsZjtrfyExcel;
|
|
|
-import org.sky.scientific.service.IYsZjtrfyService;
|
|
|
-import org.sky.scientific.utils.ImportError;
|
|
|
+import org.sky.scientific.utils.ImportResult;
|
|
|
+import org.sky.scientific.utils.ValidationUtils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@EqualsAndHashCode(callSuper = true)
|
|
|
@Data
|
|
|
- public class YsZjtrfyImportListener extends AnalysisEventListener<YsZjtrfyExcel> {
|
|
|
-
|
|
|
- private IYsZjtrfyService service;
|
|
|
- private Long xmId;
|
|
|
- private List<YsZjtrfyExcel> dataList = new ArrayList<>();
|
|
|
- private static final int BATCH_COUNT = 100;
|
|
|
- private List<YsZjtrfyExcel> cachedDataList = new ArrayList<>(BATCH_COUNT);
|
|
|
- private List<ImportError> errorMessages = new ArrayList<>();
|
|
|
- private Validator validator;
|
|
|
-
|
|
|
- public YsZjtrfyImportListener(IYsZjtrfyService service, Long xmId) {
|
|
|
- this.service = service;
|
|
|
- this.xmId = xmId;
|
|
|
- ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
|
|
- this.validator = factory.getValidator();
|
|
|
- }
|
|
|
+public class YsZjtrfyImportListener extends BaseReadListener {
|
|
|
|
|
|
@Override
|
|
|
- public void invoke(YsZjtrfyExcel data, AnalysisContext context) {
|
|
|
+ public void invoke(Map<Integer, String> map, AnalysisContext context) {
|
|
|
int rowIndex = context.readRowHolder().getRowIndex() + 1;
|
|
|
|
|
|
- // 1. 基本数据校验
|
|
|
- List<String> validationErrors = validateData(data, rowIndex);
|
|
|
- if (!validationErrors.isEmpty()) {
|
|
|
- errorMessages.addAll(validationErrors.stream()
|
|
|
- .map(msg -> new ImportError(rowIndex, msg))
|
|
|
- .toList());
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 2. 内存重复检查
|
|
|
- if (isDuplicateInMemory(data, rowIndex)) {
|
|
|
- return;
|
|
|
+ ImportResult<Object> result = convertAndValidate(map, rowIndex);
|
|
|
+ resultList.add(result);
|
|
|
+ if (result.isSuccess()) {
|
|
|
+ dataList.add(result.getData());
|
|
|
+ incrementSuccessCount();
|
|
|
+ } else {
|
|
|
+ incrementErrorCount();
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
+ private ImportResult<Object> convertAndValidate(Map<Integer, String> data, int rowIndex) {
|
|
|
|
|
|
-
|
|
|
- dataList.add(data);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 数据校验
|
|
|
- */
|
|
|
- private List<String> validateData(YsZjtrfyExcel data, int rowIndex) {
|
|
|
List<String> errors = new ArrayList<>();
|
|
|
|
|
|
- // 使用注解校验
|
|
|
- Set<ConstraintViolation<YsZjtrfyExcel>> violations = validator.validate(data);
|
|
|
- for (ConstraintViolation<YsZjtrfyExcel> violation : violations) {
|
|
|
- errors.add(violation.getMessage());
|
|
|
+ YsZjtrfyExcel excel = new YsZjtrfyExcel();
|
|
|
+ excel.setXh(data.get(0));
|
|
|
+ excel.setWzmc(data.get(1));
|
|
|
+ String yongliang = data.get(2);
|
|
|
+ if(Func.isNotBlank(yongliang)){
|
|
|
+ excel.setYongLiang(ValidationUtils.safeParseInteger(yongliang));
|
|
|
+ if (Func.isNull(excel.getYongLiang())) {
|
|
|
+ errors.add("用量数据格式错误");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-// // 自定义业务校验
|
|
|
-// if (data.getAgeInt() != null && (data.getAgeInt() < 0 || data.getAgeInt() > 150)) {
|
|
|
-// errors.add("年龄必须在0-150之间");
|
|
|
-// }
|
|
|
-//
|
|
|
-// if (data.getSalaryDecimal() != null && data.getSalaryDecimal().compareTo(BigDecimal.ZERO) < 0) {
|
|
|
-// errors.add("薪资不能为负数");
|
|
|
-// }
|
|
|
-//
|
|
|
-// if (data.getStatusInt() != null && (data.getStatusInt() < 0 || data.getStatusInt() > 1)) {
|
|
|
-// errors.add("状态只能是0或1");
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 日期逻辑校验
|
|
|
-// if (data.getBirthdayDate() != null && data.getHireDateDate() != null) {
|
|
|
-// if (data.getBirthdayDate().after(data.getHireDateDate())) {
|
|
|
-// errors.add("生日不能晚于入职日期");
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- return errors;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 内存重复检查
|
|
|
- */
|
|
|
- private boolean isDuplicateInMemory(YsZjtrfyExcel data, int rowIndex) {
|
|
|
- boolean isDuplicate = false;
|
|
|
-
|
|
|
-// if (tempUsernames.contains(data.getUsername())) {
|
|
|
-// errorMessages.add(new ImportError(rowIndex, "用户名 " + data.getUsername() + " 在当前文件中重复"));
|
|
|
-// isDuplicate = true;
|
|
|
-// }
|
|
|
-//
|
|
|
-// if (tempEmails.contains(data.getEmail())) {
|
|
|
-// errorMessages.add(new ImportError(rowIndex, "邮箱 " + data.getEmail() + " 在当前文件中重复"));
|
|
|
-// isDuplicate = true;
|
|
|
-// }
|
|
|
-//
|
|
|
-// if (!isDuplicate) {
|
|
|
-// tempUsernames.add(data.getUsername());
|
|
|
-// tempEmails.add(data.getEmail());
|
|
|
-// }
|
|
|
-
|
|
|
- return isDuplicate;
|
|
|
- }
|
|
|
+ String danjia = data.get(3);
|
|
|
+ if(Func.isNotBlank(danjia)){
|
|
|
+ excel.setDanJia(ValidationUtils.safeParseDouble(danjia));
|
|
|
+ if (Func.isNull(excel.getDanJia())) {
|
|
|
+ errors.add("单价数据格式错误");
|
|
|
+ }else {
|
|
|
+ excel.setDanJia(excel.getDanJia() * 10000);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
|
+ excel.setYqsbmc(data.get(5));
|
|
|
|
|
|
-// if (Func.isEmpty(dataList)) {
|
|
|
-// throw new ServiceException("无数据可导入");
|
|
|
-// }
|
|
|
-// List<YsZjtrfyEntity> insertList = new ArrayList<>();
|
|
|
-// List<YsZjtrfyEntity> dbList = service.list(Wrappers.<YsZjtrfyEntity>lambdaQuery().eq(YsZjtrfyEntity::getXmId, xmId));
|
|
|
-// int batchCount = 20;
|
|
|
-// for (YsZjtrfyExcel excel : dataList) {
|
|
|
-// YsZjtrfyEntity entity = new YsZjtrfyEntity();
|
|
|
-// entity.setXmId(xmId);
|
|
|
-// entity.setWzmc(excel.getWzmc());
|
|
|
-// entity.setYongLiang(excel.getYongLiang());
|
|
|
-//// BeanUtil.copyProperties(excel, entity);
|
|
|
-// insertList.add(entity);
|
|
|
-// if (insertList.size() % batchCount == 0 && service.saveBatch(insertList)) {
|
|
|
-// insertList.clear();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// if (Func.isNotEmpty(insertList)) {
|
|
|
-// service.saveBatch(insertList);
|
|
|
-// }
|
|
|
- }
|
|
|
+ String ysje = data.get(6);
|
|
|
+ if(Func.isNotBlank(ysje)){
|
|
|
+ excel.setYsje(ValidationUtils.safeParseDouble(ysje));
|
|
|
+ if (Func.isNull(excel.getYsje())) {
|
|
|
+ errors.add("仪器设备的预算金额(万元)数据格式错误");
|
|
|
+ }else {
|
|
|
+ excel.setYsje(excel.getYsje() * 10000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ excel.setGdzcmc(data.get(7));
|
|
|
+ String zlfyys = data.get(8);
|
|
|
+ if(Func.isNotBlank(zlfyys)){
|
|
|
+ excel.setZlfyys(ValidationUtils.safeParseDouble(zlfyys));
|
|
|
+ if (Func.isNull(excel.getZlfyys())) {
|
|
|
+ errors.add("固定资产租赁预算金额(万元)数据格式错误");
|
|
|
+ }else {
|
|
|
+ excel.setZlfyys(excel.getZlfyys() * 10000);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- public boolean hasErrors() {
|
|
|
- return !errorMessages.isEmpty();
|
|
|
+ errors.addAll(ValidationUtils.validate(excel));
|
|
|
+ if (!errors.isEmpty()) {
|
|
|
+ return ImportResult.error(rowIndex, errors, data.toString());
|
|
|
+ }
|
|
|
+ return ImportResult.success(rowIndex, excel);
|
|
|
}
|
|
|
}
|