yujian
2019-08-14 2e5c6b46697f7b96460ad0c356740df52bf056d2
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
package com.yeshi.fanli.service.impl.integral;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.integral.IntegralExchangeMapper;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
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.integral.CodePublishRecord;
import com.yeshi.fanli.entity.integral.IntegralExchange;
import com.yeshi.fanli.entity.integral.IntegralExchange.ExchangeTypeEnum;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
import com.yeshi.fanli.exception.integral.IntegralExchangeException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
import com.yeshi.fanli.service.inter.integral.CodePublishRecordService;
import com.yeshi.fanli.service.inter.integral.IntegralExchangeService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinOriginService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserMoneyDetailService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
import com.yeshi.fanli.vo.integral.ExchangeTipVO;
import com.yeshi.fanli.vo.user.UserInfoExtraVO;
 
@Service
public class IntegralExchangeServiceImpl implements IntegralExchangeService {
 
    @Resource
    private IntegralExchangeMapper integralExchangeMapper;
    
    @Resource
    private UserInfoExtraService userInfoExtraService;
    
    @Resource
    private UserSystemCouponService userSystemCouponService;
    
    @Resource
    private UserTaoLiJinOriginService userTaoLiJinOriginService;
    
    @Resource
    private HongBaoV2Service hongBaoV2Service;
    
    @Resource
    private UserMoneyDetailService userMoneyDetailService;
    
    @Resource
    private UserInfoService userInfoService;
    
    @Resource
    private CodePublishRecordService codePublishRecordService;
    
    
 
    @Override
    public List<IntegralExchange> listValidCache(long start, int count){
        return integralExchangeMapper.listValid(start, count);
    }
    
    @Override
    public Long countValid(){
        return integralExchangeMapper.countValid();
    }
    
    
    @Override
    public ExchangeTipVO verifyExchange(Long uid, Long id) throws IntegralExchangeException{
        if (uid == null || uid <= 0) 
            throw new IntegralExchangeException(1, "用户未登录");
        
        if (id == null || id <= 0) 
            throw new IntegralExchangeException(1, "兑换id不正确");
        
        UserInfoExtraVO extraVO = userInfoExtraService.getInfoExtraVOByUid(uid);
        if (extraVO == null)
            throw new IntegralExchangeException(1, "用户相关信息不存在");
        
        IntegralExchange exchange = integralExchangeMapper.selectByPrimaryKey(id);
        if (exchange == null)
            throw new IntegralExchangeException(1, "兑换方式不存在");
        
        Integer goldCoin = exchange.getGoldCoin();
        
        ExchangeTipVO exchangeTip = new ExchangeTipVO();
        exchangeTip.setId(id);
        ExchangeTypeEnum type = exchange.getType();
        if (ExchangeTypeEnum.inviteCodeActivate == type) {
            //exchangeTip.setTip("注:兑换成功后请到“消息-系统消息”查看");
            //exchangeTip.setGoldCoin(goldCoin + "金币");
            exchangeTip.setType(type.name());
            return exchangeTip;
        }
        
        Integer goldCoinHas = extraVO.getGoldCoin();
        if (goldCoin.intValue() > goldCoinHas.intValue()) {
            exchangeTip.setType("notEnough");
            exchangeTip.setTip("当前账户中可用金币不足,无法兑换该奖品!");
            exchangeTip.setGoldCoin((goldCoin.intValue() - goldCoinHas.intValue()) + "金币");
            return exchangeTip;
        }
        
        if (ExchangeTypeEnum.freeCouponBuy == type) {
            exchangeTip.setTip("自购免单券仅能自己使用,且每个用户ID只能兑换一次。\r\n注:兑换成功后请到“我的-福利中心”中查看");
        } else if (ExchangeTypeEnum.freeCouponGive == type) {
            exchangeTip.setTip("赠送免单券兑换次数不限,赠送次数不限,受赠人若无“邀请人”成功领取后将成为你的一级队员。\r\n注:兑换成功后请到“我的-福利中心”中查看");
        } else if (ExchangeTypeEnum.rebatePercentCoupon == type) {
            exchangeTip.setTip("返利奖励券兑换次数不限,赠送次数不限,受赠人若无“邀请人”成功领取后将成为你的一级队员。\r\n注:兑换成功后请到“我的-福利中心”中查看");
        } else if (ExchangeTypeEnum.inviteCodePublish == type) {
            if(codePublishRecordService.countValidRecord(uid) > 0)
                throw new IntegralExchangeException(1, "三天之内不可重复兑换");
            exchangeTip.setInviteCode(extraVO.getInviteCode());
            exchangeTip.setTip("兑换成功后,将发布于“激活邀请码兑换功能中”,需激活邀请的用户可用金币兑换,本次展示有效期为3天。");
        } else if (ExchangeTypeEnum.taoLiJin == type) {
            exchangeTip.setName(exchange.getAmount() + "元推广红包");
            exchangeTip.setTip("注:兑换成功后请到“我的-推广红包”中查看");
        } else if (ExchangeTypeEnum.cash == type) {    
            exchangeTip.setName(exchange.getAmount() + "元现金红包");
            exchangeTip.setTip("注:兑换成功后请到“我的-账户余额”中查看");
        } else {
            throw new IntegralExchangeException(1, "兑换方式不存在");
        }
        
        exchangeTip.setGoldCoin(goldCoin + "金币");
        exchangeTip.setType(type.name());
        return exchangeTip;
    }
    
    @Override
    public IntegralExchange exchange(Long uid, Long id) throws IntegralExchangeException {
        if (uid == null || uid <= 0)
            throw new IntegralExchangeException(1, "用户未登录");
 
        if (id == null || id <= 0)
            throw new IntegralExchangeException(1, "兑换id不正确");
 
        UserInfoExtra extraVO = userInfoExtraService.getUserInfoExtra(uid);
        if (extraVO == null)
            throw new IntegralExchangeException(1, "用户相关信息不存在");
 
        IntegralExchange exchange = integralExchangeMapper.selectByPrimaryKey(id);
        if (exchange == null)
            throw new IntegralExchangeException(1, "兑换方式不存在");
 
        Integer goldCoin = exchange.getGoldCoin();
        Integer goldCoinHas = extraVO.getGoldCoin();
        if (goldCoin.intValue() > goldCoinHas.intValue()) {
            throw new IntegralExchangeException(1, "当前账户中可用金币不足,无法兑换该奖品!");
        }
        
        try {
            ExchangeTypeEnum type = exchange.getType();
            if (ExchangeTypeEnum.freeCouponBuy == type) {
                // 自购免单券
                userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, false, null);
            } else if (ExchangeTypeEnum.freeCouponGive == type) {
                // 赠送免单券
                userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, true, null);
            } else if (ExchangeTypeEnum.rebatePercentCoupon == type) {
                // 奖励免单券
                userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, true,
                        new BigDecimal(10));
            } else if (ExchangeTypeEnum.inviteCodePublish == type) {
                // 邀请码发布
                if(codePublishRecordService.countValidRecord(uid) > 0)
                    throw new IntegralExchangeException(1, "三天之内不可重复兑换");
                codePublishRecordService.publishInviteCode(uid);
            } else if (ExchangeTypeEnum.taoLiJin == type) {
                // 推广红包
                userTaoLiJinOriginService.exchangeMoney(uid, exchange.getAmount());
            } else if (ExchangeTypeEnum.cash == type) {
                // 现金红包
                BigDecimal money = exchange.getAmount();
 
                // 1、插入红包数据
                HongBaoV2 hongBaoV2 = new HongBaoV2();
                hongBaoV2.setMoney(money);
                hongBaoV2.setType(HongBaoV2.TYPE_EXCHANGE);
                hongBaoV2.setState(HongBaoV2.STATE_YILINGQU);
                hongBaoV2.setVersion(2);
                hongBaoV2.setCreateTime(new Date());
                hongBaoV2.setUpdateTime(new Date());
                hongBaoV2.setUserInfo(new UserInfo(uid));
                hongBaoV2.setPreGetTime(new Date());
                hongBaoV2.setGetTime(new Date());
                hongBaoV2Service.insertSelective(hongBaoV2);
 
                // 2.插入资金明细,用户余额
                UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createScoreConvert(money, new UserInfo(uid));
                userMoneyDetailService.addUserMoneyDetail(userMoneyDetail);
                userInfoService.addMoney(new UserInfo(uid), money);
            } else {
                throw new IntegralExchangeException(1, "兑换方式不存在");
            }
            
            String progress = exchange.getProgress();
            if (!StringUtil.isNullOrEmpty(progress))
                progress = progress.replace("{已兑换}", 1 + "").replace("{上限数}", exchange.getUpperLimit() + "");
            exchange.setProgress(progress);
            
            // 更新金币
            UserInfoExtraVO extraUpdate = new UserInfoExtraVO();
            extraUpdate.setId(extraVO.getId());
            extraUpdate.setGoldCoin(goldCoinHas.intValue() - goldCoin.intValue());
            userInfoExtraService.saveUserInfoExtra(extraUpdate);
            return exchange;
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
            throw new IntegralExchangeException(1, "兑换异常");
        }
    }
    
    
    
    @Override
    public ExchangeTipVO verifyInviteCode(Long uid, Long id) throws IntegralExchangeException{
        if (uid == null || uid <= 0)
            throw new IntegralExchangeException(1, "用户未登录");
 
        if (id == null || id <= 0)
            throw new IntegralExchangeException(1, "兑换id不正确");
 
        UserInfoExtra extraVO = userInfoExtraService.getUserInfoExtra(uid);
        if (extraVO == null)
            throw new IntegralExchangeException(1, "用户相关信息不存在");
 
        if (!StringUtil.isNullOrEmpty(extraVO.getInviteCode())) 
            throw new IntegralExchangeException(1, "邀请码已激活,无需兑换");
        
        CodePublishRecord record = codePublishRecordService.selectByPrimaryKey(id);
        if (record == null)
            throw new IntegralExchangeException(1, "兑换记录不存在");
 
        IntegralExchange exchange = integralExchangeMapper.getValidByType(ExchangeTypeEnum.inviteCodeActivate.name());
        if (exchange == null)
            throw new IntegralExchangeException(1, "兑换方式不存在");
 
        ExchangeTipVO exchangeTip = new ExchangeTipVO();
        exchangeTip.setId(id);
        Integer goldCoin = exchange.getGoldCoin();
        Integer goldCoinHas = extraVO.getGoldCoin();
        if (goldCoin.intValue() > goldCoinHas.intValue()) {
            exchangeTip.setType("notEnough");
            exchangeTip.setTip("当前账户中可用金币不足,无法兑换该奖品!");
            exchangeTip.setGoldCoin((goldCoin.intValue() - goldCoinHas.intValue()) + "金币");
            return exchangeTip;
        } 
        
        exchangeTip.setTip("注:兑换成功后请到“消息-系统消息”查看");
        exchangeTip.setGoldCoin(goldCoin + "金币");
        exchangeTip.setType(ExchangeTypeEnum.inviteCodeActivate.name());
        return exchangeTip;
    }
    
    
    @Override
    public void exchangeInviteCode(Long uid, Long id) throws IntegralExchangeException {
        if (uid == null || uid <= 0)
            throw new IntegralExchangeException(1, "用户未登录");
 
        if (id == null || id <= 0)
            throw new IntegralExchangeException(1, "兑换id不正确");
 
        UserInfoExtra extraVO = userInfoExtraService.getUserInfoExtra(uid);
        if (extraVO == null)
            throw new IntegralExchangeException(1, "用户相关信息不存在");
 
        if (!StringUtil.isNullOrEmpty(extraVO.getInviteCode())) 
            throw new IntegralExchangeException(1, "邀请码已激活,无需兑换");
        
        CodePublishRecord record = codePublishRecordService.selectByPrimaryKey(id);
        if (record == null)
            throw new IntegralExchangeException(1, "兑换记录不存在");
 
        IntegralExchange exchange = integralExchangeMapper.getValidByType(ExchangeTypeEnum.inviteCodeActivate.name());
        if (exchange == null)
            throw new IntegralExchangeException(1, "兑换方式不存在");
 
        Integer goldCoin = exchange.getGoldCoin();
        Integer goldCoinHas = extraVO.getGoldCoin();
        if (goldCoin.intValue() > goldCoinHas.intValue()) {
            throw new IntegralExchangeException(1, "当前账户中可用金币不足,无法兑换该奖品!");
        } 
        
        try {
            UserInfoExtraVO inviteExtra = userInfoExtraService.getInfoExtraVOByUid(record.getUid());
            if (inviteExtra == null || StringUtil.isNullOrEmpty(inviteExtra.getInviteCode()))
                throw new IntegralExchangeException(1, "兑换失败,该用户邀请码不存在");
            
            userInfoExtraService.activateInviteCode(uid, inviteExtra.getInviteCode());
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
            throw new IntegralExchangeException(1, "兑换异常");
        }
    }
}