XiaoRuby
2023-09-07 c28d3bb363dde2afb44c168b93379b2bf6b1f67f
standard-server/src/main/java/com/yuanchu/mom/service/impl/StandardServiceImpl.java
@@ -1,10 +1,18 @@
package com.yuanchu.mom.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.pojo.Standard;
import com.yuanchu.mom.service.StandardService;
import com.yuanchu.mom.mapper.StandardMapper;
import com.yuanchu.mom.mapper.*;
import com.yuanchu.mom.pojo.*;
import com.yuanchu.mom.pojo.dto.StandardDto;
import com.yuanchu.mom.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @author Administrator
@@ -14,6 +22,133 @@
@Service
public class StandardServiceImpl extends ServiceImpl<StandardMapper, Standard> implements StandardService {
    @Resource
    MaterialMapper materialMapper;
    @Resource
    StandardMapper standardMapper;
    @Resource
    SpecificationsMapper specificationsMapper;
    @Resource
    TechnologyService technologyService;
    @Resource
    TechnologyTemplateMapper technologyTemplateMapper;
    @Resource
    ProductService productService;
    @Resource
    TechnicalModelMapper technicalModelMapper;
    @Resource
    MbomService mbomService;
    @Resource
    MbomModelMapper mbomModelMapper;
    @Resource
    TechniqueService techniqueService;
    @Resource
    TechniqueModelMapper techniqueModelMapper;
    @Resource
    DeviceMapper deviceMapper;
    //(3级)新增-->标准,型号
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addStandard(StandardDto standardDto) {
        //校验添加该物料下的标准是否重复
        List<String> standNameList = standardMapper.selectList(Wrappers.<Standard>query().eq("material_id", standardDto.getId())).stream().map(standard -> {
            String standardName = standard.getName();
            return standardName;
        }).collect(Collectors.toList());
        if (standNameList.contains(standardDto.getStandard())){
            return "该产品下有该标准";
        }
        /*新增标准表*/
        Standard standard = new Standard();
        standard.setMaterial_id(standardDto.getId());
        standard.setName(standardDto.getStandard());
        standardMapper.insert(standard);
        /*新增型号表*/
        Specifications specifications = new Specifications();
        specifications.setStandardId(standard.getId());
        specifications.setName(standardDto.getSpecifications());
        specificationsMapper.insert(specifications);
        /*新增标准BOM-->工艺路线(批量添加)*/
        //查询物料的大类(根据物料id)
        Material material = materialMapper.selectById(standardDto.getId());
        List<TechnologyTemplate> technologyTemplateList = technologyTemplateMapper.selectList(Wrappers.<TechnologyTemplate>query().eq("type", material.getFather()));
        List<Technology> technologyList = technologyTemplateList.stream().map(technologyTemplate -> {
            Technology technology = new Technology();
            technology.setSpecificationsId(specifications.getId());
            technology.setFather(technologyTemplate.getFather());
            technology.setName(technologyTemplate.getName());
            technology.setDeviceGroup(technologyTemplate.getDeviceGroup());
            return technology;
        }).collect(Collectors.toList());
        technologyService.saveBatch(technologyList);
        /*新增标准BOM-->技术指标(批量添加)*/
        //新增的工艺路线id集合
        List<Integer> technologyIds = technologyList.stream().map(Technology::getId).collect(Collectors.toList());
        //基础数据中工艺路线id集合
        List<Integer> techTemIds = technologyTemplateList.stream().map(TechnologyTemplate::getId).collect(Collectors.toList());
        //两者长度一定一样
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < technologyIds.size(); i++) {
            List<TechnicalModel> technicalModelList = technicalModelMapper.selectList(Wrappers.<TechnicalModel>query().eq("tech_tem_id", techTemIds.get(i)));
            for (TechnicalModel technicalModel : technicalModelList) {
                Product product = new Product();
                product.setFather(technicalModel.getFather());
                product.setName(technicalModel.getName());
                product.setUnit(technicalModel.getUnit());
                product.setTechnologyId(technologyIds.get(i));
                productList.add(product);
            }
        }
        productService.saveBatch(productList);
        /*新增标准BOM-->物料清单(批量添加)*/
        List<Mbom> mbomList = new ArrayList<>();
        for (int i = 0; i < technologyIds.size(); i++) {
            List<MbomModel> mbomModelList = mbomModelMapper.selectList(Wrappers.<MbomModel>query().eq("tech_tem_id", techTemIds.get(i)));
            for (MbomModel mbomModel : mbomModelList) {
                Mbom mbom = new Mbom();
                mbom.setUnit(mbomModel.getUnit());
                mbom.setName(mbomModel.getName());
                mbom.setSupplier(mbomModel.getSupplier());
                mbom.setQualityTraceability(mbomModel.getQualityTraceability());
                mbom.setSpecifications(mbomModel.getSpecifications());
                mbom.setTechnologyId(technologyIds.get(i));
                mbomList.add(mbom);
            }
        }
        mbomService.saveBatch(mbomList);
        /*新增标准BOM-->生产工艺(批量添加)*/
        List<Technique> techniqueList = new ArrayList<>();
        for (int i = 0; i < technologyIds.size(); i++) {
            List<TechniqueModel> techniqueModelList = techniqueModelMapper.selectList(Wrappers.<TechniqueModel>query().eq("tech_tem_id", techTemIds.get(i)));
            for (TechniqueModel techniqueModel : techniqueModelList) {
                //查询设备名称
                Device device = deviceMapper.selectById(techniqueModel.getDeviceId());
                //查询基础生产工艺中每个设备的具体项目
                TechnicalModel technicalModel = technicalModelMapper.selectById(techniqueModel.getTechnicalModelId());
                Technique technique = new Technique();
                technique.setTechnologyId(technologyIds.get(i));
                technique.setDevice(device.getName());
                technique.setProductFather(technicalModel.getFather());
                technique.setProduct(technicalModel.getName());
                technique.setUnit(technicalModel.getUnit());
                techniqueList.add(technique);
            }
        }
        techniqueService.saveBatch(techniqueList);
        return "添加标准【"+ standardDto.getStandard() +"】成功!";
    }
}