yujian
2019-08-16 fb5a8918a7983bbdf83586f773884d3961bb3c6d
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
package com.yeshi.fanli.service.impl.user;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.user.TokenRecordMapper;
import com.yeshi.fanli.dto.msg.MsgOtherGiveContentDTO;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinGiveRecord;
import com.yeshi.fanli.entity.bus.user.TokenRecord;
import com.yeshi.fanli.entity.bus.user.TokenRecord.TokenTypeEnum;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
import com.yeshi.fanli.entity.bus.user.UserSystemCouponGiveRecord;
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
import com.yeshi.fanli.exception.tlj.UserTaoLiJinOriginException;
import com.yeshi.fanli.exception.user.TokenRecordException;
import com.yeshi.fanli.exception.user.UserInfoExtraException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.msg.UserOtherMsgNotificationService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinGiveRecordService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinOriginService;
import com.yeshi.fanli.service.inter.user.TokenRecordService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponGiveRecordService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.vo.msg.TokenVO;
 
@Service
public class TokenRecordServiceImpl implements TokenRecordService {
 
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
    
    @Resource
    private TokenRecordMapper tokenRecordMapper;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private UserSystemCouponService userSystemCouponService;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Resource
    private UserTaoLiJinGiveRecordService userTaoLiJinGiveRecordService;
 
    @Resource
    private UserInfoExtraService userInfoExtraService;
 
    @Resource
    private UserSystemCouponGiveRecordService userSystemCouponGiveRecordService;
    
    @Resource
    private UserTaoLiJinOriginService userTaoLiJinOriginService;
    
    @Resource
    private UserOtherMsgNotificationService userOtherMsgNotificationService;
    
    
    @Override
    public void insertSelective(TokenRecord record) {
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        tokenRecordMapper.insertSelective(record);
    }
 
    @Override
    public TokenRecord getNearByTypeAndIdentify(String type, String identify) {
        return tokenRecordMapper.getNearByTypeAndIdentify(type, identify);
    }
 
    @Override
    public TokenVO discernToken(String token, Long uid) throws TokenRecordException {
 
        // TODO token 验证
 
        TokenRecord rokenRecord = tokenRecordMapper.getByToken(token);
        if (rokenRecord == null)
            throw new TokenRecordException(1, "口令不存在");
        
        Date now = new Date();
        Date endTimeToken = rokenRecord.getEndTime();
        if (endTimeToken != null && endTimeToken.getTime() < now.getTime())
            throw new TokenRecordException(1, "口令已失效");
        
        Integer stateTtoken = rokenRecord.getState();
        if (stateTtoken != null && stateTtoken == 1)
            throw new TokenRecordException(1, "口令已失效");
 
        String nickName = null;
        String portrait = null;
        Long inviteId = rokenRecord.getUid();
        if (inviteId != null) {
            UserInfo inviteUser = userInfoService.selectByPKey(inviteId);
            if (inviteUser != null) {
                nickName = inviteUser.getNickName();
                portrait = inviteUser.getPortrait();
            }
        }
 
        if (StringUtil.isNullOrEmpty(nickName))
            nickName = Constant.systemCommonConfig.getDefaultNickName();
 
        if (StringUtil.isNullOrEmpty(portrait))
            portrait = Constant.systemCommonConfig.getDefaultPortrait();
 
        if (nickName.length() > 6) {
            nickName = nickName.substring(0, 6) + "...";
        }
 
        TokenVO tokenVO = new TokenVO();
        tokenVO.setToken(token);
        tokenVO.setNickName(nickName);
        tokenVO.setPortrait(portrait);
 
        String identify = rokenRecord.getIdentify();
        if (StringUtil.isNullOrEmpty(identify))
            throw new TokenRecordException(1, "口令标识不存在");
 
        Integer type = 0;
        boolean state = false;
        List<String> tips = new ArrayList<String>();
        TokenTypeEnum tokenType = rokenRecord.getType();
        if (tokenType == TokenTypeEnum.freeCoupon) {
            type = 10;
            UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(Long.parseLong(identify));
            if (userCoupon == null) {
                tips.add("哎呀,这张免单券已失效了!");
            } else {
                Boolean give = userCoupon.getGive();
                if (give == null)
                    give = false;
 
                if (userCoupon.getState() == UserSystemCoupon.STATE_OVERDUE) {
                    tips.add("哎呀,这张免单券已失效了!");
                } else if (userCoupon.getState() == UserSystemCoupon.STATE_END_USE) {
                    tips.add("哎呀,这张免单券已被领取了!");
                } else if (userCoupon.getState() == UserSystemCoupon.STATE_CAN_USE) {
                    throw new TokenRecordException(1, "口令已失效");
                } else if (give && userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
                    if (uid == null || uid <= 0)
                        throw new TokenRecordException(1001, "温馨提示,[免单券]需要登录后领取");
 
                    state = true;
                    UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
                    if (userInfoExtra == null || StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
                        tips.add("确认领取后,你将成为赠送者的一级队员;");
                        tips.add("获赠的免单券,需要激活后才能使用,详情参见免单券激活规则;");
                        tips.add("成功领取后,请到“我的-福利中心”查看。");
                    } else {
                        tips.add("获赠的免单券,需要激活后才能使用,详情参见免单券激活规则;");
                        tips.add("成功领取后,请到“我的-福利中心”查看。");
                    }
                } else {
                    throw new TokenRecordException(1, "口令已失效");
                }
            }
        } else if (tokenType == TokenTypeEnum.rebatePercentCoupon) {
            type = 11;
            UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(Long.parseLong(identify));
            if (userCoupon == null) {
 
            } else {
                Boolean give = userCoupon.getGive();
                if (give == null)
                    give = false;
 
                if (userCoupon.getState() == UserSystemCoupon.STATE_OVERDUE) {
                    tips.add("哎呀,这张返利奖励券已失效了!");
                } else if (userCoupon.getState() == UserSystemCoupon.STATE_END_USE) {
                    tips.add("哎呀,这张返利奖励券已被领取了!");
                } else if (userCoupon.getState() == UserSystemCoupon.STATE_CAN_USE) {
                    throw new TokenRecordException(1, "口令已失效");
                } else if (give && userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
                    if (uid == null || uid <= 0)
                        throw new TokenRecordException(1001, "温馨提示,[返利奖励券]需要登录后领取");
 
                    state = true;
                    UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
                    if (userInfoExtra == null || StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
                        tips.add("确认领取后,你将成为赠送者的一级队员;");
                        tips.add("返利券奖励券,可用于“已到账”的返利订单,在返利的基础上再获得一定比例的返利;");
                        tips.add("成功领取后,请到“我的-福利中心”中查看。");
                    } else {
                        tips.add("返利券奖励券,可用于“已到账”的返利订单,在返利的基础上再获得一定比例的返利;");
                        tips.add("成功领取后,请到“我的-福利中心”中查看。");
                    }
                } else {
                    throw new TokenRecordException(1, "口令已失效");
                }
            }
        } else if (tokenType == TokenTypeEnum.taoLiJin) {
            type = 12;
            UserTaoLiJinGiveRecord giveRecord = userTaoLiJinGiveRecordService
                    .selectByPrimaryKey(Long.parseLong(identify));
            if (giveRecord == null || giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_OVERDUE) {
                tips.add("哎呀,推广红包已失效了!");
            } else if (giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_RECEIVE) {
                tips.add("哎呀,推广红包已被领取了!");
            } else {
                if (uid == null || uid <= 0)
                    throw new TokenRecordException(1001, "温馨提示,[推广红包]需要登录后领取");
 
                state = true;
                UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
                if (userInfoExtra == null || StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
                    tips.add("确认领取后,你将成为赠送者的一级队员;");
                    tips.add("推广红包,可分享爆款商品,让分享订单成单率更高;");
                    tips.add("成功领取后,请到“我的-推广红包”中查看。");
                } else {
                    tips.add("推广红包,可分享爆款商品,让分享订单成单率更高;");
                    tips.add("成功领取后,请到“我的-推广红包”中查看。");
                }
            }
        } else {
            throw new TokenRecordException(1, "无对应类型");
        }
 
        tokenVO.setTips(tips);
        tokenVO.setState(state);
        tokenVO.setType(type);
        return tokenVO;
    }
 
    @Override
    public void receiveToken(String token, Long uid) throws TokenRecordException {
        if (uid == null || uid <= 0)
            throw new TokenRecordException(1, "用户未登录");
 
        if (StringUtil.isNullOrEmpty(token))
            throw new TokenRecordException(1, "口令不能为空");
 
        TokenRecord rokenRecord = tokenRecordMapper.getByToken(token);
        if (rokenRecord == null)
            throw new TokenRecordException(1, "口令已失效");
        
        Date now = new Date();
        Date endTimeToken = rokenRecord.getEndTime();
        if (endTimeToken != null && endTimeToken.getTime() < now.getTime())
            throw new TokenRecordException(1, "口令已失效");
        
        Integer stateTtoken = rokenRecord.getState();
        if (stateTtoken != null && stateTtoken == 1)
            throw new TokenRecordException(1, "口令已失效");
        
        String identify = rokenRecord.getIdentify();
        if (StringUtil.isNullOrEmpty(identify))
            throw new TokenRecordException(1, "口令标识不存在");
        
        
        Long giveUid = rokenRecord.getUid();
        UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
        if (userInfoExtra == null) 
            throw new TokenRecordException(1, "用户信息缺失");
            
        TokenTypeEnum tokenType = rokenRecord.getType();
        if (tokenType == TokenTypeEnum.freeCoupon) {
            long couponId = Long.parseLong(identify);
            UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(couponId);
            if (userCoupon == null)
                throw new TokenRecordException(1, "该券不存在");
 
            Boolean give = userCoupon.getGive();
            if (give == null)
                give = false;
            
            if (userCoupon.getState() == UserSystemCoupon.STATE_OVERDUE)
                throw new TokenRecordException(1, "哎呀,这张免单券已失效了!");
            
            if (userCoupon.getState() == UserSystemCoupon.STATE_END_USE)
                throw new TokenRecordException(1, "哎呀,这张免单券已被领取了!");
            
            if (userCoupon.getState() == UserSystemCoupon.STATE_CAN_USE)
                throw new TokenRecordException(1, "口令已失效");
            
            if (give && userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
                UserSystemCouponGiveRecord giveRecord = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(giveUid, couponId);
                if (giveRecord == null ) 
                    throw new TokenRecordException(1, "口令已失效");
                
                Date endTime = giveRecord.getEndTime();
                if (endTime != null && endTime.getTime() < now.getTime())
                    throw new TokenRecordException(1, "哎呀,这张免单券已失效了!");
                
                try {
                    userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.freeCoupon.name(), UserSystemCoupon.SOURCE_GIVE);
                } catch (Exception e) {
                    LogHelper.errorDetailInfo(e);
                    throw new TokenRecordException(1, "领取失败");
                }
                
                // 更新赠送券信息
                userSystemCouponService.updateGiveRecord(uid, couponId, CouponTypeEnum.freeCoupon.name());
                
                // 更新赠送记录
                giveRecord.setReceiveUid(uid);
                giveRecord.setReceiveTime(new Date());
                userSystemCouponGiveRecordService.updateByPrimaryKeySelective(giveRecord);
                
                // 消息 + 队员
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH.mm");
                        boolean addTeam = addInviteTeam(uid, giveUid, userInfoExtra);
                        String beiZhu = "无";
                        if (addTeam)
                            beiZhu = "领取人已经成为你的一级队员";
                        
                        String userName = "无";
                        UserInfo user = userInfoService.selectByPKey(uid);
                        if (user != null && !StringUtil.isNullOrEmpty(user.getNickName()))
                            userName = user.getNickName();
                        
                        MsgOtherGiveContentDTO msgOther = new MsgOtherGiveContentDTO();
                        msgOther.setType(MsgOtherGiveContentDTO.TYEP_COUPON);
                        msgOther.setTitle("赠送免单券");
                        msgOther.setGiveType("你赠送的免单券被成功领取");
                        msgOther.setReceiveInfo("昵称:  " + userName +" ID:" + uid);
                        msgOther.setGiveTime(sd.format(giveRecord.getGiveTime()));
                        msgOther.setReceiveTime(sd.format(new Date()));
                        userOtherMsgNotificationService.tokenGiveMsg(giveUid, beiZhu, msgOther);
                    }
                });
            } else {
                throw new TokenRecordException(1, "口令已失效");
            }
 
        } else if (tokenType == TokenTypeEnum.rebatePercentCoupon) {
            long couponId = Long.parseLong(identify);
            UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(couponId);
            if (userCoupon == null)
                throw new TokenRecordException(1, "该券不存在");
 
            Boolean give = userCoupon.getGive();
            if (give == null)
                give = false;
 
            if (userCoupon.getState() == UserSystemCoupon.STATE_OVERDUE)
                throw new TokenRecordException(1, "哎呀,这张返利奖励券已失效了!");
            
            if (userCoupon.getState() == UserSystemCoupon.STATE_END_USE) 
                throw new TokenRecordException(1, "哎呀,这张返利奖励券已被领取了!");
            
            if (userCoupon.getState() == UserSystemCoupon.STATE_CAN_USE)
                throw new TokenRecordException(1, "口令已失效");
            
            if (give && userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
                UserSystemCouponGiveRecord giveRecord = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(giveUid, couponId);
                if (giveRecord == null ) 
                    throw new TokenRecordException(1, "口令已失效");
                
                Date endTime = giveRecord.getEndTime();
                if (endTime != null && endTime.getTime() < now.getTime())
                    throw new TokenRecordException(1, "哎呀,这张返利奖励券已失效了!");
                
                try {
                    userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.rebatePercentCoupon.name(), UserSystemCoupon.SOURCE_GIVE);
                } catch (Exception e) {
                    LogHelper.errorDetailInfo(e);
                    throw new TokenRecordException(1, "领取失败");
                }
                
                // 更新赠送券信息
                userSystemCouponService.updateGiveRecord(uid, couponId, CouponTypeEnum.rebatePercentCoupon.name());
                
                // 更新赠送记录
                giveRecord.setReceiveUid(uid);
                giveRecord.setReceiveTime(new Date());
                userSystemCouponGiveRecordService.updateByPrimaryKeySelective(giveRecord);
                
                // 消息 + 队员
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH.mm");
                        boolean addTeam = addInviteTeam(uid, giveUid, userInfoExtra);
                        String beiZhu = "无";
                        if (addTeam)
                            beiZhu = "领取人已经成为你的一级队员";
                        
                        MsgOtherGiveContentDTO msgOther = new MsgOtherGiveContentDTO();
                        msgOther.setType(MsgOtherGiveContentDTO.TYEP_COUPON);
                        msgOther.setTitle("赠送奖励券");
                        msgOther.setGiveType("你赠送的奖励券被成功领取");
                        msgOther.setReceiveInfo("昵称:[昵称昵称昵称/无]ID:[854245]");
                        msgOther.setGiveTime(sd.format(giveRecord.getGiveTime()));
                        msgOther.setReceiveTime(sd.format(new Date()));
                        userOtherMsgNotificationService.tokenGiveMsg(giveUid, beiZhu, msgOther);
                    }
                });
            } else {
                throw new TokenRecordException(1, "口令已失效");
            }
        } else if (tokenType == TokenTypeEnum.taoLiJin) {
            UserTaoLiJinGiveRecord giveRecord = userTaoLiJinGiveRecordService
                    .selectByPrimaryKey(Long.parseLong(identify));
            if (giveRecord == null || giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_OVERDUE) {
                throw new TokenRecordException(1, "哎呀,推广红包已失效了!");
            } else if (giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_RECEIVE) {
                throw new TokenRecordException(1, "哎呀,推广红包已被领取了!");
            } else {
                Date endTime = giveRecord.getEndTime();
                if (endTime != null && endTime.getTime() < now.getTime())
                    throw new TokenRecordException(1, "哎呀,推广红包已失效了!");
                
                BigDecimal amount = giveRecord.getAmount();
                try {
                    // 领取
                    userTaoLiJinOriginService.receiveFriendsGive(uid, amount);
                    
                    // 更新记录
                    UserTaoLiJinGiveRecord updateRecord = new UserTaoLiJinGiveRecord();
                    updateRecord.setId(Long.parseLong(identify));
                    updateRecord.setReceiveTime(new Date());
                    updateRecord.setReceiveUid(uid);
                    updateRecord.setState(UserTaoLiJinGiveRecord.STATE_RECEIVE);
                    userTaoLiJinGiveRecordService.updateByPrimaryKeySelective(updateRecord);
                } catch (UserTaoLiJinOriginException e) {
                    e.printStackTrace();
                }
            }
        } else {
            throw new TokenRecordException(1, "无对应类型");
        }
 
    }
    
    
    /**
     *     未激活邀请码: 上下级关系确立
     * @param uid
     * @param giveUid
     * @param userInfoExtra
     * @return
     */
    public boolean addInviteTeam(Long uid, Long giveUid, UserInfoExtra userInfoExtra) {
        boolean invite = false;
        if (StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
            UserInfoExtra userInfoExtraGive = userInfoExtraService.getUserInfoExtra(giveUid);
            if (userInfoExtraGive != null ) {
                String inviteCode = userInfoExtraGive.getInviteCode();
                if(!StringUtil.isNullOrEmpty(inviteCode)) {
                    try {
                        userInfoExtraService.activateInviteCode(uid, inviteCode);
                        invite = true;
                    } catch (UserInfoExtraException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return invite;
    }
    
}