yujian
2019-09-27 c833ab8f5968c755ded82eea5e8ce2aca841a0cb
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
package com.yeshi.fanli.service.impl.user;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.yeshi.utils.DateUtil;
 
import com.yeshi.fanli.dao.mybatis.user.UserLotteryRecordMapper;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.UserLotteryRecord;
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
import com.yeshi.fanli.entity.system.SystemCoupon;
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
import com.yeshi.fanli.exception.user.UserInfoExtraException;
import com.yeshi.fanli.exception.user.UserLotteryRecordException;
import com.yeshi.fanli.exception.user.UserSystemCouponException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.config.SystemCouponService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserLotteryRecordService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
@Service
public class UserLotteryRecordServiceImpl implements UserLotteryRecordService {
 
    @Resource
    private UserLotteryRecordMapper userLotteryRecordMapper;
    
    @Resource
    private ConfigService configService;
 
    @Resource
    private UserSystemCouponService userSystemCouponService;
    
    @Resource
    private SystemCouponService systemCouponService;
    
    @Resource
    private UserInfoExtraService userInfoExtraService;
 
    
    public UserLotteryRecord getLotteryByTypeAndUid(Long uid, String type) {
        int num = 0;
        UserLotteryRecord record = userLotteryRecordMapper.getByTypeAndUid(uid, type);
        if (record != null) {
            // 今日之内是否已抽过奖
            Date updateTime = record.getUpdateTime();
            if (updateTime != null && DateUtil.isSameDay(updateTime, new Date())) {
                num = record.getCount();
            } else {
                if (UserLotteryRecord.TYPE_DAILY_REBATE.equals(type)) {
                    // 1、天天送抽奖次数
                    num = UserLotteryRecord.COUNT_DAILY_REBATE;
                } else if(UserLotteryRecord.TYPE_NEWBIES.equals(type))  {
                    // 2、新人抽奖
                    num = record.getCount();
                }
            }
        } else {
            if (UserLotteryRecord.TYPE_DAILY_REBATE.equals(type)) {
                // 1、天天送抽奖次数
                num = UserLotteryRecord.COUNT_DAILY_REBATE;
            } else if(UserLotteryRecord.TYPE_NEWBIES.equals(type))  {
                // 2、新人抽奖 : 15天之内首次登录系统
                boolean isNewUser = userInfoExtraService.isNewUser(uid);
                if (isNewUser) {
                    num = UserLotteryRecord.COUNT_NEWBIES;
                }
            }
            
            if (num > 0)  {
                // 插入抽奖记录
                record = new UserLotteryRecord();
                record.setUid(uid);
                record.setType(type);
                record.setCount(num);
                record.setCreateTime(new Date());
                record.setUpdateTime(new Date());
                userLotteryRecordMapper.insertSelective(record);
            }
        }
        
        if (record == null) {
            record = new UserLotteryRecord();
            record.setUid(uid);
            record.setType(type);
            record.setCount(num);
        } else {
            record.setCount(num);
        }
        return record;
    }
    
    
    @Override
    public JSONObject getLotteryCountNewbies(Long uid) throws UserLotteryRecordException,Exception {
        
        if (uid == null || uid == 0) {
             throw new UserLotteryRecordException(1, "未登录系统");
        }
    
                
        // 兼容1.5.3之前版本
        UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
        if (userInfoExtra == null) {
            throw new UserLotteryRecordException(1, "未登录系统");
        }
        
        //抽奖次数
        int count = 0;  
        Integer lotteryNewbies = userInfoExtra.getLotteryNewbies();
        if (lotteryNewbies != null) {
            count = lotteryNewbies;
        } else {
            UserLotteryRecord record = getLotteryByTypeAndUid(uid, UserLotteryRecord.TYPE_NEWBIES);
            if (record != null) {
                count = record.getCount();
            }
        }
        
        // 抽奖规则
        String lotteryRule = configService.get("lottery_rule_newbies");
                    
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("rule", lotteryRule);
        return data;
    }
    
    
    @Override
    public Map<String, Object> executeLotteryNewbies(Long uid) throws UserLotteryRecordException, Exception{
        if (uid == null || uid == 0) {
             throw new UserLotteryRecordException(1, "未登录系统");
        }
        
        UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
        if (userInfoExtra == null) {
            throw new UserLotteryRecordException(1, "未登录系统");
        }
        
        int count = 0;
        boolean isold = true;
        UserLotteryRecord record = null;
    
        Integer lotteryNewbies = userInfoExtra.getLotteryNewbies();
        if (lotteryNewbies != null) {
            // 老版的次数   兼容1.5.3之前版本
            count = lotteryNewbies;
        } else {
            isold = false; // 新版
            record = getLotteryByTypeAndUid(uid, UserLotteryRecord.TYPE_NEWBIES);
            if (record != null) {
                count = record.getCount();
            }
        }
        
        if (count <= 0) { 
            throw new UserLotteryRecordException(2, "抽奖次数不足");
        } 
        
        String prize = null;
        String couponType = null;
        // 新人抽奖 -已抽中奖品
        List<UserSystemCoupon> list = userSystemCouponService.getUserCouponBySource(uid, UserSystemCoupon.SOURCE_NEWBIES);
        
        if (list == null || list.size() == 0) {
            prize = getLotteryPrizeNewbies(count, 0, null);
        } else if (list.size() == 1) {
            
            UserSystemCoupon userSystemCoupon = list.get(0);
            SystemCoupon systemCoupon = userSystemCoupon.getSystemCoupon();
            if (systemCoupon != null) {
                SystemCoupon coupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId());
                couponType = coupon.getType().name();
            }
            
            if (couponType != null && couponType.trim().length() > 0) {
                prize = getLotteryPrizeNewbies(count, 1, couponType);
            }
        }
        
        if (prize == null || prize.trim().length() == 0) {
            prize = "NoPrize";
        } else if (prize.equals(couponType)) {
            // 已存在此券
            prize = "NoPrize";
        } else {
            try {
                userSystemCouponService.insertUserCoupon(uid, prize, UserSystemCoupon.SOURCE_NEWBIES, null,true);
            } catch (UserSystemCouponException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        
        // 减少次数
        count--;
        
        // 更新记录
        if (isold) {
            UserInfoExtra extra = new UserInfoExtra();
            extra.setId(userInfoExtra.getId());
            extra.setLotteryNewbies(count);
            try {
                userInfoExtraService.saveUserInfoExtra(extra);
            } catch (UserInfoExtraException e) {
                e.printStackTrace();
            }
        } else {
            UserLotteryRecord updateRecord = new UserLotteryRecord();
            updateRecord.setId(record.getId());
            updateRecord.setCount(count);
            updateRecord.setUpdateTime(new Date());
            userLotteryRecordMapper.updateByPrimaryKeySelective(updateRecord);
        }
        
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("count", count);
        map.put("result", prize);
        return map;
    }
    
    /**
     * 新人用户抽奖
     * @param count
     * @param countPrize
     * @param couponType
     * @return
     */
    @Override
    public String getLotteryPrizeNewbies(int count, int countPrize, String couponType) {
        String prize = null;
        String freeCoupon = CouponTypeEnum.welfareFreeCoupon.name(); // 福利免费券
        String rebateCoupon = CouponTypeEnum.rebatePercentCoupon.name(); // 奖励券
 
        if (countPrize == 1 && count == 1) {
            // 最后一次: 只收到一个奖品
            if (rebateCoupon.equals(couponType)) {
                prize = freeCoupon; // 福利免费券
            } else {
                prize = rebateCoupon; // 奖励券
            }
        } else if (countPrize == 0 && count == 2) {
            // 后面两次必中
            long result = (1 + Math.round(Math.random() * (9)));
            if (result <= 5) {
                prize = freeCoupon; // 福利免费券
            } else {
                prize = rebateCoupon; // 奖励券
            }
        } else { 
            long result = (1 + Math.round(Math.random() * (9)));
            if (result <= 3) {
                prize = freeCoupon; // 福利免费券
            } else if (result <= 6) {
                prize = rebateCoupon; // 奖励券
            }
        }
        return prize;
    }
    
    
    
    @Override
    public JSONObject getLotteryCountDaily(Long uid) throws UserLotteryRecordException,Exception {
        
        if (uid == null || uid == 0) {
             throw new UserLotteryRecordException(1, "未登录系统");
        }
        
        //抽奖次数
        int count = 0;  
        
        UserLotteryRecord record = getLotteryByTypeAndUid(uid, UserLotteryRecord.TYPE_DAILY_REBATE);
        if (record != null) {
            count = record.getCount();
        }
        
        // 抽奖规则
        String lotteryRule = configService.get("lottery_rule_daily_rebate");
                    
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("rule", lotteryRule);
        return data;
    }
    
    
    @Override
    public Map<String, Object> executeLotteryDaily(Long uid) throws UserLotteryRecordException, Exception{
        if (uid == null || uid == 0) {
             throw new UserLotteryRecordException(1, "未登录系统");
        }
        
        UserLotteryRecord record = getLotteryByTypeAndUid(uid, UserLotteryRecord.TYPE_DAILY_REBATE);
        if (record == null) {
            throw new UserLotteryRecordException(1, "暂无抽奖机会");
        }
        
        int count = record.getCount();
        // 抽奖次数不足
        if (count == 0) { 
            throw new UserLotteryRecordException(2,  "抽奖次数不足");
        } 
        
        String prize = null;
        // 今日天天抽奖-已抽中拥有券数量
        int todayHas = userSystemCouponService.countTodatyUserCouponBySource(uid, UserSystemCoupon.SOURCE_DAILY_REBATE);
        
        if (todayHas == 0) {
            // 抽奖
            prize = getLotteryPrizeDaily(count);
            LogHelper.test(uid + "中奖结果:" + prize);
            try {
                int num = 0;
                if ("rebateCoupon".equals(prize)) {
                    num = 1;
                } else if ("doubleCoupon".equals(prize)) {
                    num = 2;
                } 
                
                // 插入奖励券
                if (num > 0) {
                    userSystemCouponService.randomRewardCoupon(num, uid, UserSystemCoupon.SOURCE_DAILY_REBATE);
                }
                
            } catch (UserSystemCouponException e) {
                throw new UserLotteryRecordException(1, "抽奖失败");
            }
        }
        
        // 减少次数
        count--;
        
        // 更新记录
        UserLotteryRecord updateRecord = new UserLotteryRecord();
        updateRecord.setId(record.getId());
        updateRecord.setCount(count);
        updateRecord.setUpdateTime(new Date());
        userLotteryRecordMapper.updateByPrimaryKeySelective(updateRecord);
        
        LogHelper.test(uid + "返回H5中奖结果:" + prize);
        if (StringUtil.isNullOrEmpty(prize)) {
            prize = "NoPrize";
        }
        LogHelper.test(uid + "返回H5中奖结果处理空值:" + prize);
        
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("count", count);
        map.put("result", prize);
        return map;
    }
    
    
    /**
     *  奖品抽取
     * 
     * @param count 剩余次数
     * @return
     */
    public String getLotteryPrizeDaily(int count) {
        
        String prize = null;
        
        String rebateCoupon = "rebateCoupon"; // 一张奖励券
        String doubleCoupon = "doubleCoupon"; // 两张奖励券
        
        if (count == 1) {
            // 剩余最后一次必中
            long result = (1 + Math.round(Math.random() * (9)));
            if (result <= 7) {
                prize = rebateCoupon;
            } else {
                prize = doubleCoupon;
            }
        } else {
            long result = (1 + Math.round(Math.random() * (9)));
            if (result <= 3) {
                prize = rebateCoupon;
            } else if (result <= 5) {
                prize = doubleCoupon; 
            }
        }
        return prize;
    }
    
}