|
@@ -1,26 +1,39 @@
|
|
|
package com.sd.business.service.work.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.google.common.collect.HashBasedTable;
|
|
|
+import com.ruoyi.common.constant.StatusConstant;
|
|
|
import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.sd.business.entity.bom.po.BomSpec;
|
|
|
import com.sd.business.entity.order.po.OrderSku;
|
|
|
import com.sd.business.entity.sku.po.SkuSpec;
|
|
|
import com.sd.business.entity.work.dto.WorkOrderSelectDto;
|
|
|
import com.sd.business.entity.work.enums.WorkOrderFixationSpecEnum;
|
|
|
import com.sd.business.entity.work.po.WorkOrder;
|
|
|
+import com.sd.business.entity.work.po.WorkOrderDetail;
|
|
|
import com.sd.business.entity.work.vo.WorkOrderVo;
|
|
|
import com.sd.business.mapper.work.WorkOrderMapper;
|
|
|
+import com.sd.business.service.bom.BomSpecService;
|
|
|
+import com.sd.business.service.order.OrderSkuService;
|
|
|
import com.sd.business.service.sku.SkuSpecService;
|
|
|
+import com.sd.business.service.work.WorkOrderDetailService;
|
|
|
import com.sd.business.service.work.WorkOrderService;
|
|
|
+import com.sd.business.util.CodeEnum;
|
|
|
import com.sd.framework.util.Assert;
|
|
|
+import com.sd.framework.util.TransactionUtil;
|
|
|
import com.sd.framework.util.sql.Sql;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.locks.ReentrantLock;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@@ -35,8 +48,16 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder> implements WorkOrderService {
|
|
|
|
|
|
+ private static final ReentrantLock schedulingLock = new ReentrantLock();
|
|
|
+
|
|
|
@Autowired
|
|
|
private SkuSpecService skuSpecService;
|
|
|
+ @Autowired
|
|
|
+ private BomSpecService bomSpecService;
|
|
|
+ @Autowired
|
|
|
+ private OrderSkuService orderSkuService;
|
|
|
+ @Autowired
|
|
|
+ private WorkOrderDetailService workOrderDetailService;
|
|
|
|
|
|
@Override
|
|
|
public Page<WorkOrderVo> getPage(WorkOrderSelectDto dto) {
|
|
@@ -65,45 +86,108 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void scheduling(List<OrderSku> list) {
|
|
|
+ public void scheduling() {
|
|
|
+
|
|
|
+ // 查找未排版完成的订单sku
|
|
|
+ List<OrderSku> list = orderSkuService.list(q -> q.eq(OrderSku::getCompleteScheduling, StatusConstant.NO));
|
|
|
|
|
|
if (list.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 固定排版订单
|
|
|
- List<OrderSku> fixationList = new ArrayList<>();
|
|
|
+ // 根据主材和打印纸区分订单sku
|
|
|
+ HashBasedTable<Long, Long, List<OrderSku>> map = HashBasedTable.create();
|
|
|
+ for (OrderSku orderSku : list) {
|
|
|
|
|
|
- // 不固定排版订单
|
|
|
- List<OrderSku> notFixationList = new ArrayList<>();
|
|
|
+ List<OrderSku> itemList = map.get(orderSku.getBomSpecId(), orderSku.getPrintingPaperBomSpecId());
|
|
|
|
|
|
- List<Long> skuSpecIdList = list.stream().map(OrderSku::getSkuSpecId).collect(Collectors.toList());
|
|
|
- Map<Long, SkuSpec> skuSpecMap = skuSpecService.mapKEntity(BaseIdPo::getId, q -> q.in(BaseIdPo::getId, skuSpecIdList));
|
|
|
+ if (itemList == null) {
|
|
|
+ itemList = new ArrayList<>();
|
|
|
+ map.put(orderSku.getBomSpecId(), orderSku.getPrintingPaperBomSpecId(), itemList);
|
|
|
+ }
|
|
|
|
|
|
- for (OrderSku orderSku : list) {
|
|
|
- Long skuSpecId = orderSku.getSkuSpecId();
|
|
|
- SkuSpec skuSpec = skuSpecMap.get(skuSpecId);
|
|
|
- boolean fixation = WorkOrderFixationSpecEnum.isFixation(skuSpec.getLength(), skuSpec.getWidth());
|
|
|
+ itemList.add(orderSku);
|
|
|
|
|
|
- if (fixation) {
|
|
|
- fixationList.add(orderSku);
|
|
|
- } else {
|
|
|
- notFixationList.add(orderSku);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Long bomSpecId : map.rowKeySet()) {
|
|
|
+ for (Long printingPaperBomSpecId : map.columnKeySet()) {
|
|
|
+ List<OrderSku> orderSkuList = map.get(bomSpecId, printingPaperBomSpecId);
|
|
|
+ scheduling(bomSpecId, printingPaperBomSpecId, orderSkuList);
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 排版
|
|
|
+ *
|
|
|
+ * @param bomSpecId 主材规格id
|
|
|
+ * @param printingPaperBomSpecId 打印纸规格id
|
|
|
+ * @param list 排版列表
|
|
|
+ */
|
|
|
+ private void scheduling(Long bomSpecId, Long printingPaperBomSpecId, List<OrderSku> list) {
|
|
|
+
|
|
|
+
|
|
|
+ boolean b = schedulingLock.tryLock();
|
|
|
|
|
|
+ if (!b) {
|
|
|
+ throw new ServiceException("正在排版中,请稍后刷新数据");
|
|
|
}
|
|
|
|
|
|
- // 固定排版
|
|
|
- fixationScheduling(skuSpecMap, fixationList);
|
|
|
+ try {
|
|
|
+
|
|
|
+ if (ObjectUtil.isNotEmpty(list)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 固定排版订单
|
|
|
+ List<OrderSku> fixationList = new ArrayList<>();
|
|
|
+
|
|
|
+ // 不固定排版订单
|
|
|
+ List<OrderSku> notFixationList = new ArrayList<>();
|
|
|
+
|
|
|
+ List<Long> skuSpecIdList = list.stream().map(OrderSku::getSkuSpecId).collect(Collectors.toList());
|
|
|
+ Map<Long, SkuSpec> skuSpecMap = skuSpecService.mapKEntity(BaseIdPo::getId, q -> q.in(BaseIdPo::getId, skuSpecIdList));
|
|
|
+
|
|
|
+ for (OrderSku orderSku : list) {
|
|
|
+ Long skuSpecId = orderSku.getSkuSpecId();
|
|
|
+ SkuSpec skuSpec = skuSpecMap.get(skuSpecId);
|
|
|
+ boolean fixation = WorkOrderFixationSpecEnum.isFixation(skuSpec.getLength(), skuSpec.getWidth());
|
|
|
+
|
|
|
+ if (fixation) {
|
|
|
+ fixationList.add(orderSku);
|
|
|
+ } else {
|
|
|
+ notFixationList.add(orderSku);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 固定排版
|
|
|
+ fixationScheduling(bomSpecId, printingPaperBomSpecId, skuSpecMap, fixationList);
|
|
|
+
|
|
|
+ // 非固定排版
|
|
|
+ notFixationScheduling(bomSpecId, printingPaperBomSpecId, skuSpecMap, notFixationList);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("排版业务发生异常", e);
|
|
|
+ throw new ServiceException("排版业务发生异常");
|
|
|
+ } finally {
|
|
|
+ schedulingLock.unlock();
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 固定排版
|
|
|
*/
|
|
|
- private void fixationScheduling(Map<Long, SkuSpec> skuSpecMap, List<OrderSku> list) {
|
|
|
+ private void fixationScheduling(Long bomSpecId,
|
|
|
+ Long printingPaperBomSpecId,
|
|
|
+ Map<Long, SkuSpec> skuSpecMap,
|
|
|
+ List<OrderSku> list) {
|
|
|
|
|
|
- if (list.isEmpty()) {
|
|
|
+ if (ObjectUtil.isNotEmpty(list)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -114,20 +198,138 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
|
|
|
if (skuSpec == null) {
|
|
|
throw new ServiceException("未知Sku规格");
|
|
|
}
|
|
|
+ if (skuSpec.getLength() == null) {
|
|
|
+ throw new ServiceException("品号为【" + skuSpec.getCode() + "】的sku未定义长度");
|
|
|
+ }
|
|
|
+ if (skuSpec.getWidth() == null) {
|
|
|
+ throw new ServiceException("品号为【" + skuSpec.getCode() + "】的sku未定义宽度");
|
|
|
+ }
|
|
|
return WorkOrderFixationSpecEnum.getEnum(skuSpec.getLength(), skuSpec.getWidth());
|
|
|
}
|
|
|
));
|
|
|
|
|
|
- map.forEach(this::doFixationScheduling);
|
|
|
+ map.forEach(((k, v) -> doFixationScheduling(bomSpecId, printingPaperBomSpecId, k, v)));
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 非固定排版
|
|
|
+ */
|
|
|
+ private void notFixationScheduling(Long bomSpecId,
|
|
|
+ Long printingPaperBomSpecId,
|
|
|
+ Map<Long, SkuSpec> skuSpecMap,
|
|
|
+ List<OrderSku> list) {
|
|
|
+
|
|
|
+ if (ObjectUtil.isNotEmpty(list)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ BomSpec bomSpec = bomSpecService.getById(bomSpecId);
|
|
|
+ if (bomSpec == null) {
|
|
|
+ throw new ServiceException("未知主材规格,id:" + bomSpecId);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bomSpec.getLength() == null) {
|
|
|
+ throw new ServiceException("品号为【" + bomSpec.getCode() + "】的sku未定义长度");
|
|
|
+ }
|
|
|
+ if (bomSpec.getWidth() == null) {
|
|
|
+ throw new ServiceException("品号为【" + bomSpec.getCode() + "】的sku未定义宽度");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
* 固定排版
|
|
|
*/
|
|
|
- private void doFixationScheduling(WorkOrderFixationSpecEnum workOrderFixationSpecEnum, List<OrderSku> list) {
|
|
|
+ private void doFixationScheduling(Long bomSpecId,
|
|
|
+ Long printingPaperBomSpecId,
|
|
|
+ WorkOrderFixationSpecEnum workOrderFixationSpecEnum,
|
|
|
+ List<OrderSku> list) {
|
|
|
+
|
|
|
+ // 本次所需排版数量
|
|
|
+ int schedulingNum = workOrderFixationSpecEnum.getMaxNum();
|
|
|
+
|
|
|
+ // 未排单数量
|
|
|
+ int notSchedulingNum = list.stream().map(item -> item.getQuantity().subtract(item.getSchedulingNum()))
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add).intValue();
|
|
|
+
|
|
|
+ // 如果未排版数量小于本次所需排版数量,则不排单
|
|
|
+ if (notSchedulingNum < schedulingNum) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成工单
|
|
|
+ WorkOrder workOrder = new WorkOrder();
|
|
|
+ workOrder.setId(IdWorker.getId());
|
|
|
+ workOrder.setCode(CodeEnum.WORK_ORDER_CODE.getCode());
|
|
|
+ workOrder.setProductionBatchNumber(1);
|
|
|
+ workOrder.setType(1);
|
|
|
+ workOrder.setFixationMaxNum(new BigDecimal(schedulingNum));
|
|
|
+ workOrder.setFixationSpec(workOrderFixationSpecEnum.getLength() + "*" + workOrderFixationSpecEnum.getWidth());
|
|
|
+ workOrder.setMasterBomSpecId(bomSpecId);
|
|
|
+ workOrder.setPrintingPaperBomSpecId(printingPaperBomSpecId);
|
|
|
+ workOrder.setSchedulingNum(new BigDecimal(schedulingNum));
|
|
|
+ workOrder.setSyncProduction(StatusConstant.NO);
|
|
|
+ workOrder.setIntegrity(StatusConstant.NO);
|
|
|
+
|
|
|
+ // 生产工单明细
|
|
|
+ List<WorkOrderDetail> workOrderDetailList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (OrderSku orderSku : list) {
|
|
|
+
|
|
|
+ // 此单未排版数量
|
|
|
+ int itemSchedulingNum = orderSku.getQuantity().subtract(orderSku.getSchedulingNum()).intValue();
|
|
|
+
|
|
|
+ // 未排排数量小于本次所需排版数量
|
|
|
+ if (schedulingNum >= itemSchedulingNum) {
|
|
|
+
|
|
|
+ // 赋值排版数量等于sku数量
|
|
|
+ orderSku.setSchedulingNum(orderSku.getQuantity());
|
|
|
+ orderSku.setCompleteScheduling(StatusConstant.YES);
|
|
|
+
|
|
|
+ for (int i = 0; i < itemSchedulingNum; i++) {
|
|
|
+ addWorkOrderDetail(workOrder, workOrderDetailList, orderSku);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重新计算需要排版数量
|
|
|
+ schedulingNum -= itemSchedulingNum;
|
|
|
+ } else {
|
|
|
+ orderSku.setSchedulingNum(new BigDecimal(itemSchedulingNum - schedulingNum));
|
|
|
+ orderSku.setCompleteScheduling(StatusConstant.NO);
|
|
|
+
|
|
|
+ for (int i = 0; i < schedulingNum; i++) {
|
|
|
+ addWorkOrderDetail(workOrder, workOrderDetailList, orderSku);
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存数据
|
|
|
+ TransactionUtil.execute(() -> {
|
|
|
+ orderSkuService.updateBatchById(list);
|
|
|
+ save(workOrder);
|
|
|
+ workOrderDetailService.saveBatch(workOrderDetailList);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 再次执行判断是否还可以排版
|
|
|
+ doFixationScheduling(bomSpecId, printingPaperBomSpecId, workOrderFixationSpecEnum, list);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void addWorkOrderDetail(WorkOrder workOrder, List<WorkOrderDetail> workOrderDetailList, OrderSku orderSku) {
|
|
|
+ WorkOrderDetail workOrderDetail = new WorkOrderDetail();
|
|
|
+ workOrderDetail.setProductionBatchNumber(1);
|
|
|
+ workOrderDetail.setWorkOrderId(workOrder.getId());
|
|
|
+ workOrderDetail.setOrderInfoId(orderSku.getOrderId());
|
|
|
+ workOrderDetail.setOrderSkuId(orderSku.getId());
|
|
|
|
|
|
+ // todo 赋值 x,y轴
|
|
|
|
|
|
+ workOrderDetailList.add(workOrderDetail);
|
|
|
}
|
|
|
|
|
|
}
|