Showing
2 changed files
with
90 additions
and
82 deletions
@@ -29,6 +29,8 @@ import com.yoho.activity.common.cache.Cacheable; | @@ -29,6 +29,8 @@ import com.yoho.activity.common.cache.Cacheable; | ||
29 | import com.yoho.activity.common.helper.SendCouponHelper; | 29 | import com.yoho.activity.common.helper.SendCouponHelper; |
30 | import com.yoho.activity.common.vo.LotteryRespData; | 30 | import com.yoho.activity.common.vo.LotteryRespData; |
31 | import com.yoho.activity.service.ILotteryService; | 31 | import com.yoho.activity.service.ILotteryService; |
32 | +import com.yoho.core.redis.YHRedisTemplate; | ||
33 | +import com.yoho.core.redis.YHValueOperations; | ||
32 | import com.yoho.error.exception.ServiceException; | 34 | import com.yoho.error.exception.ServiceException; |
33 | import com.yoho.lottery.dal.LotteryMapper; | 35 | import com.yoho.lottery.dal.LotteryMapper; |
34 | import com.yoho.lottery.dal.LotteryRecordMapper; | 36 | import com.yoho.lottery.dal.LotteryRecordMapper; |
@@ -74,7 +76,15 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA | @@ -74,7 +76,15 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA | ||
74 | @Autowired | 76 | @Autowired |
75 | private CacheAop cacheAop; | 77 | private CacheAop cacheAop; |
76 | 78 | ||
79 | + @Resource(name = "yhValueOperations") | ||
80 | + private YHValueOperations<String, String> yhValueOperations; | ||
81 | + | ||
82 | + @Resource(name = "yhRedisTemplate") | ||
83 | + private YHRedisTemplate<String, String> yhRedisTemplate; | ||
84 | + | ||
77 | private ILotteryService lotteryService; | 85 | private ILotteryService lotteryService; |
86 | + | ||
87 | + | ||
78 | 88 | ||
79 | @Override | 89 | @Override |
80 | @Cacheable(expireTime = CacheKeyAndTime.LOTTERY_INTO) | 90 | @Cacheable(expireTime = CacheKeyAndTime.LOTTERY_INTO) |
@@ -145,21 +155,11 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA | @@ -145,21 +155,11 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA | ||
145 | @Override | 155 | @Override |
146 | public LotteryRespData lucky(Integer lotteryId, Integer userId, String orderCode) { | 156 | public LotteryRespData lucky(Integer lotteryId, Integer userId, String orderCode) { |
147 | logger.info("用户{}的订单{}进行抽奖!", userId, orderCode); | 157 | logger.info("用户{}的订单{}进行抽奖!", userId, orderCode); |
148 | - // 已抽奖检查 | ||
149 | - LotteryRespData orderLotteryState = getOrderLotteryState(lotteryId, userId, null); | ||
150 | - Map<String, LotteryRecord> recordMap = orderLotteryState.getLotteryRecordMap(); | ||
151 | - if (recordMap.containsKey(orderCode)) { | ||
152 | - logger.info("用户{}的订单{}不符合抽奖条件!", userId, orderCode); | ||
153 | - return orderLotteryState; | ||
154 | - } | ||
155 | - | ||
156 | - // 本活动中奖次数 | ||
157 | - // int lotteryCount = lotteryRecordMapper.selectUserLotteryPrizeCount(lotteryId, userId, userId % 10); | ||
158 | - // 只能中一次 | ||
159 | - if (recordMap.size() > 0) { | ||
160 | - logger.info("用户{}的订单{}已经中过奖!", userId, orderCode); | ||
161 | - return savePrizeAndGetResp(lotteryId, userId, orderCode, null); | 158 | + boolean isIn = yhValueOperations.setIfAbsent(userId.toString(), "IN"); |
159 | + if(!isIn) { | ||
160 | + logger.info("用户{}的抽奖不能多个客户端一起抽奖!", userId); | ||
162 | } | 161 | } |
162 | + | ||
163 | 163 | ||
164 | // 查询抽奖统计信息 | 164 | // 查询抽奖统计信息 |
165 | /*List<LotteryPrizeInfo> lotteryPrizeInfo = lotteryRecordMapper.selectLotteryPrizeInfo(lotteryId, userId % 10); | 165 | /*List<LotteryPrizeInfo> lotteryPrizeInfo = lotteryRecordMapper.selectLotteryPrizeInfo(lotteryId, userId % 10); |
@@ -169,77 +169,86 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA | @@ -169,77 +169,86 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA | ||
169 | }*/ | 169 | }*/ |
170 | 170 | ||
171 | 171 | ||
172 | - // 查询奖品总数信息 | ||
173 | - LotteryRespData lotteryInfo = lotteryService.getLotteryInfo(lotteryId); | ||
174 | - List<Prize> prizeList = lotteryInfo.getPrizes(); | ||
175 | - Map<Integer, Integer> allMap = new HashMap<>(); | ||
176 | - for (Prize p : prizeList) { | ||
177 | - allMap.put(p.getId(), p.getTotal()); | ||
178 | - } | ||
179 | - /* | ||
180 | - // 奖品剩余计算 | ||
181 | - Map<Integer, Integer> remainMap = new HashMap<>(); | ||
182 | - for (Integer prizeId : allMap.keySet()) { | ||
183 | - Integer all = allMap.get(prizeId); | ||
184 | - Integer used = usedMap.get(prizeId); | ||
185 | - if (used == null) { | ||
186 | - used = 0; | 172 | + try { |
173 | + // 已抽奖检查 | ||
174 | + LotteryRespData orderLotteryState = getOrderLotteryState(lotteryId, userId, null); | ||
175 | + Map<String, LotteryRecord> recordMap = orderLotteryState.getLotteryRecordMap(); | ||
176 | + if (recordMap.containsKey(orderCode)) { | ||
177 | + logger.info("用户{}的订单{}不符合抽奖条件!", userId, orderCode); | ||
178 | + return orderLotteryState; | ||
187 | } | 179 | } |
188 | - remainMap.put(prizeId, all - used); | ||
189 | - }*/ | ||
190 | - | ||
191 | - // 随机抽取一个 | ||
192 | - Integer prizeId = selectOne(allMap); | ||
193 | - logger.info("用户{}的订单{}抽到奖项{}!", userId, orderCode, prizeId); | ||
194 | - | ||
195 | - // 未抽到 | ||
196 | - if (prizeId == null) { | ||
197 | - return savePrizeAndGetResp(lotteryId, userId, orderCode, null); | ||
198 | - } | ||
199 | - | ||
200 | - Prize prize = null; | ||
201 | - for(Prize p : prizeList) { | ||
202 | - if(p.getId().equals(prizeId)) { | ||
203 | - prize = p; | 180 | + // 本活动中奖次数 |
181 | + // int lotteryCount = lotteryRecordMapper.selectUserLotteryPrizeCount(lotteryId, userId, userId % 10); | ||
182 | + // 只能中一次 | ||
183 | + if (recordMap.size() > 0) { | ||
184 | + logger.info("用户{}的订单{}已经中过奖!", userId, orderCode); | ||
185 | + return savePrizeAndGetResp(lotteryId, userId, orderCode, null); | ||
204 | } | 186 | } |
205 | - } | ||
206 | - if (prize == null) { | ||
207 | - logger.warn("错误的奖品id:活动id{} 奖品id{}", lotteryId, prizeId); | ||
208 | - return savePrizeAndGetResp(lotteryId, userId, orderCode, null); | ||
209 | - } | ||
210 | - | ||
211 | - int update = 0; | ||
212 | - if (prize.getRemain() > 0) { | ||
213 | - // 更新剩余奖品 | ||
214 | - update = prizeMapper.subtractPrize(prizeId); | ||
215 | - } | ||
216 | - | ||
217 | - // 奖品已经抽完 | ||
218 | - if (update == 0) { | ||
219 | - logger.info("用户{}的订单{}抽到奖项{},该奖项剩余量为0!", userId, orderCode, prizeId); | ||
220 | - try { | ||
221 | - cacheAop.clearCache(LotteryServiceImpl.class.getMethod("getLotteryInfo", Integer.class), | ||
222 | - new Object[] { lotteryId }); | ||
223 | - } catch (Exception e) { | ||
224 | - logger.warn("清除LotteryServiceImpl.getLotteryInfo lotteryId={}错误", lotteryId); | 187 | + // 查询奖品总数信息 |
188 | + LotteryRespData lotteryInfo = lotteryService.getLotteryInfo(lotteryId); | ||
189 | + List<Prize> prizeList = lotteryInfo.getPrizes(); | ||
190 | + Map<Integer, Integer> allMap = new HashMap<>(); | ||
191 | + for (Prize p : prizeList) { | ||
192 | + allMap.put(p.getId(), p.getTotal()); | ||
225 | } | 193 | } |
226 | - return savePrizeAndGetResp(lotteryId, userId, orderCode, null); | ||
227 | - } | ||
228 | - | ||
229 | - LotteryRespData data = savePrizeAndGetResp(lotteryId, userId, orderCode, prizeId); | ||
230 | - /*Prize prize = prizeMapper.selectByPrimaryKey(prizeId); | ||
231 | - if (!hasRealPrize(prize)) { | ||
232 | - logger.info("用户{}的订单{}抽到奖项{},该奖项为nothing!", userId, orderCode, prizeId); | ||
233 | - data.setOrderLotteryCode(STATE_USE_NO_PRIZE); | ||
234 | - data.setOrderLotteryMessage(MSG_NO_PRIZE); | 194 | + /* |
195 | + // 奖品剩余计算 | ||
196 | + Map<Integer, Integer> remainMap = new HashMap<>(); | ||
197 | + for (Integer prizeId : allMap.keySet()) { | ||
198 | + Integer all = allMap.get(prizeId); | ||
199 | + Integer used = usedMap.get(prizeId); | ||
200 | + if (used == null) { | ||
201 | + used = 0; | ||
202 | + } | ||
203 | + remainMap.put(prizeId, all - used); | ||
204 | + }*/ | ||
205 | + // 随机抽取一个 | ||
206 | + Integer prizeId = selectOne(allMap); | ||
207 | + logger.info("用户{}的订单{}抽到奖项{}!", userId, orderCode, prizeId); | ||
208 | + // 未抽到 | ||
209 | + if (prizeId == null) { | ||
210 | + return savePrizeAndGetResp(lotteryId, userId, orderCode, null); | ||
211 | + } | ||
212 | + Prize prize = null; | ||
213 | + for (Prize p : prizeList) { | ||
214 | + if (p.getId().equals(prizeId)) { | ||
215 | + prize = p; | ||
216 | + } | ||
217 | + } | ||
218 | + if (prize == null) { | ||
219 | + logger.warn("错误的奖品id:活动id{} 奖品id{}", lotteryId, prizeId); | ||
220 | + return savePrizeAndGetResp(lotteryId, userId, orderCode, null); | ||
221 | + } | ||
222 | + int update = 0; | ||
223 | + if (prize.getRemain() > 0) { | ||
224 | + // 更新剩余奖品 | ||
225 | + update = prizeMapper.subtractPrize(prizeId); | ||
226 | + } | ||
227 | + // 奖品已经抽完 | ||
228 | + if (update == 0) { | ||
229 | + logger.info("用户{}的订单{}抽到奖项{},该奖项剩余量为0!", userId, orderCode, prizeId); | ||
230 | + try { | ||
231 | + cacheAop.clearCache(LotteryServiceImpl.class.getMethod("getLotteryInfo", Integer.class), | ||
232 | + new Object[] { lotteryId }); | ||
233 | + } catch (Exception e) { | ||
234 | + logger.warn("清除LotteryServiceImpl.getLotteryInfo lotteryId={}错误", lotteryId); | ||
235 | + } | ||
236 | + return savePrizeAndGetResp(lotteryId, userId, orderCode, null); | ||
237 | + } | ||
238 | + LotteryRespData data = savePrizeAndGetResp(lotteryId, userId, orderCode, prizeId); | ||
239 | + /*Prize prize = prizeMapper.selectByPrimaryKey(prizeId); | ||
240 | + if (!hasRealPrize(prize)) { | ||
241 | + logger.info("用户{}的订单{}抽到奖项{},该奖项为nothing!", userId, orderCode, prizeId); | ||
242 | + data.setOrderLotteryCode(STATE_USE_NO_PRIZE); | ||
243 | + data.setOrderLotteryMessage(MSG_NO_PRIZE); | ||
244 | + return data; | ||
245 | + }*/ | ||
246 | + data.setPrize(prize); | ||
247 | + givePrize(userId, orderCode, prize); | ||
235 | return data; | 248 | return data; |
236 | - }*/ | ||
237 | - | ||
238 | - data.setPrize(prize); | ||
239 | - | ||
240 | - givePrize(userId, orderCode, prize); | ||
241 | - | ||
242 | - return data; | 249 | + } finally { |
250 | + yhRedisTemplate.delete(userId.toString()); | ||
251 | + } | ||
243 | } | 252 | } |
244 | 253 | ||
245 | @Cacheable(expireTime = CacheKeyAndTime.LOTTERY_USER_LOTTERY) | 254 | @Cacheable(expireTime = CacheKeyAndTime.LOTTERY_USER_LOTTERY) |
-
Please register or login to post a comment