| | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.production.pojo.ProductMaterial; |
| | | import com.ruoyi.production.pojo.ProductMaterialSku; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | |
| | | import com.ruoyi.production.pojo.ProductProcess; |
| | | import com.ruoyi.production.pojo.ProductStructure; |
| | | import com.ruoyi.production.service.*; |
| | | import com.ruoyi.project.system.domain.SysDictData; |
| | | import com.ruoyi.project.system.mapper.SysDictDataMapper; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | private ProductProcessService productProcessService; |
| | | |
| | | @Autowired |
| | | private ProductMaterialService productMaterialService; |
| | | |
| | | @Autowired |
| | | private ProductMaterialSkuService productMaterialSkuService; |
| | | private SysDictDataMapper sysDictDataMapper; |
| | | |
| | | @Override |
| | | public IPage<ProductBomDto> listPage(Page page, ProductBomDto productBomDto) { |
| | | public IPage<ProductBomDto> listPage(Page<ProductBom> page, ProductBomDto productBomDto) { |
| | | return productBomMapper.listPage(page, productBomDto); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult add(ProductBom productBom) { |
| | | if (productBom == null || productBom.getDictCode() == null) { |
| | | throw new ServiceException("æ°å¢å¤±è´¥,产åç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | |
| | | SysDictData sysDictData = sysDictDataMapper.selectDictDataById(productBom.getDictCode()); |
| | | if (sysDictData == null) { |
| | | throw new ServiceException("æ°å¢å¤±è´¥,产åç±»åä¸åå¨"); |
| | | } |
| | | |
| | | boolean save = productBomMapper.insert(productBom) > 0; |
| | | if (save) { |
| | | String no = "BM." + String.format("%05d", productBom.getId()); |
| | | productBom.setBomNo(no); |
| | | productBomMapper.updateById(productBom); |
| | | |
| | | // æ¥è¯¢åºäº§å模åä¿¡æ¯ |
| | | if (productBom.getProductModelId() == null) { |
| | | throw new ServiceException("è¯·éæ©äº§åæ¨¡å"); |
| | | } |
| | | |
| | | ProductMaterialSku productModel = productMaterialSkuService.getById(productBom.getProductModelId()); |
| | | if (productModel == null) { |
| | | throw new ServiceException("éæ©çäº§åæ¨¡åä¸åå¨"); |
| | | } |
| | | ProductMaterial productMaterial = productMaterialService.getById(productModel.getProductId()); |
| | | |
| | | // æ·»å åå§ç产åç»æ |
| | | ProductStructure productStructure = new ProductStructure(); |
| | | productStructure.setProductModelId(productBom.getProductModelId()); |
| | | productStructure.setUnit(productMaterial != null ? productMaterial.getUnit() : null); |
| | | productStructure.setUnitQuantity(BigDecimal.valueOf(1)); |
| | | productStructure.setBomId(productBom.getId()); |
| | | |
| | | productStructureService.save(productStructure); |
| | | |
| | | return AjaxResult.success(); |
| | | } |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult uploadBom(MultipartFile file) { |
| | | public AjaxResult uploadBom(MultipartFile file, Long dictCode) { |
| | | if (dictCode == null) { |
| | | return AjaxResult.error("导å
¥å¤±è´¥,产åç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | SysDictData sysDictData = sysDictDataMapper.selectDictDataById(dictCode); |
| | | if (sysDictData == null) { |
| | | return AjaxResult.error("导å
¥å¤±è´¥,产åç±»åä¸åå¨"); |
| | | } |
| | | |
| | | ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class); |
| | | List<BomImportDto> list; |
| | | try { |
| | |
| | | |
| | | if (list == null || list.isEmpty()) return AjaxResult.error("æ°æ®ä¸ºç©º"); |
| | | |
| | | // å¤çå·¥åº |
| | | // å¤çåæ®µæ¸
ç |
| | | list.forEach(dto -> { |
| | | dto.setParentName(clean(dto.getParentName())); |
| | | dto.setParentSpec(clean(dto.getParentSpec())); |
| | | dto.setChildName(clean(dto.getChildName())); |
| | | dto.setChildSpec(clean(dto.getChildSpec())); |
| | | dto.setParentCode(clean(dto.getParentCode())); |
| | | dto.setCode(clean(dto.getCode())); |
| | | }); |
| | | handleProcess(list); |
| | | Map<String, Long> processMap = productProcessService.list().stream() |
| | | .collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1)); |
| | | |
| | | // å建 BOM æ°æ® |
| | | BomImportDto first = list.get(0); |
| | | ProductMaterialSku rootModel = findModel(first.getParentName(), first.getParentSpec()); |
| | | ProductBom bom = new ProductBom(); |
| | | bom.setProductModelId(rootModel.getId()); |
| | | bom.setVersion("1.0"); |
| | | bom.setDictCode(dictCode); |
| | | productBomMapper.insert(bom); |
| | | bom.setBomNo("BM." + String.format("%05d", bom.getId())); |
| | | productBomMapper.updateById(bom); |
| | | |
| | | // è®°å½å·²ç»æå
¥ç»æçèç¹ï¼Key = "åç§°+è§æ ¼", Value = structure_id |
| | | // è®°å½å·²ç»æå
¥ç»æçèç¹ï¼Key = "ç¼ç ", Value = structure_id |
| | | Map<String, Long> treePathMap = new HashMap<>(); |
| | | |
| | | for (int i = 0; i < list.size(); i++) { |
| | | BomImportDto dto = list.get(i); |
| | | String parentKey = dto.getParentName() + "|" + dto.getParentSpec(); |
| | | String childKey = dto.getChildName() + "|" + dto.getChildSpec(); |
| | | String currentCode = dto.getCode(); |
| | | String parentCode = dto.getParentCode(); |
| | | |
| | | //å¤çæ ¹èç¹,第ä¸è¡ä¸å项为空 |
| | | if (i == 0 && StringUtils.isBlank(dto.getChildName())) { |
| | | // å¤çæ ¹èç¹ï¼ä¸è¬æç¬¬ä¸è¡ä¸æ²¡æç¶é¡¹ç¼å· |
| | | if (i == 0 && StringUtils.isBlank(parentCode)) { |
| | | ProductStructure rootNode = new ProductStructure(); |
| | | rootNode.setBomId(bom.getId()); |
| | | rootNode.setParentId(null); // 顶屿²¡æç¶èç¹ |
| | | rootNode.setProductModelId(rootModel.getId()); |
| | | if (processMap.containsKey(dto.getProcess())) { |
| | | rootNode.setProcessId(processMap.get(dto.getProcess())); |
| | | } |
| | | rootNode.setUnitQuantity(BigDecimal.ONE); |
| | | ProductMaterial rootMaterial = productMaterialService.getById(rootModel.getProductId()); |
| | | rootNode.setUnit(rootMaterial != null ? rootMaterial.getUnit() : null); |
| | | productStructureService.save(rootNode); |
| | | |
| | | treePathMap.put(parentKey, rootNode.getId()); |
| | | treePathMap.put(currentCode, rootNode.getId()); |
| | | continue; |
| | | } |
| | | |
| | | // å¤çåå±çº§èç¹ |
| | | // æ¾å°ç¶èç¹å¨æ°æ®åºéç ID |
| | | Long parentStructureId = treePathMap.get(parentKey); |
| | | Long parentStructureId = treePathMap.get(parentCode); |
| | | if (parentStructureId == null) { |
| | | // 妿 Map éæ¾ä¸å°ï¼è¯´æ Excel 顺åºä¹±äºæè
æ°æ®æè¯¯ |
| | | throw new ServiceException("导å
¥å¤±è´¥: ç¶é¡¹[" + dto.getParentName() + "]å¿
é¡»å¨å
¶å项ä¹åå®ä¹"); |
| | | throw new ServiceException("导å
¥å¤±è´¥: ç¶é¡¹[" + parentCode + "]å¿
é¡»å¨å
¶å项ä¹åå®ä¹"); |
| | | } |
| | | |
| | | // è·åå项模åä¿¡æ¯ |
| | | ProductMaterialSku childModel = findModel(dto.getChildName(), dto.getChildSpec()); |
| | | |
| | | // æå
¥ç»æè¡¨ |
| | | ProductStructure node = new ProductStructure(); |
| | | node.setBomId(bom.getId()); |
| | | node.setParentId(parentStructureId); // ç¶èç¹ID |
| | | node.setProductModelId(childModel.getId()); |
| | | node.setUnitQuantity(dto.getUnitQty()); |
| | | ProductMaterial childMaterial = productMaterialService.getById(childModel.getProductId()); |
| | | node.setUnit(childMaterial != null ? childMaterial.getUnit() : null); |
| | | if (processMap.containsKey(dto.getProcess())) { |
| | | node.setProcessId(processMap.get(dto.getProcess())); |
| | | } |
| | | productStructureService.save(node); |
| | | |
| | | // æå½åå项记å½å° Map,ä½ä¸ºä»¥åæ´æ·±å±çº§çç¶é¡¹æ¥æ¾ä¾æ® |
| | | // åä¸ç¶é¡¹ä¸çååå项ä¸éè¦éå¤è®°å½ |
| | | treePathMap.put(childKey, node.getId()); |
| | | // æå½å项记å½å° Map, ä½ä¸ºä»¥åæ´æ·±å±çº§çç¶é¡¹æ¥æ¾ä¾æ® |
| | | treePathMap.put(currentCode, node.getId()); |
| | | } |
| | | |
| | | return AjaxResult.success("BOM导å
¥æå"); |
| | |
| | | for (ProductStructureDto root : treeData) { |
| | | // æ·»å æ ¹èç¹ |
| | | BomImportDto rootRow = new BomImportDto(); |
| | | rootRow.setParentName(root.getProductName()); |
| | | rootRow.setParentSpec(root.getModel()); |
| | | rootRow.setCode(root.getId().toString()); |
| | | rootRow.setUnitQty(root.getUnitQuantity()); |
| | | rootRow.setRemark(""); |
| | | exportList.add(rootRow); |
| | |
| | | } |
| | | |
| | | BomImportDto row = new BomImportDto(); |
| | | // ç¶ç±»ä¿¡æ¯ |
| | | row.setParentName(parent.getProductName()); |
| | | row.setParentSpec(parent.getModel()); |
| | | // åç±»ä¿¡æ¯ |
| | | row.setChildName(child.getProductName()); |
| | | row.setChildSpec(child.getModel()); |
| | | // ç¶ç±»ç¼å· |
| | | row.setParentCode(parent.getId().toString()); |
| | | // æ¬èº«ç¼å· |
| | | row.setCode(child.getId().toString()); |
| | | row.setUnitQty(child.getUnitQuantity()); |
| | | row.setProcess(child.getProcessName()); |
| | | // row.setProcess(); |
| | | row.setRemark(""); |
| | | |
| | | exportList.add(row); |
| | | |
| | |
| | | map.put(node.getId(), node); |
| | | populateMap(node.getChildren(), map); |
| | | } |
| | | } |
| | | |
| | | private ProductMaterialSku findModel(String name, String spec) { |
| | | ProductMaterial product = productMaterialService.getOne(new LambdaQueryWrapper<ProductMaterial>() |
| | | .eq(ProductMaterial::getProductName, name).last("limit 1")); |
| | | if (product == null) throw new ServiceException("äº§åæªç»´æ¤ï¼" + name); |
| | | |
| | | ProductMaterialSku model = productMaterialSkuService.getOne(new LambdaQueryWrapper<ProductMaterialSku>() |
| | | .eq(ProductMaterialSku::getProductId, product.getId()) |
| | | .eq(ProductMaterialSku::getModel, spec).last("limit 1")); |
| | | if (model == null) throw new ServiceException("è§æ ¼æªç»´æ¤ï¼" + name + "[" + spec + "]"); |
| | | return model; |
| | | } |
| | | |
| | | private void handleProcess(List<BomImportDto> list) { |