Authored by zhouxiang

10.28会员日

... ... @@ -14,3 +14,6 @@ controller/target/
*.class
other/
target/
*.name
queue/.idea/compiler.xml
queue/.idea/
... ...
package com.yoho.activity.common.bo;
import java.io.Serializable;
/**
* Created by zhouxiang on 2016/10/29.
*/
public class DrawPrizeBo {
private Integer prizeType;
private Integer prizeValue;
private Integer drawResult;
public DrawPrizeBo(Integer prizeType, Integer prizeValue, Integer drawResult) {
this.prizeType = prizeType;
this.prizeValue = prizeValue;
this.drawResult = drawResult;
}
public Integer getPrizeType() {
return prizeType;
}
public void setPrizeType(Integer prizeType) {
this.prizeType = prizeType;
}
public Integer getPrizeValue() {
return prizeValue;
}
public void setPrizeValue(Integer prizeValue) {
this.prizeValue = prizeValue;
}
public Integer getDrawResult() {
return drawResult;
}
public void setDrawResult(Integer drawResult) {
this.drawResult = drawResult;
}
@Override
public String toString() {
return "DrawPrizeBo{" +
"prizeType=" + prizeType +
", prizeValue=" + prizeValue +
", drawResult=" + drawResult +
'}';
}
}
... ...
... ... @@ -18,11 +18,11 @@ public interface IUserdayPrizeLogDAO {
int updateByPrimaryKey(UserdayPrizeLog record);
UserdayPrizeLog selectByUidForLeaveWords(Integer uid);
UserdayPrizeLog selectByUidForLeaveWords(@Param("uid") Integer uid,@Param("winningDate") int winningDate);
int insertLog(UserdayPrizeLog userdayPrizeLog);
List<UserdayPrizeLog> selectByUidAndPrizeType(@Param("uid") int uid, @Param("prizeType") int prizeType);
List<UserdayPrizeLog> selectByUidAndPrizeType(@Param("uid") int uid, @Param("drawType") int drawType,@Param("winningDate") int winningDate);
int selectNumByPrizeType(@Param("prizeType") int prize_type);
int selectNumByPrizeType(@Param("drawType") int draw_type);
}
\ No newline at end of file
... ...
... ... @@ -12,6 +12,9 @@ public class UserdayPrizeLog extends BaseModel {
private Integer uid;
//抽奖类型
private Integer drawType;
// 奖品类型:1-有货币,2-优惠券
private Integer prizeType;
... ... @@ -32,6 +35,23 @@ public class UserdayPrizeLog extends BaseModel {
this.createTime = createTime;
}
public UserdayPrizeLog(Integer uid, Integer drawType, Integer prizeType, Integer prizeValue, Integer winningDate, Integer createTime) {
this.uid = uid;
this.drawType = drawType;
this.prizeType = prizeType;
this.prizeValue = prizeValue;
this.winningDate = winningDate;
this.createTime = createTime;
}
public Integer getDrawType() {
return drawType;
}
public void setDrawType(Integer drawType) {
this.drawType = drawType;
}
public Integer getId() {
return id;
}
... ...
... ... @@ -5,12 +5,13 @@
<id column="id" property="id" jdbcType="INTEGER" />
<result column="uid" property="uid" jdbcType="INTEGER" />
<result column="prize_type" property="prizeType" jdbcType="INTEGER" />
<result column="draw_type" property="drawType" jdbcType="INTEGER" />
<result column="prize_value" property="prizeValue" jdbcType="INTEGER" />
<result column="winning_date" property="winningDate" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List">
id, uid, prize_type, prize_value, winning_date, create_time
id, uid, prize_type, prize_value, winning_date, create_time,draw_type
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select
... ... @@ -108,25 +109,26 @@
select
<include refid="Base_Column_List" />
from userday_prize_log
where uid = #{uid,jdbcType=INTEGER} and prize_type = 2 and winning_date = UNIX_TIMESTAMP(CURRENT_DATE()) limit 1
where uid = #{uid,jdbcType=INTEGER} and draw_type = 2 and winning_date = #{winningDate,jdbcType=INTEGER} limit 1
</select>
<select id="selectByUidAndPrizeType" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from userday_prize_log
where uid = #{uid,jdbcType=INTEGER} and prize_type = #{prizeType,jdbcType=INTEGER} and winning_date = UNIX_TIMESTAMP(CURRENT_DATE())
where uid = #{uid,jdbcType=INTEGER} and draw_type = #{drawType,jdbcType=INTEGER} and winning_date = #{winningDate,jdbcType=INTEGER}
ORDER BY create_time DESC
</select>
<select id="selectNumByPrizeType" resultType="java.lang.Integer">
select IFNULL(COUNT(DISTINCT uid), 0) from userday_prize_log where prize_type = #{prizeType,jdbcType=INTEGER}
select IFNULL(COUNT(DISTINCT uid), 0) from userday_prize_log where draw_type = #{drawType,jdbcType=INTEGER}
</select>
<insert id="insertLog" parameterType="com.yoho.userday.dal.model.UserdayPrizeLog">
insert into userday_prize_log (uid, prize_type,
prize_value, winning_date, create_time
prize_value, winning_date, create_time,draw_type
)
values (#{uid,jdbcType=INTEGER}, #{prizeType,jdbcType=INTEGER},
#{prizeValue,jdbcType=INTEGER}, UNIX_TIMESTAMP(CURRENT_DATE()), #{createTime,jdbcType=INTEGER}
#{prizeValue,jdbcType=INTEGER}, #{winningDate,jdbcType=INTEGER},
#{createTime,jdbcType=INTEGER}, #{drawType,jdbcType=INTEGER}
)
</insert>
</mapper>
\ No newline at end of file
... ...
package com.yoho.activity.service;
import com.yoho.activity.common.bo.DrawPrizeBo;
import com.yoho.userday.dal.model.UserdayPrizeLog;
import java.util.List;
... ... @@ -9,7 +10,7 @@ import java.util.List;
*/
public interface IUserdayDrawPrizeService {
int drawPrize(int uid) throws Exception;
DrawPrizeBo drawPrize(int uid) throws Exception;
boolean existsPrizeLog(List<UserdayPrizeLog> userdayPrizeLogList);
}
... ...
package com.yoho.activity.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.yoho.activity.common.bo.DrawPrizeBo;
import com.yoho.activity.common.helper.SendCouponHelper;
import com.yoho.activity.service.IUserdayDrawPrizeService;
import com.yoho.error.exception.ServiceException;
import com.yoho.userday.dal.model.UserdayPrizeLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* 会员日,抽奖
*/
@Service
public class UserdayDrawCouponOrCoinServiceImpl implements IUserdayDrawPrizeService {
private static final Logger log = LoggerFactory.getLogger(UserdayDrawCouponOrCoinServiceImpl.class);
@Autowired
private SendCouponHelper sendCouponHelper;
@Resource(name = "couponMap")
private Map<Integer,Integer> couponMap;
@Override
public DrawPrizeBo drawPrize(int uid) throws Exception {
log.info("enter drawPrize. param uid={}", uid);
// (1)校验uid
if (1 > uid) {
throw new ServiceException(401, "参数不合法");
}
//获取中奖返回对象
DrawPrizeBo drawPrizeBo = getDrawPrize();
log.info("drawPrize: draw prize complete. drawPrizeBo {}", drawPrizeBo);
// 发送有货币
if (1 == drawPrizeBo.getPrizeType().intValue()) {
Boolean isSuccess = sendCouponHelper.sendYOHOBi(uid, drawPrizeBo.getPrizeValue(), 6);
if(!isSuccess){
//设置为未中奖
drawPrizeBo.setDrawResult(0);
drawPrizeBo.setPrizeType(0);
drawPrizeBo.setPrizeValue(0);
}
return drawPrizeBo;
}
// 发送优惠券
if (2 == drawPrizeBo.getPrizeType().intValue()) {
String couponCode = sendCouponHelper.sendCoupon(drawPrizeBo.getPrizeValue().toString(), uid);
if(StringUtils.isEmpty(couponCode)){
//设置为未中奖
drawPrizeBo.setDrawResult(0);
drawPrizeBo.setPrizeType(0);
drawPrizeBo.setPrizeValue(0);
}
return drawPrizeBo;
}
// (4)返回
return drawPrizeBo;
}
@Override
public boolean existsPrizeLog(List<UserdayPrizeLog> userdayPrizeLogList) {
log.debug("enter existsPrizeLog.");
// 返回
if (null != userdayPrizeLogList && 0 < userdayPrizeLogList.size()) {
return true;
}
return false;
}
/**
* 抽奖
* @return
*/
private DrawPrizeBo getDrawPrize() {
int random = new Random().nextInt(100);
//100有货币(15%)
if (random > 84) {
return new DrawPrizeBo(1,100,1);
}
//200有货币(4%)
if(random > 80){
return new DrawPrizeBo(1,200,2);
}
//500有货币(1%)
if(random > 79){
return new DrawPrizeBo(1,500,3);
}
//10元优惠券(40%)
if(random > 39){
return new DrawPrizeBo(2,couponMap.get(10),4);
}
//299-40优惠券(35%)
if(random > 4){
return new DrawPrizeBo(2,couponMap.get(40),5);
}
//499-70优惠券(5%)
return new DrawPrizeBo(2,couponMap.get(70),6);
}
}
... ...
package com.yoho.activity.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.yoho.activity.common.bo.DrawPrizeBo;
import com.yoho.activity.common.helper.SendCouponHelper;
import com.yoho.activity.service.IUserdayDrawPrizeService;
import com.yoho.error.exception.ServiceException;
... ... @@ -29,7 +30,7 @@ public class UserdayDrawCouponServiceImpl implements IUserdayDrawPrizeService {
private String prizeConpon;
@Override
public int drawPrize(int uid) throws Exception {
public DrawPrizeBo drawPrize(int uid) throws Exception {
log.info("enter drawPrize. param uid={}", uid);
// (1)校验uid
... ... @@ -54,9 +55,7 @@ public class UserdayDrawCouponServiceImpl implements IUserdayDrawPrizeService {
}
// (4)返回
return couponId;
return new DrawPrizeBo(1,couponId,couponId);
}
@Override
... ...
package com.yoho.activity.service.impl;
import com.yoho.activity.common.bo.DrawPrizeBo;
import com.yoho.activity.common.helper.SendCouponHelper;
import com.yoho.activity.service.IUserdayDrawPrizeService;
import com.yoho.core.rest.client.ServiceCaller;
... ... @@ -29,7 +30,7 @@ public class UserdayDrawYohoCoinServiceImpl implements IUserdayDrawPrizeService
ServiceCaller serviceCaller;
@Override
public int drawPrize(int uid) throws Exception {
public DrawPrizeBo drawPrize(int uid) throws Exception {
log.info("enter drawPrize. param uid={}", uid);
// (1)校验uid
... ... @@ -70,7 +71,7 @@ public class UserdayDrawYohoCoinServiceImpl implements IUserdayDrawPrizeService
}
// (6)返回
return yohoCoinNum;
return new DrawPrizeBo(1,yohoCoinNum,yohoCoinNum);
}
@Override
... ...
package com.yoho.activity.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import javax.annotation.Resource;
import com.yoho.activity.common.bo.DrawPrizeBo;
import com.yoho.activity.common.redis.CacheKeyEnum;
import com.yoho.activity.common.redis.RedisValueCache;
import org.apache.commons.collections.CollectionUtils;
... ... @@ -14,7 +13,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.yoho.activity.common.utils.DateUtils;
import com.yoho.activity.service.IUserdayDrawPrizeService;
... ... @@ -28,7 +26,6 @@ import com.yoho.userday.dal.model.UserdayPrizeLog;
/**
* 会员日,中奖记录
*/
@Service
public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
private static final Logger log = LoggerFactory.getLogger(UserdayPrizeLogServiceImpl.class);
... ... @@ -43,6 +40,8 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
@Value("${userday.open.day}")
String userdayOpenDay;
private Map<Integer,String> errMsg;
@Override
public UserdayPrizeLog queryPrizeLogForLeaveWords(int uid) throws Exception {
log.info("enter queryPrizeLogForLeaveWords. param uid={}", uid);
... ... @@ -52,18 +51,18 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
throw new ServiceException(401, "参数不合法");
}
return userdayPrizeLogDAO.selectByUidForLeaveWords(uid);
return userdayPrizeLogDAO.selectByUidForLeaveWords(uid,getTodyTime());
}
@Override
public int addPrizeLog(int uid, int prize_type) throws Exception {
log.info("enter addPrizeLog. param uid={}, prize_type={}", uid, prize_type);
public int addPrizeLog(int uid, int draw_type) throws Exception {
log.info("enter addPrizeLog. param uid={}, prize_type={}", uid, draw_type);
// (1)校验uid, prize_type
if (1 > uid || 1 > prize_type) {
if (1 > uid || 1 > draw_type) {
throw new ServiceException(401, "参数不合法");
}
IUserdayDrawPrizeService userdayDrawPrizeService = userdayDrawPrizeServiceMap.get(prize_type);
IUserdayDrawPrizeService userdayDrawPrizeService = userdayDrawPrizeServiceMap.get(draw_type);
if (null == userdayDrawPrizeService) {
throw new ServiceException(401, "参数不合法");
}
... ... @@ -74,22 +73,23 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
throw new ServiceException(403, "非会员日,不允许抽奖");
}
// (3)是否参加过疯狂大转盘
if (2 == prize_type) {
List<UserdayPrizeLog> userdayPrizeLogList = userdayPrizeLogDAO.selectByUidAndPrizeType(uid, prize_type);
// (3)是否参加过疯狂大转盘 或 3学生专享优惠抽奖
if (2 == draw_type || 3 == draw_type) {
List<UserdayPrizeLog> userdayPrizeLogList = userdayPrizeLogDAO.selectByUidAndPrizeType(uid, draw_type,getTodyTime());
if (null != userdayPrizeLogList && 0 < userdayPrizeLogList.size()) {
throw new ServiceException(406, "您今天已经参加过疯狂大转盘");
throw new ServiceException(406, errMsg.get(draw_type));
}
}
// (4)获取奖品值
int prize_value = userdayDrawPrizeService.drawPrize(uid);
// (4)获取奖品返回对象
DrawPrizeBo drawPrize = userdayDrawPrizeService.drawPrize(uid);
// (5)保存抽奖日志
UserdayPrizeLog userdayPrizeLog = new UserdayPrizeLog(uid, prize_type, prize_value, DateUtil.currentTimeSeconds());
UserdayPrizeLog userdayPrizeLog = new UserdayPrizeLog(uid, draw_type,drawPrize.getPrizeType(),
drawPrize.getPrizeValue(),getTodyTime(), DateUtil.currentTimeSeconds());
userdayPrizeLogDAO.insertLog(userdayPrizeLog);
return prize_value;
return drawPrize.getDrawResult();
}
@Override
... ... @@ -106,7 +106,7 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
}
// (2)查询当天中奖日志
List<UserdayPrizeLog> userdayPrizeLogList = userdayPrizeLogDAO.selectByUidAndPrizeType(uid, prize_type);
List<UserdayPrizeLog> userdayPrizeLogList = userdayPrizeLogDAO.selectByUidAndPrizeType(uid, prize_type,getTodyTime());
// (3)返回
return userdayDrawPrizeService.existsPrizeLog(userdayPrizeLogList);
... ... @@ -122,7 +122,7 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
}
// (2)查询当天中奖日志
List<UserdayPrizeLog> userdayPrizeLogList = userdayPrizeLogDAO.selectByUidAndPrizeType(uid, prize_type);
List<UserdayPrizeLog> userdayPrizeLogList = userdayPrizeLogDAO.selectByUidAndPrizeType(uid, prize_type,getTodyTime());
if (CollectionUtils.isEmpty(userdayPrizeLogList)) {
return null;
}
... ... @@ -140,11 +140,24 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
}
@Override
public int queryPrizeLogNum(int prize_type) {
log.info("enter queryPrizeLogNum. param prize_type={}", prize_type);
int realNum = userdayPrizeLogDAO.selectNumByPrizeType(prize_type);
public int queryPrizeLogNum(int draw_type) {
log.info("enter queryPrizeLogNum. param draw_type={}", draw_type);
int realNum = userdayPrizeLogDAO.selectNumByPrizeType(draw_type);
Integer virtualNum = redisValueCache.get(CacheKeyEnum.USERDAY_DRAWPRIZENUM, "", Integer.class);
return realNum + (null == virtualNum ? 0 : virtualNum);
}
public void setErrMsg(Map<Integer, String> errMsg) {
this.errMsg = errMsg;
}
/**
* 获取当天时间
* @return
*/
private int getTodyTime(){
long current=System.currentTimeMillis();//当前时间毫秒数
long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
return (int) (zero/1000);
}
}
... ...
... ... @@ -47,8 +47,24 @@
value-type="com.yoho.activity.service.IUserdayDrawPrizeService">
<entry key="1" value-ref="userdayDrawYohoCoinServiceImpl"></entry>
<entry key="2" value-ref="userdayDrawCouponServiceImpl"></entry>
<entry key="3" value-ref="userdayDrawCouponOrCoinServiceImpl"></entry>
</util:map>
<util:map id="couponMap" key-type="java.lang.Integer" value-type="java.lang.Integer">
<entry key="10" value="14144"/>
<entry key="40" value="14146"/>
<entry key="70" value="14148"/>
</util:map>
<bean id="userdayPrizeLogServiceImpl" class="com.yoho.activity.service.impl.UserdayPrizeLogServiceImpl">
<property name="errMsg">
<map>
<entry key="2" value="您今天已经参加过疯狂大转盘"/>
<entry key="3" value="抱歉您的抽奖次数已用完,更多抽奖机会请关注下个会员日"/>
</map>
</property>
</bean>
<mvc:interceptors>
<ref bean="securityInterceptor" />
<ref bean="threadProfileInterceptor" />
... ...
... ... @@ -73,5 +73,5 @@ rabbit_password=yoho
userday.coupon={"20":"14146", "10":"14144"}
userday.open.day=18
userday.open.day=28
userday.drawPrizeNum.redisSwitch=true
\ No newline at end of file
... ...