| | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.BooleanUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.route.vo.MesProRouteImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.route.vo.MesProRouteImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.route.vo.MesProRoutePageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.route.vo.MesProRouteSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.route.MesProRouteDO; |
| | |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.route.MesProRouteProductDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.route.MesProRouteProductBomDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemTypeDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.pro.route.MesProRouteMapper; |
| | | import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService; |
| | | import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemTypeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.Comparator; |
| | | import java.util.Arrays; |
| | | import java.util.stream.Collectors; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | |
| | | @Resource |
| | | @Lazy |
| | | private MesMdItemService itemService; |
| | | @Resource |
| | | private MesMdItemTypeService itemTypeService; |
| | | @Resource |
| | | private MesWmWarehouseAreaService warehouseAreaService; |
| | | |
| | | @Override |
| | | public Long createRoute(MesProRouteSaveReqVO createReqVO) { |
| | |
| | | // 3. 所有产品必须配置了 BOM 消耗 |
| | | List<MesProRouteProductDO> productList = routeProductService.getRouteProductListByRouteId(routeId); |
| | | for (MesProRouteProductDO product : productList) { |
| | | MesMdItemDO item = itemService.validateItemExists(product.getItemId()); |
| | | if (product.getWarehouseId() == null || product.getLocationId() == null || product.getAreaId() == null) { |
| | | throw exception(PRO_ROUTE_PRODUCT_WAREHOUSE_REQUIRED, item.getName()); |
| | | } |
| | | warehouseAreaService.validateWarehouseAreaExists(product.getWarehouseId(), |
| | | product.getLocationId(), product.getAreaId()); |
| | | List<MesProRouteProductBomDO> bomList = routeProductBomService |
| | | .getRouteProductBomList(routeId, null, product.getItemId()); |
| | | if (CollUtil.isEmpty(bomList)) { |
| | | MesMdItemDO item = itemService.validateItemExists(product.getItemId()); |
| | | throw exception(PRO_ROUTE_ENABLE_PRODUCT_NO_BOM, item.getName()); |
| | | } |
| | | } |
| | | validateProcessOutputs(processList, productList); |
| | | } |
| | | |
| | | private void validateProcessOutputs(List<MesProRouteProcessDO> processList, |
| | | List<MesProRouteProductDO> productList) { |
| | | if (CollUtil.isEmpty(productList)) { |
| | | throw exception(PRO_ROUTE_LAST_OUTPUT_INVALID, processList.get(processList.size() - 1).getProcessId()); |
| | | } |
| | | Set<Long> boundItemIds = productList.stream().map(MesProRouteProductDO::getItemId).collect(Collectors.toSet()); |
| | | Set<Long> finishedItemIds = productList.stream() |
| | | .map(MesProRouteProductDO::getItemId) |
| | | .filter(itemId -> isItemUnderCategory(itemId, "FG")) |
| | | .collect(Collectors.toSet()); |
| | | MesProRouteProcessDO lastProcess = processList.stream() |
| | | .max(Comparator.comparing(MesProRouteProcessDO::getSort)).orElseThrow(); |
| | | for (MesProRouteProcessDO process : processList) { |
| | | Set<Long> outputIds = parseOutputItemIds(process.getOutputItemId()); |
| | | if (CollUtil.isEmpty(outputIds)) { |
| | | throw exception(PRO_ROUTE_PROCESS_OUTPUT_REQUIRED, process.getProcessId()); |
| | | } |
| | | for (Long outputId : outputIds) { |
| | | if (!boundItemIds.contains(outputId)) { |
| | | MesMdItemDO item = itemService.validateItemExists(outputId); |
| | | throw exception(PRO_ROUTE_PROCESS_OUTPUT_NOT_BOUND, item.getName()); |
| | | } |
| | | } |
| | | if (process.getId().equals(lastProcess.getId())) { |
| | | if (finishedItemIds.isEmpty() || !outputIds.equals(finishedItemIds)) { |
| | | throw exception(PRO_ROUTE_LAST_OUTPUT_INVALID, process.getProcessId()); |
| | | } |
| | | } else if (outputIds.stream().anyMatch(itemId -> !isItemUnderCategory(itemId, "SF"))) { |
| | | throw exception(PRO_ROUTE_MIDDLE_OUTPUT_INVALID, process.getProcessId()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private Set<Long> parseOutputItemIds(String value) { |
| | | if (StrUtil.isBlank(value)) { |
| | | return Collections.emptySet(); |
| | | } |
| | | return Arrays.stream(value.split(",")).map(String::trim).filter(StrUtil::isNotBlank) |
| | | .map(Long::valueOf).collect(Collectors.toSet()); |
| | | } |
| | | |
| | | private boolean isItemUnderCategory(Long itemId, String rootCode) { |
| | | MesMdItemDO item = itemService.validateItemExists(itemId); |
| | | MesMdItemTypeDO type = itemTypeService.getItemType(item.getItemTypeId()); |
| | | while (type != null) { |
| | | if (rootCode.equals(type.getCode())) { |
| | | return true; |
| | | } |
| | | if (type.getParentId() == null || MesMdItemTypeDO.PARENT_ID_ROOT.equals(type.getParentId())) { |
| | | return false; |
| | | } |
| | | type = itemTypeService.getItemType(type.getParentId()); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | |
| | | return routeMapper.selectByIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesProRouteImportRespVO importRouteList(List<MesProRouteImportExcelVO> importList, |
| | | boolean updateSupport) { |
| | | // 1. 校验非空 |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(PRO_ROUTE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐行处理 |
| | | MesProRouteImportRespVO respVO = MesProRouteImportRespVO.builder() |
| | | .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>()) |
| | | .failureCodes(new LinkedHashMap<>()).build(); |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | importList.forEach(importItem -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String key = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(importItem.getCode())) { |
| | | respVO.getFailureCodes().put(key, "工艺路线编码不能为空"); |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(importItem.getName())) { |
| | | respVO.getFailureCodes().put(key, "工艺路线名称不能为空"); |
| | | return; |
| | | } |
| | | // 2.1 判断:创建 or 更新 |
| | | MesProRouteDO existRoute = routeMapper.selectByCode(importItem.getCode()); |
| | | if (existRoute == null) { |
| | | // 2.1.1 新增(与手工创建一致:默认禁用,通过启用接口校验后启用) |
| | | MesProRouteDO route = BeanUtils.toBean(importItem, MesProRouteDO.class); |
| | | route.setId(null); |
| | | route.setStatus(CommonStatusEnum.DISABLE.getStatus()); |
| | | routeMapper.insert(route); |
| | | respVO.getCreateCodes().add(importItem.getCode()); |
| | | } else if (updateSupport) { |
| | | // 2.1.2 更新(状态由启用/禁用接口单独维护,导入不修改状态) |
| | | MesProRouteDO updateObj = BeanUtils.toBean(importItem, MesProRouteDO.class); |
| | | updateObj.setId(existRoute.getId()); |
| | | updateObj.setCode(existRoute.getCode()); |
| | | routeMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(importItem.getCode()); |
| | | } else { |
| | | respVO.getFailureCodes().put(key, "工艺路线编码已存在"); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |