From 1cdccaf0a06161fcc2a5797e617508c8a7e1f300 Mon Sep 17 00:00:00 2001
From: p-honggang.li <p-honggang.li@pcitc.com>
Date: 星期六, 06 九月 2025 03:14:53 +0800
Subject: [PATCH] 修改订单创建,发起工作流调用,采用后端处理,增加事务,修改审核同通过和驳回处理,修改根据用户id获取工作流代办信息,再根据代办ids查询订单信息

---
 src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java |  163 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 145 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java b/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java
index d2605cb..a75f266 100644
--- a/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java
+++ b/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java
@@ -7,6 +7,7 @@
 import com.webmanage.common.BusinessException;
 import com.webmanage.common.PageResult;
 import com.webmanage.dto.AddPointsFlowDTO;
+import com.webmanage.dto.DeductUserPointsDTO;
 import com.webmanage.dto.PointsFlowQueryDTO;
 import com.webmanage.emun.RuleTypeEnum;
 import com.webmanage.entity.PointsFlow;
@@ -14,6 +15,9 @@
 import com.webmanage.entity.UserPoints;
 import com.webmanage.mapper.PointsFlowMapper;
 import com.webmanage.mapper.UserPointsMapper;
+import com.webmanage.mapper.PointsTransactionMapper;
+import com.webmanage.entity.PointsTransaction;
+import com.webmanage.service.OrderInfoService;
 import com.webmanage.service.PointsFlowService;
 import com.webmanage.service.PointsRuleService;
 import lombok.extern.slf4j.Slf4j;
@@ -39,6 +43,10 @@
 
     @Resource
     private PointsRuleService pointsRuleService;
+
+    @Resource
+    private PointsTransactionMapper pointsTransactionMapper;
+
 
     @Override
     public PageResult<PointsFlow> getPersonalPointsFlowPage(PointsFlowQueryDTO queryDTO) {
@@ -174,10 +182,10 @@
         }
 
         // 姣忔棩涓婇檺鏍¢獙锛堝垎鍒牎楠岋級
-        if (applyUser && userRule.getIsLimit() != null && userRule.getIsLimit() == 0) {
+        if (applyUser && userRule.getIsLimit() != null && userRule.getIsLimit() == 1) {
             checkDailyLimitByRule(userId, unitId, userRule, userPointsChange);
         }
-        if (applyProvider && providerRule.getIsLimit() != null && providerRule.getIsLimit() == 0) {
+        if (applyProvider && providerRule.getIsLimit() != null && providerRule.getIsLimit() == 1) {
             checkDailyLimitByRule(providerId, providerId, providerRule, providerPointsChange);
         }
 
@@ -213,7 +221,7 @@
         if (applyProvider && providerPointsChange != 0) {
             PointsFlow providerFlow = new PointsFlow();
             providerFlow.setUserId(providerId);
-            providerFlow.setUnitId(providerId);
+            providerFlow.setUnitId(null);
             providerFlow.setDataType(ruleType);
             providerFlow.setDataCategory(addPointsFlowDTO.getCategory());
             providerFlow.setPoints(providerPointsChange);
@@ -225,8 +233,8 @@
             if (!providerSaved) {
                 throw new BusinessException("淇濆瓨鎻愪緵鑰呯Н鍒嗘祦姘村け璐�");
             }
-
-            updateProviderUnitPoints(providerId, providerPointsChange);
+            updateUserPointsByRule(providerId, null, userPointsChange);
+            // updateProviderUnitPoints(providerId, providerPointsChange);
         }
 
         return true;
@@ -349,11 +357,14 @@
         QueryWrapper<PointsFlow> wrapper = new QueryWrapper<>();
         wrapper.eq("deleted", 0)
                .eq("user_id", userId)
-               .eq("unit_id", unitId)
 //               .eq("data_category", pointsRule.getRuleName())
                 .eq("rule_id",pointsRule.getId())
+                .eq("data_type", 0)
                .ge("flow_time", startOfDay)
                .le("flow_time", endOfDay);
+        if(StringUtils.hasText(unitId)){
+            wrapper.eq("unit_id", unitId);
+        }
 
         List<PointsFlow> todayFlows = list(wrapper);
         
@@ -381,7 +392,7 @@
      * 妫�鏌ョН鍒嗕綑棰濇槸鍚﹁冻澶�
      */
     private void checkBalanceSufficient(String userId, String unitId, Integer requiredPoints) {
-        // 妫�鏌ヤ釜浜虹Н鍒嗕綑棰�
+        // 浠呮鏌ヤ釜浜虹Н鍒嗕綑棰濓紙绉婚櫎鍗曚綅浣欓鍒ゅ畾锛�
         QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>();
         userWrapper.eq("deleted", 0)
                   .eq("user_id", userId);
@@ -391,15 +402,7 @@
             throw new BusinessException("涓汉绉垎浣欓涓嶈冻锛屽綋鍓嶄綑棰�: " + (userPoints != null ? userPoints.getBalance() : 0) + "锛岄渶瑕佹墸闄�: " + requiredPoints);
         }
 
-        // 妫�鏌ュ崟浣嶇Н鍒嗕綑棰�
-        QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>();
-        unitWrapper.eq("deleted", 0)
-                  .eq("unit_id", unitId);
-        
-        UserPoints unitPoints = userPointsMapper.selectOne(unitWrapper);
-        if (unitPoints == null || unitPoints.getBalance() < requiredPoints) {
-            throw new BusinessException("鍗曚綅绉垎浣欓涓嶈冻锛屽綋鍓嶄綑棰�: " + (unitPoints != null ? unitPoints.getBalance() : 0) + "锛岄渶瑕佹墸闄�: " + requiredPoints);
-        }
+        // 鍘熷崟浣嶄綑棰濇牎楠屽凡绉婚櫎
     }
 
     /**
@@ -492,7 +495,9 @@
             
             userPoints = new UserPoints();
             userPoints.setUserId(userId);
-            userPoints.setUnitId(unitId);
+            if(StringUtils.hasText(unitId)){
+                userPoints.setUnitId(unitId);
+            }
             userPoints.setBalance(pointsValue);
             userPoints.setTotalEarned(pointsValue > 0 ? pointsValue : 0);
             userPoints.setTotalConsumed(pointsValue < 0 ? Math.abs(pointsValue) : 0);
@@ -522,7 +527,7 @@
         }
 
         // 鏇存柊鍗曚綅绉垎璐︽埛
-        QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>();
+         /* QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>();
         unitWrapper.eq("deleted", 0)
                   .eq("unit_id", unitId);
         
@@ -562,6 +567,92 @@
             
             unitPoints.setUpdateTime(LocalDateTime.now());
             userPointsMapper.updateById(unitPoints);
+        } */
+    }
+
+    /**
+     * 鎵e噺鐢ㄦ埛绉垎
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public boolean deductUserPoints(DeductUserPointsDTO deductDTO) {
+        try {
+            String userId = deductDTO.getUserId();
+            String providerId = deductDTO.getProviderId();
+            String unitId = deductDTO.getUnitId();
+            Integer points = deductDTO.getPoints();
+            String orderId = deductDTO.getOrderId();
+            String remark = deductDTO.getRemark();
+            String dataCategory = deductDTO.getDataCategory();
+            Integer dataType = deductDTO.getDataType();
+
+            if (!StringUtils.hasText(userId)) {
+                throw new BusinessException("鐢ㄦ埛ID涓嶈兘涓虹┖");
+            }
+            if (points == null || points <= 0) {
+                throw new BusinessException("鎵e噺绉垎鏁伴噺蹇呴』澶т簬0");
+            }
+
+            // 妫�鏌ョ敤鎴风Н鍒嗕綑棰濇槸鍚﹀厖瓒�
+            checkBalanceSufficient(userId, unitId, points);
+
+            // 鍒涘缓绉垎娴佹按璁板綍
+            PointsFlow pointsFlow = new PointsFlow();
+            pointsFlow.setUserId(userId);
+            pointsFlow.setUnitId(unitId);
+            pointsFlow.setDataCategory(dataCategory != null ? dataCategory : "绉垎浜ゆ槗");
+            pointsFlow.setDataType(dataType != null ? dataType : 1);
+            pointsFlow.setPoints(-points); // 璐熸暟琛ㄧず鎵e噺
+            pointsFlow.setName(remark != null ? remark : "绉垎鎵e噺"); // name瀛楁瀛樺偍remark鍐呭
+            pointsFlow.setFlowTime(LocalDateTime.now());
+            pointsFlow.setRlueId(null); // 鐩存帴鎵e噺锛屼笉鍏宠仈瑙勫垯
+
+            boolean saved = save(pointsFlow);
+            if (!saved) {
+                throw new BusinessException("淇濆瓨绉垎娴佹按澶辫触");
+            }
+
+            // 鏇存柊鐢ㄦ埛绉垎璐︽埛
+            updateUserPointsOnly(userId, -points);
+
+            // 鏂板绉垎浜ゆ槗璁板綍锛堜笌娴佹按鍚屼簨鍔★級
+            PointsTransaction trans = new PointsTransaction();
+            // tb_points_transaction 鐨� chk_data_category 浠呭厑璁� '鐢ㄦ埛鍙備笌'銆�'鍏朵粬'
+            // 杩欓噷灏嗕笟鍔$被鐩槧灏勪负鏁版嵁搴撳厑璁哥殑鍊�
+            trans.setDataCategory("鐢ㄦ埛鍙備笌");
+            trans.setTransactionName(pointsFlow.getName());
+            trans.setTransactionTime(LocalDateTime.now());
+            trans.setPointsChange(-points);
+            trans.setRuleType("娑堣��");
+            try {
+                trans.setUserId(Long.valueOf(userId));
+            } catch (Exception ignore) {}
+            try {
+                if (StringUtils.hasText(unitId)) trans.setUnitId(Long.valueOf(unitId));
+            } catch (Exception ignore) {}
+            trans.setUserType("涓汉鐢ㄦ埛");
+            trans.setRuleId(null);
+            trans.setDetailId(null);
+            trans.setCreatedAt(LocalDateTime.now());
+            trans.setDeleted(0);
+            int inserted = pointsTransactionMapper.insert(trans);
+            log.info("Points transaction inserted rows={}, id={}", inserted, trans.getId());
+            if (inserted <= 0 || trans.getId() == null) {
+                throw new BusinessException("淇濆瓨绉垎浜ゆ槗璁板綍澶辫触");
+            }
+            // 涓烘彁渚涜�呮柊澧炵Н鍒�
+            AddPointsFlowDTO addPointsFlowDTO = new AddPointsFlowDTO();
+            addPointsFlowDTO.setCategory("璧勬簮浜ゆ槗");
+            addPointsFlowDTO.setCount(1);
+            addPointsFlowDTO.setProviderId(providerId);
+            addPointsFlowDTO.setRuleType(0);
+            addPointsFlowDTO.setRuleNameCode("digital_product_transaction");
+            addPointsFlowDTO.setDescription("浜у搧浜ゆ槗");
+            addPointsFlowByRule(addPointsFlowDTO);
+            return true;
+        } catch (Exception e) {
+            log.error("鎵e噺鐢ㄦ埛绉垎澶辫触", e);
+            throw new BusinessException("鎵e噺鐢ㄦ埛绉垎澶辫触锛�" + e.getMessage());
         }
     }
 
@@ -606,4 +697,40 @@
             userPointsMapper.updateById(unitPoints);
         }
     }
+
+    /**
+     * 浠呮洿鏂颁釜浜虹Н鍒嗚处鎴凤紙涓嶆搷浣滃崟浣嶈处鎴凤級
+     */
+    private void updateUserPointsOnly(String userId, Integer pointsValue) {
+        // 鏇存柊涓汉绉垎璐︽埛
+        QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>();
+        userWrapper.eq("deleted", 0)
+                  .eq("user_id", userId);
+        UserPoints userPoints = userPointsMapper.selectOne(userWrapper);
+        if (userPoints == null) {
+            if (pointsValue < 0) {
+                throw new BusinessException("涓汉绉垎浣欓涓嶈冻锛屾棤娉曟墸闄ょН鍒�");
+            }
+            userPoints = new UserPoints();
+            userPoints.setUserId(userId);
+            userPoints.setUnitId(null);
+            userPoints.setBalance(pointsValue);
+            userPoints.setTotalEarned(pointsValue > 0 ? pointsValue : 0);
+            userPoints.setTotalConsumed(pointsValue < 0 ? Math.abs(pointsValue) : 0);
+            userPointsMapper.insert(userPoints);
+        } else {
+            if (pointsValue < 0 && userPoints.getBalance() + pointsValue < 0) {
+                throw new BusinessException("涓汉绉垎浣欓涓嶈冻锛屽綋鍓嶄綑棰�: " + userPoints.getBalance() + "锛岄渶瑕佹墸闄�: " + Math.abs(pointsValue));
+            }
+            userPoints.setBalance(userPoints.getBalance() + pointsValue);
+            if (pointsValue > 0) {
+                userPoints.setTotalEarned(userPoints.getTotalEarned() != null ? userPoints.getTotalEarned() + pointsValue : pointsValue);
+            }
+            if (pointsValue < 0) {
+                userPoints.setTotalConsumed(userPoints.getTotalConsumed() != null ? userPoints.getTotalConsumed() + Math.abs(pointsValue) : Math.abs(pointsValue));
+            }
+            userPoints.setUpdateTime(LocalDateTime.now());
+            userPointsMapper.updateById(userPoints);
+        }
+    }
 }

--
Gitblit v1.8.0