【物料变更】批量导入任务
This commit is contained in:
parent
f4485365fd
commit
7972909aa6
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.nflg.product.material.mapper.master;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.nflg.product.material.pojo.entity.MaterialUpdateImportTaskEntity;
|
||||||
|
|
||||||
|
public interface MaterialUpdateImportTaskMapper extends BaseMapper<MaterialUpdateImportTaskEntity> {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,88 @@
|
||||||
|
package com.nflg.product.material.pojo.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.*;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 物料变更批量导入任务表
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
@ApiModel(value = "com-nflg-product-material-pojo-entity-MaterialUpdateImportTaskEntity")
|
||||||
|
@TableName(value = "t_material_update_import_task")
|
||||||
|
public class MaterialUpdateImportTaskEntity implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 行id
|
||||||
|
*/
|
||||||
|
@TableId(value = "row_id", type = IdType.ASSIGN_ID)
|
||||||
|
@ApiModelProperty(value = "行id")
|
||||||
|
private Long rowId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件名称
|
||||||
|
*/
|
||||||
|
@TableField(value = "file_name")
|
||||||
|
@ApiModelProperty(value = "文件名称")
|
||||||
|
private String fileName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总条数
|
||||||
|
*/
|
||||||
|
@TableField(value = "total_num")
|
||||||
|
@ApiModelProperty(value = "总条数")
|
||||||
|
private Integer totalNum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功条数
|
||||||
|
*/
|
||||||
|
@TableField(value = "success_num")
|
||||||
|
@ApiModelProperty(value = "成功条数")
|
||||||
|
private Integer successNum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败条数
|
||||||
|
*/
|
||||||
|
@TableField(value = "fail_num")
|
||||||
|
@ApiModelProperty(value = "失败条数")
|
||||||
|
private Integer failNum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败原因
|
||||||
|
*/
|
||||||
|
@TableField(value = "fail_reason")
|
||||||
|
@ApiModelProperty(value = "失败原因")
|
||||||
|
private String failReason;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务状态:0进行中 1已完成
|
||||||
|
*/
|
||||||
|
@TableField(value = "task_status")
|
||||||
|
@ApiModelProperty(value = "任务状态:0进行中 1已完成")
|
||||||
|
private Integer taskStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建人
|
||||||
|
*/
|
||||||
|
@TableField(value = "created_by", fill = FieldFill.INSERT)
|
||||||
|
@ApiModelProperty(value = "创建人")
|
||||||
|
private String createdBy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*/
|
||||||
|
@TableField(value = "created_time", fill = FieldFill.INSERT)
|
||||||
|
@ApiModelProperty(value = "创建时间")
|
||||||
|
private LocalDateTime createdTime;
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -21,6 +21,7 @@ import com.nflg.product.base.core.exception.NflgBusinessException;
|
||||||
import com.nflg.product.base.core.vo.PageVO;
|
import com.nflg.product.base.core.vo.PageVO;
|
||||||
import com.nflg.product.material.constant.*;
|
import com.nflg.product.material.constant.*;
|
||||||
import com.nflg.product.material.mapper.master.MaterialUpdateBillMapper;
|
import com.nflg.product.material.mapper.master.MaterialUpdateBillMapper;
|
||||||
|
import com.nflg.product.material.mapper.master.MaterialUpdateImportTaskMapper;
|
||||||
import com.nflg.product.material.pojo.dto.ExcelDTO.MaterialStateUpExcelDTO;
|
import com.nflg.product.material.pojo.dto.ExcelDTO.MaterialStateUpExcelDTO;
|
||||||
import com.nflg.product.material.pojo.dto.ExcelDTO.TwentyMaterialTemplateExcelDTO;
|
import com.nflg.product.material.pojo.dto.ExcelDTO.TwentyMaterialTemplateExcelDTO;
|
||||||
import com.nflg.product.material.pojo.dto.MaterialMainAddAttrParamDTO;
|
import com.nflg.product.material.pojo.dto.MaterialMainAddAttrParamDTO;
|
||||||
|
|
@ -48,9 +49,12 @@ import org.ttzero.excel.reader.ExcelReader;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -89,6 +93,8 @@ public class MaterialUpdateBillService extends ServiceImpl<MaterialUpdateBillMap
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private RedisTemplate<String, String> redisTemplate;
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
|
@Resource
|
||||||
|
private MaterialUpdateImportTaskMapper materialUpdateImportTaskMapper;
|
||||||
|
|
||||||
private static final String PREFIX = "frontend:material";
|
private static final String PREFIX = "frontend:material";
|
||||||
|
|
||||||
|
|
@ -410,11 +416,21 @@ public class MaterialUpdateBillService extends ServiceImpl<MaterialUpdateBillMap
|
||||||
return ResultVO.error("导入失败,最多支持10000条批量导入");
|
return ResultVO.error("导入失败,最多支持10000条批量导入");
|
||||||
}
|
}
|
||||||
redisTemplate.opsForValue().set(buildKey(userCode + ":updateBatchImport"), file.getOriginalFilename());
|
redisTemplate.opsForValue().set(buildKey(userCode + ":updateBatchImport"), file.getOriginalFilename());
|
||||||
|
Map<Integer, String> indexMaterialMap = new HashMap<>(excelContext.size());
|
||||||
|
for (int i = 0; i < excelContext.size(); i++) {
|
||||||
|
indexMaterialMap.put(i + 1, excelContext.get(i).getMaterialNo());
|
||||||
|
}
|
||||||
|
Long rowId = IdWorker.getId();
|
||||||
// 大数据量,子线程分批次提交OA(每批1000条),主线程提前返回结果给前端
|
// 大数据量,子线程分批次提交OA(每批1000条),主线程提前返回结果给前端
|
||||||
CompletableFuture<Integer> task1 = CompletableFuture.supplyAsync(() -> {
|
CompletableFuture<Integer> task1 = CompletableFuture.supplyAsync(() -> {
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
|
insertMaterialUpdateImportTask(rowId, excelContext.size(), file.getOriginalFilename(), userCode);
|
||||||
List<List<MaterialStateUpExcelDTO>> lists = Lists.partition(excelContext, 1000);
|
List<List<MaterialStateUpExcelDTO>> lists = Lists.partition(excelContext, 1000);
|
||||||
lists.forEach(items -> this.handleImportDataForState(items, applyDeptName, deptEnt, realName, userCode));
|
for (int i = 0; i < lists.size(); i++) {
|
||||||
|
List<MaterialStateUpExcelDTO> items = lists.get(i);
|
||||||
|
this.handleImportDataForState(items, applyDeptName, deptEnt, realName, userCode);
|
||||||
|
}
|
||||||
|
updateMaterialUpdateImportTask(rowId, excelContext.size(), excelContext.size(), 0, "");
|
||||||
long endTime = System.currentTimeMillis();
|
long endTime = System.currentTimeMillis();
|
||||||
log.info("物料变更批量导入耗时(ms): {}", endTime - startTime);
|
log.info("物料变更批量导入耗时(ms): {}", endTime - startTime);
|
||||||
redisTemplate.delete(buildKey(userCode + ":updateBatchImport"));
|
redisTemplate.delete(buildKey(userCode + ":updateBatchImport"));
|
||||||
|
|
@ -423,6 +439,8 @@ public class MaterialUpdateBillService extends ServiceImpl<MaterialUpdateBillMap
|
||||||
task1.exceptionally(ex -> {
|
task1.exceptionally(ex -> {
|
||||||
// 异常处理器,打印异常信息并返回默认值
|
// 异常处理器,打印异常信息并返回默认值
|
||||||
log.error("物料变更批量导入异常: " + ex.getMessage());
|
log.error("物料变更批量导入异常: " + ex.getMessage());
|
||||||
|
String errMaterialNo = getErrMaterialNo(ex.getMessage());
|
||||||
|
updateMaterialUpdateImportTask(rowId, excelContext.size(), excelContext.size(), 0, "");
|
||||||
redisTemplate.delete(buildKey(userCode + ":updateBatchImport"));
|
redisTemplate.delete(buildKey(userCode + ":updateBatchImport"));
|
||||||
return -1;
|
return -1;
|
||||||
});
|
});
|
||||||
|
|
@ -466,6 +484,46 @@ public class MaterialUpdateBillService extends ServiceImpl<MaterialUpdateBillMap
|
||||||
return ResultVO.success(importVO);
|
return ResultVO.success(importVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Long insertMaterialUpdateImportTask(Long rowId, Integer totalNum, String fileName, String userCode) {
|
||||||
|
MaterialUpdateImportTaskEntity save = new MaterialUpdateImportTaskEntity();
|
||||||
|
save.setRowId(rowId);
|
||||||
|
save.setFileName(fileName);
|
||||||
|
save.setTotalNum(totalNum);
|
||||||
|
save.setTaskStatus(0);
|
||||||
|
save.setCreatedBy(userCode);
|
||||||
|
save.setCreatedTime(LocalDateTime.now());
|
||||||
|
materialUpdateImportTaskMapper.insert(save);
|
||||||
|
return save.getRowId();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateMaterialUpdateImportTask(Long rowId, Integer totalNum, Integer successNum, Integer failNum, String exceptionMsg) {
|
||||||
|
MaterialUpdateImportTaskEntity update = new MaterialUpdateImportTaskEntity();
|
||||||
|
update.setSuccessNum(successNum);
|
||||||
|
update.setFailNum(failNum);
|
||||||
|
// 批量导入总条数${total_num}条,成功${success_num}条,失败${fail_num}条,第${success_num}条后因为“${fail_reason}”而终止
|
||||||
|
String desc = StrUtil.format("批量导入总条数{}条,成功{}条,失败${}条", totalNum, successNum, failNum);
|
||||||
|
if (failNum > 0) {
|
||||||
|
desc += StrUtil.format(",第{}条后因为“{}”而终止", successNum, exceptionMsg);
|
||||||
|
}
|
||||||
|
update.setFailReason(desc);
|
||||||
|
update.setTaskStatus(1);
|
||||||
|
materialUpdateImportTaskMapper.updateById(update);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 从报错信息找到第一个物料号
|
||||||
|
private String getErrMaterialNo(String exceptionMsg) {
|
||||||
|
if (StrUtil.isEmpty(exceptionMsg)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
Pattern pattern = Pattern.compile("\\d{10}");
|
||||||
|
Matcher matcher = pattern.matcher(exceptionMsg);
|
||||||
|
if (matcher.find()) {
|
||||||
|
return matcher.group();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 状态变更
|
* 状态变更
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.nflg.product.material.mapper.master.MaterialUpdateImportTaskMapper">
|
||||||
|
</mapper>
|
||||||
Loading…
Reference in New Issue