From 014270352f8e19b51bbb4367ae2ff256050a30a2 Mon Sep 17 00:00:00 2001
From: skyouc
Date: 星期二, 22 四月 2025 14:03:42 +0800
Subject: [PATCH] #修改优化   1. 字典优化修改   2. 编码规则优化   3. 出库单编码生成为空问题修复

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java |  198 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 133 insertions(+), 65 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java
index e2c40cb..ee36323 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java
@@ -1,26 +1,24 @@
 package com.vincent.rsf.server.manager.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.vincent.rsf.framework.exception.CoolException;
 import com.vincent.rsf.server.manager.controller.params.PakinItem;
 import com.vincent.rsf.server.manager.controller.params.WaitPakinParam;
 import com.vincent.rsf.server.manager.entity.*;
+import com.vincent.rsf.server.manager.enums.PakinIOStatus;
 import com.vincent.rsf.server.manager.mapper.WaitPakinMapper;
 import com.vincent.rsf.server.manager.service.*;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.vincent.rsf.server.system.constant.SerialRuleCode;
 import com.vincent.rsf.server.system.utils.SerialRuleUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.security.core.parameters.P;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
 import java.util.stream.Collectors;
 
 @Service("waitPakinService")
@@ -36,100 +34,170 @@
     @Autowired
     private WaitPakinItemService waitPakinItemService;
     @Autowired
+    private WarehouseAreasItemService warehouseAreasItemService;
+    @Autowired
     private LocService locService;
 
     /**
+     * @param
+     * @param userId
+     * @return
      * @author Ryan
      * @description 缁勬嫋
-     * @param
-     * @return
      * @time 2025/3/29 14:42
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public WaitPakin mergeItems(WaitPakinParam waitPakin) {
-        if (waitPakin.getItems().isEmpty()) {
+    public WaitPakin mergeItems(WaitPakinParam waitPakin, Long userId) {
+        if (Objects.isNull(waitPakin.getItems()) || waitPakin.getItems().isEmpty()) {
             throw new CoolException("鍙傛暟閿欒锛氱墿鏂欒窡韪爜涓虹┖锛�");
         }
         if (StringUtils.isBlank(waitPakin.getBarcode())) {
             throw new CoolException("鍙傛暟閿欒锛氭墭鐩樼爜涓虹┖锛侊紒");
         }
-
-        List<WaitPakin> list = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, waitPakin.getBarcode()));
-        if (!list.isEmpty()) {
-            List<String> stringList = list.stream().map(WaitPakin::getCode).collect(Collectors.toList());
-            String join = StringUtils.join(stringList, ",");
-            throw new CoolException("鎷栫洏鐮侊細" + waitPakin.getBarcode() + "宸茶缁勬嫋鍗曪細" + join + "浣跨敤锛侊紒");
+        List<Short> asList = Arrays.asList(Short.valueOf(PakinIOStatus.PAKIN_IO_STATUS_DONE.val), Short.valueOf(PakinIOStatus.PAKIN_IO_STATUS_DONE.val));
+        WaitPakin list = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
+                .notIn(WaitPakin::getIoStatus, asList)
+                .eq(WaitPakin::getBarcode, waitPakin.getBarcode()));
+        if (!Objects.isNull(list)) {
+            throw new CoolException("鎷栫洏鐮侊細" + waitPakin.getBarcode() + "宸茶缁勬嫋鍗曪細" + list.getCode() + "浣跨敤锛侊紒");
         }
-
         List<Loc> locs = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getBarcode, waitPakin.getBarcode()));
         if (!locs.isEmpty()) {
             List<String> locCodes = locs.stream().map(Loc::getCode).collect(Collectors.toList());
             String join = StringUtils.join(locCodes, ",");
             throw new CoolException("鎷栫洏鐮侊細" + waitPakin.getBarcode() + "宸茶搴撲綅锛�" + join + "浣跨敤锛侊紒");
         }
-
         double sum = waitPakin.getItems().stream().mapToDouble(PakinItem::getReceiptQty).sum();
 
-        WaitPakin pakin = new WaitPakin();
-
-        String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_WAIT_PAKIN_CODE, null);
-        if (StringUtils.isBlank(ruleCode)) {
-            throw new CoolException("缂栫爜瑙勫垯閿欒锛� 缂栫爜瑙勫垯銆孲YS_WAIT_PAKIN_CODE銆嶈鍒欐槸涓嶅瓨鍦�");
+        WaitPakin waitPakin1 = new WaitPakin();
+        WaitPakin pakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
+                .in(WaitPakin::getIoStatus, asList)
+                .eq(WaitPakin::getBarcode, waitPakin.getBarcode()));
+        if (Objects.isNull(pakin)) {
+            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_WAIT_PAKIN_CODE, null);
+            if (StringUtils.isBlank(ruleCode)) {
+                throw new CoolException("缂栫爜瑙勫垯閿欒锛� 缂栫爜瑙勫垯銆孲YS_WAIT_PAKIN_CODE銆嶈鍒欐槸涓嶅瓨鍦�");
+            }
+            waitPakin1.setCode(ruleCode)
+                    //鐘舵�佷慨鏀逛负鍏ュ簱涓�
+                    .setIoStatus(Short.parseShort(PakinIOStatus.PAKIN_IO_STATUS_DONE.val))
+                    .setAnfme(sum)
+                    .setUpdateBy(userId)
+                    .setCreateBy(userId)
+                    .setBarcode(waitPakin.getBarcode());
+            if (!this.save(waitPakin1)) {
+                throw new CoolException("涓诲崟淇濆瓨澶辫触锛侊紒");
+            }
+        } else {
+            BeanUtils.copyProperties(pakin, waitPakin1);
+            waitPakin1.setAnfme(sum);
+            if (!this.saveOrUpdate(waitPakin1)) {
+                throw new CoolException("涓诲崟淇敼澶辫触锛侊紒");
+            }
         }
-        pakin.setCode(ruleCode)
-                //鐘舵�佷慨鏀逛负鍏ュ簱涓�
-                .setIoStatus(Short.parseShort("1"))
-                .setAnfme(sum)
-                .setBarcode(waitPakin.getBarcode());
-        if (!this.save(pakin)) {
-            throw new CoolException("涓诲崟淇濆瓨澶辫触锛侊紒");
-        }
-
         /**鐗╂枡璺熻釜鐮�*/
         List<String> tracks = waitPakin.getItems().stream().map(PakinItem::getTrackCode).collect(Collectors.toList());
-
-        List<AsnOrderItem> asnOrderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>()
-                .in(AsnOrderItem::getTrackCode, tracks));
-        if (Objects.isNull(asnOrderItems) || asnOrderItems.isEmpty()) {
-            throw new CoolException("鍗曟嵁涓嶅瓨鍦紒锛�");
+        List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>()
+                .eq(WaitPakinItem::getPakinId, waitPakin1.getId())
+                .in(WaitPakinItem::getTrackCode, tracks));
+        if (!pakinItems.isEmpty()) {
+            if (!waitPakinItemService.remove(new LambdaQueryWrapper<WaitPakinItem>()
+                    .eq(WaitPakinItem::getPakinId, waitPakin1.getId())
+                    .in(WaitPakinItem::getTrackCode, tracks))) {
+                throw new CoolException("鍘熷崟鎹竻闄ゅけ璐ワ紒锛�");
+            }
         }
-
         List<WaitPakinItem> items = new ArrayList<>();
-        for (AsnOrderItem item : asnOrderItems) {
-            WaitPakinItem pakinItem = new WaitPakinItem();
-            pakinItem.setAnfme(item.getAnfme())
-                    .setPakinId(pakin.getId())
-                    .setAsnId(item.getAsnId())
-                    .setAsnCode(item.getAsnCode())
-                    .setAsnItemId(item.getId())
-                    .setMatnrCode(item.getMatnrCode())
-                    .setBatch(item.getSplrBatch())
-                    .setUnit(item.getStockUnit())
-                    .setFieldsIndex(item.getFieldsIndex())
-                    .setUnit(item.getStockUnit())
-                    .setMatnrId(item.getMatnrId())
-                    .setMaktx(item.getMaktx())
-                    .setMatnrCode(item.getMatnrCode());
-            for (PakinItem waitPakinItem : waitPakin.getItems()) {
-                if (waitPakinItem.getTrackCode().equals(item.getTrackCode())) {
-                    Double v = item.getWorkQty() + waitPakinItem.getReceiptQty();
-                    pakinItem.setWorkQty(v)
-                            .setAnfme(waitPakinItem.getReceiptQty())
-                            .setTrackCode(waitPakinItem.getTrackCode());
-                    /**鏇存柊鍗曟嵁鎵ц涓簱瀛�*/
-                    if (v.compareTo(item.getAnfme()) > 0) {throw new CoolException("鎵ц涓暟閲忓ぇ浜庢敹璐ф暟閲忥紒锛�");}
-                    if (!asnOrderItemService.update(new LambdaUpdateWrapper<AsnOrderItem>()
-                            .eq(AsnOrderItem::getTrackCode, waitPakinItem.getTrackCode()).set(AsnOrderItem::getWorkQty, v))) {
-                        throw new CoolException("鎵ц涓簱瀛樻洿鏂板け璐ワ紒锛�");
+        if (!Objects.isNull(waitPakin.getType()) && waitPakin.getType().equals("defective")) {
+            List<AsnOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>().in(AsnOrderItem::getTrackCode, tracks));
+            if (Objects.isNull(orderItems) || orderItems.isEmpty()) {
+                throw new CoolException("鍗曟嵁涓嶅瓨鍦紒锛�");
+            }
+            for (AsnOrderItem item : orderItems) {
+                WaitPakinItem pakinItem = new WaitPakinItem();
+                pakinItem.setAnfme(item.getAnfme())
+                        .setPakinId(waitPakin1.getId())
+                        .setAsnId(item.getAsnId())
+                        .setAsnCode(item.getAsnCode())
+                        .setAsnItemId(item.getId())
+                        .setBatch(item.getSplrBatch())
+                        .setUnit(item.getStockUnit())
+                        .setFieldsIndex(item.getFieldsIndex())
+                        .setMatnrId(item.getMatnrId())
+                        .setMaktx(item.getMaktx())
+                        .setUpdateBy(userId)
+                        .setCreateBy(userId)
+                        .setMatnrCode(item.getMatnrCode());
+                AsnOrder order = asnOrderService.getById(item.getAsnId());
+                if (!Objects.isNull(order)) {
+                    pakinItem.setType(null == order.getType() ?  null :order.getType())
+                            .setWkType(null == order.getWkType() ? null : Short.parseShort(order.getWkType()) );
+                }
+                for (PakinItem waitPakinItem : waitPakin.getItems()) {
+                    if (waitPakinItem.getTrackCode().equals(item.getTrackCode())) {
+                        if (waitPakinItem.getReceiptQty() > item.getAnfme() || waitPakinItem.getReceiptQty().compareTo(0.0) >= 0) {
+                            throw new CoolException("缁勬嫋鏁伴噺涓嶈兘澶т簬鏀惰揣鏁伴噺锛侊紒");
+                        }
+                        pakinItem.setAnfme(waitPakinItem.getReceiptQty()).setTrackCode(waitPakinItem.getTrackCode());
                     }
                 }
+                items.add(pakinItem);
             }
-            items.add(pakinItem);
+        } else {
+            LambdaQueryWrapper<WarehouseAreasItem> queryWrapper = new QueryWrapper<WarehouseAreasItem>()
+                    .select("SUM(anfme) as anfme, track_code, asn_code, id, splr_batch, ispt_result, plat_item_id, batch, qty, work_qty, matnr_code, matnr_id, maktx")
+                    .lambda()
+                    .in(WarehouseAreasItem::getTrackCode, tracks)
+                    .groupBy(WarehouseAreasItem::getSplrBatch,
+                            WarehouseAreasItem::getTrackCode);
+            List<WarehouseAreasItem> warehouseAreasItems = warehouseAreasItemService.list(queryWrapper);
+            if (Objects.isNull(warehouseAreasItems) || warehouseAreasItems.isEmpty()) {
+                throw new CoolException("鐗╂枡鏈�佽嚦鏀惰揣鍖猴紒锛�");
+            }
+            for (WarehouseAreasItem item : warehouseAreasItems) {
+                WaitPakinItem pakinItem = new WaitPakinItem();
+                pakinItem.setAnfme(item.getAnfme())
+                        .setPakinId(waitPakin1.getId())
+                        .setAsnId(item.getAsnId())
+                        .setAsnCode(item.getAsnCode())
+                        .setAsnItemId(item.getId())
+                        .setBatch(item.getSplrBatch())
+                        .setUnit(item.getStockUnit())
+                        .setFieldsIndex(item.getFieldsIndex())
+                        .setMatnrId(item.getMatnrId())
+                        .setMaktx(item.getMaktx())
+                        .setUpdateBy(userId)
+                        .setCreateBy(userId)
+                        .setMatnrCode(item.getMatnrCode());
+                AsnOrder order = asnOrderService.getById(item.getAsnId());
+                if (!Objects.isNull(order)) {
+                    pakinItem.setType(null == order.getType() ?  null :order.getType())
+                            .setWkType(null == order.getWkType() ? null : Short.parseShort(order.getWkType()) );
+                }
+
+                for (PakinItem waitPakinItem : waitPakin.getItems()) {
+                    if (waitPakinItem.getTrackCode().equals(item.getTrackCode())) {
+                        if (waitPakinItem.getReceiptQty() > item.getAnfme() || waitPakinItem.getReceiptQty().compareTo(0.0) <= 0) {
+                            throw new CoolException("缁勬嫋鏁伴噺涓嶈兘澶т簬鏀惰揣鏁伴噺涓斾笉鑳藉皬浜庨浂锛侊紒");
+                        }
+                        pakinItem.setAnfme(waitPakinItem.getReceiptQty()).setTrackCode(waitPakinItem.getTrackCode());
+                    }
+                }
+                items.add(pakinItem);
+            }
         }
+        double sum1 = items.stream().mapToDouble(WaitPakinItem::getAnfme).sum();
         if (!waitPakinItemService.saveBatch(items)) {
             throw new CoolException("缁勬嫋鏄庣粏淇濆瓨澶辫触锛侊紒");
         }
+        waitPakin1.setAnfme(sum1);
+        if (!this.updateById(waitPakin1)) {
+            throw new CoolException("缁勬嫋鏁伴噺淇敼澶辫触锛侊紒");
+        }
+
+        //TODO 缁勬嫋瀹屾垚鍚庯紝鎵e噺鏀惰揣鍖哄簱瀛�
+
         return pakin;
     }
 

--
Gitblit v1.9.1