From b0c5d432ec041221dcbe81ca5ae3aa20fe3bddc6 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Mon, 22 Mar 2021 20:17:58 +0800
Subject: [PATCH] Merge branch 'score_shop' of http://120.27.238.55:7000/r/beauty-erp into score_shop

---
 zq-erp/src/main/java/com/matrix/system/score/service/ScoreVipDetailService.java |  236 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 231 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 807819d..9002b42 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,246 @@
 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.core.conditions.query.QueryWrapper;
 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.core.tools.DateUtil;
+import com.matrix.system.common.bean.BusParameterSettings;
+import com.matrix.system.common.dao.BusParameterSettingsDao;
+import com.matrix.system.common.dao.SysUsersDao;
+import com.matrix.system.hive.dao.SysVipInfoDao;
+import com.matrix.system.score.constant.ScoreSettingConstant;
+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.Calendar;
+import java.util.Date;
+import java.util.List;
 
 /**
- * @description 客户积分余额
  * @author jyy
+ * @description 客户积分余额
  * @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;
+
+    @Autowired
+    SysUsersDao sysUsersDao;
+
+    @Autowired
+    BusParameterSettingsDao busParameterSettingsDao;
+
+    /**
+     * 扣除用户积分
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void deductionScore(String openId, Long vipId,Long shopId, Integer score, Long businessId, int type,String remark) {
+        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.setType(type);
+            scoreUseRecord.setShopId(shopId);
+            scoreUseRecord.setOpenId(openId);
+            scoreUseRecord.setVipId(vipId);
+            scoreUseRecord.setRemarks(remark);
+            scoreUseRecordDao.insert(scoreUseRecord);
+
+            if(surplus > 0 || surplus == 0){
+                break;
+            }
+        }
+        if(score>0){
+            throw new GlobleException("积分不足");
+        }
+    }
+
+    /**
+     * 新增用户积分
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void addScore(String openId, Long vipId, Long oprationUserId,Long shopId, Integer score, Long businessId, int type,String remark) {
+        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必须有一个");
+        }
+        if(score<0){
+            throw new IllegalArgumentException("score必须有为大于零的数");
+        }
+
+        String createBy=MatrixConstance.SYSTEM_USER;
+        if(oprationUserId!=null){
+            createBy=sysUsersDao.selectById(oprationUserId).getSuName();
+        }
+
+        //计算过期时间
+        BusParameterSettings yxqSetting = busParameterSettingsDao.selectCompanyParamByCode(ScoreSettingConstant.VALID_PERIOD_POINTS, companyId);
+        Calendar cal=Calendar.getInstance();
+        int year = cal.get(Calendar.YEAR);
+        int sxYear=year+(Integer.parseInt(yxqSetting.getParamValue())-1);
+        Date sxys=DateUtil.stringToDate(year+"-12-31 23:59",DateUtil.DATE_FORMAT_MM );
+
+        ScoreVipDetail scoreVipDetail=new ScoreVipDetail();
+
+        scoreVipDetail.setCreateBy(createBy);
+        scoreVipDetail.setUpdateBy(createBy);
+        scoreVipDetail.setCreateTime(DateTime.now());
+        scoreVipDetail.setUpdateTime(DateTime.now());
+        scoreVipDetail.setState(ScoreVipDetail.SCORE_STATUS_YX);
+        scoreVipDetail.setRemainScore(score);
+        scoreVipDetail.setUsedScore(0);
+        scoreVipDetail.setBusinessId(businessId);
+        scoreVipDetail.setValiditeTime(sxys);
+        scoreVipDetail.setOpenId(openId);
+        scoreVipDetail.setVipId(vipId);
+        scoreVipDetail.setGainTime(DateTime.now());
+        scoreVipDetail.setAllScore(score);
+        scoreVipDetail.setType(type);
+        scoreVipDetail.setCompanyId(companyId);
+        scoreVipDetailDao.insert(scoreVipDetail);
+
+        //新增添加记录
+        ScoreUseRecord   scoreUseRecord = new ScoreUseRecord();
+        scoreUseRecord.setPreScore(score);
+        scoreUseRecord.setCreateBy(createBy);
+        scoreUseRecord.setUpdateBy(createBy);
+        scoreUseRecord.setCreateTime(DateTime.now());
+        scoreUseRecord.setUpdateTime(DateTime.now());
+        scoreUseRecord.setNowScore(score);
+        scoreUseRecord.setCompanyId(companyId);
+        scoreUseRecord.setScoreVipDetailId(scoreVipDetail.getId());
+        scoreUseRecord.setBusinessId(businessId);
+        scoreUseRecord.setRecNum(score);
+        scoreUseRecord.setType(type);
+        scoreUseRecord.setOpenId(openId);
+        scoreUseRecord.setVipId(vipId);
+        scoreUseRecord.setShopId(shopId);
+        scoreUseRecord.setOprationUserId(oprationUserId);
+        scoreUseRecord.setRemarks(remark);
+        scoreUseRecordDao.insert(scoreUseRecord);
 
 
+    }
 
+    /**
+     * 退积分
+     */
+    public void refundScore(String openId, Long vipId, Integer score, Long oldBusinessId, 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必须有一个");
+        }
+        if(score<0){
+            throw new IllegalArgumentException("score必须有为大于零的数");
+        }
 
-  
+        //查询原始使用记录
+        QueryWrapper queryWrapper=new QueryWrapper();
+        queryWrapper.eq("business_id",oldBusinessId);
+        queryWrapper.eq("company_id",companyId);
+        queryWrapper.eq("type",type);
+        List<ScoreUseRecord> scoreUseRecordList = scoreUseRecordDao.selectList(queryWrapper);
+        scoreUseRecordList.forEach(scoreUseRecord -> {
+            ScoreVipDetail scoreVipDetail = scoreVipDetailDao.selectById(scoreUseRecord.getScoreVipDetailId());
+            if(scoreVipDetail!=null){
+                if(DateTime.now().isAfter(scoreVipDetail.getValiditeTime())){
+                    //积分还有效
+                    scoreVipDetail.setRemainScore( scoreVipDetail.getRemainScore()+score);
+                    scoreVipDetail.setUsedScore(scoreVipDetail.getUsedScore()-score);
+                    scoreVipDetail.setState(ScoreVipDetail.SCORE_STATUS_YX);
+                    scoreVipDetailDao.updateById(scoreVipDetail);
+                    scoreUseRecordDao.deleteById(scoreUseRecord.getId());
+                }
+            }
+
+        });
+    }
+
+    public void removeByBusinessId(String openId, Long vipId,  Long oldBusinessId, 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必须有一个");
+        }
+        if(oldBusinessId==null){
+            throw new IllegalArgumentException("oldBusinessId必须有");
+        }
+        QueryWrapper queryWrapper=new QueryWrapper();
+        queryWrapper.eq("business_id",oldBusinessId);
+        queryWrapper.eq("company_id",companyId);
+        queryWrapper.eq("type",type);
+        scoreVipDetailDao.delete(queryWrapper);
+        scoreUseRecordDao.delete(queryWrapper);
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.1