24282 1 год назад
Родитель
Сommit
4dae2ea07c

+ 48 - 10
sd-business/src/main/java/com/sd/business/service/work/impl/WorkOrderServiceImpl.java

@@ -42,6 +42,7 @@ 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 org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
 import java.util.ArrayList;
@@ -186,12 +187,55 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
     @Override
     public Integer notSchedulingSkuNum() {
         List<OrderSku> orderSkuList = getNotSchedulingSku(null, null);
-        return orderSkuList.stream().mapToInt(item -> item.getQuantity().subtract(item.getSchedulingNum()).intValue()).sum();
+        return orderSkuList.stream()
+                .mapToInt(item -> item.getQuantity().subtract(item.getSchedulingNum()).intValue())
+                .sum();
     }
 
+    @Transactional(rollbackFor = Exception.class)
     @Override
     public void saveScheduling(WorkOrderDto dto) {
+        schedulingLock.lock();
+        try {
+            Long id = dto.getId();
+            Assert.notNull(id, "id不能为空");
+
+            List<WorkOrderDetail> oldList = workOrderDetailService.list(q -> q.eq(WorkOrderDetail::getWorkOrderId, id));
+            List<WorkOrderDetail> newList = dto.getWorkOrderDetailList();
+
+            // 新增订单skuId集合
+            List<Long> addOrderSkuIdList = newList.stream().filter(item -> item.getId() == null)
+                    .map(WorkOrderDetail::getOrderSkuId).collect(Collectors.toList());
+
+            // 删除订单skuId集合
+            Set<Long> newIdList = newList.stream().map(BaseIdPo::getId).collect(Collectors.toSet());
+            List<Long> deleteOrderSkuIdList = oldList.stream().filter(item -> !newIdList.contains(item.getId()))
+                    .map(WorkOrderDetail::getOrderSkuId).collect(Collectors.toList());
 
+            Set<Long> orderSkuIdSet = Stream.concat(addOrderSkuIdList.stream(), deleteOrderSkuIdList.stream())
+                    .collect(Collectors.toSet());
+
+            Map<Long, OrderSku> map = orderSkuService.mapKEntity(BaseIdPo::getId, q -> q.in(BaseIdPo::getId, orderSkuIdSet));
+            for (Long item : deleteOrderSkuIdList) {
+                OrderSku orderSku = map.get(item);
+                Assert.notNull(orderSku, "未知订单skuId:" + item);
+                orderSku.setSchedulingNum(orderSku.getSchedulingNum().subtract(BigDecimal.ONE));
+                orderSku.setCompleteScheduling(StatusConstant.NO);
+            }
+            for (Long item : addOrderSkuIdList) {
+                OrderSku orderSku = map.get(item);
+                Assert.notNull(orderSku, "未知订单skuId:" + item);
+                orderSku.setSchedulingNum(orderSku.getSchedulingNum().add(BigDecimal.ONE));
+                orderSku.setCompleteScheduling(orderSku.getSchedulingNum().compareTo(orderSku.getQuantity()) == 0
+                        ? StatusConstant.YES : StatusConstant.NO);
+            }
+
+            updateById(dto);
+            workOrderDetailService.editLinked(newList, WorkOrderDetail::getWorkOrderId, id);
+            orderSkuService.updateBatchById(map.values());
+        } finally {
+            schedulingLock.unlock();
+        }
     }
 
     /**
@@ -488,15 +532,9 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
      */
     private SkuSpec getAndVerifySkuSpec(Long skuSpecId, Map<Long, SkuSpec> skuSpecMap) {
         SkuSpec skuSpec = skuSpecMap.get(skuSpecId);
-        if (skuSpec == null) {
-            throw new ServiceException("未知sku规格,id:" + skuSpecId);
-        }
-        if (skuSpec.getLength() == null) {
-            throw new ServiceException("品号为【" + skuSpec.getCode() + "】的sku未定义长度");
-        }
-        if (skuSpec.getWidth() == null) {
-            throw new ServiceException("品号为【" + skuSpec.getCode() + "】的sku未定义宽度");
-        }
+        Assert.notNull(skuSpec, "未知sku规格,id:" + skuSpecId);
+        Assert.notNull(skuSpec.getLength(), "品号为【" + skuSpec.getCode() + "】的sku未定义长度");
+        Assert.notNull(skuSpec.getWidth(), "品号为【" + skuSpec.getCode() + "】的sku未定义宽度");
         return skuSpec;
     }