From afa3a318dad2934722ff9c197be7b03eb3ebcf6a Mon Sep 17 00:00:00 2001
From: 935090232@qq.com <ak473600000>
Date: Sun, 21 Mar 2021 22:51:52 +0800
Subject: [PATCH] 扣除用户积分

---
 zq-erp/src/main/java/com/matrix/system/score/service/ScoreVipDetailService.java |   97 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 92 insertions(+), 5 deletions(-)

diff --git a/zq-erp/src/main/java/com/matrix/system/score/service/ScoreVipDetailService.java b/zq-erp/src/main/java/com/matrix/system/score/service/ScoreVipDetailService.java
index 44ea832..38cfd4d 100644
--- a/zq-erp/src/main/java/com/matrix/system/score/service/ScoreVipDetailService.java
+++ b/zq-erp/src/main/java/com/matrix/system/score/service/ScoreVipDetailService.java
@@ -1,20 +1,107 @@
 package com.matrix.system.score.service;
 
-import com.matrix.system.score.dao.ScoreVipDetailDao;
-import com.matrix.system.score.entity.ScoreVipDetail;
+import cn.hutool.core.date.DateTime;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.matrix.biz.dao.BizUserDao;
+import com.matrix.core.constance.MatrixConstance;
+import com.matrix.core.exception.GlobleException;
+import com.matrix.system.hive.dao.SysVipInfoDao;
+import com.matrix.system.score.dao.ScoreUseRecordDao;
+import com.matrix.system.score.dao.ScoreVipDetailDao;
+import com.matrix.system.score.entity.ScoreUseRecord;
+import com.matrix.system.score.entity.ScoreVipDetail;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
 
 /**
+ * @author jyy
  * @description 客户积分余额
- * @author yourName
  * @date 2021-02-26 15:26
  */
 @Service
-public class ScoreVipDetailService  extends ServiceImpl<ScoreVipDetailDao, ScoreVipDetail>{
+public class ScoreVipDetailService extends ServiceImpl<ScoreVipDetailDao, ScoreVipDetail> {
 
+    @Autowired
+    ScoreVipDetailDao scoreVipDetailDao;
 
+    @Autowired
+    ScoreUseRecordDao scoreUseRecordDao;
 
+    @Autowired
+    BizUserDao bizUserDao;
 
-  
+    @Autowired
+    SysVipInfoDao vipInfoDao;
+
+    /**
+     * 扣除用户积分
+     *
+     * @param openId
+     * @param score
+     * @param businessId
+     * @param type
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void deductionScore(String openId, Long vipId, Integer score, Long businessId, int type) {
+        Long companyId=null;
+        if(openId!=null){
+            companyId= bizUserDao.findByOpenId(openId).getCompanyId();
+        }else if(vipId!=null){
+            companyId= vipInfoDao.selectById(vipId).getCompanyId();
+        }else{
+            throw new IllegalArgumentException("vipId,openId必须有一个");
+        }
+
+        List<ScoreVipDetail> effectiveScoreList = scoreVipDetailDao.selectEffectiveScore(openId,null);
+
+        for (ScoreVipDetail scoreVipDetail : effectiveScoreList) {
+
+            ScoreUseRecord scoreUseRecord = new ScoreUseRecord();
+            scoreUseRecord.setPreScore(scoreVipDetail.getRemainScore());
+
+            int surplus = scoreVipDetail.getRemainScore() - score;
+            int currentDedution = 0;
+            if (surplus > 0 || surplus == 0) {
+                //余额充足
+                currentDedution = score;
+                scoreVipDetail.setRemainScore(surplus);
+                if(surplus==0){
+                    scoreVipDetail.setState(ScoreVipDetail.SCORE_STATUS_WX);
+                }
+                scoreVipDetailDao.updateById(scoreVipDetail);
+                score=0;
+            } else {
+                currentDedution = scoreVipDetail.getRemainScore();
+                scoreVipDetail.setState(ScoreVipDetail.SCORE_STATUS_WX);
+                scoreVipDetail.setRemainScore(0);
+                scoreVipDetailDao.updateById(scoreVipDetail);
+                score=Math.abs(surplus);
+            }
+            //新增扣除记录
+            scoreUseRecord.setCreateBy(MatrixConstance.SYSTEM_USER);
+            scoreUseRecord.setUpdateBy(MatrixConstance.SYSTEM_USER);
+            scoreUseRecord.setCreateTime(DateTime.now());
+            scoreUseRecord.setUpdateTime(DateTime.now());
+            scoreUseRecord.setNowScore(scoreVipDetail.getRemainScore());
+            scoreUseRecord.setCompanyId(companyId);
+            scoreUseRecord.setScoreVipDetailId(scoreVipDetail.getId());
+            scoreUseRecord.setBusinessId(businessId);
+            scoreUseRecord.setRecNum(currentDedution);
+            scoreUseRecord.setRecType(type);
+            scoreUseRecord.setOpenId(openId);
+            scoreUseRecord.setVipId(vipId);
+            scoreUseRecordDao.insert(scoreUseRecord);
+
+            if(surplus > 0 || surplus == 0){
+                break;
+            }
+        }
+        if(score>0){
+            throw new GlobleException("积分不足");
+        }
+
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.1