yujian
2019-04-08 1da17d215d48e3e3aa9e8d7a3ef526904764f408
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
package com.yeshi.fanli.service.impl.user;
 
import java.util.Date;
import java.util.HashMap;
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.UserLotteryRecord;
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
import com.yeshi.fanli.exception.user.UserLotteryRecordException;
import com.yeshi.fanli.exception.user.UserSystemCouponException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.order.CommonOrderCountService;
import com.yeshi.fanli.service.inter.user.UserLotteryRecordService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
 
@Service
public class UserLotteryRecordServiceImpl implements UserLotteryRecordService {
 
    @Resource
    private UserLotteryRecordMapper userLotteryRecordMapper;
    
    @Resource
    private ConfigService configService;
 
    @Resource
    private CommonOrderCountService commonOrderCountService;
    
    @Resource
    private UserSystemCouponService userSystemCouponService;
 
    
    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、新人抽奖
                boolean hasOrder = commonOrderCountService.hasRebateAndShareOrder(uid);
                // 新用户存在5次抽奖机会 (无订单:返利、分享订单)
                if (!hasOrder) {
                    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);
        }
        
        return record;
    }
    
    
    @Override
    public Map<String, Object> 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_newbies");
                    
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("count", count);
        map.put("rule", lotteryRule);
        
        return map;
    }
    
    
    @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,  "抽奖次数不足");
        } 
        
        int countPrize = 0;
        
        
        String prize = getLotteryPrizeDaily(countPrize, count);
        if (prize == null || prize.trim().length() == 0) {
            prize = "NoPrize";
        }
        
        try {
            // 插入券信息
            if ("rebateCoupon".equals(prize)) {
                String rebateCoupon = CouponTypeEnum.rebatePercentCoupon.name(); // 奖励券
                userSystemCouponService.insertUserCoupon(uid, rebateCoupon, UserSystemCoupon.SOURCE_NEWBIES);
            } else if ("doubleCoupon".equals(prize)) {
                String rebateCoupon = CouponTypeEnum.rebatePercentCoupon.name(); // 奖励券
                userSystemCouponService.insertUserCoupon(uid, rebateCoupon, UserSystemCoupon.SOURCE_NEWBIES);
                userSystemCouponService.insertUserCoupon(uid, rebateCoupon, UserSystemCoupon.SOURCE_NEWBIES);
            } 
        } 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);
        
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("count", count);
        map.put("result", prize);
        return map;
    }
    
    
    /**
     *  奖品抽取
     * 
     * @param countPrize   已抽次数
     * @param countUsed 券id
     * @return
     */
    public String getLotteryPrizeDaily(int countPrize, int count) {
        
        String prize = null;
        
        String rebateCoupon = "rebateCoupon"; // 一张奖励券
        String doubleCoupon = "doubleCoupon"; // 奖励券
        
        if (countPrize == 0 && 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 <= 2) {
                prize = rebateCoupon;
            } else if (result <= 5) {
                prize = doubleCoupon; 
            }
        }
        return prize;
    }
    
}