瀏覽代碼

出库功能改进

xian 4 年之前
父節點
當前提交
6974f48cb2

+ 76 - 83
watero-rst-service/src/main/java/com/iamberry/rst/service/cm/InventoryServiceImpl.java

@@ -19,6 +19,7 @@ import com.iamberry.rst.util.PageUtil;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.text.SimpleDateFormat;
@@ -72,9 +73,9 @@ public class InventoryServiceImpl implements InventoryService {
         return inventoryMapper.insert(inventoryInfo);
     }
 
-    @Transactional
     @Override
-    public synchronized Integer addInventory(InventoryInfo invent) {
+    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.REPEATABLE_READ)
+    public Integer addInventory(InventoryInfo invent) {
         Integer flag = 0;
         //查询是否是组合产品
         if(invent != null && invent.getInventoryProductColorId() != null){
@@ -98,101 +99,93 @@ public class InventoryServiceImpl implements InventoryService {
         return flag;
     }
 
-    public Integer addInv(InventoryInfo invent){
-        Integer flag = 0;
+    private Integer addInv(InventoryInfo invent){
+        // 检查仓库数据
         if(invent.getWarehouseId() == null){
-            throw new RuntimeException("【出库】更新库存失败,出库仓库ID。");
+            throw new RuntimeException("【出库】更新库存失败,出库仓库ID为空。");
         }
+        // 查询产品信息是否为空
         if(invent.getInventoryProductBar() == null && invent.getInventoryProductColorId() == null){
             throw new RuntimeException("【出库】更新库存失败,出库需要产品SKU或颜色ID。");
+        }
+        ProductColor productColor = null;
+        if(!StringUtils.isEmpty(invent.getInventoryProductBar())){
+            productColor = productColorMapper.getProductColorByBar(invent.getInventoryProductBar());
         }else{
-            ProductColor productColor = null;
-            if(!StringUtils.isEmpty(invent.getInventoryProductBar())){
-                productColor = productColorMapper.getProductColorByBar(invent.getInventoryProductBar());
-            }else{
-                productColor = productColorMapper.getProductColorById(invent.getInventoryProductColorId());
-            }
-            if(productColor != null){
-                invent.setInventoryProductBar(productColor.getColorBar());
-            }else{
-                throw new RuntimeException("【出库】更新库存失败,产品SKU或颜色ID出错。");
-            }
+            productColor = productColorMapper.getProductColorById(invent.getInventoryProductColorId());
         }
-
+        if(productColor == null){
+            throw new RuntimeException("【出库】更新库存失败,产品SKU或颜色ID出错。");
+        }
+        // 查询仓库信息
         OrderWarehouse orderWarehouse = orderWarehouseMapper.getOrderWarehouseById(invent.getWarehouseId());
         if(orderWarehouse == null){
             throw new RuntimeException("【出库】更新库存失败,查询仓库信息失败。");
         }
-        String warehouseName = orderWarehouse.getWarehouseName();
-        InventoryInfo info = inventoryMapper.getByInventoryByBar(invent.getInventoryProductBar(),invent.getWarehouseId());
-        if(info != null){
-            boolean isNumUpadate = false;
-            Integer logType = 0;
-            Integer num = 0;    //数量
-            Integer logQualityType=0;//1.良品 2不良品
-            InventoryInfo newInent = new InventoryInfo();
-            newInent.setInventoryId(info.getInventoryId());
+        // 查询当前仓库管理的库存信息
+        InventoryInfo info = inventoryMapper.getByInventoryByBar(productColor.getColorBar(),invent.getWarehouseId());
+        if (info == null) {
+            throw new RuntimeException("【入库】更新库存失败,未查询到库存信息,请确认“"+ orderWarehouse.getWarehouseName() +"”是否有该产品["+ invent.getInventoryProductBar()+ "]的库存信息。");
+        }
 
-            String record = "";
-            if(!StringUtils.isEmpty(invent.getInventoryRecentRecord())){
-                record = invent.getInventoryRecentRecord();
-            }
-            //日志记录与 库存最后记录保持一致
-            String logRecord = "";
+        // 获取当前仓库管理的数据
+        InventoryInfo newInent = new InventoryInfo();
+        newInent.setInventoryId(info.getInventoryId());
+        newInent.setWarehouseId(invent.getWarehouseId());
 
-            //inventoryGoodProductNum;//良品数量
-            if(invent.getInventoryGoodProductNum() != null && invent.getInventoryGoodProductNum() != 0 ){
-                logRecord = "[良品]入库"+invent.getInventoryGoodProductNum()+"件,"+record;
-                newInent.setInventoryGoodProductNum(info.getInventoryGoodProductNum() + invent.getInventoryGoodProductNum());
-                newInent.setInventoryRecentRecord(logRecord);
-                isNumUpadate = true;
-                num = invent.getInventoryGoodProductNum();
-                logQualityType = 1;
-            }else if(invent.getInventoryDefectiveProductNum() != null && invent.getInventoryDefectiveProductNum() != 0 ){
-                //inventoryDefectiveProductNum;//不良品数量
-                logRecord = "[不良品]入库"+invent.getInventoryDefectiveProductNum()+"件,"+record;
-                newInent.setInventoryDefectiveProductNum(info.getInventoryDefectiveProductNum() + invent.getInventoryDefectiveProductNum());
-                newInent.setInventoryRecentRecord(logRecord);
-                isNumUpadate = true;
-                num = invent.getInventoryDefectiveProductNum();
-                logQualityType = 2;
-            }
-//            else if(invent.getInventoryRemainingNum() != null && invent.getInventoryRemainingNum() != 0 ){
-//                //  inventoryRemainingNum;//库存剩余数量
-//                    newInent.setInventoryRemainingNum(info.getInventoryRemainingNum() - invent.getInventoryRemainingNum());
-//                    info.setInventoryRecentRecord("[剩余库存]出库:"+invent.getInventoryRemainingNum()+"件");
-//                    isNumUpadate = true;
-//                    num = invent.getInventoryRemainingNum();
-//            }
-            if(isNumUpadate && logQualityType != 0 && num != 0){
-                flag =  inventoryMapper.updateById(newInent);
-                if(flag < 1){
-                    throw new RuntimeException("【入库】更新库存失败,库存修改失败。");
-                }
-                InventoryLog inventoryLog = new InventoryLog();
-                inventoryLog.setLogProductName(info.getInventoryProductName());
-                inventoryLog.setLogProductBar(info.getInventoryProductBar());
-                inventoryLog.setLogRemainingNum(num);
-                if(invent.getLogType() != null && invent.getLogType()!=0){
-                    inventoryLog.setLogType(invent.getLogType()); //类型1.采购 2.发货 3.次品
-                }else{
-                    inventoryLog.setLogType(1); //类型1.采购 2.发货 3.次品
-                }
-                inventoryLog.setLogOperationType(1);    //增加
-                inventoryLog.setLogDesc(logRecord);
-                inventoryLog.setLogQualityType(logQualityType);
-                inventoryLog.setLogWarehouseId(invent.getWarehouseId());
-                flag = inventoryLogMapper.insert(inventoryLog);
-                if(flag < 1){
-                    throw new RuntimeException("【入库】更新库存失败,增加库存记录失败。");
-                }
-            }else{
-                throw new RuntimeException("【入库】更新库存失败,数量未变动或数据出错。");
-            }
+        synchronized (this) {
+            // 此处使用空修改,将多个线程阻塞,只允许一个线程通过,使用mysql的行级锁进行排他
+            inventoryMapper.updateById(newInent);
+        }
+        // 备注信息
+        String record = invent.getInventoryRecentRecord() == null ? "" : invent.getInventoryRecentRecord();
+        //日志记录与 库存最后记录保持一致
+        String logRecord = "";
+
+        // logQualityType: 1.良品 2不良品  num:本次变动的数量
+        int logQualityType=0, num = 0;;
+        if(invent.getInventoryGoodProductNum() != null && invent.getInventoryGoodProductNum() != 0 ){
+            //良品数量
+            logRecord = "[良品]入库"+invent.getInventoryGoodProductNum()+"件,"+record;
+            newInent.setInventoryGoodProductNum(info.getInventoryGoodProductNum() + invent.getInventoryGoodProductNum());
+            newInent.setInventoryRecentRecord(logRecord);
+            num = invent.getInventoryGoodProductNum();
+            logQualityType = 1;
+        }else if(invent.getInventoryDefectiveProductNum() != null && invent.getInventoryDefectiveProductNum() != 0 ){
+            //不良品数量
+            logRecord = "[不良品]入库"+invent.getInventoryDefectiveProductNum()+"件,"+record;
+            newInent.setInventoryDefectiveProductNum(info.getInventoryDefectiveProductNum() + invent.getInventoryDefectiveProductNum());
+            newInent.setInventoryRecentRecord(logRecord);
+            num = invent.getInventoryDefectiveProductNum();
+            logQualityType = 2;
+        } else {
+            throw new RuntimeException("【入库】更新库存失败,数量未变动或数据出错。");
+        }
+        if(inventoryMapper.updateById(newInent) <= 0){
+            throw new RuntimeException("【入库】更新库存失败,库存修改失败。");
+        }
+        /*
+         * 添加库存日志信息
+         */
+        InventoryLog inventoryLog = new InventoryLog();
+        inventoryLog.setLogProductName(info.getInventoryProductName());
+        inventoryLog.setLogProductBar(info.getInventoryProductBar());
+        inventoryLog.setLogRemainingNum(num);
+        if(invent.getLogType() != null && invent.getLogType()!=0){
+            //类型1.采购 2.发货 3.次品
+            inventoryLog.setLogType(invent.getLogType());
         }else{
-            throw new RuntimeException("【入库】更新库存失败,未查询到库存信息,请确认“"+ warehouseName +"”是否有该产品["+ invent.getInventoryProductBar()+ "]的库存信息。");
+            //类型1.采购 2.发货 3.次品
+            inventoryLog.setLogType(1);
         }
-        return flag;
+        inventoryLog.setLogOperationType(1);
+        inventoryLog.setLogDesc(logRecord);
+        inventoryLog.setLogQualityType(logQualityType);
+        inventoryLog.setLogWarehouseId(invent.getWarehouseId());
+        if (inventoryLogMapper.insert(inventoryLog) <= 0) {
+            throw new RuntimeException("【入库】添加库存日志失败。");
+        }
+        return 1;
     }
 
     @Transactional

+ 4 - 2
watero-rst-service/src/main/java/com/iamberry/rst/service/cm/SalesOrderServiceImpl.java

@@ -190,10 +190,12 @@ public class SalesOrderServiceImpl implements SalesOrderService {
                 List<SalesOrderItem> listOrderitem = salesOrderService.listSalesOrderItem(salesOrderItem);
                 for (SalesOrderItem soi:listOrderitem) {
                     InventoryInfo inventoryInfo2 = new InventoryInfo();
-                    inventoryInfo2.setInventoryGoodProductNum(soi.getItemNum());    //良品数量
+                    //良品数量
+                    inventoryInfo2.setInventoryGoodProductNum(soi.getItemNum());
                     inventoryInfo2.setInventoryProductBar(soi.getItemColorBar());
                     inventoryInfo2.setWarehouseId(so.getSalesWarehouseId());
-                    inventoryInfo2.setLogType(1);   //类型1.采购 2.发货 3.次品
+                    //类型1.采购 2.发货 3.次品
+                    inventoryInfo2.setLogType(1);
                     inventoryInfo2.setInventoryRecentRecord("作废订单返还库存-作废订单号:"+ so.getSalesDealCode());
                     inventoryService.addInventory(inventoryInfo2);
                 }

+ 1 - 1
watero-rst-service/src/main/java/com/iamberry/rst/service/fm/mapper/inventoryMapper.xml

@@ -157,7 +157,7 @@
       <if test="inventoryRecentRecord != null and inventoryRecentRecord != ''" >
         inventory_recent_record = #{inventoryRecentRecord,jdbcType=INTEGER},
       </if>
-
+      inventory_update_time = now()
     </set>
     where inventory_id = #{inventoryId,jdbcType=INTEGER}
   </update>

+ 9 - 7
watero-rst-web/src/main/java/com/iamberry/rst/controllers/order/AdminSalesOrderController.java

@@ -173,6 +173,7 @@ public class AdminSalesOrderController {
      * @return
      * @throws Exception
      */
+    @Deprecated
     @RequestMapping("/list_sdorder_page")
     @RequiresPermissions("salesOrder:select:salesOrder")
     public ModelAndView listsdOrderPage(HttpServletRequest request, SalesOrder salesOrder,
@@ -2237,8 +2238,10 @@ public class AdminSalesOrderController {
         if(listString == null){
             return "数据不合格,请检查文档内的数据是否正常!";
         }
-        List<String> orderNumbers =   ExcelUtil.readCell2(path,listString.get(0));//订单号
-        List<String> costs =   ExcelUtil.readCell2(path,listString.get(1));//成本
+        //订单号
+        List<String> orderNumbers =   ExcelUtil.readCell2(path,listString.get(0));
+        //成本
+        List<String> costs =   ExcelUtil.readCell2(path,listString.get(1));
         if(orderNumbers == null || costs == null){
             return "数据不合格,请检查文档内的数据是否正常!";
         }
@@ -2246,14 +2249,13 @@ public class AdminSalesOrderController {
             return "数据不合格,请检查文档内的数据是否正常!";
         }
         SalesOrder salesOrder = new SalesOrder();
-        Integer success = 0,error = 0;
-        List<String> errorOrders = new ArrayList();
+        int success = 0,error = 0;
+        ArrayList<String> errorOrders = new ArrayList<>(costs.size());
         for (int i = 0;i< orderNumbers.size();i++){
-            if( orderNumbers.get(i) != null &&  !orderNumbers.get(i).equals("") &&
-                    costs.get(i) != null &&  !costs.get(i).equals("")){
+            if( orderNumbers.get(i) != null &&  !"".equals(orderNumbers.get(i)) && costs.get(i) != null &&  !"".equals(costs.get(i))){
                 salesOrder.setSalesPostNum(orderNumbers.get(i));
                 Double co = Double.parseDouble(costs.get(i)) * 100;
-                Integer rentCount = Integer.valueOf(co.intValue());
+                Integer rentCount = co.intValue();
                 salesOrder.setSalesPostageCost(rentCount);
                 Integer msg = salesOrderService.updateSalesOrderBySalesPostNum(salesOrder);
                 if(msg > 0){