Helius
2021-08-05 c1b462af63ae1d293bf967d3c3da07d9348f8e76
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package com.xzx.gc.user.service;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.common.utils.RedisUtil;
import com.xzx.gc.common.utils.StringUtils;
import com.xzx.gc.entity.*;
import com.xzx.gc.user.dto.*;
import com.xzx.gc.user.mapper.*;
import com.xzx.gc.user.vo.HeadInfoVo;
import com.xzx.gc.user.vo.HeadProfitListVo;
import com.xzx.gc.user.vo.HeadTeamVo;
import com.xzx.gc.user.mapper.AccountMapper;
import com.xzx.gc.user.mapper.SysMessageMapper;
import com.xzx.gc.user.mapper.UserHeadRelateMapper;
import com.xzx.gc.user.mapper.UserShareInfoMapper;
import com.xzx.gc.user.vo.*;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xzx.gc.model.JsonResult;
import com.xzx.gc.user.mapper.RedPaperRuleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
@Transactional
public class DistribService {
 
    @Resource
    private RedPaperRuleMapper redPaperRuleMapper;
    @Autowired
    private UserHeadRelateMapper userHeadRelateMapper;
    @Autowired
    private UserHeadDetailsMapper userHeadDetailsMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private UserShareInfoMapper userShareInfoMapper;
    @Autowired
    private SysMessageMapper sysMessageMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ScoreDetailsMapper scoreDetailsMapper;
 
    public ViewSettingVo viewSetting(String ruleStr) {
        ViewSettingVo viewSettingVo = new ViewSettingVo();
        RedPaperRule redPaperRule = redPaperRuleMapper.selectByRuleName(ruleStr);
        if(ObjectUtil.isEmpty(redPaperRule)){
            viewSettingVo.setFristReward(BigDecimal.ZERO);
            viewSettingVo.setOrderNumFrist(0);
            viewSettingVo.setRegularPoint(0);
            viewSettingVo.setRegularMoneyPercent(BigDecimal.ZERO);
//            viewSettingVo.setFinishOrderNum(0);
            viewSettingVo.setPointReward(0);
            viewSettingVo.setStatus((short) 0);
        }else{
            String shareRatio = redPaperRule.getShareRatio();
            if(StrUtil.isNotEmpty(shareRatio)){
                List<String> shareRatios = StrUtil.split(shareRatio,',',false,false);
                if(ObjectUtil.isNotEmpty(shareRatios)){
                    viewSettingVo.setId(redPaperRule.getId());
                    viewSettingVo.setFristReward(StrUtil.isEmpty(shareRatios.get(0))?BigDecimal.ZERO:new BigDecimal(shareRatios.get(0)));
                    viewSettingVo.setOrderNumFrist(StrUtil.isEmpty(shareRatios.get(1))?0:Integer.parseInt(shareRatios.get(1)));
                    viewSettingVo.setRegularPoint(StrUtil.isEmpty(shareRatios.get(2))?0:Integer.parseInt(shareRatios.get(2)));
                    viewSettingVo.setRegularMoneyPercent(StrUtil.isEmpty(shareRatios.get(3))?BigDecimal.ZERO:new BigDecimal(shareRatios.get(3)));
//                    viewSettingVo.setFinishOrderNum(StrUtil.isEmpty(shareRatios.get(4))?0:Integer.parseInt(shareRatios.get(4)));
                    viewSettingVo.setPointReward(StrUtil.isEmpty(shareRatios.get(4))?0:Integer.parseInt(shareRatios.get(4)));
                    viewSettingVo.setStatus(redPaperRule.getStatus());
                }
            }else{
                viewSettingVo.setId(redPaperRule.getId());
                viewSettingVo.setFristReward(BigDecimal.ZERO);
                viewSettingVo.setOrderNumFrist(0);
                viewSettingVo.setRegularPoint(0);
                viewSettingVo.setRegularMoneyPercent(BigDecimal.ZERO);
//                viewSettingVo.setFinishOrderNum(0);
                viewSettingVo.setPointReward(0);
                viewSettingVo.setStatus((short) 0);
            }
        }
        return viewSettingVo;
    }
 
 
    public void join(String headId, String userId) {
        log.info("#加入团队:{}, {}#", headId, userId);
        AccountInfo headInfo = accountMapper.selectOneByUserId(headId);
        if (AccountInfo.IS_HEAD_N.equals(headInfo.getIsHead())) {
            throw new RestException(-3, "不是团长");
        }
 
        if (AccountInfo.IS_PROHIBIT_Y.equals(headInfo.getIsProhibit())) {
            throw new RestException(-3, "该用户被冻结");
        }
 
        UserHeadRelate existHeadRelate = userHeadRelateMapper.selectMemberByUserId(userId);
        if (existHeadRelate != null) {
            throw new RestException(-3, "已加入其他团队");
        }
 
        AccountInfo accountInfo = accountMapper.selectOneByUserId(userId);
        if (AccountInfo.IS_HEAD_Y.equals(accountInfo.getIsHead())) {
            throw new RestException(-3, "团长不能加入团队");
        }
 
        UserShareInfo userShareInfo = userShareInfoMapper.selectByRegistPhone(accountInfo.getAccountName());
        if (userShareInfo != null) {
            throw new RestException(-3, "您已与推广员绑定, 无法加入");
        }
 
        UserHeadRelate userHeadRelate = new UserHeadRelate();
        userHeadRelate.setHeadUserId(headId);
        userHeadRelate.setUserId(userId);
        userHeadRelate.setScore(BigDecimal.ZERO);
        userHeadRelate.setCreatedTime(new Date());
        userHeadRelate.setAmount(BigDecimal.ZERO);
        userHeadRelateMapper.insert(userHeadRelate);
 
        SysMessage sysMessage = new SysMessage();
        sysMessage.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        sysMessage.setCreateUserId(headId);
        sysMessage.setUserId(headId);
        sysMessage.setMobilePhone(accountInfo.getAccountName());
        sysMessage.setMessageType("4");
        sysMessage.setMessageSubTypeName("团队消息");
        sysMessage.setMessage("手机号:" + accountInfo.getAccountName().replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2") + "的用户, 成功加入您的团队");
        sysMessage.setFlag("2");
        sysMessageMapper.insert(sysMessage);
    }
 
    public HeadInfoVo findHeadInfo(String userId) {
        AccountInfo accountInfo = accountMapper.selectOneByUserId(userId);
 
        HeadInfoVo headInfoVo = userHeadRelateMapper.selectHeadStatisticsData(userId);
        if (headInfoVo == null) {
            headInfoVo = new HeadInfoVo();
        }
 
        headInfoVo.setIsHead(accountInfo.getIsHead());
        return headInfoVo;
    }
 
    public List<HeadProfitListVo> findHeadProfitListInPage(HeadProfitLitDto profitLitDto) {
        PageHelper.startPage(profitLitDto.getPage(), profitLitDto.getLimit());
        return userHeadDetailsMapper.selectHeadProfitListInPage(profitLitDto.getUserId());
    }
 
    public List<HeadTeamVo> findHeadTeamListInPage(HeadTeamDto teamDto) {
        PageHelper.startPage(teamDto.getPage(), teamDto.getLimit());
        return userHeadRelateMapper.selectHeadTeamListInPage(teamDto.getUserId());
    }
 
    public void addSetting(UpdateSettingDto model) {
        RedPaperRule redPaperRule = new RedPaperRule();
        StringBuffer stringBuffer = new StringBuffer();
        BigDecimal fristReward = ObjectUtil.isEmpty(model.getFristReward()) ? BigDecimal.ZERO : model.getFristReward();
        stringBuffer.append(fristReward+",");
        int orderNumFrist = ObjectUtil.isEmpty(model.getOrderNumFrist()) ? 0 : model.getOrderNumFrist();
        stringBuffer.append(orderNumFrist+",");
        int regularPoint = ObjectUtil.isEmpty(model.getRegularPoint()) ? 0 : model.getRegularPoint();
        stringBuffer.append(regularPoint+",");
        BigDecimal regularMoneyPercent = ObjectUtil.isEmpty(model.getRegularMoneyPercent()) ? BigDecimal.ZERO : model.getRegularMoneyPercent();
        stringBuffer.append(regularMoneyPercent+",");
//        int finishOrderNum = ObjectUtil.isEmpty(model.getFinishOrderNum()) ? 0 : model.getFinishOrderNum();
//        stringBuffer.append(finishOrderNum+",");
        int pointReward = ObjectUtil.isEmpty(model.getPointReward()) ? 0 : model.getPointReward();
        stringBuffer.append(pointReward);
        redPaperRule.setStatus((short) 0);
        redPaperRule.setRuleName("分销规则");
        redPaperRule.setSharingProfitType("frist_reward,order_num_frist,regular_point,regular_money_percent,point_reward");
        redPaperRule.setShareRatio(stringBuffer.toString());
        redPaperRuleMapper.insert(redPaperRule);
    }
 
    public void updateSetting(UpdateSettingDto model) {
        RedPaperRule redPaperRule = redPaperRuleMapper.selectByPrimaryKey(model.getId());
        if(ObjectUtil.isNotEmpty(redPaperRule)){
            StringBuffer stringBuffer = new StringBuffer();
            BigDecimal fristReward = ObjectUtil.isEmpty(model.getFristReward()) ? BigDecimal.ZERO : model.getFristReward();
            stringBuffer.append(fristReward+",");
            int orderNumFrist = ObjectUtil.isEmpty(model.getOrderNumFrist()) ? 0 : model.getOrderNumFrist();
            stringBuffer.append(orderNumFrist+",");
            int regularPoint = ObjectUtil.isEmpty(model.getRegularPoint()) ? 0 : model.getRegularPoint();
            stringBuffer.append(regularPoint+",");
            BigDecimal regularMoneyPercent = ObjectUtil.isEmpty(model.getRegularMoneyPercent()) ? BigDecimal.ZERO : model.getRegularMoneyPercent();
            stringBuffer.append(regularMoneyPercent+",");
//            int finishOrderNum = ObjectUtil.isEmpty(model.getFinishOrderNum()) ? 0 : model.getFinishOrderNum();
//            stringBuffer.append(finishOrderNum+",");
            int pointReward = ObjectUtil.isEmpty(model.getPointReward()) ? 0 : model.getPointReward();
            stringBuffer.append(pointReward);
            redPaperRule.setStatus(model.getStatus());
            redPaperRule.setSharingProfitType("first_reward,order_num_first,regular_point,regular_money_percent,point_reward");
            redPaperRule.setShareRatio(stringBuffer.toString());
            redPaperRuleMapper.updateByPrimaryKey(redPaperRule);
        }
    }
 
    public Map<String, Object> distribList(DistribListDto distribListDto) {
        PageHelper.startPage(distribListDto.getPage(), distribListDto.getLimit());
        if(StrUtil.isNotEmpty(distribListDto.getQueryCol())){
            String decode = StringUtils.encode(distribListDto.getQueryCol());
            distribListDto.setName(decode);
            distribListDto.setPhone(distribListDto.getQueryCol());
        }
        List<DistribListVo> distribListVos = accountMapper.selectDsitribListByQueryCol(distribListDto);
        if(CollUtil.isNotEmpty(distribListVos)){
            for(DistribListVo distribListVo : distribListVos){
                String nickname = distribListVo.getNickname();
                String decode = StringUtils.decode(nickname);
                distribListVo.setNickname(decode);
            }
        }
        PageInfo<DistribListVo> pageInfo = new PageInfo<>(distribListVos);
        Map<String, Object> data = new HashMap<>();
        int count = Convert.toInt(pageInfo.getTotal());
        data.put("data", distribListVos);
        data.put("count", count);
        data.put("code", 0);
        return data;
    }
 
    public void adddistrib(String id) {
        AccountInfo accountInfo = accountMapper.selectByPrimaryKey(id);
        accountInfo.setIsHead(1);
        accountMapper.updateByPrimaryKey(accountInfo);
    }
 
    public Map<String, Object> distribInfoList(DistribInfoListDto distribInfoListDto) {
        PageHelper.startPage(distribInfoListDto.getPage(), distribInfoListDto.getLimit());
        if(StrUtil.isNotEmpty(distribInfoListDto.getName())){
            String decode = StringUtils.encode(distribInfoListDto.getName());
            distribInfoListDto.setName(decode);
        }
        List<DistribInfoListVo> distribInfoListVos = accountMapper.selectDsitribInfoListByQueryCol(distribInfoListDto);
        if(CollUtil.isNotEmpty(distribInfoListVos)){
            for(DistribInfoListVo distribInfoListVo : distribInfoListVos){
                String nickname = distribInfoListVo.getNickname();
                String decode = StringUtils.decode(nickname);
                distribInfoListVo.setNickname(decode);
            }
        }
        PageInfo<DistribInfoListVo> pageInfo = new PageInfo<>(distribInfoListVos);
        Map<String, Object> data = new HashMap<>();
        int count = Convert.toInt(pageInfo.getTotal());
        data.put("data", distribInfoListVos);
        data.put("count", count);
        data.put("code", 0);
        return data;
 
    }
 
    public ViewDistribInfoVo viewDistribInfo(String id) {
        //获取团长基本信息
        ViewDistribInfoVo viewDistribInfoVo = accountMapper.selectDistribByAccountId(id);
        if(StrUtil.isNotEmpty(viewDistribInfoVo.getNickname())){
            String decode = StringUtils.decode(viewDistribInfoVo.getNickname());
            viewDistribInfoVo.setNickname(decode);
        }
        return accountMapper.selectDistribByAccountId(id);
    }
 
    public Map<String, Object> userHeadInfoList(UserHeadInfoListDto userHeadInfoListDto) {
        String id = userHeadInfoListDto.getId();
        AccountInfo accountInfo = accountMapper.selectByPrimaryKey(id);
        String userId = accountInfo.getUserId();
        PageHelper.startPage(userHeadInfoListDto.getPage(), userHeadInfoListDto.getLimit());
        List<UserHeadInfoVo> userHeadInfoVos = accountMapper.selectUserHeadInfoByAccountId(userId);
        if(CollUtil.isNotEmpty(userHeadInfoVos)){
            for(UserHeadInfoVo userHeadInfoVo : userHeadInfoVos){
                String nickname = userHeadInfoVo.getNickname();
                String decode = StringUtils.decode(nickname);
                userHeadInfoVo.setNickname(decode);
            }
        }
        PageInfo<UserHeadInfoVo> pageInfo = new PageInfo<>(userHeadInfoVos);
        Map<String, Object> data = new HashMap<>();
        int count = Convert.toInt(pageInfo.getTotal());
        data.put("data", userHeadInfoVos);
        data.put("count", count);
        data.put("code", 0);
        return data;
    }
 
    public Map<String, Object> orderItemsList(UserHeadInfoListDto userHeadInfoListDto) {
        String id = userHeadInfoListDto.getId();
        AccountInfo accountInfo = accountMapper.selectByPrimaryKey(id);
        String userId = accountInfo.getUserId();
        PageHelper.startPage(userHeadInfoListDto.getPage(), userHeadInfoListDto.getLimit());
        List<OrderItemsVo> orderItemsVos = accountMapper.selectOrderItemsByUserId(userId);
        if(CollUtil.isNotEmpty(orderItemsVos)){
            for(OrderItemsVo orderItemsVo : orderItemsVos){
                if(ObjectUtil.isNotEmpty(orderItemsVo)){
                    String nickname = orderItemsVo.getNickname();
                    String decode = StringUtils.decode(nickname);
                    orderItemsVo.setNickname(decode);
                    Long orderId = orderItemsVo.getOrderId();
                    List<JhyOrderItems> jhyOrderItems = accountMapper.selectOrderById(orderId);
                    if(CollUtil.isNotEmpty(jhyOrderItems)){
                        orderItemsVo.setOrderItems(jhyOrderItems);
                    }
                }
            }
        }
        PageInfo<OrderItemsVo> pageInfo = new PageInfo<>(orderItemsVos);
        Map<String, Object> data = new HashMap<>();
        int count = Convert.toInt(pageInfo.getTotal());
        data.put("data", orderItemsVos);
        data.put("count", count);
        data.put("code", 0);
        return data;
    }
 
    public void prohibitDistrib(ProhibitDistribDto model) {
        Integer status = model.getStatus();
        String id = model.getId();
        AccountInfo accountInfo = accountMapper.selectByPrimaryKey(id);
        accountInfo.setIsProhibit(status.toString());
        accountMapper.updateByPrimaryKey(accountInfo);
    }
 
 
    public Map<String, Object> distribDataList(DistribDataListDto distribDataListDto) {
        if(StrUtil.isNotEmpty(distribDataListDto.getQueryCol())){
            String decode = StringUtils.encode(distribDataListDto.getQueryCol());
            distribDataListDto.setName(decode);
            distribDataListDto.setPhone(distribDataListDto.getQueryCol());
        }
        PageHelper.startPage(distribDataListDto.getPage(), distribDataListDto.getLimit());
        List<DistribDataListVo> distribDataListVos = accountMapper.distribDataList(distribDataListDto);
        if(CollUtil.isNotEmpty(distribDataListVos)){
            for(DistribDataListVo distribDataListVo : distribDataListVos){
                if(ObjectUtil.isNotEmpty(distribDataListVo)){
                    String nickname = distribDataListVo.getNickname();
                    String decode = StringUtils.decode(nickname);
                    distribDataListVo.setNickname(decode);
                    String id = distribDataListVo.getId();
                    String kg = accountMapper.selectOrderByHeadUserId(id,distribDataListDto);
                    distribDataListVo.setKg(kg);
                    String jdsl = accountMapper.selectJDSLByHeadUserId(id,distribDataListDto);
                    distribDataListVo.setJdsl(jdsl);
                }
            }
        }
        PageInfo<DistribDataListVo> pageInfo = new PageInfo<>(distribDataListVos);
        Map<String, Object> data = new HashMap<>();
        int count = Convert.toInt(pageInfo.getTotal());
        data.put("data", distribDataListVos);
        data.put("count", count);
        data.put("code", 0);
        return data;
    }
 
    public DistribDataInfoVo distribDataInfo(DistribDataInfoDto distribDataInfoDto) {
        return accountMapper.selectDistribDataInfoVo();
    }
 
    public Map<String, Object> distribDataExport(ExportDistribDataListDto distribDataListDto) {
        if(StrUtil.isNotEmpty(distribDataListDto.getQueryCol())){
            String decode = StringUtils.encode(distribDataListDto.getQueryCol());
            distribDataListDto.setName(decode);
            distribDataListDto.setPhone(distribDataListDto.getQueryCol());
        }
        List<DistribDataListVo> distribDataListVos = accountMapper.distribDataExport(distribDataListDto);
        if(CollUtil.isNotEmpty(distribDataListVos)){
            for(DistribDataListVo distribDataListVo : distribDataListVos){
                if(ObjectUtil.isNotEmpty(distribDataListVo)){
                    String nickname = distribDataListVo.getNickname();
                    String decode = StringUtils.decode(nickname);
                    distribDataListVo.setNickname(decode);
                    String id = distribDataListVo.getId();
                    String kg = accountMapper.selectOrderExportByHeadUserId(id,distribDataListDto);
                    distribDataListVo.setKg(kg);
                    String jdsl = accountMapper.selectJDSLExportByHeadUserId(id,distribDataListDto);
                    distribDataListVo.setJdsl(jdsl);
                }
            }
        }
        PageInfo<DistribDataListVo> pageInfo = new PageInfo<>(distribDataListVos);
        Map<String, Object> data = new HashMap<>();
        int count = Convert.toInt(pageInfo.getTotal());
        data.put("data", distribDataListVos);
        data.put("count", count);
        data.put("code", 0);
        return data;
    }
 
    public ViewScoreSettingVo viewScoreSetting(String ruleStr) {
        ViewScoreSettingVo viewScoreSettingVo = new ViewScoreSettingVo();
        RedPaperRule redPaperRule = redPaperRuleMapper.selectByRuleName(ruleStr);
        if(ObjectUtil.isEmpty(redPaperRule)){
            viewScoreSettingVo.setRegisterScore(0);
            viewScoreSettingVo.setShareScore(0);
            viewScoreSettingVo.setShareScoreTime(0);
            viewScoreSettingVo.setFirstScore(0);
            viewScoreSettingVo.setStatus((short) 0);
        }else{
            String shareRatio = redPaperRule.getShareRatio();
            if(StrUtil.isNotEmpty(shareRatio)){
                List<String> shareRatios = StrUtil.split(shareRatio,',',false,false);
                if(ObjectUtil.isNotEmpty(shareRatios)){
                    viewScoreSettingVo.setId(redPaperRule.getId());
                    viewScoreSettingVo.setRegisterScore(StrUtil.isEmpty(shareRatios.get(0))?0:Integer.parseInt(shareRatios.get(0)));
                    viewScoreSettingVo.setShareScore(StrUtil.isEmpty(shareRatios.get(1))?0:Integer.parseInt(shareRatios.get(1)));
                    viewScoreSettingVo.setShareScoreTime(StrUtil.isEmpty(shareRatios.get(2))?0:Integer.parseInt(shareRatios.get(2)));
                    viewScoreSettingVo.setFirstScore(StrUtil.isEmpty(shareRatios.get(3))?0:Integer.parseInt(shareRatios.get(3)));
                    viewScoreSettingVo.setStatus(redPaperRule.getStatus());
                }
            }else{
                viewScoreSettingVo.setId(redPaperRule.getId());
                viewScoreSettingVo.setRegisterScore(0);
                viewScoreSettingVo.setShareScore(0);
                viewScoreSettingVo.setShareScoreTime(0);
                viewScoreSettingVo.setFirstScore(0);
                viewScoreSettingVo.setStatus(redPaperRule.getStatus());
            }
        }
        return viewScoreSettingVo;
    }
 
    public void addScoreSetting(UpdateScoreSetting model) {
        RedPaperRule redPaperRule = new RedPaperRule();
        StringBuffer stringBuffer = new StringBuffer();
        int registerScore = ObjectUtil.isEmpty(model.getRegisterScore()) ? 0 : model.getRegisterScore();
        stringBuffer.append(registerScore+",");
        int shareScore = ObjectUtil.isEmpty(model.getShareScore()) ? 0 : model.getShareScore();
        stringBuffer.append(shareScore+",");
        int shareScoreTime = ObjectUtil.isEmpty(model.getShareScoreTime()) ? 0 : model.getShareScoreTime();
        stringBuffer.append(shareScoreTime+",");
        int firstScore = ObjectUtil.isEmpty(model.getFirstScore()) ? 0 : model.getFirstScore();
        stringBuffer.append(firstScore);
        redPaperRule.setStatus((short) 0);
        redPaperRule.setRuleName("积分运行设置");
        redPaperRule.setRuleType("10");
        redPaperRule.setSharingProfitType("register_score,share_score,share_score_time,first_score");
        redPaperRule.setShareRatio(stringBuffer.toString());
        redPaperRuleMapper.insert(redPaperRule);
    }
 
    public void updateScoreSetting(UpdateScoreSetting model) {
        RedPaperRule redPaperRule = redPaperRuleMapper.selectByPrimaryKey(model.getId());
        if(ObjectUtil.isNotEmpty(redPaperRule)){
            StringBuffer stringBuffer = new StringBuffer();
            int registerScore = ObjectUtil.isEmpty(model.getRegisterScore()) ? 0 : model.getRegisterScore();
            stringBuffer.append(registerScore+",");
            int shareScore = ObjectUtil.isEmpty(model.getShareScore()) ? 0 : model.getShareScore();
            stringBuffer.append(shareScore+",");
            int shareScoreTime = ObjectUtil.isEmpty(model.getShareScoreTime()) ? 0 : model.getShareScoreTime();
            stringBuffer.append(shareScoreTime+",");
            int firstScore = ObjectUtil.isEmpty(model.getFirstScore()) ? 0 : model.getFirstScore();
            stringBuffer.append(firstScore);
            redPaperRule.setStatus(model.getStatus());
            redPaperRule.setSharingProfitType("register_score,share_score,share_score_time,first_score");
            redPaperRule.setShareRatio(stringBuffer.toString());
            redPaperRuleMapper.updateByPrimaryKey(redPaperRule);
        }
    }
 
    public void shareToMoments(String userId) {
        List<RedPaperRule> redPaperRules = redPaperRuleMapper.selectByRuleType("10");
        if (CollUtil.isEmpty(redPaperRules)) {
            return;
        }
 
        RedPaperRule rule = redPaperRules.get(0);
        String shareTimes = getRuleValue(rule, CommonEnum.朋友圈分享限次.getValue());
 
        String shareScore = getRuleValue(rule, CommonEnum.朋友圈分享奖励.getValue());
        String key = "moments:times:" + userId;
        Integer times = redisUtil.getAsInt("moments:times:" + userId);
        times = times == null ? 1 : times + 1;
 
        if (StrUtil.isNotBlank(shareTimes)) {
            if (times > Integer.parseInt(shareTimes)) {
                throw new RestException("达到24小时内最大分享次数");
            }
 
            if (StrUtil.isNotBlank(shareScore)) {
                return;
            }
 
            AccountInfo accountInfo = accountMapper.selectOneByUserId(userId);
            BigDecimal returnScore = new BigDecimal(shareScore);
            BigDecimal collectScore = StrUtil.isNotBlank(accountInfo.getCollectScore()) ? new BigDecimal(accountInfo.getCollectScore()) : BigDecimal.ZERO;
            accountInfo.setCollectScore(collectScore.add(returnScore).setScale(0, RoundingMode.DOWN).toString());
            accountMapper.updateByPrimaryKey(accountInfo);
 
            ScoreDetails scoreDetails = new ScoreDetails();
            scoreDetails.setCurrentScore(new BigDecimal(accountInfo.getCollectScore()));
            scoreDetails.setOriginalScore(collectScore);
            scoreDetails.setChangeScore(returnScore);
            scoreDetails.setCreatedTime(new Date());
            scoreDetails.setType(ScoreDetails.SCORE_TYPE_INVITE_FIRST_ORDER);
            scoreDetails.setUserId(accountInfo.getUserId());
            scoreDetailsMapper.insert(scoreDetails);
 
            SysMessage sysMessage = new SysMessage();
            sysMessage.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            sysMessage.setCreateUserId(accountInfo.getUserId());
            sysMessage.setUserId(accountInfo.getUserId());
            sysMessage.setMessageType("4");
            sysMessage.setMessageSubTypeName("朋友圈分享");
            sysMessage.setMessage("朋友圈分享, 积分:" + returnScore);
            sysMessage.setFlag("2");
            sysMessageMapper.insert(sysMessage);
 
            redisUtil.setex(key, times.toString(), 84400);
        }
    }
 
    private String getRuleValue(RedPaperRule redPaperRule, String key) {
        List<String> rules = StrUtil.split(redPaperRule.getSharingProfitType(), ',');
        List<String> values = StrUtil.split(redPaperRule.getShareRatio(), ',');
 
        int i = rules.indexOf(key);
        return values.get(i);
    }
}