Authored by Lixiaodi

增加抽奖记录查询功能

@@ -5,10 +5,16 @@ public interface CacheKeyAndTime { @@ -5,10 +5,16 @@ public interface CacheKeyAndTime {
5 /** 活动信息缓存时间. */ 5 /** 活动信息缓存时间. */
6 int LOTTERY_INTO = 300; 6 int LOTTERY_INTO = 300;
7 7
  8 + /** 中奖信息缓存时间. */
  9 + int LOTTERY_PRIZE_INTO = 300;
  10 +
8 /** 用户抽奖记录. */ 11 /** 用户抽奖记录. */
9 int LOTTERY_USER_LOTTERY = 3600; 12 int LOTTERY_USER_LOTTERY = 3600;
10 13
11 /** 用户订单缓存. */ 14 /** 用户订单缓存. */
12 int LOTTERY_USER_ORDER = 7200; 15 int LOTTERY_USER_ORDER = 7200;
13 16
  17 + /** 用户信息缓存. */
  18 + int LOTTERY_USER_INFO = 7200;
  19 +
14 } 20 }
@@ -4,6 +4,7 @@ import java.util.List; @@ -4,6 +4,7 @@ import java.util.List;
4 import java.util.Map; 4 import java.util.Map;
5 5
6 import com.yoho.lottery.dal.model.Lottery; 6 import com.yoho.lottery.dal.model.Lottery;
  7 +import com.yoho.lottery.dal.model.LotteryPrize;
7 import com.yoho.lottery.dal.model.LotteryRecord; 8 import com.yoho.lottery.dal.model.LotteryRecord;
8 import com.yoho.lottery.dal.model.Prize; 9 import com.yoho.lottery.dal.model.Prize;
9 10
@@ -19,6 +20,9 @@ public class LotteryRespData { @@ -19,6 +20,9 @@ public class LotteryRespData {
19 private Lottery lottery; 20 private Lottery lottery;
20 private List<Prize> prizes; 21 private List<Prize> prizes;
21 22
  23 + // 中奖信息
  24 + private List<LotteryPrize> lotteryPrize;
  25 +
22 // 订单抽奖状态 26 // 订单抽奖状态
23 private Integer orderLotteryCode; 27 private Integer orderLotteryCode;
24 private String orderLotteryMessage; 28 private String orderLotteryMessage;
@@ -59,6 +59,35 @@ public class LotteryController { @@ -59,6 +59,35 @@ public class LotteryController {
59 } 59 }
60 60
61 /** 61 /**
  62 + * 查询微信抽奖活动中奖信息.
  63 + *
  64 + * @param lotteryId 微信抽奖活动id
  65 + * @return 微信抽奖活动中奖信息
  66 + */
  67 + @RequestMapping("/getLotteryPrizeInfo")
  68 + @ResponseBody
  69 + public ApiResponse getLotteryPrizeInfo(Integer lotteryId, Integer pageId, Integer pageCount) {
  70 + logger.info("Start getLotteryPrizeInfo with req {}", lotteryId);
  71 +
  72 + if (pageId == null || pageId <= 0 || pageCount == null || pageCount <= 0) {
  73 + logger.warn("页码信息不合法,lotteryId={},pageId={},pageCount={}", lotteryId, pageId, pageCount);
  74 + throw new ServiceException(500, "页码信息不合法");
  75 + }
  76 + // 单页最大500
  77 + pageCount = Math.min(pageCount, 500);
  78 +
  79 + checkLotteryId(lotteryId);
  80 +
  81 + LotteryRespData respData = lotteryService.getLotteryPrizeInfo(lotteryId, (pageId - 1) * pageCount, pageCount);
  82 +
  83 + // clear properties
  84 + clearUnuseProp(respData.getLottery(), respData.getPrizes());
  85 +
  86 + logger.info("End getLotteryPrizeInfo with req {}", lotteryId);
  87 + return new ApiResponse(respData);
  88 + }
  89 +
  90 + /**
62 * 查询用户订单列表(时间:15天 至 一个月,是否已经抽奖) 91 * 查询用户订单列表(时间:15天 至 一个月,是否已经抽奖)
63 * 92 *
64 * @param uid 用户id 93 * @param uid 用户id
  1 +package com.yoho.lottery.dal;
  2 +
  3 +import com.yoho.lottery.dal.model.LotteryPrize;
  4 +import java.util.List;
  5 +
  6 +import org.apache.ibatis.annotations.Param;
  7 +
  8 +public interface LotteryPrizeMapper {
  9 + int deleteByPrimaryKey(Integer id);
  10 +
  11 + int insert(LotteryPrize record);
  12 +
  13 + LotteryPrize selectByPrimaryKey(Integer id);
  14 +
  15 + List<LotteryPrize> selectAll();
  16 +
  17 + int updateByPrimaryKey(LotteryPrize record);
  18 +
  19 + List<LotteryPrize> selectPrize(@Param("lotteryId") Integer lotteryId, @Param("startIndex") int startIndex, @Param("length") int length);
  20 +}
  1 +package com.yoho.lottery.dal.model;
  2 +
  3 +public class LotteryPrize {
  4 + private Integer id;
  5 +
  6 + private Integer lotteryId;
  7 +
  8 + private Integer userId;
  9 +
  10 + private String userName;
  11 +
  12 + private Integer prizeId;
  13 +
  14 + private String prizeName;
  15 +
  16 + private String prizeRemark;
  17 +
  18 + private String orderCode;
  19 +
  20 + private Integer createTime;
  21 +
  22 + public Integer getId() {
  23 + return id;
  24 + }
  25 +
  26 + public void setId(Integer id) {
  27 + this.id = id;
  28 + }
  29 +
  30 + public Integer getLotteryId() {
  31 + return lotteryId;
  32 + }
  33 +
  34 + public void setLotteryId(Integer lotteryId) {
  35 + this.lotteryId = lotteryId;
  36 + }
  37 +
  38 + public Integer getUserId() {
  39 + return userId;
  40 + }
  41 +
  42 + public void setUserId(Integer userId) {
  43 + this.userId = userId;
  44 + }
  45 +
  46 + public String getUserName() {
  47 + return userName;
  48 + }
  49 +
  50 + public void setUserName(String userName) {
  51 + this.userName = userName;
  52 + }
  53 +
  54 + public Integer getPrizeId() {
  55 + return prizeId;
  56 + }
  57 +
  58 + public void setPrizeId(Integer prizeId) {
  59 + this.prizeId = prizeId;
  60 + }
  61 +
  62 + public String getPrizeName() {
  63 + return prizeName;
  64 + }
  65 +
  66 + public void setPrizeName(String prizeName) {
  67 + this.prizeName = prizeName;
  68 + }
  69 +
  70 + public String getPrizeRemark() {
  71 + return prizeRemark;
  72 + }
  73 +
  74 + public void setPrizeRemark(String prizeRemark) {
  75 + this.prizeRemark = prizeRemark;
  76 + }
  77 +
  78 + public String getOrderCode() {
  79 + return orderCode;
  80 + }
  81 +
  82 + public void setOrderCode(String orderCode) {
  83 + this.orderCode = orderCode;
  84 + }
  85 +
  86 + public Integer getCreateTime() {
  87 + return createTime;
  88 + }
  89 +
  90 + public void setCreateTime(Integer createTime) {
  91 + this.createTime = createTime;
  92 + }
  93 +}
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3 +<mapper namespace="com.yoho.lottery.dal.LotteryPrizeMapper">
  4 + <resultMap id="BaseResultMap" type="com.yoho.lottery.dal.model.LotteryPrize">
  5 + <id column="id" jdbcType="INTEGER" property="id" />
  6 + <result column="lottery_id" jdbcType="INTEGER" property="lotteryId" />
  7 + <result column="user_id" jdbcType="INTEGER" property="userId" />
  8 + <result column="user_name" jdbcType="VARCHAR" property="userName" />
  9 + <result column="prize_id" jdbcType="INTEGER" property="prizeId" />
  10 + <result column="prize_name" jdbcType="VARCHAR" property="prizeName" />
  11 + <result column="prize_remark" jdbcType="VARCHAR" property="prizeRemark" />
  12 + <result column="order_code" jdbcType="VARCHAR" property="orderCode" />
  13 + <result column="create_time" jdbcType="INTEGER" property="createTime" />
  14 + </resultMap>
  15 + <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  16 + delete from lottery_prize
  17 + where id = #{id,jdbcType=INTEGER}
  18 + </delete>
  19 + <insert id="insert" parameterType="com.yoho.lottery.dal.model.LotteryPrize">
  20 + insert into lottery_prize (id, lottery_id, user_id,
  21 + user_name, prize_id, prize_name,
  22 + prize_remark, order_code, create_time
  23 + )
  24 + values (#{id,jdbcType=INTEGER}, #{lotteryId,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER},
  25 + #{userName,jdbcType=VARCHAR}, #{prizeId,jdbcType=INTEGER}, #{prizeName,jdbcType=VARCHAR},
  26 + #{prizeRemark,jdbcType=VARCHAR}, #{orderCode,jdbcType=VARCHAR}, #{createTime,jdbcType=INTEGER}
  27 + )
  28 + </insert>
  29 + <update id="updateByPrimaryKey" parameterType="com.yoho.lottery.dal.model.LotteryPrize">
  30 + update lottery_prize
  31 + set lottery_id = #{lotteryId,jdbcType=INTEGER},
  32 + user_id = #{userId,jdbcType=INTEGER},
  33 + user_name = #{userName,jdbcType=VARCHAR},
  34 + prize_id = #{prizeId,jdbcType=INTEGER},
  35 + prize_name = #{prizeName,jdbcType=VARCHAR},
  36 + prize_remark = #{prizeRemark,jdbcType=VARCHAR},
  37 + order_code = #{orderCode,jdbcType=VARCHAR},
  38 + create_time = #{createTime,jdbcType=INTEGER}
  39 + where id = #{id,jdbcType=INTEGER}
  40 + </update>
  41 + <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  42 + select id, lottery_id, user_id, user_name, prize_id, prize_name, prize_remark, order_code,
  43 + create_time
  44 + from lottery_prize
  45 + where id = #{id,jdbcType=INTEGER}
  46 + </select>
  47 + <select id="selectAll" resultMap="BaseResultMap">
  48 + select id, lottery_id, user_id, user_name, prize_id, prize_name, prize_remark, order_code,
  49 + create_time
  50 + from lottery_prize
  51 + </select>
  52 + <select id="selectPrize" resultMap="BaseResultMap">
  53 + select user_name, prize_id, prize_name,prize_remark
  54 + from lottery_prize where lottery_id=#{lotteryId,jdbcType=INTEGER} order by id desc limit #{startIndex,jdbcType=INTEGER}, #{length,jdbcType=INTEGER}
  55 + </select>
  56 +
  57 +</mapper>
@@ -7,6 +7,7 @@ import com.yoho.activity.common.vo.LotteryRespData; @@ -7,6 +7,7 @@ import com.yoho.activity.common.vo.LotteryRespData;
7 import com.yoho.lottery.dal.model.LotteryRecord; 7 import com.yoho.lottery.dal.model.LotteryRecord;
8 import com.yoho.lottery.dal.model.Prize; 8 import com.yoho.lottery.dal.model.Prize;
9 import com.yoho.service.model.order.response.OrderInfoResponse; 9 import com.yoho.service.model.order.response.OrderInfoResponse;
  10 +import com.yoho.service.model.response.UserBaseRspBO;
10 11
11 /** 12 /**
12 * 描述:微信抽奖接口 13 * 描述:微信抽奖接口
@@ -64,4 +65,8 @@ public interface ILotteryService { @@ -64,4 +65,8 @@ public interface ILotteryService {
64 LotteryRecord[] getLotteryRecordByIds(List<Integer> ids, int shardFactor); 65 LotteryRecord[] getLotteryRecordByIds(List<Integer> ids, int shardFactor);
65 OrderInfoResponse getOrderInfo(Integer userId, String orderCode); 66 OrderInfoResponse getOrderInfo(Integer userId, String orderCode);
66 67
  68 + LotteryRespData getLotteryPrizeInfo(Integer lotteryId, int startIndex, int length);
  69 +
  70 + UserBaseRspBO getUserInfo(Integer userId);
  71 +
67 } 72 }
@@ -38,10 +38,12 @@ import com.yoho.core.redis.YHValueOperations; @@ -38,10 +38,12 @@ import com.yoho.core.redis.YHValueOperations;
38 import com.yoho.core.rest.client.ServiceCaller; 38 import com.yoho.core.rest.client.ServiceCaller;
39 import com.yoho.error.exception.ServiceException; 39 import com.yoho.error.exception.ServiceException;
40 import com.yoho.lottery.dal.LotteryMapper; 40 import com.yoho.lottery.dal.LotteryMapper;
  41 +import com.yoho.lottery.dal.LotteryPrizeMapper;
41 import com.yoho.lottery.dal.LotteryRecordMapper; 42 import com.yoho.lottery.dal.LotteryRecordMapper;
42 import com.yoho.lottery.dal.PrizeMapper; 43 import com.yoho.lottery.dal.PrizeMapper;
43 import com.yoho.lottery.dal.WeixinBindPrizeMapper; 44 import com.yoho.lottery.dal.WeixinBindPrizeMapper;
44 import com.yoho.lottery.dal.model.Lottery; 45 import com.yoho.lottery.dal.model.Lottery;
  46 +import com.yoho.lottery.dal.model.LotteryPrize;
45 import com.yoho.lottery.dal.model.LotteryRecord; 47 import com.yoho.lottery.dal.model.LotteryRecord;
46 import com.yoho.lottery.dal.model.Prize; 48 import com.yoho.lottery.dal.model.Prize;
47 import com.yoho.lottery.dal.model.WeixinBindPrize; 49 import com.yoho.lottery.dal.model.WeixinBindPrize;
@@ -52,6 +54,8 @@ import com.yoho.service.model.order.response.OrderInfoResponse; @@ -52,6 +54,8 @@ import com.yoho.service.model.order.response.OrderInfoResponse;
52 import com.yoho.service.model.order.response.OrderQueryForLotteryResponse; 54 import com.yoho.service.model.order.response.OrderQueryForLotteryResponse;
53 import com.yoho.service.model.order.response.Orders; 55 import com.yoho.service.model.order.response.Orders;
54 import com.yoho.service.model.order.response.OrdersGoods; 56 import com.yoho.service.model.order.response.OrdersGoods;
  57 +import com.yoho.service.model.request.UserBaseReqBO;
  58 +import com.yoho.service.model.response.UserBaseRspBO;
55 59
56 @Service 60 @Service
57 public class LotteryServiceImpl implements ILotteryService , ApplicationContextAware { 61 public class LotteryServiceImpl implements ILotteryService , ApplicationContextAware {
@@ -99,6 +103,9 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA @@ -99,6 +103,9 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
99 @Resource 103 @Resource
100 private WeixinBindPrizeMapper weixinBindPrizeMapper; 104 private WeixinBindPrizeMapper weixinBindPrizeMapper;
101 105
  106 + @Resource
  107 + private LotteryPrizeMapper lotteryPrizeMapper;
  108 +
102 @Autowired 109 @Autowired
103 private SendCouponHelper sendCouponHelper; 110 private SendCouponHelper sendCouponHelper;
104 111
@@ -136,6 +143,18 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA @@ -136,6 +143,18 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
136 } 143 }
137 144
138 @Override 145 @Override
  146 + @Cacheable(expireTime = CacheKeyAndTime.LOTTERY_PRIZE_INTO)
  147 + public LotteryRespData getLotteryPrizeInfo(Integer lotteryId, int startIndex, int length) {
  148 +
  149 + LotteryRespData data = new LotteryRespData();
  150 + Lottery lottery = lotteryMapper.selectByPrimaryKey(lotteryId);
  151 + checkLottery(lotteryId, lottery);
  152 + List<LotteryPrize> prize = lotteryPrizeMapper.selectPrize(lotteryId, startIndex, length);
  153 + data.setLotteryPrize(prize);
  154 + return data;
  155 + }
  156 +
  157 + @Override
139 public LotteryRespData getOrderLotteryState(Integer lotteryId, Integer userId, String orderCode) { 158 public LotteryRespData getOrderLotteryState(Integer lotteryId, Integer userId, String orderCode) {
140 LotteryRespData lotteryInfo = lotteryService.getLotteryInfo(lotteryId); 159 LotteryRespData lotteryInfo = lotteryService.getLotteryInfo(lotteryId);
141 160
@@ -198,7 +217,8 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA @@ -198,7 +217,8 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
198 state.setOrderLotteryMessage(MSG_MULTI); 217 state.setOrderLotteryMessage(MSG_MULTI);
199 return state; 218 return state;
200 } 219 }
201 - 220 + // 设置过期时间:4秒防止发生异常情况后永久阻塞
  221 + yhRedisTemplate.expire(lockKey, 4, TimeUnit.SECONDS);
202 222
203 // 查询抽奖统计信息 223 // 查询抽奖统计信息
204 /*List<LotteryPrizeInfo> lotteryPrizeInfo = lotteryRecordMapper.selectLotteryPrizeInfo(lotteryId, userId % 10); 224 /*List<LotteryPrizeInfo> lotteryPrizeInfo = lotteryRecordMapper.selectLotteryPrizeInfo(lotteryId, userId % 10);
@@ -277,6 +297,24 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA @@ -277,6 +297,24 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
277 logger.warn("错误的奖品id:活动id{} 奖品id{}", lotteryId, prizeId); 297 logger.warn("错误的奖品id:活动id{} 奖品id{}", lotteryId, prizeId);
278 return savePrizeAndGetResp(lotteryId, userId, orderCode, null); 298 return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
279 } 299 }
  300 +
  301 + // 订单金额
  302 + BigDecimal orderMoney = getOrderMoney(userId, orderCode);
  303 + // 异常情况
  304 + if (orderMoney.compareTo(BigDecimal.ZERO) == 0) {
  305 + logger.warn("订单金额异常为0,设置未中奖!");
  306 + return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
  307 + }
  308 +
  309 + // 金额大于300的不可以抽中面单奖
  310 + if (StringUtils.equals(prize.getPrizeType(), "orderyohocoin")) {
  311 + BigDecimal maxMoney = new BigDecimal("300");
  312 + if (orderMoney.compareTo(maxMoney) > 0) {
  313 + logger.warn("订单金额为{},大于300不可以中免单奖,设置未中奖!", orderMoney);
  314 + return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
  315 + }
  316 + }
  317 +
280 int update = 0; 318 int update = 0;
281 if (prize.getRemain() > 0) { 319 if (prize.getRemain() > 0) {
282 // 更新剩余奖品 320 // 更新剩余奖品
@@ -350,6 +388,20 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA @@ -350,6 +388,20 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
350 logger.info("用户{}的订单{}抽到奖项{},进行发奖!", userId, orderCode, prize); 388 logger.info("用户{}的订单{}抽到奖项{},进行发奖!", userId, orderCode, prize);
351 executor.execute(() -> { 389 executor.execute(() -> {
352 try { 390 try {
  391 + UserBaseRspBO userInfo = lotteryService.getUserInfo(userId);
  392 +
  393 + // 记录中奖统计记录
  394 + LotteryPrize bean = new LotteryPrize();
  395 + bean.setLotteryId(prize.getLotteryId());
  396 + bean.setUserId(userId);
  397 + bean.setUserName(userInfo.getNickname());
  398 + bean.setOrderCode(orderCode);
  399 + bean.setPrizeId(prize.getId());
  400 + bean.setPrizeName(prize.getName());
  401 + bean.setPrizeRemark(prize.getRemark());
  402 + bean.setCreateTime((int) (System.currentTimeMillis() / 1000));
  403 + lotteryPrizeMapper.insert(bean);
  404 +
353 if ("coupons".equals(prize.getPrizeType())) { 405 if ("coupons".equals(prize.getPrizeType())) {
354 sendCouponHelper.sendCoupon(prize.getPrizeValue(), userId); 406 sendCouponHelper.sendCoupon(prize.getPrizeValue(), userId);
355 } 407 }
@@ -487,6 +539,20 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA @@ -487,6 +539,20 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
487 throw e; 539 throw e;
488 } 540 }
489 } 541 }
  542 + @Override
  543 + @Cacheable(expireTime = CacheKeyAndTime.LOTTERY_USER_INFO)
  544 + public UserBaseRspBO getUserInfo(Integer userId) {
  545 + try {
  546 + UserBaseReqBO req = new UserBaseReqBO();
  547 + req.setUid(userId);
  548 + UserBaseRspBO rsp = serviceCaller.call("users.selectUserBaseInfo", req, UserBaseRspBO.class);
  549 + logger.info("获得用户信息:{}", rsp);
  550 + return rsp;
  551 + } catch (Exception e) {
  552 + logger.error("获取用户信息失败!uid={} orderCode={}! msg={}", userId, e.getMessage());
  553 + throw e;
  554 + }
  555 + }
490 556
491 private List<LotteryOrderVO> setLotteryOrderStatus(List<LotteryOrderVO> orderVOList, List<LotteryRecord> recordList){ 557 private List<LotteryOrderVO> setLotteryOrderStatus(List<LotteryOrderVO> orderVOList, List<LotteryRecord> recordList){
492 for (LotteryOrderVO orderVO : orderVOList) { 558 for (LotteryOrderVO orderVO : orderVOList) {
@@ -22,5 +22,6 @@ datasources: @@ -22,5 +22,6 @@ datasources:
22 - com.yoho.lottery.dal.WeixinOrderListMapper 22 - com.yoho.lottery.dal.WeixinOrderListMapper
23 - com.yoho.lottery.dal.LotteryRecordMapper 23 - com.yoho.lottery.dal.LotteryRecordMapper
24 - com.yoho.lottery.dal.WeixinBindPrizeMapper 24 - com.yoho.lottery.dal.WeixinBindPrizeMapper
  25 + - com.yoho.lottery.dal.LotteryPrizeMapper
25 26
26 readOnlyInSlave: true 27 readOnlyInSlave: true