Fancy
2024-08-27 5e75ef24e5dd45ae934dc7f03f6c7736ca73c9ff
pms-parent/pms-web-product/src/main/java/com/dy/pmsProduct/taskPlan/AssemblySv.java
@@ -15,6 +15,7 @@
import com.dy.pmsGlobal.pojoPr.PrBatchNumber;
import com.dy.pmsGlobal.pojoPr.PrDevice;
import com.dy.pmsGlobal.pojoPr.PrProductionProcess;
import com.dy.pmsGlobal.util.QrCodeConstant;
import com.dy.pmsGlobal.util.QrCodeUtil;
import com.dy.pmsGlobal.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
@@ -23,6 +24,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -33,11 +36,10 @@
    private static final int BATCH_SIZE = 100; // 批量处理大小
    private static final String CODE_FORMAT = "%03d";
    private static final String CODE_FORMAT_7 = "%07d";
    private static final String TYPE_CODE_EQUIP = "110";
    private PrAssemblyPlanMapper assemblyDao;
    private UserUtil userUtil;
    private PrBatchNumberMapper batchDao;
    private PrDeviceMapper equipDao;
    private PrDeviceMapper deviceDao;
    private PltProductMapper productDao;
    private PrProductionProcessMapper processDao;
    @Autowired
@@ -53,8 +55,8 @@
        this.userUtil = userUtil;
    }
    @Autowired
    public void setEquipDao(PrDeviceMapper equipDao) {
        this.equipDao = equipDao;
    public void setDeviceDao(PrDeviceMapper deviceDao) {
        this.deviceDao = deviceDao;
    }
    @Autowired
    public void setProductDao(PltProductMapper productDao) {
@@ -72,10 +74,7 @@
        if(assemblyDao.exists(plan.name,plan.id)){
            throw new RuntimeException("计划名称不能重复");
        }
        PrProductionProcess process = processDao.selectByPrimaryKey(plan.processId);
        if(process == null || !process.proId.equals(plan.proId)){
            throw new RuntimeException("产品与生产流程不匹配");
        }
        extractedCheck(plan);
        PrBatchNumber batch = new PrBatchNumber();
        batch.batchNumber = getNextCode();
        batch.proId = plan.proId;
@@ -86,7 +85,7 @@
            plan.creator = loginUser.id;
        }
        batchDao.insertSelective(batch);
        insertEquip(plan.proId,batch.id,batch.batchNumber,plan.number,1);
        insertDevice(plan.proId,batch.id,batch.batchNumber,plan.number,1);
        plan.batchId = batch.id;
        plan.status =PlanStatusEnum.NORMAL.getCode();
@@ -100,32 +99,55 @@
        if(assemblyDao.exists(plan.name,plan.id)){
            throw new RuntimeException("计划名称不能重复");
        }
        PrProductionProcess process = processDao.selectByPrimaryKey(plan.processId);
        if(process == null || !process.proId.equals(plan.proId)){
            throw new RuntimeException("产品与生产流程不匹配");
        }
        PrAssemblyPlan origPlan = assemblyDao.selectByPrimaryKey(plan.id);
        PrBatchNumber batch = batchDao.selectByPrimaryKey(origPlan.batchId);
        //如果有投入,产品和流程不能变更
        if(origPlan.getInputNumber() > 0){
            if(!origPlan.proId.equals(plan.proId) || !origPlan.processId.equals(plan.processId)){
                throw new RuntimeException("计划已投入,产品与生产流程不能变更");
            }
        }
        extractedCheck(plan);
        //产品改变则更新全部已生成设备号
        if(origPlan.proId != plan.proId){
            batch.proId = plan.proId;
            batchDao.updateByPrimaryKeySelective(batch);
            equipDao.deleteByBatchId(origPlan.batchId);
            insertEquip(plan.proId,origPlan.batchId,batch.batchNumber,plan.number,1);
            deviceDao.deleteByBatchId(origPlan.batchId);
            insertDevice(plan.proId,origPlan.batchId,batch.batchNumber,plan.number,1);
        }else if(origPlan.number < plan.number){
            //数量增加则新增设备号
            insertEquip(plan.proId,origPlan.batchId,batch.batchNumber,plan.number,origPlan.number+1);
            insertDevice(plan.proId,origPlan.batchId,batch.batchNumber,plan.number,origPlan.number+1);
        }
        return assemblyDao.updateByPrimaryKeySelective(plan);
    }
    private void extractedCheck(PrAssemblyPlan plan) {
        PrProductionProcess process = processDao.selectByPrimaryKey(plan.processId);
        if(process == null || !process.proId.equals(plan.proId)){
            throw new RuntimeException("产品与生产流程不匹配");
        }
        //开始日期要小于结束日期
        if(plan.startDate.compareTo(plan.endDate) > 0){
            throw new RuntimeException("开始日期不能大于结束日期");
        }
        //Fancy add 2024/07/09   如果状态为执行 ,则结束日期必须大于等于当前日期
        if(plan.status == PlanStatusEnum.NORMAL.getCode()){
            String endDateStr = plan.getEndDate();
            LocalDate endDate = LocalDate.parse(endDateStr, DateTimeFormatter.ISO_LOCAL_DATE);
            LocalDate nextDay = endDate.plusDays(1);
            LocalDate today = LocalDate.now(); // 获取当前日期
            if (nextDay.isBefore(today)) {
                throw new RuntimeException("执行状态结束日期必须大于等于当前日期,请修改结束日期");
            }
        }
    }
    @Transactional
    public int addEquip(Long planId,int num){
    public int addDevice(Long planId,int num){
        PrAssemblyPlan plan = assemblyDao.selectByPrimaryKey(planId);
        PrBatchNumber batch = batchDao.selectByPrimaryKey(plan.batchId);
        int count =insertEquip(plan.proId,plan.batchId,batch.batchNumber,num + plan.number,plan.number+1);
        int count =insertDevice(plan.proId,plan.batchId,batch.batchNumber,num + plan.number,plan.number+1);
//        plan.number = plan.number + num;
//        assemblyDao.updateByPrimaryKeySelective (plan);
        return count;
@@ -140,27 +162,27 @@
     * @param number 数量
     * @return 插入数量
     */
    private int insertEquip(Long proId,Long batchId,String batchCode,Integer number,int startIndex){
    private int insertDevice(Long proId,Long batchId,String batchCode,Integer number,int startIndex){
        log.info("批量插入设备");
        PltProduct product = productDao.selectByPrimaryKey(proId);
        List<PrDevice> list = new java.util.ArrayList<>();
        for(int i= startIndex ;i<= number;i++){
            PrDevice equip = new PrDevice();
            equip.proId = proId;
            equip.batchId = batchId;
            equip.deviceNo = getEquipNo(product.code,batchCode,i);
            equip.status = 0;
            list.add(equip);
            PrDevice device = new PrDevice();
            device.proId = proId;
            device.batchId = batchId;
            device.deviceNo = getDeviceNo(product.code,batchCode,i);
            device.status = 0;
            list.add(device);
        }
        return insertBatchEquip(list);
        return insertBatchDevice(list);
    }
    private int insertBatchEquip(List<PrDevice> list) {
    private int insertBatchDevice(List<PrDevice> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i += BATCH_SIZE) {
            List<PrDevice> subList = list.subList(i, Math.min(i + BATCH_SIZE, list.size()));
            // 调用MyBatis插入这批数据
            count += equipDao.insertBatch(subList);
            count += deviceDao.insertBatch(subList);
        }
        return count;
    }
@@ -180,26 +202,26 @@
            return currentDay + String.format(CODE_FORMAT, nextCode);
        }
    }
    private static String getEquipNo(String proCode,String batchCode,int index){
        String equipNo = TYPE_CODE_EQUIP + proCode + batchCode +
    private static String getDeviceNo(String proCode,String batchCode,int index){
        String deviceNo = QrCodeConstant.TypeProduct + proCode + batchCode +
                String.format(CODE_FORMAT_7, index);
        return equipNo;
        return deviceNo;
    }
    public static void main(String[] args) {
        List<PrDevice> list = new java.util.ArrayList<>();
        for(int i= 1 ;i<= 10000;i++){
            PrDevice equip = new PrDevice();
            equip.proId = 2024052310512000003l;
            equip.batchId = 2024053017154400004l;
            equip.deviceNo = getEquipNo("002","2024001",i);
            equip.status = 0;
            PrDevice device = new PrDevice();
            device.proId = 2024052310512000003l;
            device.batchId = 2024053017154400004l;
            device.deviceNo = getDeviceNo("002","2024001",i);
            device.status = 0;
            list.add(equip);
            list.add(device);
        }
        list.forEach(equip->{
            System.out.println(equip.deviceNo);
        list.forEach(device->{
            System.out.println(device.deviceNo);
        });
    }
@@ -227,23 +249,23 @@
        return rsVo ;
    }
    public QueryResultVo<List<PrDevice>> selectSomeEquip(QueryVo queryVo) {
    public QueryResultVo<List<PrDevice>> selectSomeDevice(QueryVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        //查询符合条件的记录总数
        Long itemTotal = equipDao.selectSomeCount(params);
        Long itemTotal = deviceDao.selectSomeCount(params);
        QueryResultVo<List<PrDevice>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ;
        //计算分页等信息
        rsVo.calculateAndSet(itemTotal, params);
        //查询符合条件的记录
        rsVo.obj = equipDao.selectSome(params);
        rsVo.obj.forEach(equip->{
            equip.qrCode = QrCodeUtil.genQrCodeString(equip.deviceNo);
        rsVo.obj = deviceDao.selectSome(params);
        rsVo.obj.forEach(device->{
            device.qrCode = QrCodeUtil.genQrCodeString(device.deviceNo);
        });
        return rsVo ;
    }
    public List<PrDevice> selectEquipByBatchId(Long batchId) {
        return equipDao.selectByBatchId(batchId);
    public List<PrDevice> selectDeviceByBatchId(Long batchId) {
        return deviceDao.selectByBatchId(batchId);
    }
    /**
@@ -253,6 +275,11 @@
     */
    @Transactional
    public int updateStatus(PrAssemblyPlan plan) {
        if(plan.status == PlanStatusEnum.NORMAL.getCode()){
            PrAssemblyPlan assemblyPlan = assemblyDao.selectByPrimaryKey(plan.id);
            assemblyPlan.status = plan.status;
            extractedCheck(assemblyPlan);
        }
        PrAssemblyPlan param = new PrAssemblyPlan();
        param.id =plan.id;
        param.status = plan.status;