Fixiaobai
2023-11-12 17eb62f9d9cb2706296ea35f241dd8aafbebd6f2
mes-technology/src/main/java/com/chinaztt/mes/technology/service/impl/StructureServiceImpl.java
@@ -17,6 +17,7 @@
package com.chinaztt.mes.technology.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Console;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
@@ -46,9 +47,15 @@
import com.chinaztt.mes.technology.mapper.StructureMapper;
import com.chinaztt.mes.technology.service.StructureService;
import com.chinaztt.ztt.common.core.util.R;
import com.chinaztt.ztt.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@@ -57,6 +64,7 @@
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.CompletableFuture;
/**
@@ -78,6 +86,12 @@
   private NumberGenerator<Structure> numberGenerator;
   private DictUtils dictUtils;
   private IfsFeignClient ifsFeignClient;
   @Autowired
   private PlatformTransactionManager transactionManager;
   private RedisTemplate redisTemplate;
   @Autowired
   private ThreadPoolTaskExecutor threadPoolTaskExecutor;
   /**
    * @Author: Hans
@@ -256,59 +270,83 @@
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public void importExcel(List<StructureData> list) {
      list.forEach(System.out::println);
      System.out.println("================================");
      return;
      //if (CollectionUtil.isEmpty(list)) {
      //   return;
      //}
      //List<String> uinck = new ArrayList<>();
      //for (StructureData data : list) {
      //   Part part = partMapper.selectOne(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, data.getFatherPartNo()).eq(Part::getEngChgLevel, data.getFatherVersion()));
      //   if (part == null) {
      //      throw new RuntimeException("零件号:" + data.getFatherPartNo() + "不存在");
      //   }
      //   Structure structure = baseMapper.selectOne(Wrappers.<Structure>lambdaQuery()
      //         .eq(Structure::getPartId, part.getId())
      //         .eq(Structure::getBomTypeDb, data.getStructureType())
      //         .eq(Structure::getAlternativeNo, data.getAlternativeNo()));
      //   if (structure == null) {
      //      structure = new Structure();
      //      structure.setPartId(part.getId());
      //      structure.setVersion(data.getFatherVersion());
      //      structure.setAlternativeDesc(data.getAlternativeDesc());
      //      structure.setActive(true);
      //      structure.setBomTypeDb(data.getStructureType());
      //      structure.setIfsSync(false);
      //      structure.setAlternativeNo(data.getAlternativeNo());
      //      baseMapper.insert(structure);
      //   }
      //   if (uinck.contains(structure.getId() + "_" + data.getLineItemNo())) {
      //      throw new RuntimeException("零件号:" + part.getPartNo() + "行项号重复");
      //   }
      //   Part childPart = partMapper.selectOne(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, data.getChildPartNo()).eq(Part::getEngChgLevel, data.getChildVersion()));
      //   if (childPart == null) {
      //      throw new RuntimeException("零件号:" + data.getChildPartNo() + "不存在");
      //   }
      //   StructureComponent component = new StructureComponent();
      //   component.setStructureId(structure.getId());
      //   component.setPartId(childPart.getId());
      //   component.setPlanningMethod(childPart.getPlanningMethod());
      //   component.setQpa(new BigDecimal(data.getQpa()));
      //   component.setLineItemNo(Integer.valueOf(data.getLineItemNo()));
      //   component.setDiscNum(StringUtils.isEmpty(data.getDiskNum()) ? null : new Long(data.getDiskNum()));
      //   //查询工序 如果有多个获取第一个
      //   List<Operation> operationList = operationMapper.selectList(Wrappers.<Operation>lambdaQuery()
      //         .eq(Operation::getName, data.getOperationName())
      //         .eq(Operation::getActive, true).orderByAsc(Operation::getId));
      //   if (CollectionUtil.isEmpty(operationList)) {
      //      throw new RuntimeException("工序" + data.getOperationName() + "不存在");
      //   }
      //   component.setOperationId(operationList.get(0).getId());
      //   structureComponentMapper.insert(component);
      //   uinck.add(structure.getId() + "_" + data.getLineItemNo());
      //}
      String key = "syncStructure_lock" + SecurityUtils.getUser().getId();
      if (redisTemplate.hasKey(key)) {
         throw new RuntimeException("有导入任务正在处理");
      }
      CompletableFuture.runAsync(()->{
         asyncImport(list);
         redisTemplate.delete(key);
      },threadPoolTaskExecutor);
   }
   void asyncImport(List<StructureData> list){
      if (CollectionUtil.isEmpty(list)) {
         return;
      }
      for (StructureData data : list) {
         List<Part> parts = partMapper.selectList(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, data.getPartNo()));
         Part part = parts.get(0);
         if (part == null) {
            log.error("零件号:" + data.getPartNo() + "不存在");
            return;
         }
         Structure structure = baseMapper.selectOne(Wrappers.<Structure>lambdaQuery().eq(Structure::getPartId, part.getId()));
         if (structure == null) {
            structure = new Structure();
            BeanUtils.copyProperties(data, structure);
            structure.setPartId(part.getId());
            structure.setMaster(false);
            structure.setVersion("1");
            //structure.setActive(true);
            structure.setBomTypeDb("M");
            structure.setIfsSync(false);
            structure.setAlternativeNo("*");
            structure.setSharpenerNo(data.getSharpenerNo());
            structure.setCaveNum(data.getCaveNum());
            structure.setGramWeight(data.getGramWeight());
            structure.setMoldingCycle(data.getMoldingCycle());
            structure.setMachine(data.getMachine());
            structure.setScale(data.getScale());
            structure.setDryingTime(data.getDryingTime());
            structure.setDryingTemperature(data.getDryingTemperature());
            structure.setPackingInfo(data.getPackingInfo());
            structure.setCustom(data.getCustom());
            baseMapper.insert(structure);
         }
         List<Part> childParts = partMapper.selectList(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, data.getSonPartNo()));
         Part childPart = childParts.get(0);
         if (childPart == null) {
            log.error("零件号:" + data.getSonPartNo() + "不存在");
            return;
         }
         List<Operation> operations = operationMapper.selectList(Wrappers.<Operation>lambdaQuery().eq(Operation::getName, data.getOperationName()));
         Operation operation = operations.get(0);
         StructureComponent component = new StructureComponent();
         component.setStructureId(structure.getId());
         component.setPartId(childPart.getId());
         component.setPlanningMethod(childPart.getPlanningMethod());
         QueryWrapper<StructureComponent> queryWrapper = new QueryWrapper<>();
         queryWrapper.lambda().select(StructureComponent::getLineItemNo).eq(StructureComponent::getStructureId,structure.getId()).orderByDesc(StructureComponent::getLineItemNo);
         List<StructureComponent> structureComponents = structureComponentMapper.selectList(queryWrapper);
         component.setQpa(new BigDecimal(data.getTotalNUm()));// todo: 产品结构导入数量,还缺一个行号
         Console.log("值===》"+structureComponents);
         if(structureComponents.size()<1){
            component.setLineItemNo(1);
         }else {
            Integer lineItemNo = structureComponents.get(0).getLineItemNo();
            component.setLineItemNo(lineItemNo+1);
         }
         component.setOperationId(operation.getId());
         component.setPlanningMethod("A");
         component.setSpecs(childPart.getSpecs());
         component.setPartModel(childPart.getPartModel());
         structureComponentMapper.insert(component);
      }
   }
   @Override
@@ -457,6 +495,9 @@
      Structure structure = baseMapper.selectById(component.getStructureId());
      structure.setIfsSync(false);
      validate(structure.getPartId(), component.getPartId());
      Part part = partMapper.selectById(component.getPartId());
      component.setPartModel(part.getPartModel());
      component.setSpecs(part.getSpecs());
      structureComponentMapper.insert(component);
      // 对接ifs
//      List<StructureDTO> structureDTOS = structureMapper.getStructureDtoByIds(Arrays.asList(component.getStructureId()));