From f0da95925bcb8ad19b0d1cc78afe4b4a4ae188a6 Mon Sep 17 00:00:00 2001
From: p-honggang.li <p-honggang.li@pcitc.com>
Date: 星期四, 28 八月 2025 09:55:51 +0800
Subject: [PATCH] Merge branch 'master' of http://xearth.cn:6600/r/web-manage/web-manage-back

---
 src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java |  196 ++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 152 insertions(+), 44 deletions(-)

diff --git a/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java b/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java
index 8cdf5be..b21c1e4 100644
--- a/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java
+++ b/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java
@@ -42,7 +42,7 @@
 
     @Override
     public PageResult<PointsFlow> getPersonalPointsFlowPage(PointsFlowQueryDTO queryDTO) {
-        if (queryDTO.getUserId() == null) {
+        if (!StringUtils.hasText(queryDTO.getUserId())) {
             throw new BusinessException("鐢ㄦ埛ID涓嶈兘涓虹┖");
         }
 
@@ -123,68 +123,118 @@
             throw new BusinessException("鍙傛暟涓嶈兘涓虹┖");
         }
 
-        Long userId = addPointsFlowDTO.getUserId();
-        Long unitId = addPointsFlowDTO.getUnitId();
+        String userId = addPointsFlowDTO.getUserId();
+        String providerId = addPointsFlowDTO.getProviderId();
+        String unitId = addPointsFlowDTO.getUnitId();
         Integer ruleType = addPointsFlowDTO.getRuleType();
         String category = addPointsFlowDTO.getCategory();
         String ruleNameCode = addPointsFlowDTO.getRuleNameCode();
         Integer count = addPointsFlowDTO.getCount() != null ? addPointsFlowDTO.getCount() : 1;
 
-        // 鏍规嵁ruleType銆乺uleNameCode銆乧ategory鏌ヨ鐢熸晥鏃堕棿鏈�鏂扮殑绉垎瑙勫垯
-        PointsRule pointsRule = getLatestEffectiveRule(ruleType, ruleNameCode, category);
-        if (pointsRule == null) {
-            throw new BusinessException("绉垎瑙勫垯涓嶅瓨鍦ㄦ垨鏈惎鐢�: ruleType=" + ruleType + ", ruleNameCode=" + ruleNameCode + ", category=" + category);
+        boolean applyUser = StringUtils.hasText(userId) ;
+        boolean applyProvider = StringUtils.hasText(providerId) ;;
+        if (!applyUser && !applyProvider) {
+            throw new BusinessException("userId 涓� providerId 涓嶈兘鍚屾椂涓虹┖");
         }
 
-        // 楠岃瘉瑙勫垯绫诲瀷鏄惁鍖归厤
-        if (!ruleType.equals(pointsRule.getRuleType())) {
-            throw new BusinessException("瑙勫垯绫诲瀷涓嶅尮閰嶏紝鏈熸湜: " + pointsRule.getRuleType() + "锛屽疄闄�: " + ruleType);
+        // 鍒嗗埆鑾峰彇鐢ㄦ埛渚�/鎻愪緵鑰呬晶鏈�鏂扮敓鏁堣鍒欙紙鎸� points_winner 杩囨护锛�1鐢ㄦ埛銆�0鎻愪緵鑰咃級
+        PointsRule userRule = null;
+        PointsRule providerRule = null;
+        if (applyUser) {
+            userRule = getLatestEffectiveRule(ruleType, ruleNameCode, category, 1);
+            if (userRule == null) {
+                throw new BusinessException("鏈壘鍒扮敤鎴蜂晶鐢熸晥瑙勫垯: ruleType=" + ruleType + ", ruleNameCode=" + ruleNameCode + ", category=" + category + ", points_winner=1");
+            }
+            if (!ruleType.equals(userRule.getRuleType())) {
+                throw new BusinessException("鐢ㄦ埛渚ц鍒欑被鍨嬩笉鍖归厤锛屾湡鏈�: " + userRule.getRuleType() + "锛屽疄闄�: " + ruleType);
+            }
+        }
+        if (applyProvider) {
+            providerRule = getLatestEffectiveRule(ruleType, ruleNameCode, category, 0);
+            if (providerRule == null) {
+                throw new BusinessException("鏈壘鍒版彁渚涜�呬晶鐢熸晥瑙勫垯: ruleType=" + ruleType + ", ruleNameCode=" + ruleNameCode + ", category=" + category + ", points_winner=0");
+            }
+            if (!ruleType.equals(providerRule.getRuleType())) {
+                throw new BusinessException("鎻愪緵鑰呬晶瑙勫垯绫诲瀷涓嶅尮閰嶏紝鏈熸湜: " + providerRule.getRuleType() + "锛屽疄闄�: " + ruleType);
+            }
         }
 
-        // 璁$畻绉垎鍊�
-        Integer basePoints = pointsRule.getPointsValue() != null ? pointsRule.getPointsValue() : 0;
-        Integer totalPoints = basePoints * count;
-
-        // 濡傛灉鏄秷璐圭被鍨嬶紝绉垎涓鸿礋鏁�
-        if (ruleType == RuleTypeEnum.CONSUME.getCode()) { // 1琛ㄧず娑堣垂绫诲瀷
-            totalPoints = -totalPoints;
+        // 璁$畻涓や晶绉垎鍙樺寲锛堝悇鑷寜瑙勫垯璁$畻锛�
+        int userPointsChange = 0;
+        int providerPointsChange = 0;
+        if (applyUser) {
+            int base = userRule.getPointsValue() != null ? userRule.getPointsValue() : 0;
+            int amount = base * count;
+            userPointsChange = (ruleType == RuleTypeEnum.CONSUME.getCode()) ? -Math.abs(amount) : Math.abs(amount);
+        }
+        if (applyProvider) {
+            int base = providerRule.getPointsValue() != null ? providerRule.getPointsValue() : 0;
+            int amount = base * count;
+            providerPointsChange = (ruleType == RuleTypeEnum.CONSUME.getCode()) ? -Math.abs(amount) : Math.abs(amount);
         }
 
-        // 妫�鏌ユ瘡鏃ョН鍒嗕笂闄�
-        if (pointsRule.getIsLimit() != null && pointsRule.getIsLimit() == 0) { // 0琛ㄧず鏈夋瘡鏃ヤ笂闄�
-            checkDailyLimitByRule(userId, unitId, pointsRule, totalPoints);
+        // 姣忔棩涓婇檺鏍¢獙锛堝垎鍒牎楠岋級
+        if (applyUser && userRule.getIsLimit() != null && userRule.getIsLimit() == 0) {
+            checkDailyLimitByRule(userId, unitId, userRule, userPointsChange);
+        }
+        if (applyProvider && providerRule.getIsLimit() != null && providerRule.getIsLimit() == 0) {
+            checkDailyLimitByRule(providerId, providerId, providerRule, providerPointsChange);
         }
 
-        // 濡傛灉鏄墸绉垎鎿嶄綔锛屽厛妫�鏌ヤ綑棰濇槸鍚﹁冻澶�
-        if (totalPoints < 0) {
-            checkBalanceSufficient(userId, unitId, Math.abs(totalPoints));
+        // 浣欓鏍¢獙锛堜粎鍦ㄦ墸鍑忔椂锛�
+        if (applyUser && userPointsChange < 0) {
+            checkBalanceSufficient(userId, unitId, Math.abs(userPointsChange));
+        }
+        if (applyProvider && providerPointsChange < 0) {
+            checkBalanceSufficient(providerId, providerId, Math.abs(providerPointsChange));
         }
 
-        // 鍒涘缓绉垎娴佹按璁板綍
-        PointsFlow pointsFlow = new PointsFlow();
-        pointsFlow.setUserId(userId);
-        pointsFlow.setUnitId(unitId);
-        pointsFlow.setDataType(ruleType);
-        pointsFlow.setDataCategory(addPointsFlowDTO.getCategory());
-        pointsFlow.setPoints(totalPoints);
-        pointsFlow.setName(addPointsFlowDTO.getDescription() != null ? addPointsFlowDTO.getDescription() : pointsRule.getRuleDescription());
-        pointsFlow.setFlowTime(LocalDateTime.now());
-        pointsFlow.setRlueId(pointsRule.getId());
+        // 鐢熸垚鐢ㄦ埛渚ф祦姘村苟鏇存柊璐︽埛
+        if (applyUser && userPointsChange != 0) {
+            PointsFlow pointsFlow = new PointsFlow();
+            pointsFlow.setUserId(userId);
+            pointsFlow.setUnitId(unitId);
+            pointsFlow.setDataType(ruleType);
+            pointsFlow.setDataCategory(addPointsFlowDTO.getCategory());
+            pointsFlow.setPoints(userPointsChange);
+            pointsFlow.setName(addPointsFlowDTO.getDescription() != null ? addPointsFlowDTO.getDescription() : userRule.getRuleDescription());
+            pointsFlow.setFlowTime(LocalDateTime.now());
+            pointsFlow.setRlueId(userRule.getId());
 
-        boolean saved = save(pointsFlow);
-        if (!saved) {
-            throw new BusinessException("淇濆瓨绉垎娴佹按澶辫触");
+            boolean saved = save(pointsFlow);
+            if (!saved) {
+                throw new BusinessException("淇濆瓨绉垎娴佹按澶辫触");
+            }
+
+            updateUserPointsByRule(userId, unitId, userPointsChange);
         }
 
-        // 鏇存柊鐢ㄦ埛绉垎璐︽埛
-        updateUserPointsByRule(userId, unitId, totalPoints);
+        // 鐢熸垚鎻愪緵鑰呬晶娴佹按骞舵洿鏂拌处鎴�
+        if (applyProvider && providerPointsChange != 0) {
+            PointsFlow providerFlow = new PointsFlow();
+            providerFlow.setUserId(providerId);
+            providerFlow.setUnitId(providerId);
+            providerFlow.setDataType(ruleType);
+            providerFlow.setDataCategory(addPointsFlowDTO.getCategory());
+            providerFlow.setPoints(providerPointsChange);
+            providerFlow.setName(addPointsFlowDTO.getDescription() != null ? addPointsFlowDTO.getDescription() : providerRule.getRuleDescription());
+            providerFlow.setFlowTime(LocalDateTime.now());
+            providerFlow.setRlueId(providerRule.getId());
+
+            boolean providerSaved = save(providerFlow);
+            if (!providerSaved) {
+                throw new BusinessException("淇濆瓨鎻愪緵鑰呯Н鍒嗘祦姘村け璐�");
+            }
+
+            updateProviderUnitPoints(providerId, providerPointsChange);
+        }
 
         return true;
     }
 
     @Override
-    public UserPoints getUserPointsTotal(Long userId) {
-        if (userId == null) {
+    public UserPoints getUserPointsTotal(String userId) {
+        if (!StringUtils.hasText(userId)) {
             throw new BusinessException("鐢ㄦ埛ID涓嶈兘涓簄ull");
         }
         
@@ -272,9 +322,25 @@
     }
 
     /**
+     * 鏍规嵁ruleType銆乺uleNameCode銆乧ategory銆乸oints_winner鏌ヨ鐢熸晥鏃堕棿鏈�鏂扮殑绉垎瑙勫垯
+     */
+    private PointsRule getLatestEffectiveRule(Integer ruleType, String ruleNameCode, String category, Integer pointsWinner) {
+        QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
+        wrapper.eq("deleted", 0)
+               .eq("is_enabled", 0)
+               .eq("rule_type", ruleType)
+               .eq("rule_name_code", ruleNameCode)
+               .eq("data_category", category)
+               .eq(pointsWinner != null, "points_winner", pointsWinner)
+               .orderByDesc("created_at")
+               .last("LIMIT 1");
+        return pointsRuleService.getOne(wrapper);
+    }
+
+    /**
      * 妫�鏌ユ瘡鏃ョН鍒嗕笂闄愶紙鍩轰簬瑙勫垯锛�
      */
-    private void checkDailyLimitByRule(Long userId, Long unitId, PointsRule pointsRule, Integer currentPoints) {
+    private void checkDailyLimitByRule(String userId, String unitId, PointsRule pointsRule, Integer currentPoints) {
         // 鑾峰彇浠婃棩寮�濮嬪拰缁撴潫鏃堕棿
         LocalDate today = LocalDate.now();
         LocalDateTime startOfDay = today.atStartOfDay();
@@ -315,7 +381,7 @@
     /**
      * 妫�鏌ョН鍒嗕綑棰濇槸鍚﹁冻澶�
      */
-    private void checkBalanceSufficient(Long userId, Long unitId, Integer requiredPoints) {
+    private void checkBalanceSufficient(String userId, String unitId, Integer requiredPoints) {
         // 妫�鏌ヤ釜浜虹Н鍒嗕綑棰�
         QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>();
         userWrapper.eq("deleted", 0)
@@ -371,7 +437,7 @@
     /**
      * 鏇存柊鐢ㄦ埛绉垎
      */
-    private void updateUserPoints(Long userId, Long unitId, Integer pointsValue) {
+    private void updateUserPoints(String userId, String unitId, Integer pointsValue) {
         // 鏇存柊涓汉绉垎
         QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>();
         userWrapper.eq("deleted", 0)
@@ -412,7 +478,7 @@
     /**
      * 鏍规嵁瑙勫垯鏇存柊鐢ㄦ埛绉垎璐︽埛
      */
-    private void updateUserPointsByRule(Long userId, Long unitId, Integer pointsValue) {
+    private void updateUserPointsByRule(String userId, String unitId, Integer pointsValue) {
         // 鏇存柊涓汉绉垎璐︽埛
         QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>();
         userWrapper.eq("deleted", 0)
@@ -499,4 +565,46 @@
             userPointsMapper.updateById(unitPoints);
         }
     }
+
+    /**
+     * 浠呮洿鏂版彁渚涜�咃紙鍗曚綅锛夌Н鍒嗚处鎴�
+     */
+    private void updateProviderUnitPoints(String providerUnitId, Integer pointsValue) {
+        QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>();
+        unitWrapper.eq("deleted", 0)
+                  .eq("unit_id", providerUnitId);
+
+        UserPoints unitPoints = userPointsMapper.selectOne(unitWrapper);
+        if (unitPoints == null) {
+            if (pointsValue < 0) {
+                throw new BusinessException("鎻愪緵鑰呯Н鍒嗕綑棰濅笉瓒筹紝鏃犳硶鎵i櫎绉垎");
+            }
+
+            unitPoints = new UserPoints();
+            unitPoints.setUserId(null);
+            unitPoints.setUnitId(providerUnitId);
+            unitPoints.setBalance(pointsValue);
+            unitPoints.setTotalEarned(pointsValue > 0 ? pointsValue : 0);
+            unitPoints.setTotalConsumed(pointsValue < 0 ? Math.abs(pointsValue) : 0);
+            userPointsMapper.insert(unitPoints);
+        } else {
+            if (pointsValue < 0 && unitPoints.getBalance() + pointsValue < 0) {
+                throw new BusinessException("鎻愪緵鑰呯Н鍒嗕綑棰濅笉瓒筹紝褰撳墠浣欓: " + unitPoints.getBalance() + "锛岄渶瑕佹墸闄�: " + Math.abs(pointsValue));
+            }
+
+            unitPoints.setBalance(unitPoints.getBalance() + pointsValue);
+
+            if (pointsValue > 0) {
+                unitPoints.setTotalEarned(unitPoints.getTotalEarned() != null ?
+                        unitPoints.getTotalEarned() + pointsValue : pointsValue);
+            }
+            if (pointsValue < 0) {
+                unitPoints.setTotalConsumed(unitPoints.getTotalConsumed() != null ?
+                        unitPoints.getTotalConsumed() + Math.abs(pointsValue) : Math.abs(pointsValue));
+            }
+
+            unitPoints.setUpdateTime(LocalDateTime.now());
+            userPointsMapper.updateById(unitPoints);
+        }
+    }
 }

--
Gitblit v1.8.0