From eb13b96cc3b01a3d44fbce6a730004e158418ab5 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期五, 09 四月 2021 14:06:58 +0800
Subject: [PATCH] #

---
 src/main/java/zy/cloud/wms/manager/service/impl/WorkServiceImpl.java |  393 +++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 345 insertions(+), 48 deletions(-)

diff --git a/src/main/java/zy/cloud/wms/manager/service/impl/WorkServiceImpl.java b/src/main/java/zy/cloud/wms/manager/service/impl/WorkServiceImpl.java
index 891e1e2..df67520 100644
--- a/src/main/java/zy/cloud/wms/manager/service/impl/WorkServiceImpl.java
+++ b/src/main/java/zy/cloud/wms/manager/service/impl/WorkServiceImpl.java
@@ -10,9 +10,9 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import zy.cloud.wms.common.model.BillDto;
+import zy.cloud.wms.common.model.MatnrDto;
 import zy.cloud.wms.common.model.OrderStoDto;
 import zy.cloud.wms.common.service.MainService;
-import zy.cloud.wms.common.service.asrs.AsrsService;
 import zy.cloud.wms.common.service.erp.ErpService;
 import zy.cloud.wms.common.utils.VersionUtils;
 import zy.cloud.wms.manager.entity.*;
@@ -20,9 +20,7 @@
 import zy.cloud.wms.manager.entity.result.StoPrintVo;
 import zy.cloud.wms.manager.service.*;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 /**
  * Created by vincent on 2021/2/25
@@ -47,9 +45,81 @@
     @Autowired
     private MatService matService;
     @Autowired
-    private AsrsService asrsService;
-    @Autowired
     private ErpService erpService;
+    @Autowired
+    private CheckService checkService;
+
+    @Override
+    @Transactional
+    public R stockPakin(PakinParam param, Long userId) {
+        Node node = nodeService.selectByUuid(param.getNodeId());
+        if (node == null) {
+            node = nodeService.selectById(param.getNodeId());
+        }
+        if (node == null) {
+            return R.error("璐т綅涓嶅瓨鍦�");
+        }
+        if (Cools.isEmpty(param.getMats())) {
+            return R.error("鍏ュ簱鐗╂枡涓嶈兘涓虹┖");
+        }
+        Date now = new Date();
+        for (MatnrDto dto : param.getMats()) {
+            Mat mat = matService.selectByMatnr(dto.getMatnr());
+            if (mat == null) {
+                throw new CoolException("鐗╂枡鏁版嵁閿欒锛岃鑱旂郴绠$悊鍛�");
+            }
+            LocDetl locDetl = locDetlService.getLocDetl(node.getId(), dto.getMatnr());
+            if (locDetl == null) {
+                // 鏇存柊璐т綅搴撳瓨鏄庣粏
+                locDetl = new LocDetl();
+                locDetl.setLocNo(node.getName());
+                locDetl.setNodeId(node.getId());
+                locDetl.setZpallet("");
+                locDetl.setAnfme(dto.getCount());
+                VersionUtils.setLocDetl(locDetl, mat);
+//                locDetl.setDocId(param.getDocType().longValue());
+//                locDetl.setDocNum(docNum);
+                locDetl.setStatus(1);
+                locDetl.setCreateBy(userId);
+                locDetl.setCreateTime(now);
+                locDetl.setUpdateBy(userId);
+                locDetl.setUpdateTime(now);
+                boolean insert = locDetlService.insert(locDetl);
+                if (!insert) {
+                    throw new CoolException("鏂板搴撳瓨鏄庣粏妗eけ璐�");
+                }
+            } else {
+                if (!locDetlService.incrementStock(node.getId(), dto.getMatnr(), dto.getCount())) {
+                    throw new CoolException("鏂板搴撳瓨鏄庣粏妗eけ璐�");
+                }
+            }
+        }
+
+        // 涓婃姤缁檈rp
+        List<BillDto> dtos = new ArrayList<>();
+        for (MatnrDto dto : param.getMats()) {
+            BillDto billDto = new BillDto();
+            billDto.setMatnr(dto.getMatnr());
+            billDto.setQty(dto.getCount());
+            dtos.add(billDto);
+        }
+        Integer docId = Optional.ofNullable(param.getDocType()).orElse(34);
+        String docNum = String.valueOf(snowflakeIdWorker.nextId());
+        switch (docId) {
+            case 34:    // 閲囪喘鍗�
+                docNum = "PU-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15);
+                break;
+            case 45:    // 閿�鍞��璐у崟
+                docNum = "SR-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15);
+                break;
+            default:
+                break;
+        }
+        if (!erpService.uploadBill(dtos, docId, docNum)) {
+            throw new CoolException("鏃犳硶涓婃姤鑷砮rp");
+        }
+        return R.ok("鍏ュ簱鎴愬姛");
+    }
 
     @Override
     @Transactional
@@ -182,9 +252,59 @@
         List<Pakout> pakouts = pakoutService.selectList(new EntityWrapper<Pakout>()
                 .eq("doc_num", param.getNumber())
                 .in("wrk_sts", 1, 2));
-        // 淇敼搴撳瓨鏁伴噺
         for (Pakout pakout : pakouts) {
             LocDetl locDetl = locDetlService.getLocDetl(pakout.getNodeId(), pakout.getMatnr());
+            // 缂鸿揣 or 绔嬪簱
+            if (locDetl == null) {
+                // 缂鸿揣
+                if (pakout.getLocNo().equals("缂鸿揣")) {
+                    Double count = Optional.ofNullable(locDetlService.selectCountByMatnr(pakout.getMatnr())).orElse(0.0D);
+                    if (count < pakout.getAnfme()) {
+                        throw new CoolException(pakout.getMaktx()+"缂鸿揣");
+                    }
+                    Date now = new Date();
+                    // 鏌ユ壘鎺ㄨ崘璐т綅鏄惁搴撳瓨瓒冲
+                    List<LocDetl> locDetls = locDetlService.findOfSort(pakout.getMatnr());
+                    double issued = Optional.ofNullable(pakout.getAnfme()).orElse(0.0D) ;
+                    for (LocDetl locDetl1 : locDetls) {
+                        if (issued > 0) {
+                            // 淇濆瓨鍑哄簱閫氱煡鍗�
+                            Pakout newPakout = new Pakout();
+                            newPakout.setWrkSts(3L);
+                            newPakout.setAnfme(issued>=locDetl1.getAnfme()?locDetl1.getAnfme():issued);
+                            newPakout.setZpallet(locDetl1.getZpallet());
+                            newPakout.setLocNo(locDetl1.getLocNo());
+                            newPakout.setNodeId(locDetl1.getNodeId());
+                            newPakout.setWrkNo(String.valueOf(snowflakeIdWorker.nextId()));
+                            VersionUtils.setPakout(newPakout, locDetl1);
+                            newPakout.setDocId(pakout.getDocId()); // 鍗曟嵁绫诲瀷
+                            newPakout.setDocNum(pakout.getDocNum());    // 鍗曟嵁缂栧彿
+                            newPakout.setCreateTime(now);
+                            newPakout.setUpdateTime(now);
+                            newPakout.setStatus(1);
+                            if (!pakoutService.insert(newPakout)) {
+                                throw new CoolException("淇濆瓨鍑哄簱閫氱煡鍗曞け璐�");
+                            }
+                            if (issued>=locDetl1.getAnfme()) {
+                                // 鍒犻櫎搴撳瓨鏄庣粏
+                                if (!locDetlService.removeStock(locDetl1.getNodeId(), newPakout.getMatnr())) {
+                                    throw new CoolException("鍒犻櫎搴撳瓨鏄庣粏澶辫触");
+                                }
+                            } else {
+                                // 淇敼搴撳瓨鏄庣粏鏁伴噺
+                                if (!locDetlService.reduceStock(locDetl1.getNodeId(), newPakout.getMatnr(), issued)) {
+                                    throw new CoolException("淇敼搴撳瓨鏄庣粏鏁伴噺澶辫触");
+                                }
+                            }
+                            // 鍓╀綑寰呭嚭鏁伴噺閫掑噺
+                            issued = issued - locDetl1.getAnfme();
+                        }
+                    }
+
+                }
+                continue;
+            }
+            // 淇敼搴撳瓨鏁伴噺
             if (pakout.getAnfme()>=locDetl.getAnfme()) {
                 // 鍒犻櫎搴撳瓨鏄庣粏
                 if (!locDetlService.removeStock(locDetl.getNodeId(), pakout.getMatnr())) {
@@ -197,6 +317,7 @@
                 }
             }
         }
+
         // 淇敼鍑哄簱鍗曠姸鎬�
         if (!pakoutService.stockOutComplete(param.getWrkNo(), param.getNumber(), userId)) {
             return R.error("淇濆瓨鏁版嵁澶辫触");
@@ -206,20 +327,23 @@
             throw new CoolException("淇敼鍗曟嵁鐘舵�佸け璐�");
         }
         // 閲囪喘閫�璐у崟
-        if (param.getNumber().startsWith("PR-")) {
-            List<BillDto> dtos = new ArrayList<>();
-            for (Pakout pakout : pakouts) {
-                BillDto billDto = new BillDto();
-                billDto.setMatnr(pakout.getMatnr());
-                billDto.setQty(pakout.getAnfme());
-                dtos.add(billDto);
-            }
-            // erp鍗曟嵁涓婁紶 -- 閲囪喘閫�璐у崟
-            if (!erpService.uploadBill(dtos, 6, pakouts.get(0).getDocNum())) {
-                throw new CoolException("鏃犳硶涓婃姤鑷砮rp");
-            }
+//        if (param.getNumber().startsWith("PR-")) {
+            // erp鍗曟嵁涓婁紶 todo
+//            Long docId = pakouts.get(0).getDocId();
+//            if (null != docId) {
+//                List<BillDto> dtos = new ArrayList<>();
+//                for (Pakout pakout : pakouts) {
+//                    BillDto billDto = new BillDto();
+//                    billDto.setMatnr(pakout.getMatnr());
+//                    billDto.setQty(pakout.getAnfme());
+//                    dtos.add(billDto);
+//                }
+//                if (!erpService.uploadBill(dtos, docId.intValue() , pakouts.get(0).getDocNum())) {
+//                    throw new CoolException("鏃犳硶涓婃姤鑷砮rp");
+//                }
+//            }
 
-        }
+//        }
         return R.ok();
     }
 
@@ -360,10 +484,12 @@
                     DateUtils.convert(now),    // 鍗曟嵁鏃ユ湡[闈炵┖]
                     param.getCustomerTypeId(),    // 瀹㈡埛缂栧彿
                     null,    // 缁忔墜浜虹紪鍙穂绌篯
+                    null,
                     dto.getMatnr(),    // 鍟嗗搧缂栧彿
                     dto.getCount(),    // 鍟嗗搧鏁伴噺
                     dto.getCount(),    // 鎬绘暟閲�
                     Cools.isEmpty(mat.getName())?0.0D:Double.parseDouble(mat.getName()),    // 鍟嗗搧鍗曚环
+                    null,       // 鍟嗗搧鏄庣粏琛屽彿
                     null,    // 鍟嗗搧澶囨敞[绌篯
                     1,    // 鐘舵��
                     now,    // 娣诲姞鏃堕棿
@@ -378,38 +504,209 @@
     }
 
     @Override
+    public R stockCheck(StoCheckParam param, Long userId) {
+        if (Cools.isEmpty(param.getLocNo())) {
+            return R.error("璇烽�夋嫨璐т綅");
+        }
+        Node node = nodeService.selectByUuid(param.getLocNo());
+        if (Cools.isEmpty(node)) {
+            return R.error("璐т綅寮傚父,璇疯仈绯荤鐞嗗憳");
+        }
+        // 鍑嗗鏁版嵁
+        Date now = new Date();
+        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("node_id", node.getId()));
+        List<String> locDetlMatnrs = new ArrayList<>();
+        locDetls.forEach(locDetl -> locDetlMatnrs.add(locDetl.getMatnr()));
+        Iterator<LocDetl> detlIterator = locDetls.iterator();
+        List<PakinMatDto> dtos = param.getDtos();
+
+        // 1.鐩堜簭 淇敼鏁伴噺
+        while (detlIterator.hasNext()){
+            LocDetl locDetl = detlIterator.next();
+            for (PakinMatDto dto : dtos) {
+                if (locDetl.getMatnr().equals(dto.getMatnr())) {
+                    if (!locDetl.getAnfme().equals(dto.getCount())) {
+                        Check check = new Check(
+                                String.valueOf(snowflakeIdWorker.nextId()),    // 鐩樼偣缂栧彿
+                                node.getId(),    // 鐩樼偣璐т綅
+                                node.getUuid(),    // 璐т綅缂栧彿
+                                node.getName(),    // 璐т綅鍚嶇О
+                                locDetl.getMatnr(),    // 鍟嗗搧缂栧彿
+                                locDetl.getMaktx(),    // 鍟嗗搧鍚嶇О
+                                locDetl.getUnit(),    // 鍗曚綅
+                                null,    // 鍗曟嵁绫诲瀷
+                                null,    // 鍗曟嵁缂栧彿
+                                locDetl.getAnfme(),    // 璐﹂潰鏁伴噺
+                                dto.getCount(),    // 瀹炵洏鏁伴噺
+                                dto.getCount() - locDetl.getAnfme(), // 宸��
+                                locDetl.getAnfme() < dto.getCount() ? 1 : 2,    // 鐩堜簭
+                                1,    // 鐘舵��
+                                userId,    // 娣诲姞浜哄憳
+                                now,    // 娣诲姞鏃堕棿
+                                userId,    // 淇敼浜哄憳
+                                now,    // 淇敼鏃堕棿
+                                null    // 澶囨敞
+                        );
+                        if (!checkService.insert(check)) {
+                            throw new CoolException("淇濆瓨鐩樼偣璁板綍澶辫触");
+                        }
+                    }
+                    detlIterator.remove();
+                    break;
+                }
+            }
+        }
+
+        // 2.鐩樹簭 鍒犻櫎搴撳瓨
+        for (LocDetl locDetl : locDetls) {
+            Check check = new Check(
+                    String.valueOf(snowflakeIdWorker.nextId()),    // 鐩樼偣缂栧彿
+                    node.getId(),    // 鐩樼偣璐т綅
+                    node.getUuid(),    // 璐т綅缂栧彿
+                    node.getName(),    // 璐т綅鍚嶇О
+                    locDetl.getMatnr(),    // 鍟嗗搧缂栧彿
+                    locDetl.getMaktx(),    // 鍟嗗搧鍚嶇О
+                    locDetl.getUnit(),    // 鍗曚綅
+                    null,    // 鍗曟嵁绫诲瀷
+                    null,    // 鍗曟嵁缂栧彿
+                    locDetl.getAnfme(),    // 璐﹂潰鏁伴噺
+                    0D,    // 瀹炵洏鏁伴噺
+                    0D - locDetl.getAnfme(), // 宸��
+                    2,    // 鐩堜簭
+                    1,    // 鐘舵��
+                    userId,    // 娣诲姞浜哄憳
+                    now,    // 娣诲姞鏃堕棿
+                    userId,    // 淇敼浜哄憳
+                    now,    // 淇敼鏃堕棿
+                    null    // 澶囨敞
+            );
+            if (!checkService.insert(check)) {
+                throw new CoolException("淇濆瓨鐩樼偣璁板綍澶辫触");
+            }
+
+        }
+
+        // 3.鐩樼泩 鏂板搴撳瓨
+        for (PakinMatDto dto : dtos) {
+            if (!locDetlMatnrs.contains(dto.getMatnr())) {
+                Mat mat = matService.selectByMatnr(dto.getMatnr());
+                Check check = new Check(
+                        String.valueOf(snowflakeIdWorker.nextId()),    // 鐩樼偣缂栧彿
+                        node.getId(),    // 鐩樼偣璐т綅
+                        node.getUuid(),    // 璐т綅缂栧彿
+                        node.getName(),    // 璐т綅鍚嶇О
+                        mat.getMatnr(),    // 鍟嗗搧缂栧彿
+                        mat.getMaktx(),    // 鍟嗗搧鍚嶇О
+                        mat.getUnit(),    // 鍗曚綅
+                        null,    // 鍗曟嵁绫诲瀷
+                        null,    // 鍗曟嵁缂栧彿
+                        0D,    // 璐﹂潰鏁伴噺
+                        dto.getCount(),    // 瀹炵洏鏁伴噺
+                        dto.getCount() - 0D, // 宸��
+                        1,    // 鐩堜簭
+                        1,    // 鐘舵��
+                        userId,    // 娣诲姞浜哄憳
+                        now,    // 娣诲姞鏃堕棿
+                        userId,    // 淇敼浜哄憳
+                        now,    // 淇敼鏃堕棿
+                        null    // 澶囨敞
+                );
+                if (!checkService.insert(check)) {
+                    throw new CoolException("淇濆瓨鐩樼偣璁板綍澶辫触");
+                }
+
+            }
+        }
+        return R.ok();
+    }
+
+    @Override
     @Transactional
-    public R stockTransfer(String number, Long userId) {
-        List<CustOrder> custOrders = custOrderService.selectList(new EntityWrapper<CustOrder>().eq("number", number).eq("status", 1));
-        boolean asrs = false;
-        List<StockTransferParam> params = new ArrayList<>();
-        for (CustOrder custOrder : custOrders) {
-            Double count = locDetlService.selectCountByMatnr(custOrder.getUserCode());
-            if (count == null) {
-                count = 0.0D;
-            }
-            if (count < custOrder.getQty()) {
-                if (!asrs) { asrs = true; }
-                StockTransferParam param = new StockTransferParam();
-                param.setMatnr(custOrder.getUserCode());
-                param.setAnfme(custOrder.getQty() - count);
-                params.add(param);
+    public R stockCheckAuditing(Long checkId, Long userId) {
+        Check check = checkService.selectById(checkId);
+        if (null == check) {
+            return R.error("鐩樼偣璁板綍宸插け鏁�");
+        }
+        Date now = new Date();
+        // 1.淇敼搴撳瓨鏁伴噺
+        if (check.getOriQty() != 0 && check.getCekQty() != 0) {
+            // 鏇存柊搴撳瓨鏄庣粏
+            if (check.getSettle() == 1) {
+                if (!locDetlService.incrementStock(check.getNodeId(), check.getMatnr(), Math.abs(check.getDiffQty()))) {
+                    throw new CoolException("淇敼搴撳瓨鏄庣粏鏁伴噺澶辫触");
+                }
+            } else if (check.getSettle() == 2) {
+                if (!locDetlService.reduceStock(check.getNodeId(), check.getMatnr(), Math.abs(check.getDiffQty()))) {
+                    throw new CoolException("淇敼搴撳瓨鏄庣粏鏁伴噺澶辫触");
+                }
             }
         }
-        if (asrs) {
-//            if (asrsService.stockTransfer(params)) {
-//                for (StockTransferParam param : params) {
-//                    // 淇敼閿�鍞鍗曞緟鍑烘暟閲�
-//                    if (!custOrderService.updateQty(number, param.getMatnr(), param.getAnfme())) {
-//                        throw new CoolException("淇敼閿�鍞鍗曞緟鍑烘暟閲忓け璐�");
-//                    }
-//                }
-//                return R.ok("搴撳瓨璋冩嫧鎴愬姛");
-//            } else {
-//                return R.error("绔嬪簱璋冩嫧澶辫触锛岃鑱旂郴绠$悊鍛�");
-//            }
+
+        // 2.鍒犻櫎搴撳瓨
+        if (check.getOriQty() != 0 && check.getCekQty() == 0) {
+            if (!locDetlService.delete(new EntityWrapper<LocDetl>().eq("node_id", check.getNodeId()).eq("matnr", check.getMatnr()))) {
+                throw new CoolException("鍒犻櫎搴撳瓨鏄庣粏鏁伴噺澶辫触");
+            }
         }
-        return R.error("鏆備笉缂鸿揣锛屼笉闇�瑕佺珛搴撹皟鎷�");
+
+        // 3.鏂板搴撳瓨
+        if (check.getOriQty() == 0 && check.getCekQty() != 0) {
+            Mat mat = matService.selectByMatnr(check.getMatnr());
+            if (mat == null) {
+                throw new CoolException("鍟嗗搧鏁版嵁寮傚父锛岃鑱旂郴绠$悊鍛�");
+            }
+            // 鏇存柊璐т綅搴撳瓨鏄庣粏
+            LocDetl locDetl = new LocDetl();
+            locDetl.setLocNo(check.getLocNo());
+            locDetl.setNodeId(check.getNodeId());
+//            locDetl.setZpallet(param.getZpallet());
+            locDetl.setAnfme(check.getCekQty());
+            VersionUtils.setLocDetl(locDetl, mat);
+            locDetl.setStatus(1);
+            locDetl.setCreateBy(userId);
+            locDetl.setCreateTime(now);
+            locDetl.setUpdateBy(userId);
+            locDetl.setUpdateTime(now);
+            if (!locDetlService.insert(locDetl)) {
+                throw new CoolException("鏂板搴撳瓨鏄庣粏澶辫触");
+            }
+        }
+
+        // 淇敼鐩樼偣璁板綍
+        String docNum = null;
+        Long docId = null;
+        switch (check.getSettle()) {
+            case 1:    // 鐩樼泩
+                docNum = "CS-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15);
+                docId = 14L; // 鎶ユ孩鍗�
+                break;
+            case 2:    // 鐩樹簭
+                docNum = "CD-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15);
+                docId = 9L;  // 鎶ユ崯鍗�
+                break;
+            default:
+                throw new CoolException("鐩樼偣璁板綍鐩堜簭鎯呭喌寮傚父");
+        }
+        check.setStatus(2);
+        check.setDocId(docId);
+        check.setDocNum(docNum);
+        check.setUpdateBy(userId);
+        check.setUpdateTime(now);
+        if (!checkService.updateById(check)) {
+            throw new CoolException("淇敼鐩樼偣璁板綍鐘舵�佸け璐�");
+        }
+
+        // 涓婃姤缁檈rp
+        List<BillDto> dtos = new ArrayList<>();
+        BillDto billDto = new BillDto();
+        billDto.setMatnr(check.getMatnr());
+        billDto.setQty(Math.abs(check.getDiffQty()));
+        dtos.add(billDto);
+        if (!erpService.uploadBill(dtos, docId.intValue(), docNum)) {
+            throw new CoolException("鏃犳硶涓婃姤鑷砮rp");
+        }
+
+        return R.ok();
     }
 
 }

--
Gitblit v1.9.1