From 260ad701e251aaa71a1bf5531bd98d93cb91c2bf Mon Sep 17 00:00:00 2001
From: chenlu <1320612696@qq.com>
Date: 星期三, 19 十一月 2025 11:18:57 +0800
Subject: [PATCH] Merge branch 'master' of http://bore.pub:10439/r/ERP_override

---
 north-glass-erp/src/main/java/com/example/erp/service/pp/ReportingWorkService.java |  300 ++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 251 insertions(+), 49 deletions(-)

diff --git a/north-glass-erp/src/main/java/com/example/erp/service/pp/ReportingWorkService.java b/north-glass-erp/src/main/java/com/example/erp/service/pp/ReportingWorkService.java
index c5241eb..82791e5 100644
--- a/north-glass-erp/src/main/java/com/example/erp/service/pp/ReportingWorkService.java
+++ b/north-glass-erp/src/main/java/com/example/erp/service/pp/ReportingWorkService.java
@@ -3,40 +3,44 @@
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.example.erp.common.AsyncQueryExecutor;
 import com.example.erp.common.Constants;
+import com.example.erp.common.Result;
 import com.example.erp.dto.pp.OrderNumberTransferDTO;
+import com.example.erp.dto.sd.OrderProcessSortDTO;
 import com.example.erp.entity.pp.*;
-import com.example.erp.entity.sd.*;
+import com.example.erp.entity.sd.BasicData;
+import com.example.erp.entity.sd.Order;
+import com.example.erp.entity.sd.OrderDetail;
+import com.example.erp.entity.sd.OrderProcessDetail;
 import com.example.erp.entity.userInfo.Log;
 import com.example.erp.entity.userInfo.SysError;
 import com.example.erp.exception.ServiceException;
 import com.example.erp.mapper.mm.FinishedOperateLogMapper;
 import com.example.erp.mapper.pp.*;
-import com.baomidou.dynamic.datasource.annotation.DS;
 import com.example.erp.mapper.sd.*;
 import com.example.erp.mapper.userInfo.LogMapper;
 import com.example.erp.service.mm.FinishedGoodsInventoryService;
 import com.example.erp.service.sd.OrderProcessDetailService;
 import com.example.erp.service.userInfo.LogService;
 import com.example.erp.service.userInfo.SysErrorService;
+import com.example.erp.tools.JacksonUtil;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
 import com.github.yulichang.wrapper.MPJLambdaWrapper;
 import lombok.RequiredArgsConstructor;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.interceptor.TransactionAspectSupport;
 import org.springframework.web.client.RestTemplate;
 
-
-import javax.annotation.PreDestroy;
 import javax.annotation.Resource;
 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
@@ -45,6 +49,8 @@
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 @Service
 @DS("pp")
@@ -58,13 +64,9 @@
     private final ReportingWorkDetailMapper reportingWorkDetailMapper;
     private final OrderMapper  orderMapper;
     private final OrderDetailMapper orderDetailMapper;
-
     private final FlowCardMapper flowCardMapper;
-
     private final OrderProcessDetailService orderProcessDetailService;
-
     private final LogService logService;
-
     private final LogMapper logMapper;
     private final OrderGlassDetailMapper orderGlassDetailMapper;
     private final SysErrorService sysErrorService;
@@ -74,6 +76,7 @@
     private final ReworkMapper reworkMapper;
     private final BasicDataMapper basicDataMapper;
     private final FinishedGoodsInventoryService finishedGoodsInventoryService;
+    private final StringRedisTemplate stringRedisTemplate;
 
     @Resource
     private AsyncQueryExecutor asyncExecutor;
@@ -270,19 +273,7 @@
         List<ReportingWorkDetail> reportingWorkDetails = JSONArray.parseArray(
                 JSONObject.toJSONString(reportingWorkJson.get("detail")), ReportingWorkDetail.class);
 
-        // 鎶ュ伐缂栧彿
-        Integer maxId = reportingWorkMapper.selectMaxReportingWorkId();
-        if (maxId == null) {
-            maxId = 0;
-        }
-        String formattedNumber = String.format("%04d", maxId + 1);
 
-        // 鏍煎紡鍖栧綋鍓嶆棩鏈�
-        Date currentDate = new Date();
-        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
-        String formattedDate = dateFormat.format(currentDate);
-        String reportingWorkId = "BG" + formattedDate + formattedNumber;
-        reportingWork.setReportingWorkId(reportingWorkId);
 
         // 澶勭悊宸ュ簭 ID
         String[] processIdStr = reportingWork.getProcessId() != null
@@ -306,9 +297,23 @@
             reportingWork.setReportingWorkTime(LocalDateTime.now());
         }
 
-        // 鏄惁绾胯ˉ 1鐜拌ˉ 0鏈幇琛�
+        // 鏄惁鐜拌ˉ 1鐜拌ˉ 0鏈幇琛�
         int isPatch = reportingWorkJson.getInteger("isPatch") != null
                 ? reportingWorkJson.getInteger("isPatch") : 0;
+
+        // 鎶ュ伐缂栧彿
+        Integer maxId = reportingWorkMapper.selectMaxReportingWorkId();
+        if (maxId == null) {
+            maxId = 0;
+        }
+        String formattedNumber = String.format("%04d", maxId + 1);
+
+        // 鏍煎紡鍖栧綋鍓嶆棩鏈�
+        Date currentDate = new Date();
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
+        String formattedDate = dateFormat.format(currentDate);
+        String reportingWorkId = "BG" + formattedDate + formattedNumber;
+        reportingWork.setReportingWorkId(reportingWorkId);
 
         // 涓昏〃鎻掑叆
         reportingWorkMapper.insert(reportingWork);
@@ -433,7 +438,7 @@
                 reportingWorkDetailMapper.insert(reportingWorkDetail);
             }
 
-            // 鏇存柊娴佺▼鍗℃姤宸ユ暟閲�
+            // 鍒ゆ柇鏈�鍚庝竴閬撳伐搴忥紝鏇存柊娴佺▼鍗℃姤宸ユ暟閲�
             if (reportingWork.getNextProcess() == null || reportingWork.getNextProcess().isEmpty()) {
                 LambdaUpdateWrapper<FlowCard> flowCardLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                 flowCardLambdaUpdateWrapper
@@ -649,22 +654,22 @@
         String nowDate = LocalDate.now().toString();
         //鑾峰彇鎶ュ伐宸ュ簭鏄惁涓哄鍚堝伐绋�
         String laminating = reportingWorkMapper.getProcessLaminating(thisProcess);
-//鍚堢墖宸ュ簭
-if (laminating.indexOf("step")!=-1){
-    LambdaUpdateWrapper
-                <ReportingWork> updateWrapper = new LambdaUpdateWrapper<>();
-        updateWrapper
-                .eq(ReportingWork::getProcessId, processIdStr[0])
-                .eq(ReportingWork::getThisProcess, reportingWork.get("process"))
-                .eq(ReportingWork::getReviewedState, 0)
-                .setSql("reviewed_state =1")
-                .set(ReportingWork::getReviewed, userName)
-                .set(ReportingWork::getExamineTime, nowDate);
-        reportingWorkMapper.update(null, updateWrapper);
-}else {
-    reportingWorkMapper.ReviewReportingWorkMp(processIdStr[0],reportingWork.get("process"),technologyStr,userName);
+        //鍚堢墖宸ュ簭
+        if (laminating.indexOf("step")!=-1){
+            LambdaUpdateWrapper
+                        <ReportingWork> updateWrapper = new LambdaUpdateWrapper<>();
+                updateWrapper
+                        .eq(ReportingWork::getProcessId, processIdStr[0])
+                        .eq(ReportingWork::getThisProcess, reportingWork.get("process"))
+                        .eq(ReportingWork::getReviewedState, 0)
+                        .setSql("reviewed_state =1")
+                        .set(ReportingWork::getReviewed, userName)
+                        .set(ReportingWork::getExamineTime, nowDate);
+                reportingWorkMapper.update(null, updateWrapper);
+        }else {
+            reportingWorkMapper.ReviewReportingWorkMp(processIdStr[0],reportingWork.get("process"),technologyStr,userName);
 
-}
+        }
         return true;
     }
 
@@ -1112,7 +1117,9 @@
             List<ReportingWork> ReportingWorks = reportingWorkMapper.selectJoinList(ReportingWork.class,
                     new MPJLambdaWrapper<ReportingWork>()
                             .select(ReportingWork::getReportingWorkId)
-                            .leftJoin(ReportingWorkDetail.class, ReportingWorkDetail::getReportingWorkId, ReportingWork::getReportingWorkId)
+                            .leftJoin(ReportingWorkDetail.class,
+                                    ReportingWorkDetail::getReportingWorkId,
+                                    ReportingWork::getReportingWorkId)
                             .eq(ReportingWork::getProcessId,reportingWork.getProcessId())
                             .eq(ReportingWork::getThisProcess,reportingWork.getThisProcess())
                             .eq(ReportingWork::getDeviceName,reportingWork.getDeviceName())
@@ -1127,12 +1134,7 @@
             //鍒ゆ柇鎶ュ伐琛ㄦ槸鍚﹀瓨鍦紝涓嶅瓨鍦ㄥ垯鏂板鎶ュ伐涓诲壇琛ㄦ暟鎹紝瀛樺湪鍒欎慨鏀瑰壇琛ㄦ暟鎹�
             if(ReportingWorks.isEmpty()){
                 //鑾峰彇褰撳墠鏈�澶ф姤宸ュ崟鍙风敓鎴愭姤宸ュ崟鍙�
-                String formattedNumber = String.format("%04d", reportingWorkMapper.selectMaxReportingWorkId() + 1);
-                //鏍煎紡鍖栧綋鍓嶆棩鏈�
-                Date currentDate = new Date();
-                SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
-                String formattedDate = dateFormat.format(currentDate);
-                String reportingWorkId = "BG" + formattedDate + formattedNumber;
+                String reportingWorkId = this.getTodayMaxReportingId();
 
                 reportingWork.setReportingWorkId(reportingWorkId);
                 reportingWorkDetail.setReportingWorkId(reportingWorkId);
@@ -1195,7 +1197,7 @@
             //灏嗗紓甯镐紶鍏ユ暟鎹簱
             SysError sysError = new SysError();
             sysError.setError(e +Arrays.toString(e.getStackTrace()));
-            sysError.setFunc("姹夌幓mes鎶ュ伐");
+            sysError.setFunc("mes鎶ュ伐");
             sysErrorService.insert(sysError);
             throw new ServiceException(Constants.Code_500, "鏁版嵁璇锋眰寮傚父锛岃妫�鏌�");
 
@@ -1582,4 +1584,204 @@
         map.put("breakageReason", reportingWorkMapper.selectBasicNameByType("breakagereason"));
         return map;
     }
+
+
+    //@Transactional(rollbackFor = Exception.class , noRollbackFor = ServiceException.class)
+    public Result mesReportingWorkSvToRedis(Map<String, Object> reportingWorkMap) throws JsonProcessingException, InterruptedException {
+
+        //璁剧疆鍥炴粴鐐�
+        //Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
+            //鎺ユ敹瑙f瀽涓婚檮琛ㄤ俊鎭�
+        String titleJson = JacksonUtil.writeValueAsString(reportingWorkMap.get("title"));
+        String detailJson = JacksonUtil.writeValueAsString(reportingWorkMap.get("detail"));
+        ReportingWork reportingWork = JacksonUtil.readValue(titleJson, ReportingWork.class);
+        ReportingWorkDetail reportingWorkDetail = JacksonUtil.readValue(detailJson, ReportingWorkDetail.class);
+        String Base_KEY = "reportingWork:"+reportingWork.getProcessId()+"/"+reportingWorkDetail.getTechnologyNumber()+":"+reportingWork.getThisProcess()+":"+reportingWork.getDeviceName();
+        String reportingWork_KEY = Base_KEY+":title";
+        String reportingWorkDetail_KEY = Base_KEY+":detail:"+reportingWorkDetail.getOrderNumber();
+
+        String process_KEY = "reportingWork:"+reportingWork.getProcessId()+"/"+reportingWorkDetail.getTechnologyNumber()+":process";
+
+        //鍒ゆ柇閿佹槸鍚﹀瓨鍦紝瀛樺湪绛夊緟5绉�
+        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("lock:reportingWork"))){
+            Thread.sleep(5*1000);
+            if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("lock:reportingWork"))){
+                throw new ServiceException(Constants.Code_600, "褰撳墠鏈嶅姟鍣ㄦ鍦ㄥ鐞嗘姤宸ユ暟鎹紝璇风◢鍚庨噸璇曪紒");
+            }
+        }
+
+
+        //鍒ゆ柇宸ュ簭鏄惁瀛樺湪redis涓�
+        if(!Boolean.TRUE.equals(stringRedisTemplate.hasKey(process_KEY+":"+reportingWork.getThisProcess()))){
+            List<OrderProcessSortDTO> orderProcessSortDTOList  = orderProcessDetailMapper.selectProcessSort(
+                    reportingWork.getOrderId(),
+                    reportingWork.getProcessId(),
+                    reportingWorkDetail.getOrderNumber(),
+                    reportingWorkDetail.getTechnologyNumber()
+            );
+            if (orderProcessSortDTOList.isEmpty()){
+                throw new ServiceException(Constants.Code_600, "鏈娴嬪埌娴佺▼鍗′俊鎭紝璇锋鏌�");
+            }
+
+            orderProcessSortDTOList.forEach(orderProcessSortDTO -> {
+                Map<String, String> map = new HashMap<>();
+                map.put("sort",orderProcessSortDTO.getSort());
+                map.put("recombination",orderProcessSortDTO.getRecombination());
+                stringRedisTemplate.opsForHash().putAll(process_KEY+":"+orderProcessSortDTO.getProcess(), map);
+            });
+        }
+        String recombination = (String) stringRedisTemplate.opsForHash().get(process_KEY+":"+reportingWork.getThisProcess(),"recombination");
+
+        if (!recombination.isEmpty()){
+            throw new ServiceException(Constants.Code_600, "澶嶅悎宸ュ簭璇峰埌ERP涓繘琛屾姤宸�");
+        }
+
+
+
+
+
+        //鍒ゆ柇鏄惁redis姝ゆ姤宸ョ紪鍙穔ey鏄惁瀛樺湪
+        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey(reportingWork_KEY))){
+            stringRedisTemplate.opsForHash().increment(
+                    reportingWork_KEY,
+                    "thisCompletedQuantity",
+                    reportingWork.getThisCompletedQuantity()
+            );
+        }
+        else{
+            stringRedisTemplate.opsForHash().putAll(
+                    reportingWork_KEY,
+                    JacksonUtil.readValueObjectToString(reportingWork,new TypeReference<Map<String, String>>() {})
+            );
+        }
+        //鑾峰彇鍙姤鏁伴噺
+
+        //鍒ゆ柇鏄惁redis姝ゆ姤宸ユ槑缁嗕腑key鏄惁瀛樺湪
+        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey(reportingWorkDetail_KEY))){
+            stringRedisTemplate.opsForHash().increment(
+                    reportingWorkDetail_KEY,
+                    "completedQuantity",
+                    reportingWorkDetail.getCompletedQuantity()
+            );
+        }else {
+            stringRedisTemplate.opsForHash().putAll(
+                    reportingWorkDetail_KEY,
+                    JacksonUtil.readValueObjectToString(reportingWorkDetail,new TypeReference<Map<String, String>>() {})
+            );
+        }
+        return Result.success("鎻愪氦鎴愬姛");
+    }
+
+    public void redisToMysqlReportingWork() throws InterruptedException {
+        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("lock:reportingWork"))){
+            throw new ServiceException(Constants.Code_600, "瀹氭椂浠诲姟:褰撳墠鏈嶅姟鍣ㄦ鍦ㄥ鐞嗘姤宸ユ暟鎹紝璇风◢鍚庨噸璇曪紒");
+        }
+        //鑾峰彇鎵�鏈塳ey
+        String pattern = "reportingWork:*";
+        Set<String> keys = stringRedisTemplate.keys(pattern);
+
+        //鍒ゆ柇redis涓槸鍚﹀瓨鍦ㄦ姤宸ユ暟鎹�,瀛樺湪鍒欐坊鍔犻攣 骞朵笖淇敼key杞Щ鍒颁笂浼爉ysql鐨刱ey涓�
+        if (keys != null && !keys.isEmpty()) {
+            //娣诲姞閿�
+            stringRedisTemplate.opsForValue().set("lock:reportingWork","1",60, TimeUnit.SECONDS);
+
+            if(Boolean.FALSE.equals(stringRedisTemplate.hasKey("upload:*" ))){
+                keys.forEach(key -> {
+                    stringRedisTemplate.rename(key, "upload:" + key);
+                });
+            }
+        }
+        //娣诲姞鏆傚仠鏃堕棿锛屾柟寮忚幏鍙杕ap鏃讹紝redis涓暟鎹繕鍦ㄥ彉鍖�
+        Thread.sleep(200);
+
+
+        // 淇濆瓨鏃ュ織锛屾妸redis涓殑鏁版嵁淇濆瓨鍒版棩蹇椾腑
+        Map<String, Map<Object, Object>> map = this.getAllHashesByPattern("upload:reportingWork:*");
+        Log log = new Log();
+        log.setContent(JacksonUtil.writeValueAsString(map));
+        log.setFunction("reportingWorkRedisToMysql");
+        log.setOperator("鑷姩瀹氭椂鎶ュ伐");
+        logService.saveLog(log);
+        //鍒犻櫎閿侊紝璁╂帴鍙g户缁帴鏀舵姤宸�
+        stringRedisTemplate.delete("lock:reportingWork");
+
+        //鑾峰彇鍒版墍鏈夊弬鏁板悗娓呯┖redis搴�
+        // stringRedisTemplate.getConnectionFactory().getConnection().flushDb();
+
+        //浠巖edis涓幏鍙栨墍鏈夋姤宸ヤ富琛ㄦ暟鎹�
+        String titleKeyString = "upload:reportingWork:*:title";
+        Set<String> titleKeys = stringRedisTemplate.keys(titleKeyString);
+        if (titleKeys != null) {
+            titleKeys.forEach(titleKey -> {
+                ReportingWork reportingWork = JSON.parseObject(
+                        JSONObject.toJSONString(stringRedisTemplate.opsForHash().entries(titleKey))
+                        , ReportingWork.class
+                );
+                //鑾峰彇褰撳ぉ涓�鍏辨湁澶氬皯涓姤宸ョ紪鍙�
+                String reportingWorkId = this.getTodayMaxReportingId();
+                //娣诲姞鎶ュ伐缂栧彿
+                reportingWork.setReportingWorkId(reportingWorkId);
+                //寰�涓昏〃鎻掑叆鎶ュ伐涓绘暟鎹�
+                reportingWorkMapper.insert(reportingWork);
+
+                String detailKeyString = titleKey.replace(":title", ":detail:*");
+                Set<String> detailKeys = stringRedisTemplate.keys(detailKeyString);
+                if (detailKeys != null) {
+                    detailKeys.forEach(detailKey -> {
+
+                        ReportingWorkDetail reportingWorkDetail = JSON.parseObject(
+                                JSONObject.toJSONString(stringRedisTemplate.opsForHash().entries(detailKey))
+                                , ReportingWorkDetail.class
+                        );
+                        reportingWorkDetail.setReportingWorkId(reportingWorkId);
+                        reportingWorkDetailMapper.insert(reportingWorkDetail);
+
+                        //鏍规嵁璁㈠崟id锛岃鍗曞簭鍙凤紝浠ュ強灏忕墖搴忓彿 鏇存柊灏忕墖娴佺▼鐨勫畬宸ユ暟閲忎互鍙婂埡鐮存暟閲�
+                        LambdaUpdateWrapper<OrderProcessDetail> updateWrapper = new LambdaUpdateWrapper<>();
+                        updateWrapper.eq(OrderProcessDetail::getOrderNumber, reportingWorkDetail.getOrderNumber())
+                                .eq(OrderProcessDetail::getProcessId, reportingWork.getProcessId())
+                                .eq(OrderProcessDetail::getOrderId, reportingWork.getOrderId())
+                                .eq(OrderProcessDetail::getProcess, reportingWork.getThisProcess())
+                                .eq(OrderProcessDetail::getTechnologyNumber, reportingWorkDetail.getTechnologyNumber())
+                                .setSql("reporting_work_num_count = reporting_work_num_count +" + reportingWorkDetail.getCompletedQuantity())
+                                .setSql("reporting_work_num =reporting_work_num +" + reportingWorkDetail.getCompletedQuantity());
+
+                        orderProcessDetailMapper.update(null, updateWrapper);
+                    });
+                }
+
+            });
+        }
+        Set<String> uploadKeys = stringRedisTemplate.keys("upload:reportingWork:*");
+        if (uploadKeys != null) {
+            stringRedisTemplate.delete(uploadKeys);
+        }
+
+    }
+
+    public String getTodayMaxReportingId() {
+        String formattedNumber = String.format("%04d", reportingWorkMapper.selectMaxReportingWorkId() + 1);
+        //鏍煎紡鍖栧綋鍓嶆棩鏈�
+        Date currentDate = new Date();
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
+        String formattedDate = dateFormat.format(currentDate);
+        return "BG" + formattedDate + formattedNumber;
+
+    }
+
+
+/*鑾峰彇鎵�鏈夌殑鍙傛暟杞�*/
+    public Map<String, Map<Object, Object>> getAllHashesByPattern(String pattern) {
+        Set<String> keys = stringRedisTemplate.keys(pattern);
+        Map<String, Map<Object, Object>> result = new HashMap<>();
+
+        if (keys != null) {
+            for (String key : keys) {
+                Map<Object, Object> hashData = stringRedisTemplate.opsForHash().entries(key);
+                result.put(key, hashData);
+            }
+        }
+
+        return result;
+    }
 }

--
Gitblit v1.8.0