From 11f925fdce0f58d1f321f3a13a3f3e84221c6912 Mon Sep 17 00:00:00 2001
From: futian.liu <liufutianyoo@163.com>
Date: 星期一, 13 十一月 2023 09:27:14 +0800
Subject: [PATCH] 整理代码

---
 consum-base/src/main/java/com/consum/base/core/WarehouseCoreService.java |  254 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 235 insertions(+), 19 deletions(-)

diff --git a/consum-base/src/main/java/com/consum/base/core/WarehouseCoreService.java b/consum-base/src/main/java/com/consum/base/core/WarehouseCoreService.java
index e321550..feab773 100644
--- a/consum-base/src/main/java/com/consum/base/core/WarehouseCoreService.java
+++ b/consum-base/src/main/java/com/consum/base/core/WarehouseCoreService.java
@@ -1,14 +1,17 @@
 package com.consum.base.core;
 
-import com.consum.base.core.tools.SqlParameter;
-import com.consum.base.core.util.DateUtil;
+import com.consum.base.core.param.BaseWarehouseParam;
+import com.consum.base.core.utils.SqlParameter;
+import com.consum.base.core.utils.DateUtil;
 import com.consum.model.po.*;
 import com.walker.infrastructure.utils.NumberGenerator;
 import com.walker.jdbc.service.BaseServiceImpl;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
 
+import java.util.ArrayList;
 import java.util.List;
 
 
@@ -30,23 +33,31 @@
         System.out.println(mapper.getInsertSql_().getSql());
     }
 
+
+
     /**
      * 缁熶竴鍑哄叆搴�
      */
-    public CheckWarehouseResult[] unifyCheck(CheckWarehouseParam param) {
+    public List<CheckUsingResult> unifyUsingCheck(CheckUsingParam param) {
         // TODO: 10/10/2023 姝ゅ鍔犱簡鍞竴閿侊紝浼氬奖鍝嶆�ц兘锛屾�濊�冩槸鍚︽湁鏇村悎閫傜殑鍔炴硶
         try {
-            Assert.notNull(param, "wareHouse param can not null !");
-            Assert.notNull(param.getWarehouseId(), "wareHouse id can not null");
-            CheckWarehouseResult[] result;
-            synchronized (param.getWarehouseId()) {
+
+            List<CheckUsingResult> results;
+
+
+            String key = param.getLending_id().toString();
+
+            /**
+             * 姝ゅ灏嗗悓涓�涓粨搴撲笅鐨勫悓涓�涓瀷鍙蜂笂閿侊紝涓嶅悓浠撳簱銆佷笉鍚屽瀷鍙蜂笉鍙楀奖鍝嶃��
+             */
+            synchronized (key) {
                 if (param.getIsCheckIn()) {
-                    result = new CheckWarehouseResult[]{checkin(param)};
+                    results = checkUsingIn(param);
                 } else {
-                    result = checkout(param);
+                    results = checkUsingOut(param);
                 }
             }
-            return result;
+            return results;
         } catch (Exception e) {
             log.error(e.getMessage());
             throw new RuntimeException(e.getMessage());
@@ -55,9 +66,162 @@
     }
 
     /**
+     * 缁熶竴鍑哄叆搴�
+     */
+    public List<CheckWarehouseResult> unifyCheck(BaseWarehouseParam param) {
+        // TODO: 10/10/2023 姝ゅ鍔犱簡鍞竴閿侊紝浼氬奖鍝嶆�ц兘锛屾�濊�冩槸鍚︽湁鏇村悎閫傜殑鍔炴硶
+        try {
+            Assert.notNull(param, "wareHouse param can not null !");
+            Assert.notNull(param.getWarehouseId(), "wareHouse id can not null");
+            List<CheckWarehouseResult> results;
+
+            String key = param.getWarehouseId().toString() + param.getModelId().toString();
+
+            /**
+             * 姝ゅ灏嗗悓涓�涓粨搴撲笅鐨勫悓涓�涓瀷鍙蜂笂閿侊紝涓嶅悓浠撳簱銆佷笉鍚屽瀷鍙蜂笉鍙楀奖鍝嶃��
+             */
+            synchronized (key) {
+                if (param.getIsCheckIn()) {
+                    results = new ArrayList<>();
+                    results.add(checkin(param));
+                } else {
+                    results = checkout(param);
+                }
+            }
+            return results;
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            throw new RuntimeException(e.getMessage());
+        }
+
+    }
+
+    /**
+     * 鍦ㄧ敤鐗╁搧鍑哄簱
+     * @param param
+     * @return
+     */
+    private List<CheckUsingResult> checkUsingOut(CheckUsingParam param){
+
+        String sql = "SELECT\n" +
+                "	* \n" +
+                "FROM\n" +
+                "	DEP_FORM_LENDING_MODEL a \n" +
+                "WHERE\n" +
+                "	a.BASE_GOODS_MODELS_ID =:MODEL_ID \n" +
+                "	AND DEP_FORM_LENDING_ID =:LENDING_ID \n" +
+                "	AND USING_COUNT > 0\n" +
+                "ORDER BY\n" +
+                "	PROCURE_DATE ASC PRICE DESC";
+
+        List<DepFormLendingModel>  modelsList = this.select(sql,new SqlParameter("MODEL_ID", param.getModel_id())
+                .put("LENDING_ID",param.getLending_id()),new DepFormLendingModel());
+
+        List<CheckUsingResult> resultList = new ArrayList<>();
+
+        int shengyu = param.getOutputCount();
+        for (DepFormLendingModel model : modelsList) {
+
+            CheckUsingResult result = new CheckUsingResult();
+
+            DepFormLendingGoods goods = get(new DepFormLendingGoods(),"id=?",new Object[]{model.getDepFormLendingGoodsId()});
+
+            //濡傛灉鏄疉绫荤墿鍝侊紝鍒欓渶瑕佷笌鎸佹湁浜虹瓑鍚�
+            if (goods.getClassification() == "A" && (goods.getGoodsUserName().equals(param.getUser_name()))) {
+                continue;
+            }
+
+            //濡傛灉褰撳墠鎸佹湁浜烘寔鏈夌墿鍝佹暟閲忓ぇ浜庡嚭搴撴暟閲忥紝鍒欑洿鎺ュ噺鎺夋暟閲忓嵆鍙��
+            if (model.getUsingCount() > shengyu){
+                result.setInitial_count(model.getUsingCount());
+                result.setLendingModel(model);
+                model.setUsingCount(model.getCounts() - param.getOutputCount());
+                result.setEnd_count(model.getUsingCount());
+                update(model);
+                resultList.add(result);
+
+                goods.setUsingCount(goods.getUsingCount() - param.getOutputCount());
+                update(goods);
+
+                return resultList;
+            }
+
+            shengyu = shengyu - model.getUsingCount();
+            goods.setUsingCount(goods.getUsingCount() - model.getUsingCount());
+
+            result.setInitial_count(model.getUsingCount());
+            result.setLendingModel(model);
+            model.setUsingCount(0);
+            result.setEnd_count(0);
+            update(model);
+            resultList.add(result);
+
+            update(goods);
+        }
+        return resultList;
+    }
+
+    /**
+     * 鍦ㄧ敤鐗╁搧鍏�
+     * @param param
+     * @return
+     */
+    private List<CheckUsingResult> checkUsingIn(CheckUsingParam param){
+
+        DepFormLending lending = get(new DepFormLending(),"id=?",new Object[]{param.getLending_id()});
+
+        String sql = "SELECT\n" +
+                "	* \n" +
+                "FROM\n" +
+                "	DEP_FORM_LENDING_GOODS A \n" +
+                "WHERE\n" +
+                "	A.DEP_FORM_LENDING_ID = :LENDING_ID";
+
+        List<DepFormLendingGoods>  goodsList = this.select(sql,new SqlParameter()
+                .put("LENDING_ID",lending.getId()),new DepFormLendingGoods());
+
+        List<CheckUsingResult> resultList = new ArrayList<>();
+
+        for (DepFormLendingGoods goods : goodsList) {
+            for (CheckWarehouseResult output : param.getOutputList()) {
+                //濡傛灉鍑哄簱鐨勫瀷鍙蜂笌鍒嗗彂鍗曢噷鐨勫瀷鍙风浉鍚�
+                if (goods.getBaseGoodsModelsId().longValue() == output.getWhGoods().getBaseGoodsModelsId().longValue()){
+                    DepFormLendingModel model = new DepFormLendingModel();
+                    model.setId(NumberGenerator.getLongSequenceNumber());
+                    model.setCounts(goods.getCounts());
+                    model.setDepFormLendingGoodsId(goods.getId());
+                    model.setDepFormLendingId(goods.getDepFormLendingId());
+                    model.setWhGoodsDetailsId(output.getWh_goods_detail_id());
+                    model.setNowUserName(goods.getGoodsUserName());
+                    model.setNowUserPhone(goods.getGoodsUserPhone());
+                    model.setWhGoodsId(output.getWhGoods().getId());
+                    //褰撳墠鍙娇鐢ㄦ暟閲�
+                    model.setUsingCount(model.getCounts());
+                    model.setWarehouseId(output.getWhGoods().getWarehouseId());
+                    model.setWarehouseName(output.getWhGoods().getWarehouseName());
+                    model.setAgencyId(lending.getAgencyId());
+                    model.setAgencyName(lending.getAgencyName());
+                    model.setDepartmentId(lending.getDepartmentId());
+                    model.setDepartmentName(lending.getDepartmentName());
+                    insert(model);
+
+                    CheckUsingResult result = new CheckUsingResult();
+                    result.setLendingModel(model);
+                    result.setInitial_count(0);
+                    result.setEnd_count(output.getInitial_count()-output.getEnd_count());
+                    resultList.add(result);
+                }
+            }
+        }
+
+        return resultList;
+    }
+
+    /**
      * 鍑哄簱
      */
-    private CheckWarehouseResult[] checkout(CheckWarehouseParam param) {
+    private List<CheckWarehouseResult> checkout(BaseWarehouseParam param) {
+        //鏌ヨ鍑烘寚瀹氫粨搴撱�佹寚瀹氬瀷鍙凤紝鐘舵�佷负姝e父鐨勭殑搴撳瓨鐗╁搧锛屾寜鐓ч噰璐椂闂村�掑彊銆佷环鏍煎�掑彊鎺掑垪
        String sql = "SELECT\n" +
                 "	* \n" +
                 "FROM\n" +
@@ -72,14 +236,69 @@
         parameter.add("WAREHOUSE_ID",param.getWarehouseId())
                 .add("BASE_GOODS_MODELS_ID",param.getModelId());
         List<WhGoods> goodsList = select(sql,parameter,new WhGoods());
+        List<CheckWarehouseResult> resultList = new ArrayList<>();
+
+        int shengyu = param.getCount();
 
         for (WhGoods goods : goodsList){
+            if (shengyu <= 0 ) break;
 
+            //濡傛灉搴撳瓨鏁伴噺澶т簬鍑哄簱鏁伴噺锛岀洿鎺ュ噺搴撳瓨
+            if (shengyu <= goods.getWhCount()){
+                shengyu = 0;
+
+                CheckWarehouseResult result = new CheckWarehouseResult();
+                result.setWhGoods(goods);
+                result.setInitial_count(goods.getWhCount());
+                goods.setWhCount(goods.getWhCount() - param.getCount());
+                result.setEnd_count(goods.getWhCount());
+                resultList.add(result);
+
+                //濡傛灉鍑忓簱瀛樺悗锛屽簱瀛樻暟涓�0锛屽垯鍙樻洿鐘舵�佷负宸插垹闄�
+                if (goods.getWhCount() == 0){
+                    goods.setStates(0);
+                }
+                update(goods);
+
+                //濡傛灉鏄皟鎷ㄥ嚭搴� 2:璋冩嫧锛�3锛氭姤搴�
+                if (param.getOutput_type() == 2 || param.getOutput_type() == 3){
+                    WhGoods nGoods = new WhGoods();
+                    BeanUtils.copyProperties(goods,nGoods);
+                    //鍙樻洿鐘舵�佷负璋冩嫧
+                    nGoods.setStates(param.getOutput_type());
+                    nGoods.setWhCount(param.getCount());
+                    nGoods.setId(NumberGenerator.getLongSequenceNumber());
+                    insert(nGoods);
+
+                    CheckWarehouseResult nresult = new CheckWarehouseResult();
+                    nresult.setWhGoods(nGoods);
+                    nresult.setInitial_count(0);
+                    nresult.setEnd_count(nresult.getEnd_count());
+                    resultList.add(nresult);
+                }
+
+                return resultList;
+
+            }
+
+            //濡傛灉搴撳瓨鏁伴噺灏忎簬鍑哄簱鏁伴噺锛屽垯闇�瑕佸嚑涓簱瀛樼墿鍝佷竴璧峰嚭搴撱��
+
+            CheckWarehouseResult result = new CheckWarehouseResult();
+            result.setInitial_count(goods.getWhCount());
+            result.setEnd_count(0);
+            result.setWhGoods(goods);
+            resultList.add(result);
+
+            shengyu = param.getCount() - goods.getWhCount();
+            if (shengyu < 0){
+                throw new RuntimeException("鍑哄簱鏃讹紝璁$畻閿欒");
+            }
+            goods.setWhCount(0);
+            goods.setStates(0);
+            update(goods);
         }
 
-
-
-        return null;
+        throw new RuntimeException("鍑哄簱鏃讹紝璁$畻閿欒");
     }
 
     private BaseWarehouse getWarehouse(long id){
@@ -90,7 +309,7 @@
     /**
      * 鍏ュ簱
      */
-    private CheckWarehouseResult checkin(CheckWarehouseParam param) throws RuntimeException {
+    private CheckWarehouseResult checkin(BaseWarehouseParam param) throws RuntimeException {
 
         //濡傛灉鏄噰璐叆搴�
         if (param.getFirst_input_type() ==1){
@@ -104,8 +323,6 @@
             goods.setBaseGoodsModelsId(procureModel.getBaseGoodsModelsId());
             goods.setBaseGoodsModelsName(procureModel.getBaseGoodsModelsName());
             goods.setWhCount(procureModel.getCounts());
-            goods.setTransferCount(0);
-            goods.setAllCount(procureModel.getCounts());
             goods.setFirstInputCode(param.getFirstInputCode());
             goods.setFirstInputType(param.getFirst_input_type());
             goods.setFirstInputHisId(NumberGenerator.getLongSequenceNumber());
@@ -122,7 +339,7 @@
             CheckWarehouseResult result = new CheckWarehouseResult();
             result.setWhGoods(goods);
             result.setInitial_count(0);
-            result.setEnd_count(goods.getAllCount());
+            result.setEnd_count(goods.getWhCount());
             return result;
         //闆舵槦鍏ュ簱
         } else if (param.getFirst_input_type() ==2){
@@ -161,7 +378,6 @@
             WhGoods newGoods = new WhGoods();
             newGoods.setId(goods.getId());
             newGoods.setWhCount(result.getEnd_count());
-            newGoods.setAllCount(goods.getAllCount() + param.getCount());
             newGoods.setStates(1);
             newGoods.setBackDate(DateUtil.getCurrentDateFor14());
             this.update(newGoods);

--
Gitblit v1.9.1