From e01c5a713793239e26b2e304fafd551affa4bcfc Mon Sep 17 00:00:00 2001
From: skyouc
Date: 星期二, 29 四月 2025 17:36:18 +0800
Subject: [PATCH] 波次任务下发功能优化

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaveServiceImpl.java |  265 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 239 insertions(+), 26 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaveServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaveServiceImpl.java
index d42bfd6..a1d0b90 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaveServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaveServiceImpl.java
@@ -1,64 +1,277 @@
 package com.vincent.rsf.server.manager.service.impl;
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
-import com.vincent.rsf.framework.common.Cools;
 import com.vincent.rsf.framework.common.R;
 import com.vincent.rsf.framework.exception.CoolException;
-import com.vincent.rsf.server.manager.entity.AsnOrder;
-import com.vincent.rsf.server.manager.entity.AsnOrderItem;
+import com.vincent.rsf.server.api.entity.enums.TaskStsType;
+import com.vincent.rsf.server.api.entity.enums.TaskType;
+import com.vincent.rsf.server.manager.entity.*;
+import com.vincent.rsf.server.manager.enums.AsnExceStatus;
 import com.vincent.rsf.server.manager.enums.WaveExceStatus;
 import com.vincent.rsf.server.manager.mapper.WaveMapper;
-import com.vincent.rsf.server.manager.entity.Wave;
-import com.vincent.rsf.server.manager.service.AsnOrderItemService;
-import com.vincent.rsf.server.manager.service.AsnOrderService;
-import com.vincent.rsf.server.manager.service.WaveService;
+import com.vincent.rsf.server.manager.service.*;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.vincent.rsf.server.manager.utils.OptimalAlgorithmUtil;
+import com.vincent.rsf.server.system.constant.SerialRuleCode;
+import com.vincent.rsf.server.system.utils.SerialRuleUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
+import java.util.stream.Collectors;
 
+@Slf4j
 @Service("waveService")
 public class WaveServiceImpl extends ServiceImpl<WaveMapper, Wave> implements WaveService {
 
     @Autowired
     private AsnOrderItemService asnOrderItemService;
-
     @Autowired
     private AsnOrderService asnOrderService;
+    @Autowired
+    private WaveItemService waveItemService;
+    @Autowired
+    private TaskService taskService;
+    @Autowired
+    private TaskItemService taskItemService;
+    @Autowired
+    private LocItemService locItemService;
+    @Autowired
+    private LocService locService;
 
     /**
+     * @param
+     * @param loginUserId
+     * @return
      * @author Ryan
      * @description 娉㈡浠诲姟涓嬪彂
-     * @param
-     * @return
      * @time 2025/4/25 16:24
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public R publicTask(Map<String, Object> map) {
-        List<Long> ids = (List<Long>) map.get("ids");
-        if (Objects.isNull(ids) || ids.isEmpty()) {
+    public R publicTask(Map<String, Object> map, Long loginUserId) {
+        List<WaveItem> itemParams = (List<WaveItem>) map.get("waveItem");
+        if (Objects.isNull(itemParams) || itemParams.isEmpty()) {
             throw new CoolException("鍙傛暟涓嶈兘涓虹┖锛侊紒");
         }
-        List<Wave> waves = this.list(new LambdaQueryWrapper<Wave>().in(Wave::getId, ids));
-        if (Objects.isNull(waves) || waves.isEmpty()) {
+        List<WaveItem> items = JSONArray.parseArray(JSONArray.toJSONString(itemParams), WaveItem.class);
+        String waveId = map.get("wave").toString();
+        Wave waves = this.getById(Long.parseLong(waveId));
+        if (Objects.isNull(waves)) {
             throw new CoolException("娉㈡鏁版嵁涓嶅瓨鍦紒锛�");
         }
+        List<WaveItem> waveItems = waveItemService.list(new LambdaQueryWrapper<WaveItem>().eq(WaveItem::getWaveId, waves.getId()));
+        if (waveItems.isEmpty()) {
+            throw new CoolException("娉㈡鏄庣粏涓嶅瓨鍦紒锛�");
+        }
+        /**鐢熸垚鍑哄簱浠诲姟*/
+        try {
+            generateOutTask(items, loginUserId, waves);
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            throw new CoolException("鍑哄簱浠诲姟鐢熸垚澶辫触锛侊紒锛�");
+        }
+        //TODO 1. 鏍规嵁娉㈡鏄庣粏鐢熸垚鍑哄簱浠诲姟
+        // 2. 鏍规嵁鐗╂枡SKU瀵绘壘绗﹀悎鐗╂枡搴撲綅  {1. 鏍规嵁鐗╂枡缂栫爜锛屾壒娆★紝鍔ㄦ�佸瓧娈� 鏌ヨ绗﹀悎鐨勫簱浣嶏紝鍐嶆牴鎹簱浣嶄腑鐗╂枡鐨勬暟閲忛�夋嫨鏈�閫傚悎鐨勫簱浣� 2. 鍒ゆ柇褰撳墠璁㈠崟鏄叏鎷栧嚭搴撹繕鏄嫞鏂欏叆搴搣
+        // 3. 淇敼涓诲崟銆佹尝娆℃墽琛屾暟閲�
+        // 4. 鍒ゆ柇鍏ㄤ粨鍑哄簱鎴栨嫞鏂欏嚭搴�
+        List<Long> orderIds = waveItems.stream().map(WaveItem::getOrderId).collect(Collectors.toList());
+        /**淇敼鍑哄簱鍗曠姸鎬�*/
+        if (!asnOrderService.update(new LambdaUpdateWrapper<AsnOrder>()
+                .set(AsnOrder::getExceStatus, AsnExceStatus.OUT_STOCK_STATUS_TASK_WORKING.val)
+                .in(AsnOrder::getId, orderIds))) {
+            throw new CoolException("鍑哄簱鍗曟嵁鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+        }
+        return R.ok();
+    }
 
-        List<AsnOrder> orders = asnOrderService.list(new LambdaQueryWrapper<AsnOrder>().eq(AsnOrder::getId, ids));
-
-
-        asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>().eq(AsnOrderItem::getAsnId, ids));
-
-        if (!this.update(new LambdaUpdateWrapper<Wave>().set(Wave::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_TASK).in(Wave::getId,ids))) {
-            throw new CoolException("娉㈡鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+    /**
+     * @param
+     * @param loginUserId
+     * @param wave
+     * @return
+     * @author Ryan
+     * @description 鐢熸垚鍑哄簱浠诲姟
+     * @time 2025/4/28 14:01
+     */
+    @Transactional(rollbackFor = Exception.class)
+    private synchronized void generateOutTask(List<WaveItem> itemParams, Long loginUserId, Wave wave) throws Exception {
+        List<LocItem> locItemList = new ArrayList<>();
+        for (WaveItem param : itemParams) {
+            String locs = param.getStockLocs();
+            List<LocItem> locItems = JSONArray.parseArray(locs, LocItem.class);
+            if (locItems.isEmpty()) {
+                continue;
+            }
+            List<Long> list = locItems.stream().map(LocItem::getLocId).collect(Collectors.toList());
+            /**鏍规嵁渚涘簲鍟嗘壒娆★紝鐗╂枡鐮侊紝 鍔ㄦ�佸瓧娈垫煡璇㈡寚瀹氱殑鐗╂枡搴撳瓨淇℃伅*/
+            List<LocItem> items = locItemService.list(new LambdaQueryWrapper<LocItem>()
+                    .eq(LocItem::getSplrBatch, param.getSplrBatch())
+                    .in(LocItem::getLocId, list)
+                    .eq(StringUtils.isNotBlank(param.getFieldsIndex()), LocItem::getFieldsIndex, param.getFieldsIndex())
+                    .eq(LocItem::getMatnrCode, param.getMatnrCode()));
+            if (items.isEmpty()) {
+                throw new CoolException("搴撳瓨淇℃伅鏈夊彉锛岃鍙栨秷褰撳墠娉㈡锛岀敓鏂扮敓鎴愭柊鐨勬尝娆★紒锛�");
+            }
+            /***灏嗘湁璐ф湁鐨勬槑缁嗕俊鎭瓨鏀惧埌搴撲綅淇℃伅涓�*/
+            for (int i = 0; i < items.size(); i++) {
+                items.get(i)
+                        .setWaveId(param.getWaveId())
+                        .setWaveCode(param.getWaveCode())
+                        .setWaveItemId(param.getId());
+            }
+            locItemList.addAll(items);
+        }
+        if (locItemList.isEmpty()) {
+            throw new CoolException("娌℃湁鍚堥�傚簱浣嶏紒锛�");
         }
 
-        return R.ok();
+        /**鎷嗗垎娉㈡鏄庣粏搴撲綅闆嗭紝鍚堝苟鐩稿悓搴撲綅锛屽垎瑙d换鍔℃槑缁�*/
+        Map<Long, List<LocItem>> listMap = locItemList.stream().collect(Collectors.groupingBy(LocItem::getLocId));
+        /**鏍规嵁搴撲綅姹囨�讳俊鎭紝鐢熸垚浠诲姟鏄庣粏**/
+        listMap.keySet().forEach(key -> {
+            List<LocItem> locItems = listMap.get(key);
+            LocItem item1 = locItems.stream().findFirst().get();
+            WaveItem waveItem = waveItemService.getById(item1.getWaveItemId());
+            if (null == waveItem || Objects.isNull(waveItem)) {
+                throw new CoolException("鏁版嵁閿欒锛氭尝娆℃槑缁嗕笉瀛樺湪锛侊紒");
+            }
+            //TODO 褰撳墠浠诲姟瀹屾垚鍚庯紝閫氳繃瀹氭椂浜嬩欢鍒ゆ柇鏄叏鐩樺嚭搴擄紝杩樻槸鎷f枡鍐嶅叆搴�
+            Loc loc = locService.getById(key);
+            Task task = new Task();
+            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
+            if (StringUtils.isBlank(ruleCode)) {
+                throw new CoolException("缂栫爜瑙勫垯閿欒锛氳妫�鏌ャ�孲YS_TASK_CODE銆嶆槸鍚﹁缃畬鎴愶紒锛�");
+            }
+            if (Objects.isNull(loc)) {
+                throw new CoolException("搴撲綅涓嶅瓨鍦紒锛�");
+            }
+            task.setTaskCode(ruleCode)
+                    .setTaskType(TaskType.TASK_TYPE_OUT.type)
+                    .setTaskStatus(TaskStsType.GENERATE_OUT.id)
+                    .setBarcode(loc.getBarcode())
+                    .setOrgLoc(loc.getCode())
+                    .setCreateBy(loginUserId)
+                    .setUpdateBy(loginUserId)
+                    .setTargSite(wave.getTargSite());
+
+            if (!taskService.save(task)) {
+                throw new CoolException("浠诲姟鐢熸垚澶辫触锛侊紒");
+            }
+            List<TaskItem> taskItems = new ArrayList<>();
+            /**鐢熸垚浠诲姟鏄庣粏淇℃伅*/
+            for (LocItem item : locItems) {
+                TaskItem taskItem = new TaskItem();
+                BeanUtils.copyProperties(item, taskItem);
+                taskItem.setTaskId(task.getId())
+                        .setAnfme(waveItem.getAnfme())
+                        .setId(null)
+                        .setSource(item.getWaveItemId());
+                taskItems.add(taskItem);
+            }
+            if (!taskItemService.saveBatch(taskItems)) {
+                throw new CoolException("浠诲姟鏄庣粏淇濆瓨澶辫触锛侊紒");
+            }
+
+            /**淇敼娉㈡鎵ц鏁伴噺*/
+            taskItems.forEach(item -> {
+                boolean update = waveItemService.update(new LambdaUpdateWrapper<WaveItem>()
+                        .eq(WaveItem::getId, item.getSource())
+                        .set(WaveItem::getWorkQty, item.getAnfme()));
+                if (!update) {
+                    throw new CoolException("娉㈡鎵ц鏁伴噺淇敼澶辫触锛侊紒");
+                }
+            });
+
+            List<WaveItem> waveItems = waveItemService.list(new LambdaQueryWrapper<WaveItem>().eq(WaveItem::getWaveId, wave.getId()));
+            double sum = waveItems.stream().mapToDouble(WaveItem::getWorkQty).sum();
+            /**娉㈡涓诲崟淇℃伅淇敼*/
+            if (!this.update(new LambdaUpdateWrapper<Wave>()
+                    .eq(Wave::getId, wave.getId())
+                    .set(Wave::getWorkQty, sum)
+                    .set(Wave::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_TASK.val))) {
+                throw new CoolException("娉㈡涓诲崟淇℃伅淇敼澶辫触锛侊紒");
+            }
+
+        });
+    }
+
+    /**
+     * @param
+     * @return
+     * @author Ryan
+     * @description 棰勮娉㈡涓嬪彂浠诲姟
+     * @time 2025/4/27 11:09
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public List<WaveItem> mergeWavePreview(Long waveId) {
+        Wave wave = this.getById(waveId);
+        if (Objects.isNull(wave)) {
+            throw new CoolException("娉㈡涓嶈兘瀛樺湪锛侊紒");
+        }
+        List<WaveItem> waveItems = waveItemService.list(new LambdaQueryWrapper<WaveItem>().eq(WaveItem::getWaveId, waveId));
+        if (waveItems.isEmpty()) {
+            throw new CoolException("娉㈡鏄庣粏涓嶅瓨鍦紒锛�");
+        }
+        List<WaveItem> itemPreview = null;
+        try {
+            itemPreview = getLocs(waveItems);
+        } catch (Exception e) {
+            throw new CoolException("搴撲綅鑾峰彇澶辫触锛侊紒锛侊紒");
+        }
+        return itemPreview;
+    }
+
+    /**
+     * @param
+     * @param waveItems
+     * @return
+     * @author Ryan
+     * @description 鏍规嵁鐗╂枡缂栫爜锛屾壒娆★紝鍔ㄦ�佸瓧娈� 鏌ヨ绗﹀悎鐨勫簱浣嶏紝鍐嶆牴鎹簱浣嶄腑鐗╂枡鐨勬暟閲忛�夋嫨鏈�閫傚悎鐨勫簱浣�
+     * @time 2025/4/27 09:26
+     */
+    private synchronized List<WaveItem> getLocs(List<WaveItem> waveItems) throws Exception {
+        //TODO  鏍规嵁鐗╂枡缂栫爜锛屾壒娆★紝鍔ㄦ�佸瓧娈� 鏌ヨ绗﹀悎鐨勫簱浣嶏紝鍐嶆牴鎹簱浣嶄腑鐗╂枡鐨勬暟閲忛�夋嫨鏈�閫傚悎鐨勫簱浣�
+        waveItems.forEach(waveItem -> {
+            List<LocItem> locItems = locItemService.list(new QueryWrapper<LocItem>()
+                    .select("id", "loc_id", "loc_code", "order_id", "SUM(anfme) anfme", "SUM(qty) qty", "SUM(work_qty) work_qty", "splr_batch", "fields_index", "matnr_code")
+                    .lambda()
+                    .eq(LocItem::getMatnrCode, waveItem.getMatnrCode())
+                    .eq(LocItem::getSplrBatch, waveItem.getSplrBatch())
+                    .eq(StringUtils.isNotBlank(waveItem.getFieldsIndex()), LocItem::getFieldsIndex, waveItem.getFieldsIndex())
+                    .groupBy(LocItem::getMatnrCode, LocItem::getSplrBatch, LocItem::getFieldsIndex, LocItem::getId));
+            List<Double> doubles1 = locItems.stream().map(LocItem::getAnfme).collect(Collectors.toList());
+            double[] doubles = doubles1.stream().mapToDouble(Double::doubleValue).toArray();
+
+            /**浣跨敤鍥炴函绠楁硶璁$畻锛岃幏鍙栫鍚堝嚭搴撻噺鐨勬渶绠�缁勫悎*/
+            List<Integer> result = OptimalAlgorithmUtil.findCombination(doubles, waveItem.getAnfme());
+
+            String locs = "[]";
+            if (Objects.isNull(result) || result.isEmpty()) {
+                waveItem.setStockLocs(locs).setStockQty(0.0);
+            } else {
+                /**杩囨护闆嗗悎涓渶绠�鐭殑缁勫悎*/
+                List<LocItem> locsInfo = result.stream()
+                        .filter(i -> i >= 0 && i < locItems.size())
+                        .map(locItems::get).collect(Collectors.toList());
+
+                locs = JSONArray.toJSONString(locsInfo);
+                Double sumQty = locsInfo.stream().mapToDouble(LocItem::getAnfme).sum();
+                Double surQty = locsInfo.stream().mapToDouble(LocItem::getWorkQty).sum();
+                Double qty = locsInfo.stream().mapToDouble(LocItem::getQty).sum();
+                Double v = sumQty - surQty - qty;
+                waveItem.setStockLocs(locs).setStockQty(v);
+            }
+        });
+        return waveItems;
     }
 }

--
Gitblit v1.9.1