| | |
| | | // 查询主生产计划 |
| | | List<ProductionPlanDto> plans = productionPlanMapper.selectWithMaterialByIds(productionPlanDto.getIds()); |
| | | |
| | | if (plans == null || plans.isEmpty()) { |
| | | throw new ServiceException("下发失败,生产计划不存在"); |
| | | } |
| | | |
| | | // 校验是否存在不同的产品名称 |
| | | String firstProductName = plans.get(0).getProductName(); |
| | | if (plans.stream().anyMatch(p -> p.getProductName() == null || !p.getProductName().equals(firstProductName))) { |
| | |
| | | productOrder.setPlanCompleteTime(productionPlanDto.getPlanCompleteTime()); |
| | | productOrder.setStatus(ProductOrderStatusEnum.WAIT.getCode()); |
| | | productOrder.setStrength(productionPlanDto.getStrength()); |
| | | productOrder.setProductMaterialSkuId(productionPlanDto.getProductMaterialSkuId()); |
| | | productOrder.setProductMaterialSkuId(plans.get(0).getProductMaterialSkuId()); |
| | | |
| | | Long orderId = productOrderService.insertProductOrder(productOrder); |
| | | |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean add(ProductionPlanDto productionPlanDto) { |
| | | if (StringUtils.isEmpty(productionPlanDto.getApplyNo())) { |
| | | throw new ServiceException("新增失败,申请单编号不能为空"); |
| | | } |
| | | Long count = productionPlanMapper.selectCount(Wrappers.<ProductionPlan>lambdaQuery() |
| | | .eq(ProductionPlan::getApplyNo, productionPlanDto.getApplyNo())); |
| | | if (count > 0) { |
| | | throw new ServiceException("新增失败,申请单编号 " + productionPlanDto.getApplyNo() + " 已存在"); |
| | | } |
| | | productionPlanDto.setDataSourceType(DataSourceTypeEnum.MANUAL.getCode()); |
| | | productionPlanDto.setStatus(0); |
| | | productionPlanMapper.insert(productionPlanDto); |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean update(ProductionPlanDto productionPlanDto) { |
| | | // 已下发状态,不能编辑 |
| | | if (productionPlanDto.getStatus() != 0) { |
| | | throw new BaseException("已下发或部分下发状态,不能编辑"); |
| | | if (productionPlanDto == null || productionPlanDto.getId() == null) { |
| | | throw new ServiceException("编辑失败,数据不能为空"); |
| | | } |
| | | ProductionPlan productionPlan = getById(productionPlanDto.getId()); |
| | | if (productionPlan == null) { |
| | | throw new ServiceException("编辑失败,主生产计划不存在"); |
| | | } |
| | | |
| | | if (StringUtils.isNotEmpty(productionPlanDto.getApplyNo()) |
| | | && !productionPlanDto.getApplyNo().equals(productionPlan.getApplyNo())) { |
| | | |
| | | Long count = productionPlanMapper.selectCount(Wrappers.<ProductionPlan>lambdaQuery() |
| | | .eq(ProductionPlan::getApplyNo, productionPlanDto.getApplyNo()) |
| | | .ne(ProductionPlan::getId, productionPlanDto.getId())); // 排除自身 |
| | | |
| | | if (count > 0) { |
| | | throw new ServiceException("编辑失败,申请单编号 " + productionPlanDto.getApplyNo() + " 已被占用"); |
| | | } |
| | | } |
| | | // 已下发状态,不能编辑 |
| | | if (productionPlan.getStatus() != 0) { |
| | | throw new BaseException("编辑失败,该生产计划已下发或部分下发状态,禁止编辑"); |
| | | } |
| | | |
| | | // 查询是否有关联订单 |
| | | boolean hasProductOrderPlan = productOrderPlanMapper.selectList(Wrappers.<ProductOrderPlan>lambdaQuery().eq(ProductOrderPlan::getProductionPlanId, productionPlanDto.getId())).stream().anyMatch(p -> p.getProductOrderId() != null); |
| | | boolean hasProductOrderPlan = productOrderPlanMapper.selectList(Wrappers.<ProductOrderPlan>lambdaQuery() |
| | | .eq(ProductOrderPlan::getProductionPlanId, productionPlanDto.getId())) |
| | | .stream().anyMatch(p -> p.getProductOrderId() != null); |
| | | |
| | | if (hasProductOrderPlan) { |
| | | // 如果关联,方数只能递增 |
| | | ProductionPlan currentPlan = productionPlanMapper.selectById(productionPlanDto.getId()); |
| | | if (productionPlanDto.getVolume().compareTo(currentPlan.getVolume()) < 0) { |
| | | if (productionPlanDto.getVolume().compareTo(productionPlan.getVolume()) < 0) { |
| | | throw new BaseException("方数不能递减"); |
| | | } |
| | | } |
| | |
| | | if (list == null || list.isEmpty()) { |
| | | throw new ServiceException("Excel没有数据"); |
| | | } |
| | | List<ProductionPlan> entityList = new ArrayList<>(list.size()); |
| | | ProductionPlan entity; |
| | | for (ProductionPlanImportDto dto : list) { |
| | | entity = new ProductionPlan(); |
| | | BeanUtils.copyProperties(dto, entity); |
| | | entity.setAssignedQuantity(BigDecimal.ZERO); |
| | | entity.setCreateTime(LocalDateTime.now()); |
| | | entity.setUpdateTime(LocalDateTime.now()); |
| | | entity.setDataSourceType(DataSourceTypeEnum.DING_TALK.getCode()); |
| | | |
| | | // 根据物料编码填充关联ID |
| | | if (StringUtils.isNotEmpty(dto.getMaterialCode())) { |
| | | LambdaQueryWrapper<ProductMaterialSku> skuQueryWrapper = new LambdaQueryWrapper<>(); |
| | | skuQueryWrapper.eq(ProductMaterialSku::getMaterialCode, dto.getMaterialCode()); |
| | | ProductMaterialSku sku = productMaterialSkuService.getOne(skuQueryWrapper); |
| | | if (sku != null) { |
| | | entity.setProductMaterialSkuId(sku.getId()); |
| | | Set<String> applyNos = new HashSet<>(); |
| | | Set<String> materialCodes = new HashSet<>(); |
| | | for (int i = 0; i < list.size(); i++) { |
| | | ProductionPlanImportDto dto = list.get(i); |
| | | String applyNo = dto.getApplyNo(); |
| | | String materialCode = dto.getMaterialCode(); |
| | | |
| | | if (StringUtils.isEmpty(applyNo)) { |
| | | throw new ServiceException("导入失败:第 " + (i + 2) + " 行申请单编号不能为空"); |
| | | } |
| | | if (!applyNos.add(applyNo)) { |
| | | throw new ServiceException("导入失败:Excel 中存在重复的申请单编号: " + applyNo); |
| | | } |
| | | if (StringUtils.isEmpty(materialCode)) { |
| | | throw new ServiceException("导入失败:第 " + (i + 2) + " 行物料编码不能为空"); |
| | | } |
| | | |
| | | String strength = dto.getStrength(); |
| | | if (StringUtils.isNotEmpty(strength)) { |
| | | if (!"A3.5".equals(strength) && !"A5.0".equals(strength)) { |
| | | throw new ServiceException("导入失败:第 " + (i + 2) + " 行强度只能是 A3.5 或 A5.0"); |
| | | } |
| | | } |
| | | |
| | | entityList.add(entity); |
| | | materialCodes.add(materialCode); |
| | | } |
| | | |
| | | // 申请单编号是否已存在 |
| | | Long existApplyNoCount = baseMapper.selectCount(Wrappers.<ProductionPlan>lambdaQuery() |
| | | .in(ProductionPlan::getApplyNo, applyNos)); |
| | | if (existApplyNoCount > 0) { |
| | | List<String> existApplyNos = baseMapper.selectList(Wrappers.<ProductionPlan>lambdaQuery() |
| | | .in(ProductionPlan::getApplyNo, applyNos)) |
| | | .stream().map(ProductionPlan::getApplyNo).collect(Collectors.toList()); |
| | | throw new ServiceException("导入失败,申请单编号已存在: " + String.join(", ", existApplyNos)); |
| | | } |
| | | |
| | | Map<String, Long> skuMap = productMaterialSkuService.list(Wrappers.<ProductMaterialSku>lambdaQuery() |
| | | .in(ProductMaterialSku::getMaterialCode, materialCodes)) |
| | | .stream().collect(Collectors.toMap(ProductMaterialSku::getMaterialCode, ProductMaterialSku::getId, (k1, k2) -> k1)); |
| | | |
| | | List<String> missingCodes = materialCodes.stream() |
| | | .filter(code -> !skuMap.containsKey(code)) |
| | | .collect(Collectors.toList()); |
| | | if (!missingCodes.isEmpty()) { |
| | | throw new ServiceException("导入失败,以下物料编码不存在: " + String.join(", ", missingCodes)); |
| | | } |
| | | |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | List<ProductionPlan> entityList = list.stream().map(dto -> { |
| | | ProductionPlan entity = new ProductionPlan(); |
| | | BeanUtils.copyProperties(dto, entity); |
| | | entity.setProductMaterialSkuId(skuMap.get(dto.getMaterialCode())); |
| | | entity.setAssignedQuantity(BigDecimal.ZERO); |
| | | entity.setDataSourceType(DataSourceTypeEnum.MANUAL.getCode()); |
| | | entity.setStatus(0); |
| | | entity.setCreateTime(now); |
| | | entity.setUpdateTime(now); |
| | | return entity; |
| | | }).collect(Collectors.toList()); |
| | | |
| | | this.saveBatch(entityList); |
| | | } |
| | | |