| | |
| | | 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.basic.pojo.Product; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.http.HttpUtils; |
| | | import com.ruoyi.framework.config.AliDingConfig; |
| | | import com.ruoyi.production.pojo.ProductOrder; |
| | | import com.ruoyi.production.service.ProductOrderService; |
| | | import com.ruoyi.productionPlan.dto.ProductionPlanDto; |
| | | import com.ruoyi.productionPlan.dto.ProductionPlanSummaryDto; |
| | | import com.ruoyi.productionPlan.mapper.ProductionPlanMapper; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.time.Instant; |
| | | import java.time.LocalDateTime; |
| | |
| | | @Autowired |
| | | private ProductionPlanMapper productionPlanMapper; |
| | | |
| | | @Autowired |
| | | private ProductOrderService productOrderService; |
| | | |
| | | /** |
| | | * 同步锁,确保手动和定时任务不同时执行 |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 合并生产计划 |
| | | */ |
| | | @Override |
| | | public boolean combine(ProductionPlanDto productionPlanDto) { |
| | | if (productionPlanDto.getIds() == null || productionPlanDto.getIds().isEmpty()) { |
| | | return false; |
| | | } |
| | | |
| | | // 查询主生产计划 |
| | | List<ProductionPlan> plans = productionPlanMapper.selectBatchIds(productionPlanDto.getIds()); |
| | | |
| | | // 校验是否存在不同的产品名称 |
| | | String firstProductName = plans.get(0).getProductName(); |
| | | if (plans.stream().anyMatch(p -> !p.getProductName().equals(firstProductName))) { |
| | | log.warn("合并失败,存在不同的产品名称"); |
| | | return false; |
| | | } |
| | | |
| | | // 校验是否存在不同的产品规格 |
| | | String firstProductSpec = plans.get(0).getProductSpec(); |
| | | if (plans.stream().anyMatch(p -> !p.getProductSpec().equals(firstProductSpec))) { |
| | | log.warn("合并失败,存在不同的产品规格"); |
| | | return false; |
| | | } |
| | | |
| | | |
| | | // 叠加方数 |
| | | BigDecimal totalVolume = plans.stream() |
| | | .map(ProductionPlan::getVolume) |
| | | .filter(v -> v != null) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | // 判断下发数量是否大于等于方数 |
| | | if (productionPlanDto.getTotalAssignedQuantity().compareTo(totalVolume) > 0) { |
| | | log.warn("合并失败,下发数量不能大于方数"); |
| | | return false; |
| | | } |
| | | |
| | | // 根据下发数量,从第一个生产计划开始分配方数 |
| | | BigDecimal assignedVolume = BigDecimal.ZERO; |
| | | for (ProductionPlan plan : plans) { |
| | | BigDecimal volume = plan.getVolume(); |
| | | if (volume == null) { |
| | | continue; |
| | | } |
| | | |
| | | if (assignedVolume.add(volume).compareTo(productionPlanDto.getTotalAssignedQuantity()) >= 0) { |
| | | // 最后一个计划,分配剩余方数 |
| | | plan.setAssignedQuantity(productionPlanDto.getTotalAssignedQuantity().subtract(assignedVolume)); |
| | | break; |
| | | } |
| | | |
| | | // 分配当前计划方数 |
| | | plan.setAssignedQuantity(volume); |
| | | productionPlanMapper.updateById(plan); |
| | | assignedVolume = assignedVolume.add(volume); |
| | | } |
| | | |
| | | // 创建生产订单 |
| | | ProductOrder productOrder = new ProductOrder(); |
| | | String combineIds = StringUtils.join(productionPlanDto.getIds(), ","); |
| | | productOrder.setCombineProductionPlanIds(combineIds); |
| | | productOrder.setQuantity(productionPlanDto.getTotalAssignedQuantity()); |
| | | productOrder.setPlanCompleteTime(productionPlanDto.getPlanCompleteTime()); |
| | | productOrderService.addProductOrder(productOrder); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 同步数据 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |