package com.yuanchu.mom.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.yuanchu.mom.mapper.*; import com.yuanchu.mom.pojo.*; import com.yuanchu.mom.pojo.dto.MaterialDto; 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 java.util.Map; import java.util.stream.Collectors; /** * @author Administrator * @description 针对表【material】的数据库操作Service实现 * @createDate 2023-07-26 15:52:50 */ @Service public class MaterialServiceImpl extends ServiceImpl implements MaterialService { @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; //标准MOM-->左侧五级树展示 @Override public List> selectTreeByMaterial() { return materialMapper.selectTreeByMaterial(); } //(1,2级)新增-->物料,标准,型号 @Override @Transactional(rollbackFor = Exception.class) public String addMaterial(MaterialDto materialDto) { //校验添加物料是否重复 if (materialMapper.selectOne(Wrappers.query() .eq("type", materialDto.getType()) .eq("father", materialDto.getFather())).getName().equals(materialDto.getName())) { return "该类型产品大类下有该产品名称"; } /*新增物料表*/ Material material = new Material(); material.setCode(MyUtil.getTimeSixNumberCode("ML", "ML")); material.setName(materialDto.getName()); material.setType(materialDto.getType()); material.setFather(materialDto.getFather()); materialMapper.insert(material); /*新增标准表*/ Standard standard = new Standard(); standard.setName(materialDto.getStandard()); standard.setMaterial_id(material.getId()); standardMapper.insert(standard); /*新增型号表*/ Specifications specifications = new Specifications(); specifications.setName(materialDto.getSpecifications()); specifications.setStandardId(standard.getId()); specificationsMapper.insert(specifications); /*新增标准BOM-->工艺路线(批量添加)*/ List technologyTemplateList = technologyTemplateMapper.selectList(Wrappers.query().eq("type", materialDto.getFather())); 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.setSupplier(mbomModel.getSupplier()); mbom.setQualityTraceability(mbomModel.getQualityTraceability()); mbom.setSpecifications(mbomModel.getSpecifications()); mbom.setTechnologyId(technologyIds.get(i)); mbomList.add(mbom); } } mbomService.saveBatch(mbomList); /*新增标准BOM-->生产工艺(批量添加)*/ List techniqueList = new ArrayList<>(); for (int i = 0; i < technologyIds.size(); i++) { List techniqueModelList = techniqueModelMapper.selectList(Wrappers.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 "添加物料【" + materialDto.getName() + "】成功"; } }