Authored by bblu

增加查询参与抽奖人数接口

... ... @@ -16,7 +16,8 @@ public enum CacheKeyEnum {
DRAWLINE_ACTIVITYBO("yh:act_activitybo:", "drawline.activitybo.redisSwitch", "drawline.activitybo.redisExpire", "限购排队活动信息(BO)"),
DRAWLINE_QUEUELIST("yh:act_queuelist:", "drawline.queuelist.redisSwitch", "drawline.queuelist.redisExpire", "活动排队列表"),
DRAWLINE_USERACTIVITY("yh:act_useractivity:", "drawline.useractivity.redisSwitch", "drawline.useractivity.redisExpire", "活动排队列表"),
DRAWLINE_LUCKYUSERLIST("yh:act_luckyuser:", "drawline.luckyuserlist.redisSwitch", "drawline.luckyuserlist.redisExpire", "中奖用户信息列表");
DRAWLINE_LUCKYUSERLIST("yh:act_luckyuser:", "drawline.luckyuserlist.redisSwitch", "drawline.luckyuserlist.redisExpire", "中奖用户信息列表"),
USERDAY_DRAWPRIZENUM("yh:act_userday:drawPrizeNum", "userday.drawPrizeNum.redisSwitch", "userday.drawPrizeNum.redisExpire", "会员日虚拟参与抽奖人数");
// 缓存的key
private String cacheKey;
... ...
... ... @@ -280,6 +280,32 @@ public class RedisValueCache {
}
}
/**
* 清除
*
* @param cacheEnum
* 缓存开关的枚举
* @param obj
* key的后缀
*
*/
public Long increment(CacheKeyEnum cacheEnum, Object obj, long delta, long timeout, TimeUnit unit) {
logger.debug("Enter increment valueOperation redis value. cacheEnum is {}, obj is {}, delta is {}, timeout is {}, unit is {}", cacheEnum, obj, delta, timeout, unit);
try {
String cacheKey = CacheKeyHelper.getCacheKeyAndCheck(cacheEnum, obj);
// 如果获取的key为空,则说明缓存开关是关闭的
if (StringUtils.isEmpty(cacheKey)) {
return null;
}
Long result = yhValueOperations.increment(cacheKey, delta);
yHRedisTemplate.longExpire(cacheKey, timeout, unit);
return result;
} catch (Exception e) {
logger.warn("Redis exception. value redis increment . cacheEnum is {}, obj is {}, exception msg is {}", cacheEnum, obj, e.getMessage());
}
return null;
}
public static void main(String[] args) {
String str = "12345";
String jsonStr = JSON.toJSONString(str);
... ...
... ... @@ -74,4 +74,18 @@ public class UserdayPrizeLogController {
return new ApiResponse(list);
}
/**
* 查询参与抽奖人数
* @param prize_type
* @return
* @throws Exception
*/
@RequestMapping("/queryPrizeLogNum")
@ResponseBody
public ApiResponse queryPrizeLogNum(int prize_type) throws Exception {
log.info("enter queryPrizeLogNum. param prize_type={}", prize_type);
int num = userdayPrizeLogService.queryPrizeLogNum(prize_type);
return new ApiResponse(num);
}
}
... ...
... ... @@ -23,4 +23,6 @@ public interface IUserdayPrizeLogDAO {
int insertLog(UserdayPrizeLog userdayPrizeLog);
List<UserdayPrizeLog> selectByUidAndPrizeType(@Param("uid") int uid, @Param("prizeType") int prizeType);
int selectNumByPrizeType(@Param("prizeType") int prize_type);
}
\ No newline at end of file
... ...
... ... @@ -116,6 +116,9 @@
from userday_prize_log
where uid = #{uid,jdbcType=INTEGER} and prize_type = #{prizeType,jdbcType=INTEGER} and winning_date = UNIX_TIMESTAMP(CURRENT_DATE())
</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>
<insert id="insertLog" parameterType="com.yoho.userday.dal.model.UserdayPrizeLog">
insert into userday_prize_log (uid, prize_type,
... ...
... ... @@ -24,4 +24,6 @@ public interface IUserdayPrizeLogService {
* @throws Exception
*/
List<UserdayPrizeLogResponseBO> queryPrizeLog(int uid, int prize_type) throws Exception;
int queryPrizeLogNum(int prize_type);
}
... ...
... ... @@ -6,6 +6,8 @@ import java.util.Map;
import javax.annotation.Resource;
import com.yoho.activity.common.redis.CacheKeyEnum;
import com.yoho.activity.common.redis.RedisValueCache;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -35,7 +37,9 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
private IUserdayPrizeLogDAO userdayPrizeLogDAO;
@Resource(name = "userdayDrawPrizeService")
Map<Integer, IUserdayDrawPrizeService> userdayDrawPrizeServiceMap;
@Resource
RedisValueCache redisValueCache;
@Value("${userday.open.day}")
String userdayOpenDay;
... ... @@ -108,9 +112,9 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
return userdayDrawPrizeService.existsPrizeLog(userdayPrizeLogList);
}
@Override
public List<UserdayPrizeLogResponseBO> queryPrizeLog(int uid, int prize_type) throws Exception {
log.info("enter queryPrizeLog. param uid={}, prize_type={}", uid, prize_type);
@Override
public List<UserdayPrizeLogResponseBO> queryPrizeLog(int uid, int prize_type) throws Exception {
log.info("enter queryPrizeLog. param uid={}, prize_type={}", uid, prize_type);
// (1)校验uid, prize_type
if (1 > uid || 1 > prize_type) {
... ... @@ -120,19 +124,27 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService {
// (2)查询当天中奖日志
List<UserdayPrizeLog> userdayPrizeLogList = userdayPrizeLogDAO.selectByUidAndPrizeType(uid, prize_type);
if (CollectionUtils.isEmpty(userdayPrizeLogList)) {
return null;
return null;
}
List<UserdayPrizeLogResponseBO> list = new ArrayList<>(userdayPrizeLogList.size());
UserdayPrizeLogResponseBO bo = null;
for (UserdayPrizeLog dal : userdayPrizeLogList) {
bo = new UserdayPrizeLogResponseBO();
BeanUtils.copyProperties(dal, bo);
bo.setWinningTime(DateUtils.long2DateStr(((long)bo.getCreateTime()) * 1000, "HH:mm"));
list.add(bo);
bo = new UserdayPrizeLogResponseBO();
BeanUtils.copyProperties(dal, bo);
bo.setWinningTime(DateUtils.long2DateStr(((long) bo.getCreateTime()) * 1000, "HH:mm"));
list.add(bo);
}
// (3)返回
return list;
}
}
@Override
public int queryPrizeLogNum(int prize_type) {
log.info("enter queryPrizeLogNum. param prize_type={}", prize_type);
int realNum = userdayPrizeLogDAO.selectNumByPrizeType(prize_type);
Integer virtualNum = redisValueCache.get(CacheKeyEnum.USERDAY_DRAWPRIZENUM, "", Integer.class);
return realNum + (null == virtualNum ? 0 : virtualNum);
}
}
... ...
package com.yoho.activity.service.timer;
import com.yoho.activity.common.redis.CacheKeyEnum;
import com.yoho.activity.common.redis.RedisValueCache;
import com.yoho.core.common.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* 会员日:每分钟增加抽奖人数定时任务
*/
@Component
public class UserdayDrawPrizeNumTimer {
private static final Logger log = LoggerFactory.getLogger(UserdayDrawPrizeNumTimer.class);
@Resource
RedisValueCache redisValueCache;
@Value("${userday.open.day}")
String userdayOpenDay;
@Scheduled(fixedDelay = 60 * 1000)
public void run() {
log.info("UserdayDrawPrizeNumTimer run.");
// (1)校验当天是否是会员日
String dateStr = DateUtil.getcurrentDate();
if (!userdayOpenDay.equals(dateStr.substring(8, 10))) {
return;
}
redisValueCache.increment(CacheKeyEnum.USERDAY_DRAWPRIZENUM, "", 10, 2, TimeUnit.MINUTES);
}
}
... ...
... ... @@ -73,4 +73,5 @@ rabbit_password=yoho
userday.coupon={"20":"14146", "10":"14144"}
userday.open.day=14
\ No newline at end of file
userday.open.day=14
userday.drawPrizeNum.redisSwitch=true
\ No newline at end of file
... ...
... ... @@ -66,4 +66,5 @@ rabbit_user=${rabbit_user}
rabbit_password =${rabbit_password}
userday.coupon=${userday.coupon}
userday.open.day=${userday.open.day}
\ No newline at end of file
userday.open.day=${userday.open.day}
userday.drawPrizeNum.redisSwitch=true
\ No newline at end of file
... ...