Authored by Lixiaodi

增加抽奖记录查询功能

... ... @@ -5,10 +5,16 @@ public interface CacheKeyAndTime {
/** 活动信息缓存时间. */
int LOTTERY_INTO = 300;
/** 中奖信息缓存时间. */
int LOTTERY_PRIZE_INTO = 300;
/** 用户抽奖记录. */
int LOTTERY_USER_LOTTERY = 3600;
/** 用户订单缓存. */
int LOTTERY_USER_ORDER = 7200;
/** 用户信息缓存. */
int LOTTERY_USER_INFO = 7200;
}
... ...
... ... @@ -4,6 +4,7 @@ import java.util.List;
import java.util.Map;
import com.yoho.lottery.dal.model.Lottery;
import com.yoho.lottery.dal.model.LotteryPrize;
import com.yoho.lottery.dal.model.LotteryRecord;
import com.yoho.lottery.dal.model.Prize;
... ... @@ -19,6 +20,9 @@ public class LotteryRespData {
private Lottery lottery;
private List<Prize> prizes;
// 中奖信息
private List<LotteryPrize> lotteryPrize;
// 订单抽奖状态
private Integer orderLotteryCode;
private String orderLotteryMessage;
... ...
... ... @@ -59,6 +59,35 @@ public class LotteryController {
}
/**
* 查询微信抽奖活动中奖信息.
*
* @param lotteryId 微信抽奖活动id
* @return 微信抽奖活动中奖信息
*/
@RequestMapping("/getLotteryPrizeInfo")
@ResponseBody
public ApiResponse getLotteryPrizeInfo(Integer lotteryId, Integer pageId, Integer pageCount) {
logger.info("Start getLotteryPrizeInfo with req {}", lotteryId);
if (pageId == null || pageId <= 0 || pageCount == null || pageCount <= 0) {
logger.warn("页码信息不合法,lotteryId={},pageId={},pageCount={}", lotteryId, pageId, pageCount);
throw new ServiceException(500, "页码信息不合法");
}
// 单页最大500
pageCount = Math.min(pageCount, 500);
checkLotteryId(lotteryId);
LotteryRespData respData = lotteryService.getLotteryPrizeInfo(lotteryId, (pageId - 1) * pageCount, pageCount);
// clear properties
clearUnuseProp(respData.getLottery(), respData.getPrizes());
logger.info("End getLotteryPrizeInfo with req {}", lotteryId);
return new ApiResponse(respData);
}
/**
* 查询用户订单列表(时间:15天 至 一个月,是否已经抽奖)
*
* @param uid 用户id
... ...
package com.yoho.lottery.dal;
import com.yoho.lottery.dal.model.LotteryPrize;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface LotteryPrizeMapper {
int deleteByPrimaryKey(Integer id);
int insert(LotteryPrize record);
LotteryPrize selectByPrimaryKey(Integer id);
List<LotteryPrize> selectAll();
int updateByPrimaryKey(LotteryPrize record);
List<LotteryPrize> selectPrize(@Param("lotteryId") Integer lotteryId, @Param("startIndex") int startIndex, @Param("length") int length);
}
\ No newline at end of file
... ...
package com.yoho.lottery.dal.model;
public class LotteryPrize {
private Integer id;
private Integer lotteryId;
private Integer userId;
private String userName;
private Integer prizeId;
private String prizeName;
private String prizeRemark;
private String orderCode;
private Integer createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getLotteryId() {
return lotteryId;
}
public void setLotteryId(Integer lotteryId) {
this.lotteryId = lotteryId;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getPrizeId() {
return prizeId;
}
public void setPrizeId(Integer prizeId) {
this.prizeId = prizeId;
}
public String getPrizeName() {
return prizeName;
}
public void setPrizeName(String prizeName) {
this.prizeName = prizeName;
}
public String getPrizeRemark() {
return prizeRemark;
}
public void setPrizeRemark(String prizeRemark) {
this.prizeRemark = prizeRemark;
}
public String getOrderCode() {
return orderCode;
}
public void setOrderCode(String orderCode) {
this.orderCode = orderCode;
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
}
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yoho.lottery.dal.LotteryPrizeMapper">
<resultMap id="BaseResultMap" type="com.yoho.lottery.dal.model.LotteryPrize">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="lottery_id" jdbcType="INTEGER" property="lotteryId" />
<result column="user_id" jdbcType="INTEGER" property="userId" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="prize_id" jdbcType="INTEGER" property="prizeId" />
<result column="prize_name" jdbcType="VARCHAR" property="prizeName" />
<result column="prize_remark" jdbcType="VARCHAR" property="prizeRemark" />
<result column="order_code" jdbcType="VARCHAR" property="orderCode" />
<result column="create_time" jdbcType="INTEGER" property="createTime" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from lottery_prize
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yoho.lottery.dal.model.LotteryPrize">
insert into lottery_prize (id, lottery_id, user_id,
user_name, prize_id, prize_name,
prize_remark, order_code, create_time
)
values (#{id,jdbcType=INTEGER}, #{lotteryId,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR}, #{prizeId,jdbcType=INTEGER}, #{prizeName,jdbcType=VARCHAR},
#{prizeRemark,jdbcType=VARCHAR}, #{orderCode,jdbcType=VARCHAR}, #{createTime,jdbcType=INTEGER}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.yoho.lottery.dal.model.LotteryPrize">
update lottery_prize
set lottery_id = #{lotteryId,jdbcType=INTEGER},
user_id = #{userId,jdbcType=INTEGER},
user_name = #{userName,jdbcType=VARCHAR},
prize_id = #{prizeId,jdbcType=INTEGER},
prize_name = #{prizeName,jdbcType=VARCHAR},
prize_remark = #{prizeRemark,jdbcType=VARCHAR},
order_code = #{orderCode,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select id, lottery_id, user_id, user_name, prize_id, prize_name, prize_remark, order_code,
create_time
from lottery_prize
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select id, lottery_id, user_id, user_name, prize_id, prize_name, prize_remark, order_code,
create_time
from lottery_prize
</select>
<select id="selectPrize" resultMap="BaseResultMap">
select user_name, prize_id, prize_name,prize_remark
from lottery_prize where lottery_id=#{lotteryId,jdbcType=INTEGER} order by id desc limit #{startIndex,jdbcType=INTEGER}, #{length,jdbcType=INTEGER}
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -7,6 +7,7 @@ import com.yoho.activity.common.vo.LotteryRespData;
import com.yoho.lottery.dal.model.LotteryRecord;
import com.yoho.lottery.dal.model.Prize;
import com.yoho.service.model.order.response.OrderInfoResponse;
import com.yoho.service.model.response.UserBaseRspBO;
/**
* 描述:微信抽奖接口
... ... @@ -63,5 +64,9 @@ public interface ILotteryService {
boolean hasRealPrize(Prize prize);
LotteryRecord[] getLotteryRecordByIds(List<Integer> ids, int shardFactor);
OrderInfoResponse getOrderInfo(Integer userId, String orderCode);
LotteryRespData getLotteryPrizeInfo(Integer lotteryId, int startIndex, int length);
UserBaseRspBO getUserInfo(Integer userId);
}
... ...
... ... @@ -38,10 +38,12 @@ import com.yoho.core.redis.YHValueOperations;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.error.exception.ServiceException;
import com.yoho.lottery.dal.LotteryMapper;
import com.yoho.lottery.dal.LotteryPrizeMapper;
import com.yoho.lottery.dal.LotteryRecordMapper;
import com.yoho.lottery.dal.PrizeMapper;
import com.yoho.lottery.dal.WeixinBindPrizeMapper;
import com.yoho.lottery.dal.model.Lottery;
import com.yoho.lottery.dal.model.LotteryPrize;
import com.yoho.lottery.dal.model.LotteryRecord;
import com.yoho.lottery.dal.model.Prize;
import com.yoho.lottery.dal.model.WeixinBindPrize;
... ... @@ -52,6 +54,8 @@ import com.yoho.service.model.order.response.OrderInfoResponse;
import com.yoho.service.model.order.response.OrderQueryForLotteryResponse;
import com.yoho.service.model.order.response.Orders;
import com.yoho.service.model.order.response.OrdersGoods;
import com.yoho.service.model.request.UserBaseReqBO;
import com.yoho.service.model.response.UserBaseRspBO;
@Service
public class LotteryServiceImpl implements ILotteryService , ApplicationContextAware {
... ... @@ -98,6 +102,9 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
@Resource
private WeixinBindPrizeMapper weixinBindPrizeMapper;
@Resource
private LotteryPrizeMapper lotteryPrizeMapper;
@Autowired
private SendCouponHelper sendCouponHelper;
... ... @@ -134,6 +141,18 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
data.setPrizes(prize);
return data;
}
@Override
@Cacheable(expireTime = CacheKeyAndTime.LOTTERY_PRIZE_INTO)
public LotteryRespData getLotteryPrizeInfo(Integer lotteryId, int startIndex, int length) {
LotteryRespData data = new LotteryRespData();
Lottery lottery = lotteryMapper.selectByPrimaryKey(lotteryId);
checkLottery(lotteryId, lottery);
List<LotteryPrize> prize = lotteryPrizeMapper.selectPrize(lotteryId, startIndex, length);
data.setLotteryPrize(prize);
return data;
}
@Override
public LotteryRespData getOrderLotteryState(Integer lotteryId, Integer userId, String orderCode) {
... ... @@ -198,7 +217,8 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
state.setOrderLotteryMessage(MSG_MULTI);
return state;
}
// 设置过期时间:4秒防止发生异常情况后永久阻塞
yhRedisTemplate.expire(lockKey, 4, TimeUnit.SECONDS);
// 查询抽奖统计信息
/*List<LotteryPrizeInfo> lotteryPrizeInfo = lotteryRecordMapper.selectLotteryPrizeInfo(lotteryId, userId % 10);
... ... @@ -277,6 +297,24 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
logger.warn("错误的奖品id:活动id{} 奖品id{}", lotteryId, prizeId);
return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
}
// 订单金额
BigDecimal orderMoney = getOrderMoney(userId, orderCode);
// 异常情况
if (orderMoney.compareTo(BigDecimal.ZERO) == 0) {
logger.warn("订单金额异常为0,设置未中奖!");
return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
}
// 金额大于300的不可以抽中面单奖
if (StringUtils.equals(prize.getPrizeType(), "orderyohocoin")) {
BigDecimal maxMoney = new BigDecimal("300");
if (orderMoney.compareTo(maxMoney) > 0) {
logger.warn("订单金额为{},大于300不可以中免单奖,设置未中奖!", orderMoney);
return savePrizeAndGetResp(lotteryId, userId, orderCode, null);
}
}
int update = 0;
if (prize.getRemain() > 0) {
// 更新剩余奖品
... ... @@ -350,6 +388,20 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
logger.info("用户{}的订单{}抽到奖项{},进行发奖!", userId, orderCode, prize);
executor.execute(() -> {
try {
UserBaseRspBO userInfo = lotteryService.getUserInfo(userId);
// 记录中奖统计记录
LotteryPrize bean = new LotteryPrize();
bean.setLotteryId(prize.getLotteryId());
bean.setUserId(userId);
bean.setUserName(userInfo.getNickname());
bean.setOrderCode(orderCode);
bean.setPrizeId(prize.getId());
bean.setPrizeName(prize.getName());
bean.setPrizeRemark(prize.getRemark());
bean.setCreateTime((int) (System.currentTimeMillis() / 1000));
lotteryPrizeMapper.insert(bean);
if ("coupons".equals(prize.getPrizeType())) {
sendCouponHelper.sendCoupon(prize.getPrizeValue(), userId);
}
... ... @@ -487,6 +539,20 @@ public class LotteryServiceImpl implements ILotteryService , ApplicationContextA
throw e;
}
}
@Override
@Cacheable(expireTime = CacheKeyAndTime.LOTTERY_USER_INFO)
public UserBaseRspBO getUserInfo(Integer userId) {
try {
UserBaseReqBO req = new UserBaseReqBO();
req.setUid(userId);
UserBaseRspBO rsp = serviceCaller.call("users.selectUserBaseInfo", req, UserBaseRspBO.class);
logger.info("获得用户信息:{}", rsp);
return rsp;
} catch (Exception e) {
logger.error("获取用户信息失败!uid={} orderCode={}! msg={}", userId, e.getMessage());
throw e;
}
}
private List<LotteryOrderVO> setLotteryOrderStatus(List<LotteryOrderVO> orderVOList, List<LotteryRecord> recordList){
for (LotteryOrderVO orderVO : orderVOList) {
... ...
... ... @@ -22,5 +22,6 @@ datasources:
- com.yoho.lottery.dal.WeixinOrderListMapper
- com.yoho.lottery.dal.LotteryRecordMapper
- com.yoho.lottery.dal.WeixinBindPrizeMapper
- com.yoho.lottery.dal.LotteryPrizeMapper
readOnlyInSlave: true
\ No newline at end of file
... ...