yujian
2019-08-15 01e773cf27e572b8fb83538d85f31130930d93d1
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
package com.yeshi.fanli.service.impl.user;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.user.TokenRecordMapper;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
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.UserSystemCoupon;
import com.yeshi.fanli.exception.user.TokenRecordException;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.TokenRecordService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
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
    private TokenRecordMapper tokenRecordMapper;
    
    @Resource
    private UserInfoService userInfoService;
    
    @Resource
    private UserSystemCouponService userSystemCouponService;
    
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
    
    
    
    @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);
    }
 
    
    public void discernToken(String token, Long uid) throws TokenRecordException {
        
        // TODO token 验证
        
        TokenRecord rokenRecord = tokenRecordMapper.getByToken(token);
        if (rokenRecord == null) 
            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) + "...";
        }
        
        Integer stateTtoken = rokenRecord.getState();
        if (stateTtoken == 1)
            throw new TokenRecordException(1, "口令已失效");
        
        TokenVO tokenVO = new TokenVO();
        tokenVO.setNickName(nickName);
        tokenVO.setPortrait(portrait);
        
        String identify = rokenRecord.getIdentify();
        if (StringUtil.isNullOrEmpty(identify)) 
            throw new TokenRecordException(1, "口令标识不存在");
        
        boolean state = false;
        List<String> tips = new ArrayList<String>();
        TokenTypeEnum tokenType = rokenRecord.getType();
        if (tokenType == TokenTypeEnum.freeCoupon) {
            UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(Long.parseLong(identify));
            if (userCoupon == null || 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 (userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
                if (uid == null || uid <= 0)
                    throw new TokenRecordException(1001, "用户未登录");
                
                state = true;
                ThreeSale boss = threeSaleSerivce.getMyBoss(uid);
                if (boss != null) {
                    tips.add("获赠的免单券,需要激活后才能使用,详情参见免单券激活规则;");
                    tips.add("成功领取后,请到“我的-福利中心”查看。");
                } else {
                    tips.add("确认领取后,你将成为赠送者的一级队员;");
                    tips.add("获赠的免单券,需要激活后才能使用,详情参见免单券激活规则;");
                    tips.add("成功领取后,请到“我的-福利中心”查看。");
                }
            }
        } else if (tokenType == TokenTypeEnum.rebatePercentCoupon) {
            UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(Long.parseLong(identify));
            if (userCoupon == null || 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 (userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
                if (uid == null || uid <= 0)
                    throw new TokenRecordException(1001, "用户未登录");
                
                state = true;
                ThreeSale boss = threeSaleSerivce.getMyBoss(uid);
                if (boss != null) {
                    tips.add("返利券奖励券,可用于“已到账”的返利订单,在返利的基础上再获得一定比例的返利;");
                    tips.add("成功领取后,请到“我的-福利中心”中查看。");
                } else {
                    tips.add("确认领取后,你将成为赠送者的一级队员;");
                    tips.add("返利券奖励券,可用于“已到账”的返利订单,在返利的基础上再获得一定比例的返利;");
                    tips.add("成功领取后,请到“我的-福利中心”中查看。");
                }
            }
        } else if (tokenType == TokenTypeEnum.taoLiJin) {
            UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(Long.parseLong(identify));
            if (userCoupon == null || 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 (userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
                if (uid == null || uid <= 0)
                    throw new TokenRecordException(1001, "用户未登录");
                
                state = true;
                ThreeSale boss = threeSaleSerivce.getMyBoss(uid);
                if (boss != null) {
                    tips.add("返利券奖励券,可用于“已到账”的返利订单,在返利的基础上再获得一定比例的返利;");
                    tips.add("成功领取后,请到“我的-福利中心”中查看。");
                } else {
                    tips.add("确认领取后,你将成为赠送者的一级队员;");
                    tips.add("返利券奖励券,可用于“已到账”的返利订单,在返利的基础上再获得一定比例的返利;");
                    tips.add("成功领取后,请到“我的-福利中心”中查看。");
                }
            }
        } else {
            throw new TokenRecordException(1, "无对应类型");
        }
        
        tokenVO.setTips(tips);
        tokenVO.setState(state);
        tokenVO.setType(tokenType.name());
        
    }
    
}