From d3767d594de66cb5f9d1294931acefea1866f783 Mon Sep 17 00:00:00 2001
From: renkang <8417338+k94314517@user.noreply.gitee.com>
Date: 星期五, 24 一月 2025 18:24:25 +0800
Subject: [PATCH] 客户资料 巡检任务业务

---
 server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwStocktakingServiceImpl.java |  134 ++++++++++++++++++++++++++++++++++++--------
 1 files changed, 110 insertions(+), 24 deletions(-)

diff --git a/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwStocktakingServiceImpl.java b/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwStocktakingServiceImpl.java
index b905879..76ba25d 100644
--- a/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwStocktakingServiceImpl.java
+++ b/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwStocktakingServiceImpl.java
@@ -1,5 +1,6 @@
 package com.doumee.service.business.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.doumee.core.constants.ResponseStatus;
 import com.doumee.core.exception.BusinessException;
 import com.doumee.core.model.LoginUserInfo;
@@ -14,8 +15,10 @@
 import com.doumee.dao.business.YwStocktakingRecordMapper;
 import com.doumee.dao.business.YwWarehouseMapper;
 import com.doumee.dao.business.model.*;
+import com.doumee.dao.system.SystemUserMapper;
 import com.doumee.dao.system.join.NoticesJoinMapper;
 import com.doumee.dao.system.model.Notices;
+import com.doumee.dao.system.model.SystemUser;
 import com.doumee.service.business.YwStocktakingService;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -30,10 +33,7 @@
 import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -61,6 +61,9 @@
     private NoticesJoinMapper noticesJoinMapper;
 
     @Autowired
+    private SystemUserMapper systemUserMapper;
+
+    @Autowired
     private RedisTemplate<String, Object> redisTemplate;
     
     @Override
@@ -79,9 +82,16 @@
             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"鏈煡璇㈠埌浠撳簱鏁版嵁");
         }
         //鍒ゆ柇褰撳墠浠撳簱褰撳墠鏃ユ湡鏄惁瀛樺湪鐩樼偣鍗�
-        if(ywStocktakingMapper.selectCount(new QueryWrapper<YwStocktaking>().lambda().eq(YwStocktaking::getIsdeleted,Constants.ZERO).eq(YwStocktaking::getStatus,Constants.ZERO)
-                .eq(YwStocktaking::getWarehouseId,ywStocktaking.getWarehouseId()).apply(" DATE(PLAN_DATE) = '" + DateUtil.formatDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd") +"'" ))>0){
+        if(ywStocktakingMapper.selectCount(new QueryWrapper<YwStocktaking>().lambda()
+                .eq(YwStocktaking::getIsdeleted,Constants.ZERO)
+                .in(YwStocktaking::getStatus,Constants.ZERO,Constants.ONE)
+                .eq(YwStocktaking::getWarehouseId,ywStocktaking.getWarehouseId())
+                .apply(" DATE(PLAN_DATE) = '" + DateUtil.formatDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd") +"'" ))>0){
             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"["+DateUtil.formatDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd")+"]["+ywWarehouse.getName()+"]宸叉湁鐩樼偣鍗曪紝涓嶅彲閲嶅鍙戣捣鐩樼偣");
+        }
+        SystemUser systemUser = systemUserMapper.selectById(ywStocktaking.getUserId());
+        if(Objects.isNull(systemUser)){
+            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"鏈煡璇㈠埌鐩樼偣浜轰俊鎭�");
         }
         LoginUserInfo loginUserInfo = ywStocktaking.getLoginUserInfo();
         ywStocktaking.setCreator(loginUserInfo.getId());
@@ -103,6 +113,14 @@
         notices.setType(notices.getObjType());
         notices.setUserId(ywStocktaking.getUserId());
         notices.setStatus(Constants.ZERO);
+        //鏁寸悊json鏁版嵁
+        Map<String,Object> jsonMap = new HashMap<>();
+        jsonMap.put("key1","鐩樼偣鍗�"+ywStocktaking.getCode());
+        jsonMap.put("key2", DateUtil.getFomartDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd"));
+        jsonMap.put("key3", ywWarehouse.getName());
+        jsonMap.put("key4", systemUser.getRealname());
+        notices.setParam2(JSONObject.toJSONString(jsonMap));
+        notices.setParam3(Constants.ZERO+"");
         noticesJoinMapper.insert(notices);
         return ywStocktaking.getId();
     }
@@ -122,7 +140,8 @@
                     .eq(YwStocktaking::getId,id)
             );
             //鏍囪寰呭姙鍒犻櫎
-            noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda().set(Notices::getIsdeleted,Constants.ONE)
+            noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
+                    .set(Notices::getIsdeleted,Constants.ONE)
                     .eq(Notices::getIsdeleted,Constants.ZERO)
                     .eq(Notices::getObjType,Constants.ZERO)
                     .eq(Notices::getObjId,id)
@@ -147,7 +166,8 @@
                     .set(YwStocktaking::getEditor,loginUserInfo.getId())
                     .set(YwStocktaking::getStatus,Constants.THREE).eq(YwStocktaking::getId,id));
             //鏍囪寰呭姙鍒犻櫎
-            noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda().set(Notices::getIsdeleted,Constants.ONE)
+            noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
+                    .set(Notices::getIsdeleted,Constants.ONE)
                     .eq(Notices::getIsdeleted,Constants.ZERO)
                     .eq(Notices::getObjType,Constants.ZERO)
                     .eq(Notices::getObjId,id)
@@ -164,7 +184,7 @@
         if(Objects.isNull(model)){
             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"鏈煡璇㈠埌鐩樼偣鍗曟暟鎹�");
         }
-        if(model.getPlanDate().getTime()<System.currentTimeMillis()){
+        if(model.getPlanDate().getTime()>System.currentTimeMillis()){
             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鏈埌寮�濮嬫椂闂达紝鏃犳硶杩涜璇ユ搷浣�");
         }
         if(Constants.equalsInteger(model.getStatus(),Constants.ZERO)&&Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO)){
@@ -178,7 +198,8 @@
 
             //鏍囪寰呭姙宸插鐞�
             noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
-                    .set(Notices::getStatus,Constants.ONE)
+                    .set(Notices::getParam3,Constants.ONE)
+                    .eq(Notices::getStatus,Constants.ZERO)
                     .eq(Notices::getIsdeleted,Constants.ZERO)
                     .eq(Notices::getObjType,Constants.ZERO)
                     .eq(Notices::getObjId,id)
@@ -195,6 +216,7 @@
                     ywStocktakingRecord.setIsdeleted(Constants.ZERO);
                     ywStocktakingRecord.setStatus(Constants.ZERO);
                     ywStocktakingRecord.setMaterialId(ywStock.getMaterialId());
+                    ywStocktakingRecord.setUserId(model.getUserId()+"");
                     ywStocktakingRecord.setStock(ywStock.getStock());
                     ywStocktakingRecord.setStocktakingId(model.getId());
                     ywStocktakingRecordList.add(ywStocktakingRecord);
@@ -209,7 +231,7 @@
 
     @Override
     @Transactional(rollbackFor = {Exception.class,BusinessException.class})
-    public void finishById(Integer id, LoginUserInfo loginUserInfo) {
+    public void finishById(Integer id, YwOutinboundServiceImpl ywOutinboundService, LoginUserInfo loginUserInfo) {
         YwStocktaking model = ywStocktakingMapper.selectById(id);
         if(Objects.isNull(model)){
             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"鏈煡璇㈠埌鐩樼偣鍗曟暟鎹�");
@@ -222,6 +244,10 @@
         }
         List<YwStocktakingRecord> recordList = ywStocktakingRecordMapper.selectList(new QueryWrapper<YwStocktakingRecord>().lambda()
                 .eq(YwStocktakingRecord::getStocktakingId,id).eq(YwStocktakingRecord::getIsdeleted,Constants.ZERO));
+        //鐩樼泩鏁版嵁鍒楄〃
+        List<YwOutinboundRecord> inBoundRecordList = new ArrayList<>();
+        //鐩樹簭鏁版嵁鍒楄〃
+        List<YwOutinboundRecord> outBoundRecordList = new ArrayList<>();
         for (YwStocktakingRecord ywStocktakingRecord:recordList) {
             //鎻愪氦鏃� 鏈繘琛岀洏鐐圭殑鏄庣粏鏁版嵁
             if(Constants.equalsInteger(ywStocktakingRecord.getStatus(),Constants.ZERO)){
@@ -230,18 +256,30 @@
                         .set(YwStocktakingRecord::getUserId,loginUserInfo.getId())
                         .eq(YwStocktakingRecord::getId,ywStocktakingRecord.getId())
                 );
+                continue;
             }
             //鏌ヨ搴撳瓨淇℃伅 杩涜搴撳瓨鏇存柊
             YwStock ywStock = ywStockMapper.selectOne(new QueryWrapper<YwStock>().lambda().eq(YwStock::getMaterialId,ywStocktakingRecord.getMaterialId())
                     .eq(YwStock::getWarehouseId,model.getWarehouseId()).eq(YwStock::getStock,ywStocktakingRecord.getStock()));
-            if(Objects.isNull(ywStock)){
-                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"搴撳瓨淇℃伅寮傚父锛岃鑱旂郴绠$悊鍛�");
+            if(ywStock.getStock().compareTo(ywStocktakingRecord.getActStock())<Constants.ZERO ){
+                YwOutinboundRecord ywOutinboundRecord = new YwOutinboundRecord();
+                ywOutinboundRecord.setMaterialId(ywStock.getMaterialId());
+                ywOutinboundRecord.setStock(ywStocktakingRecord.getActStock().subtract(ywStock.getStock()));
+                inBoundRecordList.add(ywOutinboundRecord);
+            }else if(ywStock.getStock().compareTo(ywStocktakingRecord.getActStock())>Constants.ZERO ){
+                YwOutinboundRecord ywOutinboundRecord = new YwOutinboundRecord();
+                ywOutinboundRecord.setMaterialId(ywStock.getMaterialId());
+                ywOutinboundRecord.setStock(ywStock.getStock().subtract(ywStocktakingRecord.getActStock()));
+                outBoundRecordList.add(ywOutinboundRecord);
             }
-            if(ywStocktakingRecord.getActStock().compareTo(BigDecimal.ZERO)==Constants.ZERO){
-                ywStockMapper.deleteById(ywStock.getId());
-            }else {
-                ywStockMapper.update(new UpdateWrapper<YwStock>().lambda().set(YwStock::getStock,ywStocktakingRecord.getActStock()).eq(YwStock::getId,ywStock.getId()));
-            }
+//            if(Objects.isNull(ywStock)){
+//                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"搴撳瓨淇℃伅寮傚父锛岃鑱旂郴绠$悊鍛�");
+//            }
+//            if(ywStocktakingRecord.getActStock().compareTo(BigDecimal.ZERO)==Constants.ZERO){
+//                ywStockMapper.deleteById(ywStock.getId());
+//            }else {
+//                ywStockMapper.update(new UpdateWrapper<YwStock>().lambda().set(YwStock::getStock,ywStocktakingRecord.getActStock()).eq(YwStock::getId,ywStock.getId()));
+//            }
         }
         ywStocktakingMapper.update(new UpdateWrapper<YwStocktaking>().lambda()
                 .set(YwStocktaking::getEditDate,DateUtil.getCurrDateTime())
@@ -250,6 +288,40 @@
                 .set(YwStocktaking::getEditor,loginUserInfo.getId())
                 .set(YwStocktaking::getStatus,Constants.TWO)
                 .eq(YwStocktaking::getId,model.getId()));
+        //鐢熶骇鐩樼泩鐨勫叆搴撳崟
+        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(inBoundRecordList)){
+            YwOutinbound ywOutinbound = new YwOutinbound();
+            ywOutinbound.setDoneDate(new Date());
+            ywOutinbound.setWarehouseId(model.getWarehouseId());
+            ywOutinbound.setType(4);
+            ywOutinbound.setOrigin(1);
+            ywOutinbound.setContent("鐩樼偣鍗曘��"+model.getCode() +"銆戝鐞嗙敓鎴愮洏鐩堟暟鎹�");
+            ywOutinbound.setStatus(Constants.ZERO);
+            ywOutinbound.setRecordList(inBoundRecordList);
+            ywOutinbound.setLoginUserInfo(loginUserInfo);
+            ywOutinboundService.create(ywOutinbound);
+        }
+        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(outBoundRecordList)){
+            YwOutinbound ywOutinbound = new YwOutinbound();
+            ywOutinbound.setDoneDate(new Date());
+            ywOutinbound.setWarehouseId(model.getWarehouseId());
+            ywOutinbound.setType(10);
+            ywOutinbound.setOrigin(1);
+            ywOutinbound.setContent("鐩樼偣鍗曘��"+model.getCode() +"銆戝鐞嗙敓鎴愮洏浜忔暟鎹�");
+            ywOutinbound.setStatus(Constants.ZERO);
+            ywOutinbound.setRecordList(outBoundRecordList);
+            ywOutinbound.setLoginUserInfo(loginUserInfo);
+            ywOutinboundService.create(ywOutinbound);
+        }
+
+        //鏍囪寰呭姙宸插畬鎴�
+        noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
+                .set(Notices::getStatus,Constants.ONE)
+                .set(Notices::getParam3,Constants.TWO)
+                .eq(Notices::getIsdeleted,Constants.ZERO)
+                .eq(Notices::getObjType,Constants.ZERO)
+                .eq(Notices::getObjId,id)
+        );
 
     }
 
@@ -282,6 +354,12 @@
         if(Objects.isNull(model)){
             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"鏈煡璇㈠埌鐩樼偣鍗曟暟鎹�");
         }
+
+        SystemUser systemUser = systemUserMapper.selectById(ywStocktaking.getUserId());
+        if(Objects.isNull(systemUser)){
+            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"鏈煡璇㈠埌鐩樼偣浜轰俊鎭�");
+        }
+
         YwWarehouse ywWarehouse = ywWarehouseMapper.selectById(ywStocktaking.getWarehouseId());
         if(Objects.isNull(ywWarehouse)){
             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"鏈煡璇㈠埌浠撳簱鏁版嵁");
@@ -301,7 +379,7 @@
         //鏍囪寰呭姙宸插鐞�
         noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
                 .set(Notices::getStatus,Constants.ONE)
-                .eq(Notices::getIsdeleted,Constants.ZERO)
+                .eq(Notices::getIsdeleted,Constants.ONE)
                 .eq(Notices::getObjType,Constants.ZERO)
                 .eq(Notices::getObjId,ywStocktaking.getId())
         );
@@ -319,6 +397,14 @@
         notices.setType(notices.getObjType());
         notices.setUserId(ywStocktaking.getUserId());
         notices.setStatus(Constants.ZERO);
+        //鏁寸悊json鏁版嵁
+        Map<String,Object> jsonMap = new HashMap<>();
+        jsonMap.put("key1","鐩樼偣鍗�"+ywStocktaking.getCode());
+        jsonMap.put("key2", DateUtil.getFomartDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd"));
+        jsonMap.put("key3", ywWarehouse.getName());
+        jsonMap.put("key4", systemUser.getRealname());
+        notices.setParam2(JSONObject.toJSONString(jsonMap));
+        notices.setParam3(Constants.ZERO+"");
         noticesJoinMapper.insert(notices);
 
 
@@ -343,8 +429,8 @@
     public YwStocktaking getDetail(Integer id) {
         MPJLambdaWrapper<YwStocktaking> queryWrapper = new MPJLambdaWrapper<>();
         queryWrapper.selectAll(YwStocktaking.class)
-                .select(" s.name ", YwStocktaking::getUserName)
-                .select(" s1.name ",YwStocktaking::getCreateName)
+                .select(" s.realname ", YwStocktaking::getUserName)
+                .select(" s1.realname ",YwStocktaking::getCreateName)
                 .selectAs(YwWarehouse::getName,YwStocktaking::getWarehouseName)
                 .leftJoin(YwWarehouse.class,YwWarehouse::getId,YwStocktaking::getWarehouseId)
                 .leftJoin("system_user s on t.user_id = s.id")
@@ -383,8 +469,8 @@
         Utils.MP.blankToNull(pageWrap.getModel());
         YwStocktaking model = pageWrap.getModel();
         queryWrapper.selectAll(YwStocktaking.class)
-                .select(" s.name ", YwStocktaking::getUserName)
-                .select(" s1.name ",YwStocktaking::getCreateName)
+                .select(" s.realname ", YwStocktaking::getUserName)
+                .select(" s1.realname ",YwStocktaking::getCreateName)
                 .selectAs(YwWarehouse::getName,YwStocktaking::getWarehouseName)
                 .leftJoin(YwWarehouse.class,YwWarehouse::getId,YwStocktaking::getWarehouseId)
                 .leftJoin("system_user s on t.user_id = s.id")
@@ -392,10 +478,10 @@
                 .eq(YwStocktaking::getIsdeleted,Constants.ZERO)
                 .like(StringUtils.isNotBlank(model.getName()),YwStocktaking::getName,model.getName())
                 .eq(Objects.nonNull(model.getStatus()),YwStocktaking::getStatus,model.getStatus())
+                .eq(Objects.nonNull(model.getUserId()),YwStocktaking::getUserId,model.getUserId())
                 .orderByAsc(YwStocktaking::getStatus)
                 .orderByDesc(YwStocktaking::getPlanDate)
         ;
-
         IPage iPage = ywStocktakingMapper.selectJoinPage(page, YwStocktaking.class,queryWrapper);
         return PageData.from(iPage);
     }

--
Gitblit v1.9.3