jyy
2021-04-06 f9bb41486c8c9b89c762d5ccff850e4c23a2caa8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.matrix.system.score.service;
 
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.core.constance.MatrixConstance;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.tools.DateUtil;
import com.matrix.core.tools.StringUtils;
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.bean.SysVipInfo;
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;
 
/**
 * @author jyy
 * @description 客户积分余额
 * @date 2021-02-26 15:26
 */
@Service
public class ScoreVipDetailService extends ServiceImpl<ScoreVipDetailDao, ScoreVipDetail> {
 
    @Autowired
    ScoreVipDetailDao scoreVipDetailDao;
 
    @Autowired
    ScoreUseRecordDao scoreUseRecordDao;
 
    @Autowired
    SysVipInfoDao sysVipInfoDao;
 
    @Autowired
    SysVipInfoDao vipInfoDao;
 
    @Autowired
    SysUsersDao sysUsersDao;
 
    @Autowired
    BusParameterSettingsDao busParameterSettingsDao;
 
    /**
     * 扣除用户积分
     */
    @Transactional(rollbackFor = Exception.class)
    public void deductionScore(Long vipId, Long oprationUserId, Long shopId, Integer score, Long businessId, int type, String remark) {
        Long companyId = vipInfoDao.selectById(vipId).getCompanyId();
 
        String createBy = MatrixConstance.SYSTEM_USER;
        if (oprationUserId != null) {
            createBy = sysUsersDao.selectById(oprationUserId).getSuName();
        }
 
        List<ScoreVipDetail> effectiveScoreList = scoreVipDetailDao.selectEffectiveScore(vipId);
 
        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(createBy);
            scoreUseRecord.setUpdateBy(createBy);
            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.setVipId(vipId);
            scoreUseRecord.setRemarks(remark);
            scoreUseRecord.setOprationUserId(oprationUserId);
            scoreUseRecordDao.insert(scoreUseRecord);
 
            if (surplus > 0 || surplus == 0) {
                break;
            }
        }
        if (score > 0) {
            throw new GlobleException("积分不足");
        }
    }
 
    /**
     * 新增用户积分
     */
    @Transactional(rollbackFor = Exception.class)
    public void addScore(Long vipId, Long oprationUserId, Long shopId, Integer score, Long businessId, int type, String remark) {
        Long companyId = vipInfoDao.selectById(vipId).getCompanyId();
 
        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(sxYear + "-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.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.setVipId(vipId);
        scoreUseRecord.setShopId(shopId);
        scoreUseRecord.setOprationUserId(oprationUserId);
        scoreUseRecord.setRemarks(remark);
        scoreUseRecordDao.insert(scoreUseRecord);
 
 
    }
 
    /**
     * 根据固定等级规则新增用户积分
     */
    @Transactional(rollbackFor = Exception.class)
    public void addScoreByParamSetting(Long vipId, Long oprationUserId, Long shopId, Integer firstScore, Long businessId, int type, String remark, BusParameterSettings parameterSetting) {
 
 
        SysVipInfo vipInfo = sysVipInfoDao.selectById(vipId);
 
 
        //添加自己的积分
        if (firstScore > 0) {
            addScore(
                    vipInfo.getId(),
                    oprationUserId,
                    shopId,
                    firstScore,
                    businessId,
                    type,
                    remark
            );
        }
 
        if (StringUtils.isNotBlank(parameterSetting.getParamValue())
                && vipInfo.getRecommendId() != null) {
            //推荐注册老带新积分奖励
            SysVipInfo referrerVip = sysVipInfoDao.selectById(vipInfo.getRecommendId());
            Integer secondScore = new Integer(parameterSetting.getParamValue());
            if (secondScore > 0) {
                addScore(
                        referrerVip.getId(),
                        oprationUserId,
                        shopId,
                        secondScore,
                        businessId,
                        type,
                        remark
                );
            }
            //推荐注册二级带新积分奖励
            if (StringUtils.isNotBlank(parameterSetting.getParamValue1())
                    && referrerVip.getRecommendId() != null) {
                Integer threeScore = new Integer(parameterSetting.getParamValue());
                if (threeScore > 0) {
                    addScore(
                            referrerVip.getRecommendId(),
                            oprationUserId,
                            shopId,
                            threeScore,
                            businessId,
                            type,
                            remark
                    );
                }
            }
        }
    }
 
 
    /**
     * 退积分
     */
    public void refundScore(Long vipId, Integer score, Long oldBusinessId, int type) {
 
        Long companyId = vipInfoDao.selectById(vipId).getCompanyId();
 
        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(Long vipId, Long oldBusinessId) {
        Long companyId = vipInfoDao.selectById(vipId).getCompanyId();
 
        if (oldBusinessId == null) {
            throw new IllegalArgumentException("oldBusinessId必须有");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("business_id", oldBusinessId);
        queryWrapper.eq("company_id", companyId);
        scoreVipDetailDao.delete(queryWrapper);
        scoreUseRecordDao.delete(queryWrapper);
    }
}