...
|
...
|
@@ -4,6 +4,7 @@ |
|
|
package com.yoho.activity.service.impl;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Random;
|
...
|
...
|
@@ -34,7 +35,7 @@ import com.yoho.lottery.dal.model.Prize; |
|
|
|
|
|
@Service
|
|
|
public class LotteryServiceImpl implements ILotteryService {
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private WeixinOrderListMapper weixinOrderListMapper;
|
|
|
|
...
|
...
|
@@ -46,10 +47,10 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
private static final int STATE_USE_HAVA_PRIZE = 1;
|
|
|
/** 已抽奖:未中奖. */
|
|
|
private static final int STATE_USE_NO_PRIZE = 2;
|
|
|
|
|
|
|
|
|
private static final String MSG_NO_PRIZE = "差一点就中奖啦!";
|
|
|
private static final String MSG_HAVE_PRIZE = "恭喜您,中奖啦!";
|
|
|
|
|
|
|
|
|
private ThreadPoolExecutor executor = new ThreadPoolExecutor(12, 24, 0L, TimeUnit.MILLISECONDS,
|
|
|
new LinkedBlockingQueue<Runnable>());
|
|
|
|
...
|
...
|
@@ -58,12 +59,12 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
|
|
|
@Resource
|
|
|
private PrizeMapper prizeMapper;
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
private LotteryRecordMapper lotteryRecordMapper;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private SendCouponHelper sendCouponHelper;
|
|
|
private SendCouponHelper sendCouponHelper;
|
|
|
|
|
|
@Override
|
|
|
public LotteryRespData getLotteryInfo(Integer lotteryId) {
|
...
|
...
|
@@ -88,7 +89,8 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
checkLottery(lotteryId, lottery);
|
|
|
|
|
|
LotteryRespData data = new LotteryRespData();
|
|
|
List<LotteryRecord> records = lotteryRecordMapper.selectOrderRecords(lotteryId, userId, orderCode);
|
|
|
List<LotteryRecord> records = lotteryRecordMapper.selectOrderRecords(lotteryId, userId, orderCode,
|
|
|
lotteryId % 10);
|
|
|
// 批量查询:内部
|
|
|
if (StringUtils.isBlank(orderCode)) {
|
|
|
Map<String, LotteryRecord> lotteryRecordMap = new HashMap<>();
|
...
|
...
|
@@ -103,12 +105,22 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
data.setOrderLotteryMessage("参加抽奖");
|
|
|
} else {
|
|
|
LotteryRecord rec = records.get(0);
|
|
|
if (rec.getPrizeId() == null || rec.getPrizeId() <= 0) {
|
|
|
data.setOrderLotteryCode(STATE_USE_NO_PRIZE);
|
|
|
data.setOrderLotteryMessage("已抽奖");
|
|
|
boolean havePrize;
|
|
|
Prize prize = null;
|
|
|
if(rec.getPrizeId() == null || rec.getPrizeId() <= 0) {
|
|
|
havePrize = false;
|
|
|
} else {
|
|
|
prize = prizeMapper.selectByPrimaryKey(rec.getPrizeId());
|
|
|
havePrize = !hasRealPrize(prize);
|
|
|
}
|
|
|
if (havePrize) {
|
|
|
data.setOrderLotteryCode(STATE_USE_HAVA_PRIZE);
|
|
|
data.setOrderLotteryMessage("已中奖");
|
|
|
data.setPrize(prize);
|
|
|
clearPrize(prize);
|
|
|
} else {
|
|
|
data.setOrderLotteryCode(STATE_USE_NO_PRIZE);
|
|
|
data.setOrderLotteryMessage("已抽奖");
|
|
|
}
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -125,25 +137,27 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
}
|
|
|
|
|
|
// 本活动中奖次数
|
|
|
int lotteryCount = lotteryRecordMapper.selectUserLotteryPrizeCount(lotteryId, userId);
|
|
|
int lotteryCount = lotteryRecordMapper.selectUserLotteryPrizeCount(lotteryId, userId, lotteryId % 10);
|
|
|
// 只能中一次
|
|
|
if (lotteryCount > 0) {
|
|
|
return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
|
|
|
}
|
|
|
|
|
|
// 查询统计信息
|
|
|
List<LotteryPrizeInfo> lotteryPrizeInfo = lotteryRecordMapper.selectLotteryPrizeInfo(lotteryId);
|
|
|
// 查询抽奖统计信息
|
|
|
List<LotteryPrizeInfo> lotteryPrizeInfo = lotteryRecordMapper.selectLotteryPrizeInfo(lotteryId, lotteryId % 10);
|
|
|
Map<Integer, Integer> usedMap = new HashMap<>();
|
|
|
for (LotteryPrizeInfo pi : lotteryPrizeInfo) {
|
|
|
usedMap.put(pi.getPrizeId(), pi.getCount());
|
|
|
}
|
|
|
|
|
|
// 查询奖品总数信息
|
|
|
List<Prize> prizeList = prizeMapper.selectByLotteryId(lotteryId);
|
|
|
Map<Integer, Integer> allMap = new HashMap<>();
|
|
|
for (Prize p : prizeList) {
|
|
|
allMap.put(p.getId(), p.getTotal());
|
|
|
}
|
|
|
|
|
|
// 奖品剩余计算
|
|
|
Map<Integer, Integer> remainMap = new HashMap<>();
|
|
|
for (Integer prizeId : allMap.keySet()) {
|
|
|
Integer all = allMap.get(prizeId);
|
...
|
...
|
@@ -156,20 +170,21 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
|
|
|
// 随机抽取一个
|
|
|
Integer prizeId = selectOne(allMap);
|
|
|
|
|
|
|
|
|
// 奖品已经抽完
|
|
|
Integer remain = remainMap.get(prizeId);
|
|
|
if (remain <= 0) {
|
|
|
prizeId = null;
|
|
|
}
|
|
|
|
|
|
// 未抽到
|
|
|
if (prizeId == null) {
|
|
|
return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
|
|
|
}
|
|
|
|
|
|
LotteryRespData data = savePrizeAndGetResp(lotteryId, userId, orderCode, prizeId);
|
|
|
Prize prize = prizeMapper.selectByPrimaryKey(prizeId);
|
|
|
if ("nothing".equals(prize.getPrizeType())) {
|
|
|
if (!hasRealPrize(prize)) {
|
|
|
data.setOrderLotteryCode(STATE_USE_NO_PRIZE);
|
|
|
data.setOrderLotteryMessage(MSG_NO_PRIZE);
|
|
|
return data;
|
...
|
...
|
@@ -177,19 +192,19 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
|
|
|
data.setPrize(prize);
|
|
|
|
|
|
givePrize(userId, prize);
|
|
|
givePrize(userId, orderCode, prize);
|
|
|
|
|
|
clearPrize(prize);
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
private void givePrize(Integer userId, Prize prize) {
|
|
|
private void givePrize(Integer userId, String orderCode, Prize prize) {
|
|
|
executor.execute(() -> {
|
|
|
if ("coupons".equals(prize.getPrizeType())) {
|
|
|
sendCouponHelper.sendCoupon(prize.getPrizeValue(), userId);
|
|
|
}
|
|
|
if ("yohocoin".equals(prize.getPrizeType())) {
|
|
|
sendCouponHelper.sendYOHOBi(userId, Integer.parseInt(prize.getPrizeValue()));
|
|
|
sendCouponHelper.sendYOHOBi(userId, Integer.parseInt(prize.getPrizeValue()), orderCode);
|
|
|
}
|
|
|
if ("orderyohocoin".equals(prize.getPrizeType())) {
|
|
|
|
...
|
...
|
@@ -206,7 +221,7 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
record.setPrizeId(prizeId);
|
|
|
record.setStatus((byte) 0);
|
|
|
record.setModTime((int) (System.currentTimeMillis() / 1000));
|
|
|
lotteryRecordMapper.insert(record);
|
|
|
lotteryRecordMapper.insert(record, lotteryId % 10);
|
|
|
|
|
|
LotteryRespData state = new LotteryRespData();
|
|
|
if (prizeId == null) {
|
...
|
...
|
@@ -222,7 +237,7 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
@Override
|
|
|
public LotteryRespData getValidOrderList(Integer uid) {
|
|
|
List<Integer> weixinOrderCodeList = weixinOrderListMapper.selectOrderCodeByUid(uid);
|
|
|
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
...
|
...
|
@@ -232,9 +247,9 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
throw new ServiceException(500, "活动不存在:" + lotteryId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void clearUnuseProp(Lottery lottery, List<Prize> prize) {
|
|
|
|
|
|
|
|
|
if (lottery != null) {
|
|
|
lottery.setUserLimit(null);
|
|
|
lottery.setPersonTotal(null);
|
...
|
...
|
@@ -243,10 +258,19 @@ public class LotteryServiceImpl implements ILotteryService { |
|
|
lottery.setPid(null);
|
|
|
}
|
|
|
|
|
|
for (Prize p : prize) {
|
|
|
clearPrize(p);
|
|
|
for (Iterator<Prize> iterator = prize.iterator(); iterator.hasNext();) {
|
|
|
Prize p = iterator.next();
|
|
|
if(!hasRealPrize(p)) {
|
|
|
iterator.remove();
|
|
|
} else {
|
|
|
clearPrize(p);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private boolean hasRealPrize(Prize prize) {
|
|
|
return !StringUtils.equals("nothing", prize.getPrizeType());
|
|
|
}
|
|
|
|
|
|
private void clearPrize(Prize p) {
|
|
|
p.setId(null);
|
...
|
...
|
|