admin
2019-11-15 2a93320d3adf8c72ea127df59d0bdd043917dd79
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package com.yeshi.fanli.service.impl.user.integral;
 
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.DateUtil;
 
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.google.gson.Gson;
import com.yeshi.fanli.dao.mybatis.integral.IntegralTaskRecordMapper;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.IntegralTaskMQMsg;
import com.yeshi.fanli.dto.msg.MsgOtherRewardIntegralDTO;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.integral.IntegralDetail;
import com.yeshi.fanli.entity.integral.IntegralTask;
import com.yeshi.fanli.entity.integral.IntegralTask.FrequencyEnum;
import com.yeshi.fanli.entity.integral.IntegralTaskClass;
import com.yeshi.fanli.entity.integral.IntegralTaskClass.UniqueKeyEnum;
import com.yeshi.fanli.entity.integral.IntegralTaskRecord;
import com.yeshi.fanli.exception.integral.IntegralTaskRecordException;
import com.yeshi.fanli.exception.user.UserInfoExtraException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.msg.UserOtherMsgNotificationService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.integral.IntegralDetailService;
import com.yeshi.fanli.service.inter.user.integral.IntegralTaskClassService;
import com.yeshi.fanli.service.inter.user.integral.IntegralTaskRecordService;
import com.yeshi.fanli.service.inter.user.integral.IntegralTaskService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
 
@Service
public class IntegralTaskRecordServiceImpl implements IntegralTaskRecordService {
 
    @Resource
    private IntegralTaskRecordMapper integralTaskRecordMapper;
 
    @Resource
    private UserInfoExtraService userInfoExtraService;
 
    @Resource
    private IntegralDetailService integralDetailService;
 
    @Resource
    private IntegralTaskService integralTaskService;
 
    @Lazy
    @Resource
    private IntegralTaskClassService integralTaskClassService;
 
    @Resource
    private UserOtherMsgNotificationService userOtherMsgNotificationService;
 
    @Resource
    private ConfigService configService;
 
    @Resource(name = "producer")
    private Producer producer;
 
    @Override
    public Integer getTotalGoldCoin(long uid, Long cid, Integer dateType) {
        return integralTaskRecordMapper.getTotalGoldCoin(uid, cid, dateType);
    }
 
    @Override
    public Integer countFinished(long uid, Long cid, Integer dateType) {
        return integralTaskRecordMapper.countFinished(uid, cid, dateType);
    }
 
    @Override
    public void finishedTask(long uid, long cid, long taskId, int goldCoin) {
        IntegralTaskRecord taskRecord = new IntegralTaskRecord();
        taskRecord.setUid(uid);
        taskRecord.setCid(cid);
        taskRecord.setTaskId(taskId);
        taskRecord.setGoldCoin(goldCoin);
        taskRecord.setState(IntegralTaskRecord.STATE_WAITING_RECIEVE);
        taskRecord.setCreateTime(new Date());
        taskRecord.setUpdateTime(new Date());
        integralTaskRecordMapper.insertSelective(taskRecord);
    }
 
    @Override
    public boolean isToDaySign(long uid, Long cid) {
        Integer countFinished = integralTaskRecordMapper.countFinished(uid, cid, 1);
        if (countFinished != null && countFinished > 0) {
            return true;
        }
        return false;
    }
 
    @Override
    public Integer getNowdaySignNum(long uid, Long cid) {
        int num = 1;
        try {
            // 签到时间倒序查询近7天数据
            List<IntegralTaskRecord> list = integralTaskRecordMapper.listDaySignRecord(uid, cid);
            if (list != null && list.size() > 0) {
                Date today = new Date();
                for (IntegralTaskRecord record : list) {
                    if (DateUtil.daysBetween2(record.getCreateTime(), today) == 0) {
                        continue;
                    }
 
                    if (DateUtil.daysBetween2(record.getCreateTime(), today) != 1) {
                        break; // 天数未连续
                    }
                    today = record.getCreateTime();
                    num++;
                }
            }
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
 
        if (num > 7) {
            num = 7;
        }
        return num;
    }
 
    @Override
    public List<IntegralTaskRecord> getSignDaysRecord(long uid, Long cid) {
        List<IntegralTaskRecord> listRecord = new ArrayList<IntegralTaskRecord>();
        try {
            // 签到时间正序序查询近7天数据
            List<IntegralTaskRecord> list = integralTaskRecordMapper.listDateSignRecord(uid, cid);
            if (list != null && list.size() > 0) {
                Date date = new Date();
                for (IntegralTaskRecord record : list) {
                    Date createTime = record.getCreateTime();
                    if (DateUtil.daysBetween2(createTime, date) > 1) {
                        break; // 天数未连续
                    }
                    date = createTime;
                    listRecord.add(record);
                }
            }
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
        return listRecord;
    }
 
    @Override
    public List<IntegralTaskRecord> getSign7DaysRecord(long uid, Long cid) {
        List<IntegralTaskRecord> listRecord = new ArrayList<IntegralTaskRecord>();
        try {
            // 签到时间正序序查询近7天数据
            List<IntegralTaskRecord> list = integralTaskRecordMapper.listDaySignRecord(uid, cid);
            if (list != null && list.size() > 0) {
                Date date = new Date();
                for (IntegralTaskRecord record : list) {
                    Date createTime = record.getCreateTime();
                    if (DateUtil.daysBetween2(createTime, date) > 1) {
                        break; // 天数未连续
                    }
                    date = createTime;
                    listRecord.add(record);
 
                    if (listRecord.size() >= 7)
                        break;
                }
            }
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
        return listRecord;
    }
 
    @Override
    public List<IntegralTaskRecord> listNotReceived(int count, long uid) {
        return integralTaskRecordMapper.listNotReceived(count, uid);
    }
 
    @Override
    public List<IntegralTaskRecord> listNotReceivedExcludeId(int count, long uid, Set<Long> idList) {
        if (idList != null && idList.size() == 0) {
            idList = null;
        }
        return integralTaskRecordMapper.listNotReceivedExcludeId(count, uid, idList);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer receiveGoldCoinALL(Long uid) throws IntegralTaskRecordException {
        UserInfoExtra userInfoExtra = userInfoExtraService.getByUidForUpdate(uid);
        if (userInfoExtra == null)
            throw new IntegralTaskRecordException(1, "用户信息不全");
 
        List<IntegralTaskRecord> list = integralTaskRecordMapper.listNotReceived(Integer.MAX_VALUE, uid);
        if (list == null || list.size() == 0)
            throw new IntegralTaskRecordException(1, "暂无可领取");
 
        Integer totalGoldCoin = 0;
        for (IntegralTaskRecord record : list) {
            if (addDetail(uid, record))
                totalGoldCoin += record.getGoldCoin();
        }
 
        // 更新金币
        userInfoExtraService.updateGoldCoin(userInfoExtra.getId(), totalGoldCoin);
 
        return (userInfoExtra.getGoldCoin() == null ? 0 : userInfoExtra.getGoldCoin()) + totalGoldCoin;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer receiveGoldCoin(Long uid, Set<Long> idList) throws IntegralTaskRecordException {
        if (idList == null || idList.size() == 0) {
            throw new IntegralTaskRecordException(1, "id为空");
        }
 
        UserInfoExtra userInfoExtra = userInfoExtraService.getByUidForUpdate(uid);
        if (userInfoExtra == null) {
            throw new IntegralTaskRecordException(1, "用户信息不全");
        }
 
        Integer totalGoldCoin = 0;
        for (Long id : idList) {
            IntegralTaskRecord record = integralTaskRecordMapper.selectByPrimaryKey(id);
            if (record == null)
                continue;
 
            // 已领取
            Integer state = record.getState();
            if (state != null && state == 1)
                continue;
 
            if (addDetail(uid, record))
                totalGoldCoin += record.getGoldCoin();
        }
 
        // 更新金币
        userInfoExtraService.updateGoldCoin(userInfoExtra.getId(), totalGoldCoin);
 
        return totalGoldCoin + (userInfoExtra.getGoldCoin() == null ? 0 : userInfoExtra.getGoldCoin());
    }
 
    @Transactional
    public boolean addDetail(Long uid, IntegralTaskRecord record) throws IntegralTaskRecordException {
        Long uid2 = record.getUid();
        if (uid2 == null || uid.longValue() != uid2.longValue())
            return false; // 用户id不符合
 
        if (record.getState() != null && record.getState().intValue() == 1) {
            return false; // 已领取
        }
 
        IntegralTaskRecord taskRecord = new IntegralTaskRecord();
        taskRecord.setId(record.getId());
        taskRecord.setState(1); // 已领取
        taskRecord.setUpdateTime(new Date());
        integralTaskRecordMapper.updateByPrimaryKeySelective(taskRecord);
 
        Long cid = record.getCid();
        if (cid == null)
            return false;
 
        IntegralTaskClass taskClass = integralTaskClassService.selectByPrimaryKey(cid);
        if (taskClass == null)
            return false;
 
        Long taskId = record.getTaskId();
        if (taskId == null)
            return false;
 
        IntegralTask integralTask = integralTaskService.selectByPrimaryKey(taskId);
        if (integralTask == null)
            return false;
 
        // 加入明细
        IntegralDetail detail = new IntegralDetail();
        if (UniqueKeyEnum.dailySign == taskClass.getUniqueKey()) {
            SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            Date recordTime = record.getCreateTime();
            List<IntegralTaskRecord> list = integralTaskRecordMapper.listSignRecordByDateTime(uid, cid,
                    format.format(recordTime));
            int num = 1;
            if (list != null && list.size() > 0) {
                try {
                    Date nextDate = null;
                    for (IntegralTaskRecord tntegralTaskRecord : list) {
                        if (nextDate == null) {
                            nextDate = tntegralTaskRecord.getCreateTime();
                            continue;
                        }
 
                        if (DateUtil.daysBetween2(tntegralTaskRecord.getCreateTime(), nextDate) != 1) {
                            break; // 天数未连续
                        }
                        nextDate = tntegralTaskRecord.getCreateTime();
                        num++;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            detail.setTitle(taskClass.getName() + "-第" + num + "天");
        } else {
            FrequencyEnum frequency = integralTask.getFrequency();
            if (frequency == FrequencyEnum.everyday) {
                int num = integralTaskRecordMapper.countByTaskIdTodayNum(uid, taskId,
                        TimeUtil.getWholeTime(record.getCreateTime().getTime()), record.getId());
                if (num <= 0)
                    num = 1;
                String title = taskClass.getName() + "-" + integralTask.getName() + "-第" + num + "次";
                detail.setTitle(title);
            } else if (frequency == FrequencyEnum.onlyOne) {
                String title = taskClass.getName() + "-" + integralTask.getName();
                detail.setTitle(title);
            }
        }
 
        detail.setUid(uid);
        detail.setMoney(record.getGoldCoin());
        detail.setCreateTime(record.getCreateTime());
        integralDetailService.insertSelective(detail);
 
        return true;
    }
 
    @Override
    public int countGetCountByTaskIdAndDay(Long taskId, Long uid, Date day) {
        Date minTime = null;
        Date maxTime = null;
        if (day == null) {
            minTime = new Date(TimeUtil.convertToTimeTemp("1970-01-01", "yyyy-MM-dd"));
            maxTime = new Date(TimeUtil.convertToTimeTemp("2100-01-01", "yyyy-MM-dd"));
        } else {
            minTime = new Date(
                    TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(day.getTime(), "yyyy-MM-dd"), "yyyy-MM-dd"));
            maxTime = new Date(minTime.getTime() + 1000 * 60 * 60 * 24L);
        }
        return integralTaskRecordMapper.countGetCountByTaskIdAndDay(taskId, uid, minTime, maxTime);
    }
 
    @Transactional
    @Override
    public IntegralTaskRecord addRecord(IntegralTaskRecord record) throws IntegralTaskRecordException {
        if (record == null)
            throw new IntegralTaskRecordException(1, "记录为空");
 
        if (record.getCid() == null || record.getTaskId() == null || record.getGoldCoin() == null
                || record.getState() == null || record.getUid() == null)
            throw new IntegralTaskRecordException(2, "数据不完成");
 
        if (record.getCreateTime() == null)
            record.setCreateTime(new Date());
 
        if (record.getUpdateTime() == null)
            record.setUpdateTime(new Date());
        integralTaskRecordMapper.insertSelective(record);
        long count = integralTaskRecordMapper.getTotalGoldCoinByUid(record.getUid());
        if (count >= 200) {// 大于200发送消息
            IntegralTaskMQMsg msg = new IntegralTaskMQMsg(record.getUid(), count, new Date());
            Message message = new Message(MQTopicName.TOPIC_USER.name(), UserTopicTagEnum.integralTaskFinish.name(),
                    new Gson().toJson(msg).getBytes());
            producer.send(message);
        }
 
        return record;
    }
 
    @Override
    public List<IntegralTaskRecord> listByCidAndUidAndCreateTime(Long cid, Long uid, Date minTime, Date maxTime,
            int page, int count) {
        return integralTaskRecordMapper.listByCidAndUidAndCreateTime(cid, uid, minTime, maxTime, (page - 1) * count,
                count);
    }
 
    @Override
    public long countByCidAndUidAndCreateTime(Long cid, Long uid, Date minTime, Date maxTime) {
        return integralTaskRecordMapper.countByCidAndUidAndCreateTime(cid, uid, minTime, maxTime);
    }
 
    @Override
    public List<IntegralTaskRecord> listByUidAndTaskId(Long uid, Long taskId, int page, int count) {
        return integralTaskRecordMapper.listByUidAndTaskId(uid, taskId, (page - 1) * count, count);
    }
 
    @Override
    public long getTotalGoldCoinByUid(Long uid) {
        return integralTaskRecordMapper.getTotalGoldCoinByUid(uid);
    }
 
    @Async()
    @Override
    public void firstRebateOrderRewardBoss(Long uid, Long originUid, String beizu) {
        if (uid == null || originUid == null)
            return;
 
        String reward = configService.get("first_order_reward_boss");
        if (StringUtil.isNullOrEmpty(reward))
            return;
        firstOrderReward(uid, originUid, Integer.parseInt(reward), "一", beizu);
    }
 
    @Async()
    @Override
    public void firstRebateOrderRewardBossSuper(Long uid, Long originUid, String beizu) {
        if (uid == null || originUid == null)
            return;
 
        String reward = configService.get("first_order_reward_boss_super");
        if (StringUtil.isNullOrEmpty(reward))
            return;
        firstOrderReward(uid, originUid, Integer.parseInt(reward), "二", beizu);
    }
 
    @Async()
    @Override
    public void firstShareOrderReward(Long uid, String beizu) {
        if (uid == null)
            return;
 
        String reward = configService.get("first_share_order_reward");
        if (StringUtil.isNullOrEmpty(reward))
            return;
        firstOrderReward(uid, null, Integer.parseInt(reward), null, beizu);
    }
 
    @Async()
    @Override
    public void firstSharerOrderRewardBoss(Long uid, Long originUid, String beizu) {
        if (uid == null || originUid == null)
            return;
 
        String reward = configService.get("first_share_order_reward_boss");
        if (StringUtil.isNullOrEmpty(reward))
            return;
        firstOrderReward(uid, originUid, Integer.parseInt(reward), "一", beizu);
    }
 
    /**
     * 队员首单奖励金币
     * 
     * @param uid
     * @param addGoldCoin
     * @param level
     *            所属哪级队员
     * @param beizu
     */
    private void firstOrderReward(Long uid, Long originUid, Integer addGoldCoin, String level, String beizu) {
        String uniqueKey = "firstOrder-" + uid.toString();
        if (originUid != null)
            uniqueKey = uniqueKey + "-" + originUid;
 
        // 判断是否已领取
        IntegralDetail detailRecord = integralDetailService.getDetailByUniqueKey(uniqueKey);
        if (detailRecord != null)
            return;
 
        UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
        if (userInfoExtra == null)
            return;
 
        Integer goldCoin = userInfoExtra.getGoldCoin();
        if (goldCoin == null)
            goldCoin = 0;
        goldCoin = goldCoin + addGoldCoin;
 
        try {
            UserInfoExtra extra = new UserInfoExtra();
            extra.setId(userInfoExtra.getId());
            extra.setGoldCoin(goldCoin);
            userInfoExtraService.saveUserInfoExtra(extra);
        } catch (UserInfoExtraException e) {
            LogHelper.errorDetailInfo(e);
            return;
        }
 
        // 金币详情
        IntegralDetail detail = new IntegralDetail();
        detail.setTitle("金币奖励");
        detail.setUid(uid);
        detail.setMoney(addGoldCoin);
        detail.setRemark(beizu);
        detail.setUniqueKey(uniqueKey);
        detail.setCreateTime(new Date());
        integralDetailService.insertSelective(detail);
 
        // 消息
        MsgOtherRewardIntegralDTO msgOther = new MsgOtherRewardIntegralDTO();
        msgOther.setTitle("金币奖励");
        msgOther.setNum(addGoldCoin + "金币");
        msgOther.setTotal(goldCoin + "金币");
        if (StringUtil.isNullOrEmpty(level)) {
            msgOther.setSource("首笔分享订单,系统奖励金币 ");
            msgOther.setExplain("首笔分享订单得到的奖金不足0.01元时奖励 ");
        } else {
            msgOther.setSource(level + "级队员首次下单,系统奖励金币 ");
            msgOther.setExplain("仅新邀请队员首下单你得到的奖金不足0.01元时奖励 ");
        }
        userOtherMsgNotificationService.firstOrderRewardMsg(uid, null, msgOther);
    }
 
    /**
     * 推广红包折算金币
     * 
     * @param uid
     * @param addGoldCoin
     * @param tlj
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void taoLiJinExchange(Long uid, Integer addGoldCoin, BigDecimal tlj) {
        String uniqueKey = "taoLiJinExchange-" + uid.toString();
        IntegralDetail detailRecord = integralDetailService.getDetailByUniqueKey(uniqueKey);
        if (detailRecord != null)
            return;
 
        UserInfoExtra userInfoExtra = userInfoExtraService.getByUidForUpdate(uid);
        if (userInfoExtra == null)
            return;
 
        Integer goldCoin = userInfoExtra.getGoldCoin();
        if (goldCoin == null)
            goldCoin = 0;
        goldCoin = goldCoin + addGoldCoin;
 
        try {
            UserInfoExtra extra = new UserInfoExtra();
            extra.setId(userInfoExtra.getId());
            extra.setGoldCoin(goldCoin);
            userInfoExtraService.saveUserInfoExtra(extra);
        } catch (UserInfoExtraException e) {
            LogHelper.errorDetailInfo(e);
            return;
        }
 
        // 金币详情
        IntegralDetail detail = new IntegralDetail();
        detail.setTitle("推广红包下线-系统折算成金币");
        detail.setUid(uid);
        detail.setMoney(addGoldCoin);
        detail.setUniqueKey(uniqueKey);
        detail.setCreateTime(new Date());
        integralDetailService.insertSelective(detail);
 
        // 消息
        MsgOtherRewardIntegralDTO msgOther = new MsgOtherRewardIntegralDTO();
        msgOther.setTitle("推广红包");
        msgOther.setTotal("推广红包¥" + tlj.setScale(2));
        msgOther.setNum(addGoldCoin + "枚");
        msgOther.setSource("已进入到可用金币中");
        msgOther.setExplain("原推广红包功能下线,剩余推广红包按照10:1折算为金币 ");
        userOtherMsgNotificationService.taoLiJinExchangeMsg(uid, "本折算有且仅有一次", msgOther);
    }
}