From 24f7f046f8854d63839ad52d0cff34ea45a0f449 Mon Sep 17 00:00:00 2001
From: gongzuming <gongzuming>
Date: 星期二, 12 十一月 2024 13:10:17 +0800
Subject: [PATCH] 优化

---
 src/main/java/com/mzl/flower/service/point/CustomerPointService.java |   79 ++++++++++++++++++++++-----------------
 1 files changed, 45 insertions(+), 34 deletions(-)

diff --git a/src/main/java/com/mzl/flower/service/point/CustomerPointService.java b/src/main/java/com/mzl/flower/service/point/CustomerPointService.java
index 9950f12..9d1307e 100644
--- a/src/main/java/com/mzl/flower/service/point/CustomerPointService.java
+++ b/src/main/java/com/mzl/flower/service/point/CustomerPointService.java
@@ -15,6 +15,7 @@
 import com.mzl.flower.mapper.point.CustomerPointDetailMapper;
 import com.mzl.flower.mapper.point.CustomerPointMapper;
 import com.mzl.flower.service.BaseService;
+import com.mzl.flower.service.payment.RedisLockService;
 import com.mzl.flower.utils.DateUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
@@ -36,10 +37,13 @@
 
     private final CustomerPointDetailMapper customerPointDetailMapper;
 
+    private final  RedisLockService redisLockService;
 
-    public CustomerPointService(CustomerPointMapper customerPointMapper, CustomerPointDetailMapper customerPointDetailMapper) {
+
+    public CustomerPointService(CustomerPointMapper customerPointMapper, CustomerPointDetailMapper customerPointDetailMapper, RedisLockService redisLockService) {
         this.customerPointMapper = customerPointMapper;
         this.customerPointDetailMapper = customerPointDetailMapper;
+        this.redisLockService = redisLockService;
     }
 
     public Page<CustomerPointDTO> queryPage(QueryCustomerPointDTO dto, Page page) {
@@ -79,44 +83,51 @@
     }
 
     public void updateCustomerPoint(CustomerPointDetail detail) {
-        CustomerPoint point = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
-                .eq(CustomerPoint::getCustomerId, detail.getCustomerId()));
-        boolean isAdd = false;
-        if(point == null){
-            isAdd = true;
-            point = new CustomerPoint();
-            point.setCustomerId(detail.getCustomerId());
-            point.setUserId(detail.getUserId());
-            point.setTotalPoint(0);
-            point.setUsedPoint(0);
-            point.setExpiredPoint(0);
-            point.setDeductionPoint(0);
-            point.create(SecurityUtils.getUserId());
+        boolean lock = redisLockService.getObjectLock("LOCK_KEY_CUSTOMER_POINT_", detail.getUserId());
+        if(!lock){
+            return;
         }
+        try {
+            CustomerPoint point = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
+                    .eq(CustomerPoint::getCustomerId, detail.getCustomerId()));
+            boolean isAdd = false;
+            if(point == null){
+                isAdd = true;
+                point = new CustomerPoint();
+                point.setCustomerId(detail.getCustomerId());
+                point.setUserId(detail.getUserId());
+                point.setTotalPoint(0);
+                point.setUsedPoint(0);
+                point.setExpiredPoint(0);
+                point.setDeductionPoint(0);
+                point.create(SecurityUtils.getUserId());
+            }
 
-        if(POINT_CHANGE_TYPE.add.name().equals(detail.getChangeType())){
-            point.setTotalPoint(point.getTotalPoint() + detail.getPoint());
-        }else if(POINT_CHANGE_TYPE.reduce.name().equals(detail.getChangeType())){
-            Integer detailPoint = detail.getPoint();
-            if(POINT_TYPE.deduction.name().equals(detail.getType())){ //积分扣减
-                Integer balancePoint = point.getTotalPoint() - point.getUsedPoint()-point.getExpiredPoint()-point.getDeductionPoint();//可用积分
-                balancePoint = balancePoint.intValue()>0?balancePoint.intValue():0;
-                if(balancePoint.intValue() >= detail.getPoint().intValue()){
-                    point.setDeductionPoint(point.getDeductionPoint() + detail.getPoint());
-                }else{
-                    //积分不足,直接清0
-                    point.setDeductionPoint(point.getDeductionPoint() + balancePoint);
-                    detail.setPoint(balancePoint);
-                    detail.setRemarks(detail.getRemarks()+",当前积分不足"+detailPoint+",实际扣除积分"+balancePoint);
+            if(POINT_CHANGE_TYPE.add.name().equals(detail.getChangeType())){
+                point.setTotalPoint(point.getTotalPoint() + detail.getPoint());
+            }else if(POINT_CHANGE_TYPE.reduce.name().equals(detail.getChangeType())){
+                Integer detailPoint = detail.getPoint();
+                if(POINT_TYPE.deduction.name().equals(detail.getType())){ //积分扣减
+                    Integer balancePoint = point.getTotalPoint() - point.getUsedPoint()-point.getExpiredPoint()-point.getDeductionPoint();//可用积分
+                    balancePoint = balancePoint.intValue()>0?balancePoint.intValue():0;
+                    if(balancePoint.intValue() >= detail.getPoint().intValue()){
+                        point.setDeductionPoint(point.getDeductionPoint() + detail.getPoint());
+                    }else{
+                        //积分不足,直接清0
+                        point.setDeductionPoint(point.getDeductionPoint() + balancePoint);
+                        detail.setPoint(balancePoint);
+                        detail.setRemarks(detail.getRemarks()+",当前积分不足"+detailPoint+",实际扣除积分"+balancePoint);
+                    }
                 }
             }
+            if(isAdd){
+                customerPointMapper.insert(point);
+            }else{
+                customerPointMapper.updateById(point);
+            }
+        }finally {
+            redisLockService.releaseObjectLock("LOCK_KEY_CUSTOMER_POINT_", detail.getUserId());
         }
-        if(isAdd){
-            customerPointMapper.insert(point);
-        }else{
-            customerPointMapper.updateById(point);
-        }
-
     }
 
     public void deductionPoint(ChangePointDTO dto) {

--
Gitblit v1.9.3