Authored by Lixiaodi

微信抽奖 代码提交

... ... @@ -60,7 +60,6 @@ public class RedisCache {
log.error("init activity sort error with activityId {}", act.getId(), e);
}
}
}
/**
... ...
... ... @@ -29,6 +29,8 @@ import com.yoho.activity.common.cache.Cacheable;
import com.yoho.activity.common.helper.SendCouponHelper;
import com.yoho.activity.common.vo.LotteryRespData;
import com.yoho.activity.service.ILotteryService;
import com.yoho.core.redis.YHRedisTemplate;
import com.yoho.core.redis.YHValueOperations;
import com.yoho.error.exception.ServiceException;
import com.yoho.lottery.dal.LotteryMapper;
import com.yoho.lottery.dal.LotteryRecordMapper;
... ... @@ -74,8 +76,16 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
@Autowired
private CacheAop cacheAop;
@Resource(name = "yhValueOperations")
private YHValueOperations<String, String> yhValueOperations;
@Resource(name = "yhRedisTemplate")
private YHRedisTemplate<String, String> yhRedisTemplate;
private ILotteryService lotteryService;
@Override
@Cacheable(expireTime = CacheKeyAndTime.LOTTERY_INTO)
public LotteryRespData getLotteryInfo(Integer lotteryId) {
... ... @@ -145,6 +155,21 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
@Override
public LotteryRespData lucky(Integer lotteryId, Integer userId, String orderCode) {
logger.info("用户{}的订单{}进行抽奖!", userId, orderCode);
boolean isIn = yhValueOperations.setIfAbsent(userId.toString(), "IN");
if(!isIn) {
logger.info("用户{}的抽奖不能多个客户端一起抽奖!", userId);
}
// 查询抽奖统计信息
/*List<LotteryPrizeInfo> lotteryPrizeInfo = lotteryRecordMapper.selectLotteryPrizeInfo(lotteryId, userId % 10);
Map<Integer, Integer> usedMap = new HashMap<>();
for (LotteryPrizeInfo pi : lotteryPrizeInfo) {
usedMap.put(pi.getPrizeId(), pi.getCount());
}*/
try {
// 已抽奖检查
LotteryRespData orderLotteryState = getOrderLotteryState(lotteryId, userId, null);
Map<String, LotteryRecord> recordMap = orderLotteryState.getLotteryRecordMap();
... ... @@ -152,7 +177,6 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
logger.info("用户{}的订单{}不符合抽奖条件!", userId, orderCode);
return orderLotteryState;
}
// 本活动中奖次数
// int lotteryCount = lotteryRecordMapper.selectUserLotteryPrizeCount(lotteryId, userId, userId % 10);
// 只能中一次
... ... @@ -160,15 +184,6 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
logger.info("用户{}的订单{}已经中过奖!", userId, orderCode);
return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
}
// 查询抽奖统计信息
/*List<LotteryPrizeInfo> lotteryPrizeInfo = lotteryRecordMapper.selectLotteryPrizeInfo(lotteryId, userId % 10);
Map<Integer, Integer> usedMap = new HashMap<>();
for (LotteryPrizeInfo pi : lotteryPrizeInfo) {
usedMap.put(pi.getPrizeId(), pi.getCount());
}*/
// 查询奖品总数信息
LotteryRespData lotteryInfo = lotteryService.getLotteryInfo(lotteryId);
List<Prize> prizeList = lotteryInfo.getPrizes();
... ... @@ -187,19 +202,16 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
}
remainMap.put(prizeId, all - used);
}*/
// 随机抽取一个
Integer prizeId = selectOne(allMap);
logger.info("用户{}的订单{}抽到奖项{}!", userId, orderCode, prizeId);
// 未抽到
if (prizeId == null) {
return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
}
Prize prize = null;
for(Prize p : prizeList) {
if(p.getId().equals(prizeId)) {
for (Prize p : prizeList) {
if (p.getId().equals(prizeId)) {
prize = p;
}
}
... ... @@ -207,13 +219,11 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
logger.warn("错误的奖品id:活动id{} 奖品id{}", lotteryId, prizeId);
return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
}
int update = 0;
if (prize.getRemain() > 0) {
// 更新剩余奖品
update = prizeMapper.subtractPrize(prizeId);
}
// 奖品已经抽完
if (update == 0) {
logger.info("用户{}的订单{}抽到奖项{},该奖项剩余量为0!", userId, orderCode, prizeId);
... ... @@ -225,7 +235,6 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
}
return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
}
LotteryRespData data = savePrizeAndGetResp(lotteryId, userId, orderCode, prizeId);
/*Prize prize = prizeMapper.selectByPrimaryKey(prizeId);
if (!hasRealPrize(prize)) {
... ... @@ -234,12 +243,12 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
data.setOrderLotteryMessage(MSG_NO_PRIZE);
return data;
}*/
data.setPrize(prize);
givePrize(userId, orderCode, prize);
return data;
} finally {
yhRedisTemplate.delete(userId.toString());
}
}
@Cacheable(expireTime = CacheKeyAndTime.LOTTERY_USER_LOTTERY)
... ...