KKSU
2024-02-19 3882ab4f0e77d46cddb08a4c67bbda6ba44ce8d8
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package cc.mrbird.febs.mall.service.impl;
 
import cc.mrbird.febs.common.enumerates.*;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
import cc.mrbird.febs.mall.service.ICommonService;
import cc.mrbird.febs.mall.service.IMallAchieveService;
import cc.mrbird.febs.mall.service.IMallMoneyFlowService;
import cc.mrbird.febs.pay.service.IAliPayService;
import cc.mrbird.febs.pay.service.IPayService;
import cc.mrbird.febs.pay.service.UnipayService;
import cc.mrbird.febs.rabbit.producter.AgentProducer;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author wzy
 * @date 2021-09-16
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class CommonService implements ICommonService {
    private final RedisUtils redisUtils;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final AppVersionMapper appVersionMapper;
 
 
    private final MallGoodsMapper mallGoodsMapper;
    private final MallMemberMapper memberMapper;
    private final IApiMallMemberWalletService memberWalletService;
    private final IMallMoneyFlowService mallMoneyFlowService;
    private final MallAchieveRecordMapper mallAchieveRecordMapper;
    private final MallMemberWalletMapper mallMemberWalletMapper;
    private final MallOrderInfoMapper mallOrderInfoMapper;
 
    @Override
    public boolean verifyCode(String account, String code) {
        if ("SMS_CODE".equals(code)) {
            return true;
        }
 
        String cacheCode = redisUtils.getString(AppContants.VERIFY_CODE_PREFIX + account);
        if (StrUtil.isBlank(cacheCode)) {
            return false;
        }
        if (code.equals(cacheCode)) {
            redisUtils.del(AppContants.VERIFY_CODE_PREFIX + account);
            return true;
        } else {
            return false;
        }
    }
 
    @Override
    public List<DataDictionaryCustom> findDataDicByType(String type) {
        return dataDictionaryCustomMapper.selectDicByType(type);
    }
 
    @Override
    public List<AppVersion> findAppVersion() {
        return appVersionMapper.selectList(null);
    }
 
    @Override
    public void addDataDic(String type, String code, Object value, String description) {
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(type, code);
 
        String data = JSONObject.toJSONString(value);
        if (dic != null) {
            dic.setValue(data);
            dataDictionaryCustomMapper.updateById(dic);
            return;
        }
 
        dic = new DataDictionaryCustom();
        dic.setCode(code);
        dic.setType(type);
        dic.setValue(data);
        dic.setDescription(description);
        dataDictionaryCustomMapper.insert(dic);
    }
 
    @Override
    public void updateDataDic(String type, String code, String value) {
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(type, code);
        if (dic != null) {
            dic.setValue(value);
            dataDictionaryCustomMapper.updateById(dic);
        }
    }
 
    /**
     * 支付金额后的操作
     *      减少积分,增加流水
     *      购买套餐后,升级会员等级
     *      增加支付金额流水
     *      增加补贴额度
     *      增加贡献值,增加流水
     *      产生一条业绩记录
     * @param orderId
     */
    @Override
    public void changeWallet(Long orderId, int payType) {
        MallOrderInfo orderInfo = this.mallOrderInfoMapper.selectById(orderId);
        Long memberId = orderInfo.getMemberId();
        boolean hasTc = false;
        //总贡献值
        BigDecimal starSum = BigDecimal.ZERO;
        /**
         * 总补贴额度
         * 所有合伙人补贴至消费礼包的三倍额度需要复购一次。
         */
        BigDecimal scoreSum = BigDecimal.ZERO;
        BigDecimal achieveSum = BigDecimal.ZERO;
        List<MallOrderItem> orderItems = this.mallOrderInfoMapper.getMallOrderItemByOrderId(orderInfo.getId());
        if (CollUtil.isNotEmpty(orderItems)) {
            for (MallOrderItem orderItem : orderItems) {
                MallGoods mallGoods = mallGoodsMapper.selectById(orderItem.getGoodsId());
                /**
                 * 购买套餐赠送贡献值
                 * 补贴3倍额度
                 */
                if (mallGoods.getIsNormal() == 2) {
                    hasTc = true;
                    BigDecimal star = (mallGoods.getStar()==null ? BigDecimal.ZERO : mallGoods.getStar())
                            .multiply(new BigDecimal(orderItem.getCnt()));
                    starSum = starSum.add(star);
//                    scoreSum = scoreSum.add(
//                            new BigDecimal(mallGoods.getPresentPrice())
//                                    .multiply(new BigDecimal(3))
//                                    .multiply(new BigDecimal(orderItem.getCnt()))
//                                    .abs()
//                                    .setScale(2,BigDecimal.ROUND_DOWN));
                    String goodsName = mallGoods.getGoodsName();
                    /**
                     * 1-2档投资级别,2.5倍
                     * 3-4档投资级别,2.8倍
                     * 5-6档投资级别,3倍
                     * 目前紧急修改成按照商品名称
                     */
                    BigDecimal basePercent = new BigDecimal("3");
                    if(goodsName.equals("礼包一")){
                        basePercent = new BigDecimal("1.5");
                    }
                    if(goodsName.equals("礼包二")){
                        basePercent = new BigDecimal("2.5");
                    }
                    if(goodsName.equals("礼包三") || goodsName.equals("礼包四")){
                        basePercent = new BigDecimal("2.8");
                    }
                    if(goodsName.equals("礼包五") || goodsName.equals("礼包六")){
                        basePercent = new BigDecimal("3");
                    }
                    scoreSum = scoreSum.add(
                            new BigDecimal(mallGoods.getPresentPrice())
                                    .multiply(new BigDecimal(orderItem.getCnt()))
                                    .multiply(basePercent)
                                    .abs()
                                    .setScale(2,BigDecimal.ROUND_DOWN));
                    achieveSum = achieveSum.add(
                            new BigDecimal(mallGoods.getPresentPrice())
                                    .multiply(new BigDecimal(orderItem.getCnt()))
                                    .abs()
                                    .setScale(2,BigDecimal.ROUND_DOWN));
                }
            }
            /**
             * 增加贡献值,增加流水
             */
            if (starSum.compareTo(BigDecimal.ZERO) > 0) {
                //系统设置的个人贡献点最大值
                DataDictionaryCustom starGetDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                        DataDictionaryEnum.STAR_GET.getType(),
                        DataDictionaryEnum.STAR_GET.getCode());
                if(ObjectUtil.isNotEmpty(starGetDic)){
                    String starGetStr = StrUtil.isEmpty(starGetDic.getValue()) ? "0" : starGetDic.getValue();
                    BigDecimal starGet = new BigDecimal(starGetStr);
 
                    MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
                    BigDecimal star = new BigDecimal(mallMemberWallet.getStar());
                    if(starGet.compareTo(star.add(starSum)) < 0){
                        if(starGet.compareTo(BigDecimal.ZERO) == 0){
                            mallMemberWalletMapper.addStarByMemberId(starSum.intValue(), memberId);
                            mallMoneyFlowService.addMoneyFlow(
                                    memberId,
                                    starSum,
                                    MoneyFlowTypeEnum.STAR.getValue(),
                                    orderInfo.getOrderNo(),
                                    FlowTypeEnum.STAR.getValue(),
                                    2);
                        }
                        if(starGet.compareTo(star) > 0){
                            BigDecimal subtract = starGet.subtract(star);
                            if(subtract.compareTo(starSum) < 0){
                                starSum = subtract;
                            }
                            mallMemberWalletMapper.addStarByMemberId(starSum.intValue(), memberId);
                            mallMoneyFlowService.addMoneyFlow(
                                    memberId,
                                    starSum,
                                    MoneyFlowTypeEnum.STAR.getValue(),
                                    orderInfo.getOrderNo(),
                                    FlowTypeEnum.STAR.getValue(),
                                    2);
                        }
                    }else{
                        mallMemberWalletMapper.addStarByMemberId(starSum.intValue(), memberId);
                        mallMoneyFlowService.addMoneyFlow(
                                memberId,
                                starSum,
                                MoneyFlowTypeEnum.STAR.getValue(),
                                orderInfo.getOrderNo(),
                                FlowTypeEnum.STAR.getValue(),
                                2);
                    }
                }
            }
            /**
             * 增加补贴额度
             */
            if (scoreSum.compareTo(BigDecimal.ZERO) > 0) {
//                MallMember mallMember = memberMapper.selectById(memberId);
//                if(MemberLevelEnum.SECOND_LEVEL.getLevelCode(mallMember.getLevel()) >= 5){
//                    scoreSum = scoreSum.multiply(new BigDecimal(3));
//                }
//                if(MemberLevelEnum.SECOND_LEVEL.getLevelCode(mallMember.getLevel()) >= 3
//                &&MemberLevelEnum.SECOND_LEVEL.getLevelCode(mallMember.getLevel()) < 5){
//                    scoreSum = scoreSum.multiply(new BigDecimal(2.8));
//                }
//                if(MemberLevelEnum.SECOND_LEVEL.getLevelCode(mallMember.getLevel()) < 3){
//                    scoreSum = scoreSum.multiply(new BigDecimal(2.5));
//                }
                memberWalletService.add(scoreSum, memberId, "totalScore");
                mallMoneyFlowService.addMoneyFlow(
                        memberId,
                        scoreSum,
                        MoneyFlowTypeEnum.TOTAL_SCORE.getValue(),
                        orderInfo.getOrderNo(),
                        "",
                        scoreSum.toString(),
                        memberId,
                        1,
                        FlowTypeEnum.TOTAL_SCORE.getValue(),
                        2);
            }
        }
 
        /**
         * 购买套餐后,升级会员等级
         */
        if (hasTc) {
            MallMember mallMember = memberMapper.selectById(memberId);
            if (AgentLevelEnum.ZERO_LEVEL.name().equals(mallMember.getLevel())) {
                mallMember.setLevel(AgentLevelEnum.SECOND_LEVEL.name());
                memberMapper.updateById(mallMember);
            }
        }
        /**
         * 增加支付金额流水
         */
        mallMoneyFlowService.addMoneyFlow(
                memberId,
                orderInfo.getAmount().subtract(orderInfo.getScoreAmount()).negate(),
                MoneyFlowTypeEnum.PAY.getValue(),
                orderInfo.getOrderNo(),
                payType);
        /**
         * 减少积分,增加流水
         */
        if (orderInfo.getScoreCnt().compareTo(BigDecimal.ZERO) > 0) {
            int reduce = memberWalletService.reduce(orderInfo.getScoreCnt(), memberId, "prizeScore");
            if (reduce == 2) {
                throw new FebsException("积分不足");
            }
            mallMoneyFlowService.addMoneyFlow(
                    memberId,
                    orderInfo.getScoreCnt().negate(),
                    MoneyFlowTypeEnum.PAY.getValue(),
                    orderInfo.getOrderNo(),
                    FlowTypeEnum.PRIZE_SCORE.getValue());
        }
        /**
         * 产生一条业绩记录
         */
        if(achieveSum.compareTo(BigDecimal.ZERO) > 0){
            MallAchieveRecord mallAchieveRecord = new MallAchieveRecord();
            mallAchieveRecord.setMemberId(memberId);
            mallAchieveRecord.setAchieveTime(new Date());
            mallAchieveRecord.setAmount(achieveSum.subtract(orderInfo.getScoreAmount()));
            mallAchieveRecord.setOrderId(orderId);
            mallAchieveRecord.setPayTime(orderInfo.getPayTime());
            mallAchieveRecordMapper.insert(mallAchieveRecord);
        }
    }
 
    @Override
    public List<DataDictionaryCustom> findMemberDicByType() {
        List<DataDictionaryCustom> dataDictionaryCustoms = new ArrayList<>();
        List<String> levelType = MemberLevelNewEnum.ZERO_LEVEL.getLevelType();
        if(CollUtil.isNotEmpty(levelType)){
            for(String level : levelType){
                DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                        level, level
                );
                dataDictionaryCustoms.add(dataDictionaryCustom);
            }
        }
        return dataDictionaryCustoms;
    }
}