Authored by Lixiaodi

微信抽奖代码提交

... ... @@ -63,7 +63,18 @@ public class SendCouponHelper {
* @return
*/
public boolean sendYOHOBi(int uid, int num) {
YohoCoinRecordReq req = buildYohoCoinCostReq(uid, num);
return sendYOHOBi(uid, num, "0");
}
/**
* 发送有货币
*
* @param uid
* @param num
* @return
*/
public boolean sendYOHOBi(int uid, int num, String orderCode) {
YohoCoinRecordReq req = buildYohoCoinCostReq(uid, num, orderCode);
final String serviceName = "users.addRecord";
boolean result = false;
try {
... ... @@ -103,7 +114,7 @@ public class SendCouponHelper {
}
// 构造参数
private YohoCoinRecordReq buildYohoCoinCostReq(int uid, int num) {
private YohoCoinRecordReq buildYohoCoinCostReq(int uid, int num, String orderCode) {
YohoCoinRecordReq req = new YohoCoinRecordReq();
YohoCoinCostReqBO yohoCoinCostReq = new YohoCoinCostReqBO();
... ...
... ... @@ -89,12 +89,6 @@ public class LotteryController {
checkUserId(userId);
checkOrderCode(orderCode);
LotteryRespData checkData = lotteryService.getOrderLotteryState(lotteryId, userId, orderCode);
int checkCode = checkData.getCheckLotteryCode();
if (checkCode != 200) {
return new ApiResponse(checkCode, checkData.getCheckLotteryMessage());
}
LotteryRespData respData = lotteryService.lucky(lotteryId, userId, orderCode);
logger.info("End lucky with req {}", vo);
... ...
... ... @@ -18,9 +18,9 @@ public interface LotteryRecordMapper {
int updateByPrimaryKey(@Param("record") LotteryRecord record, @Param("shardFactor") int shardFactor);
List<LotteryRecord> selectOrderRecords(@Param("lotteryId") Integer lotteryId, @Param("userId") Integer userId,
String orderCode, @Param("shardFactor") int shardFactor);
@Param("orderCode")String orderCode, @Param("shardFactor") int shardFactor);
int selectUserLotteryPrizeCount(@Param("lotteryId") Integer lotteryId, @Param("userId") Integer userId,
Integer selectUserLotteryPrizeCount(@Param("lotteryId") Integer lotteryId, @Param("userId") Integer userId,
@Param("shardFactor") int shardFactor);
List<LotteryPrizeInfo> selectLotteryPrizeInfo(@Param("lotteryId") Integer lotteryId,
... ...
... ... @@ -51,8 +51,8 @@
and flag = #{orderCode,jdbcType=INTEGER}
</if>
</select>
<select id="selectUserLotteryPrizeCount" resultMap="BaseResultMap">
select count(*)
<select id="selectUserLotteryPrizeCount" resultType="java.lang.Integer">
select case when count(*) is null then 0 else count(*) end count
from lottery_record_${shardFactor}
where lottery_id = #{lotteryId,jdbcType=INTEGER}
and uid = #{userId,jdbcType=INTEGER} and prize_id is not null
... ...
... ... @@ -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);
... ...
... ... @@ -5,15 +5,6 @@ datasources:
- 192.168.102.219:3306
username: yh_test
password: 9nm0icOwt6bMHjMusIfMLw==
yh_lottery:
servers:
- 192.168.102.219:3306
- 192.168.102.219:3306
username: yh_test
password: 9nm0icOwt6bMHjMusIfMLw==
daos:
- com.yoho.lottery.dal.WeixinORderListMapper
yh_lottery:
servers:
... ...