package com.yuanchu.mom.service.impl; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.yuanchu.mom.mapper.*; import com.yuanchu.mom.pojo.*; import com.yuanchu.mom.pojo.dto.SpecificationsDto; import com.yuanchu.mom.service.*; import com.yuanchu.mom.utils.MyUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @author Administrator * @description 针对表【specifications】的数据库操作Service实现 * @createDate 2023-07-26 16:01:49 */ @Service public class SpecificationsServiceImpl extends ServiceImpl implements SpecificationsService { @Resource MaterialMapper materialMapper; @Resource SpecificationsMapper specificationsMapper; @Resource TechnologyService technologyService; @Resource TechnologyTemplateMapper technologyTemplateMapper; @Resource ProductService productService; @Resource TechnicalModelMapper technicalModelMapper; @Resource MbomService mbomService; @Resource MbomModelMapper mbomModelMapper; @Resource ProductMapper productMapper; @Resource MbomMapper mbomMapper; @Resource TechnologyMapper technologyMapper; //(4级)新增-->型号 @Override @Transactional(rollbackFor = Exception.class) public String addSpecifications(SpecificationsDto specificationsDto, Integer spId, Integer ver) { //校验添加该标准下的型号是否重复 List specificationsNameList = specificationsMapper.selectList(Wrappers.query().eq("standard_id", specificationsDto.getId())).stream().map(specifications -> { String specificationsName = specifications.getName(); return specificationsName; }).collect(Collectors.toList()); if (specificationsNameList.contains(specificationsDto.getSpecifications())) { return "该标准下有该型号"; } /*新增型号表*/ Specifications specifications = new Specifications(); specifications.setStandardId(specificationsDto.getId()); specifications.setName(specificationsDto.getSpecifications()); specificationsMapper.insert(specifications); /** * 根据选择的情况,判断是拉取基础数据的数据,还是某一个产品下某一个型号的数据 */ //如果spId传的参数是0,则拉取基础数据的数据 if (spId == 0) { /*新增标准BOM-->工艺路线(批量添加)*/ //根据标准id查询物料大类 Material material = materialMapper.selFath(specificationsDto.getId()); List technologyTemplateList = technologyTemplateMapper.selectList(Wrappers.query().eq("type", material.getFather())); if (ObjectUtils.isEmpty(technologyTemplateList)) { return "添加型号【" + specificationsDto.getSpecifications() + "】成功!"; } List 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 technologyIds = technologyList.stream().map(Technology::getId).collect(Collectors.toList()); //基础数据中工艺路线id集合 List techTemIds = technologyTemplateList.stream().map(TechnologyTemplate::getId).collect(Collectors.toList()); //两者长度一定一样 List productList = new ArrayList<>(); for (int i = 0; i < technologyIds.size(); i++) { List technicalModelList = technicalModelMapper.selectList(Wrappers.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 mbomList = new ArrayList<>(); for (int i = 0; i < technologyIds.size(); i++) { List mbomModelList = mbomModelMapper.selectList(Wrappers.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.setQualityTraceability(mbomModel.getQualityTraceability()); mbom.setSpecifications(mbomModel.getSpecifications()); mbom.setTechnologyId(technologyIds.get(i)); mbomList.add(mbom); } } mbomService.saveBatch(mbomList); return "添加型号【" + specificationsDto.getSpecifications() + "】成功!"; } //如果spId!=0,等于选择的具体型号,则添加该型号版本的数据 /*新增标准BOM-->工艺路线(批量添加)*/ List technologyList = technologyMapper.selAllByVerSpId(spId, ver); List technologys = technologyList.stream().map(technology -> { Technology tec = new Technology(); tec.setSpecificationsId(specifications.getId()); tec.setName(technology.getName()); tec.setFather(technology.getFather()); tec.setDeviceGroup(technology.getDeviceGroup()); tec.setProductionQuota(technology.getProductionQuota()); return tec; }).collect(Collectors.toList()); technologyService.saveBatch(technologys); //两者长度一定一样 for (int i = 0; i < technologyList.size(); i++) { /*新增标准BOM-->技术指标(批量添加)*/ List productList = productMapper.selProByVerSpe(technologyList.get(i).getId()); List products = new ArrayList<>(); for (Product product : productList) { Product pro = new Product(); pro.setTechnologyId(technologys.get(i).getId()); pro.setName(product.getName()); pro.setFather(product.getFather()); pro.setUnit(product.getUnit()); pro.setRequired(product.getRequired()); pro.setInternal(product.getInternal()); products.add(pro); } productService.saveBatch(products); /*新增标准BOM-->物料清单(批量添加)*/ List mbomList = mbomMapper.seleByTechId(technologyList.get(i).getId()); List mboms = new ArrayList<>(); for (Mbom mbom : mbomList) { Mbom mbo = new Mbom(); mbo.setTechnologyId(technologys.get(i).getId()); mbo.setName(mbom.getName()); mbo.setUnit(mbom.getUnit()); mbo.setUnit(mbom.getUnit()); mbo.setNum(mbom.getNum()); mbo.setQualityTraceability(mbom.getQualityTraceability()); mbo.setSpecifications(mbom.getSpecifications()); mboms.add(mbo); } mbomService.saveBatch(mboms); } return "添加型号【" + specificationsDto.getSpecifications() + "】成功!"; } }