Authored by chaogeng

Merge branch 'hotfix' of http://git.dev.yoho.cn/yoho30/yohobuy-activity into hotfix

... ... @@ -13,4 +13,10 @@
<artifactId>yohobuy-activity-common</artifactId>
<dependencies>
<dependency>
<groupId>com.yoho.dsf.yhactivity</groupId>
<artifactId>yohobuy-activity-dal</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
/**
*
*/
package com.yoho.activity.common.cache;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yoho.activity.common.utils.DateUtils;
import com.yoho.core.redis.YHRedisTemplate;
import com.yoho.core.redis.YHValueOperations;
import com.yoho.queue.dal.IDrawlineActivityDAO;
import com.yoho.queue.dal.IDrawlineUserQueueDAO;
import com.yoho.queue.dal.model.DrawlineActivity;
/**
* 描述:
*
* @author ping.huang
* 2016年3月15日
*/
@Component
public class RedisCache {
static Logger log = LoggerFactory.getLogger(RedisCache.class);
@Resource
YHValueOperations<String, String> yhValueOperations;
@Autowired
YHRedisTemplate<String, String> yHRedisTemplate;
@Resource
IDrawlineUserQueueDAO drawlineUserQueueDAO;
@Resource
IDrawlineActivityDAO drawlineActivityDAO;
public static final String SORT_ID_KEY = "SORT_ID_KEY_";
@PostConstruct
public void init() {
log.info("RedisCache.init enter");
//初始化所有活动
List<DrawlineActivity> activityList = drawlineActivityDAO.selectAllActivity(DateUtils.getCurrentTimeSecond());
if (activityList == null || activityList.size() == 0) {
return;
}
int maxId = 0;
//遍历所有活动,设置到redis
for (DrawlineActivity act : activityList) {
maxId = drawlineUserQueueDAO.selectMaxSortByActivityId(act.getId());
yhValueOperations.setIfAbsent(SORT_ID_KEY + act.getId().intValue(), String.valueOf(maxId));
yHRedisTemplate.longExpire(SORT_ID_KEY + act.getId().intValue(), 6 * 30 * 24 * 3600, TimeUnit.SECONDS);
}
}
/**
* 获取该活动的排序号
* @param activityId
* @return
*/
public int getSortId(int activityId) {
log.debug("getSortId with activityId is {}", activityId);
Long id = null;
try {
//该活动的排序号加一
id = yhValueOperations.increment(SORT_ID_KEY + activityId, 1);
if (id == null || id.longValue() == 0) {
id = (long) (getMaxSort(activityId) + 1);
}
} catch (Exception e) {
log.warn("getSortId error", e);
//如果发生异常,则,再取数据库一次
id = (long) (getMaxSort(activityId) + 1);
}
return id.intValue();
}
/**
* 从数据库取最大的排序号
* @param activityId
* @return
*/
private int getMaxSort(int activityId) {
int sortId = drawlineUserQueueDAO.selectMaxSortByActivityId(activityId);
int id = sortId + 1;
yhValueOperations.set(SORT_ID_KEY + activityId, String.valueOf(id));
return id;
}
}
... ...
... ... @@ -23,4 +23,6 @@ public interface IDrawlineActivityDAO {
List<DrawlineActivity> selectByNotLuckydraw();
List<DrawlineActivity> selectAllActivity(@Param("currentTime") int currentTime);
DrawlineActivity selectActivityById(@Param("activityId") int activityId, @Param("currentTime") int currentTime);
}
\ No newline at end of file
... ...
... ... @@ -39,4 +39,8 @@ public interface IDrawlineUserQueueDAO {
List<DrawlineUserQueue> selectByTypeAndActivityIds(@Param("activityIds") String activityIds, @Param("userType") int userType);
List<Map<String, Object>> selectRealUserCountByActivityIds(@Param("activityIds") String activityIds, @Param("beginTime") int beginTime, @Param("endTime") int endTime);
int selectMaxSortByActivityId(@Param("activityId") int activityId);
void insertBatch(List<DrawlineUserQueue> list);
}
\ No newline at end of file
... ...
... ... @@ -173,4 +173,8 @@
<select id="selectAllActivity" resultMap="BaseResultMap">
select * from drawline_activity where status=1 and begin_time&lt;=#{currentTime} and end_time &gt;=#{currentTime}
</select>
<select id="selectActivityById" resultMap="BaseResultMap">
select * from drawline_activity where id=#{activityId} and status=1 and begin_time&lt;=#{currentTime} and end_time &gt;=#{currentTime}
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -208,4 +208,15 @@
and #{endTime}
and activity_id in (${activityIds}) group by activity_id
</select>
<select id="selectMaxSortByActivityId" resultType="java.lang.Integer">
select ifnull(max(sort),0) from drawline_user_queue where activity_id=#{activityId}
</select>
<insert id="insertBatch">
insert into drawline_user_queue (uid, user_type,activity_id, sort, status,create_time, update_time)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.uid},#{item.userType},#{item.activityId},#{item.sort},#{item.status},#{item.createTime},#{item.updateTime})
</foreach>
</insert>
</mapper>
\ No newline at end of file
... ...
... ... @@ -11,7 +11,8 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yoho.activity.other.timer.AddVirtualUser;
import com.yoho.activity.other.timer.AddVirtualUserTimer;
import com.yoho.activity.other.timer.AddWhiteUserTimer;
import com.yoho.activity.queue.ApiResponse;
/**
... ... @@ -23,15 +24,35 @@ import com.yoho.activity.queue.ApiResponse;
public class ExecuteAddVirtualRest {
@Resource
AddVirtualUser addVirtualUser;
AddVirtualUserTimer addVirtualUserTimer;
@Resource
AddWhiteUserTimer addWhiteUserTimer;
static Logger logger = LoggerFactory.getLogger(ExecuteAddVirtualRest.class);
/**
* 添加马甲
* @param activityId
* @return
*/
@RequestMapping(params = "method=app.drawline.executeAddVirtualUser")
@ResponseBody
public ApiResponse executeAddVirtualUser() {
public ApiResponse executeAddVirtualUser(int activityId) {
logger.info("Enter executeAddVirtualUser:");
addVirtualUserTimer.run(activityId);
return new ApiResponse.ApiResponseBuilder().build();
}
/**
* 添加白名单
* @param activityId
* @return
*/
@RequestMapping(params = "method=app.drawline.executeAddWhiteUser")
@ResponseBody
public ApiResponse executeAddWhiteUser(int activityId) {
logger.info("Enter executeAddVirtualUser:");
addVirtualUser.run();
addWhiteUserTimer.run(activityId);
return new ApiResponse.ApiResponseBuilder().build();
}
}
... ...
... ... @@ -15,20 +15,18 @@ import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.yoho.activity.common.cache.RedisCache;
import com.yoho.activity.common.enums.UserTypeEnum;
import com.yoho.activity.common.utils.DateUtils;
import com.yoho.activity.common.utils.MapUtil;
import com.yoho.activity.common.utils.MapUtil.FunctionExPlus;
import com.yoho.activity.common.utils.PropertiesUtil;
import com.yoho.queue.dal.IDrawlineActivityDAO;
import com.yoho.queue.dal.IDrawlinePrizeSettingDAO;
import com.yoho.queue.dal.IDrawlineUserQueueDAO;
import com.yoho.queue.dal.IDrawlineVirtualUserDAO;
import com.yoho.queue.dal.model.DrawlineActivity;
import com.yoho.queue.dal.model.DrawlinePrizeSetting;
import com.yoho.queue.dal.model.DrawlineUserQueue;
import com.yoho.queue.dal.model.DrawlineVirtualUser;
... ... @@ -54,6 +52,8 @@ public class AddVirtualUserTimer {
IDrawlineUserQueueDAO drawlineUserQueueDAO;
@Resource
IDrawlinePrizeSettingDAO drawlinePrizeSettingDAO;
@Resource
RedisCache redisCache;
//各个活动下,待进入排队的马甲
Map<Integer, List<DrawlineVirtualUser>> waitAddUserMap = new HashMap<Integer, List<DrawlineVirtualUser>>();
... ... @@ -70,7 +70,7 @@ public class AddVirtualUserTimer {
/**
* 初始化一些参数
*/
public void init() {
public void init(int activityId) {
waitAddUserMap = new HashMap<Integer, List<DrawlineVirtualUser>>();
activityList = new ArrayList<DrawlineActivity>();
... ... @@ -78,7 +78,10 @@ public class AddVirtualUserTimer {
//first = new HashMap<Integer, Boolean>();
//初始化所有活动
activityList = drawlineActivityDAO.selectAllActivity(DateUtils.getCurrentTimeSecond());
DrawlineActivity act = drawlineActivityDAO.selectActivityById(activityId, DateUtils.getCurrentTimeSecond());
if (act != null) {
activityList.add(act);
}
if (activityList == null || activityList.size() == 0) {
return;
... ... @@ -116,18 +119,18 @@ public class AddVirtualUserTimer {
}
}
@Scheduled(fixedDelay = 60 * 1000)
public void run() {
log.info("start add virtuals");
boolean execute = PropertiesUtil.getExecuteTask();
if (!execute) {
log.info("properties is false exit task");
return;
}
// @Scheduled(fixedDelay = 60 * 1000)
public void run(int activityId) {
log.info("start add virtuals activityId={}", activityId);
// boolean execute = PropertiesUtil.getExecuteTask();
// if (!execute) {
// log.info("properties is false exit task");
// return;
// }
//初始化数据
init();
init(activityId);
log.info("add virtuals init ok");
//没有活动,直接退出
if (activityList == null || activityList.size() == 0) {
log.info("activityList is empty over");
... ... @@ -135,7 +138,8 @@ public class AddVirtualUserTimer {
}
//添加白名单
addWhiteUser();
// addWhiteUser();
log.info("add virtuals. add white ok");
//马甲全部排队完了,直接退出
if (!checkVirtual()) {
... ... @@ -175,6 +179,7 @@ public class AddVirtualUserTimer {
int num = 0, n = 0;
DrawlineUserQueue queueUser = null;
int currentTime = (int) (System.currentTimeMillis() / 1000);
List<DrawlineUserQueue> drawList = new ArrayList<DrawlineUserQueue>();
for (DrawlineActivity activity : activityList) {
n = 0;
// if (realUserCountMap.get(activity.getId()) == null || realUserCountMap.get(activity.getId()) == 0) {
... ... @@ -206,98 +211,19 @@ public class AddVirtualUserTimer {
queueUser.setStatus((byte) 1);
queueUser.setCreateTime(currentTime);
queueUser.setUpdateTime(currentTime);
//插入排队表,因为排队字段sort必须要连续,为了避免锁表到导致真实用户插入问题,这里需要在循环里面插入数据库
drawlineUserQueueDAO.insertAutoSort(queueUser);
queueUser.setSort(redisCache.getSortId(activity.getId()));
//放入list,准备插入
drawList.add(queueUser);
//插入成功后,需要从等待排队的马甲中删除
waitList.remove(waitList.get(i));
i--;
}
}
log.info("end add virtuals");
}
/**
* 添加白名单
*/
private void addWhiteUser() {
// 查询所有的奖品设置
List<DrawlinePrizeSetting> prizeList = drawlinePrizeSettingDAO.selectByActivityIds(StringUtils.join(activityIdList, ","));
if (prizeList == null || prizeList.size() == 0) {
log.info("addWhiteUser prize is empty");
return;
//批量插入
if (drawList != null && drawList.size() > 0) {
drawlineUserQueueDAO.insertBatch(drawList);
}
// list 转 map
Map<Integer, String> whileMap = null;
if (prizeList != null) {
whileMap = MapUtil.transformListMap(prizeList, new FunctionExPlus<DrawlinePrizeSetting, Integer, String>() {
@Override
public Integer applyKey(DrawlinePrizeSetting input) {
return input.getActivityId();
}
@Override
public String applyValue(DrawlinePrizeSetting input) {
return input.getWhiteList();
}
});
}
// 查询所有排队的真实用户
List<DrawlineUserQueue> queueList = drawlineUserQueueDAO.selectByTypeAndActivityIds(StringUtils.join(activityIdList, ","), UserTypeEnum.REAL_USER.getValue());
String[] whileArr = null;
String whiteStr = "";
DrawlineUserQueue queueUser = null;
int currentTime = (int) (System.currentTimeMillis() / 1000);
for (DrawlineActivity activity : activityList) {
whiteStr = whileMap.get(activity.getId());
if (StringUtils.isEmpty(whiteStr)) {
continue;
}
whileArr = whiteStr.split(",");
for (String white : whileArr) {
if (whiteIsExists(queueList, activity.getId().intValue(), Integer.parseInt(white))) {
log.debug("white user={} is queue in activityId={} exit", white, activity.getId());
continue;
}
//该白名单没有排队过,则进入排队
queueUser = new DrawlineUserQueue();
queueUser.setActivityId(activity.getId());
queueUser.setUid(Integer.parseInt(white));
queueUser.setUserType((byte) UserTypeEnum.REAL_USER.getValue());
queueUser.setStatus((byte) 1);
queueUser.setCreateTime(currentTime);
queueUser.setUpdateTime(currentTime);
//插入排队表
drawlineUserQueueDAO.insertAutoSort(queueUser);
//一次只插入一条白名单
break;
}
}
}
/**
* 判断该白名单是否已经排队过
* @param queueList
* @param activityId
* @param whiteUid
* @return
*/
private boolean whiteIsExists(List<DrawlineUserQueue> queueList, int activityId, int whiteUid) {
if (queueList == null || queueList.size() == 0) {
return false;
}
for (DrawlineUserQueue queue : queueList) {
if (queue.getActivityId() == null || queue.getUid() == null) {
continue;
}
if (queue.getActivityId().intValue() == activityId && queue.getUid().intValue() == whiteUid) {
return true;
}
}
return false;
log.info("end add virtuals");
}
/**
... ...
... ... @@ -8,7 +8,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
... ... @@ -17,11 +16,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.yoho.activity.common.cache.RedisCache;
import com.yoho.activity.common.enums.UserTypeEnum;
import com.yoho.activity.common.utils.DateUtils;
import com.yoho.activity.common.utils.MapUtil;
import com.yoho.activity.common.utils.MapUtil.FunctionExPlus;
import com.yoho.activity.common.utils.PropertiesUtil;
import com.yoho.queue.dal.IDrawlineActivityDAO;
import com.yoho.queue.dal.IDrawlinePrizeSettingDAO;
import com.yoho.queue.dal.IDrawlineUserQueueDAO;
... ... @@ -41,9 +40,9 @@ import com.yoho.queue.dal.model.DrawlineVirtualUser;
* @author ping.huang 2016年3月5日
*/
@Component
public class AddVirtualUser {
public class AddWhiteUserTimer {
static Logger log = LoggerFactory.getLogger(AddVirtualUser.class);
static Logger log = LoggerFactory.getLogger(AddWhiteUserTimer.class);
@Resource
IDrawlineVirtualUserDAO drawlineVirtualUserDAO;
... ... @@ -53,6 +52,8 @@ public class AddVirtualUser {
IDrawlineUserQueueDAO drawlineUserQueueDAO;
@Resource
IDrawlinePrizeSettingDAO drawlinePrizeSettingDAO;
@Resource
RedisCache redisCache;
//各个活动下,待进入排队的马甲
Map<Integer, List<DrawlineVirtualUser>> waitAddUserMap = new HashMap<Integer, List<DrawlineVirtualUser>>();
... ... @@ -69,7 +70,7 @@ public class AddVirtualUser {
/**
* 初始化一些参数
*/
public void init() {
public void init(int activityId) {
waitAddUserMap = new HashMap<Integer, List<DrawlineVirtualUser>>();
activityList = new ArrayList<DrawlineActivity>();
... ... @@ -77,7 +78,10 @@ public class AddVirtualUser {
//first = new HashMap<Integer, Boolean>();
//初始化所有活动
activityList = drawlineActivityDAO.selectAllActivity(DateUtils.getCurrentTimeSecond());
DrawlineActivity act = drawlineActivityDAO.selectActivityById(activityId, DateUtils.getCurrentTimeSecond());
if (act != null) {
activityList.add(act);
}
if (activityList == null || activityList.size() == 0) {
return;
... ... @@ -115,17 +119,18 @@ public class AddVirtualUser {
}
}
public void run() {
log.info("start add virtuals");
boolean execute = PropertiesUtil.getExecuteTask();
if (!execute) {
log.info("properties is false exit task");
return;
}
// @Scheduled(fixedDelay = 60 * 1000)
public void run(int activityId) {
log.info("start add virtuals activityId={}", activityId);
// boolean execute = PropertiesUtil.getExecuteTask();
// if (!execute) {
// log.info("properties is false exit task");
// return;
// }
//初始化数据
init();
init(activityId);
log.info("add virtuals init ok");
//没有活动,直接退出
if (activityList == null || activityList.size() == 0) {
log.info("activityList is empty over");
... ... @@ -134,83 +139,8 @@ public class AddVirtualUser {
//添加白名单
addWhiteUser();
log.info("add virtuals. add white ok");
//马甲全部排队完了,直接退出
if (!checkVirtual()) {
log.info("wait virtual is empty over");
return;
}
//查询所有的活动,前一分钟内实际增加的真实用户数
int currTime = DateUtils.getCurrentTimeSecond();
List<Map<String, Object>> realUserCountList = drawlineUserQueueDAO.selectRealUserCountByActivityIds(StringUtils.join(activityIdList, ","), currTime - 60, currTime);
if (realUserCountList == null) {
realUserCountList = new ArrayList<Map<String,Object>>();
}
//list转map
Map<Integer, Integer> realUserCountMap = null;
if (realUserCountList != null) {
realUserCountMap = MapUtil.transformListMap(realUserCountList, new FunctionExPlus<Map<String, Object>, Integer, Integer>() {
@Override
public Integer applyKey(Map<String, Object> input) {
return Integer.parseInt(input.get("activity_id").toString());
}
@Override
public Integer applyValue(Map<String, Object> input) {
return Integer.parseInt(input.get("nums").toString());
}
});
}
if (realUserCountMap == null) {
realUserCountMap = new HashMap<Integer, Integer>();
}
//活动下,等待排队的马甲
List<DrawlineVirtualUser> waitList = null;
//需要加入的马甲数量
int num = 0, n = 0;
DrawlineUserQueue queueUser = null;
int currentTime = (int) (System.currentTimeMillis() / 1000);
for (DrawlineActivity activity : activityList) {
n = 0;
// if (realUserCountMap.get(activity.getId()) == null || realUserCountMap.get(activity.getId()) == 0) {
// //该活动还没有一个用户参加,则不增加马甲
// continue;
// }
waitList = waitAddUserMap.get(activity.getId());
if (waitList == null || waitList.size() == 0) {
log.info("wait queue user is empty with activityId is {}, activityName is {}", activity.getId(), activity.getActivityName());
continue;
}
if (first.get(activity.getId()) == null) {
num = 100;
first.put(activity.getId(), true);
} else {
num = 5 * (realUserCountMap.get(activity.getId()) == null ? 0 : realUserCountMap.get(activity.getId()).intValue());
}
//进入排队
for (int i = 0; i < waitList.size(); i++) {
n++;
if (n > num) {
break;
}
queueUser = new DrawlineUserQueue();
queueUser.setActivityId(activity.getId());
queueUser.setUid(waitList.get(i).getUid());
queueUser.setUserType((byte) UserTypeEnum.VIRTUAL_USER.getValue());
queueUser.setStatus((byte) 1);
queueUser.setCreateTime(currentTime);
queueUser.setUpdateTime(currentTime);
//插入排队表,因为排队字段sort必须要连续,为了避免锁表到导致真实用户插入问题,这里需要在循环里面插入数据库
drawlineUserQueueDAO.insertAutoSort(queueUser);
//插入成功后,需要从等待排队的马甲中删除
waitList.remove(waitList.get(i));
i--;
}
}
log.info("end add virtuals");
}
... ... @@ -249,6 +179,7 @@ public class AddVirtualUser {
String whiteStr = "";
DrawlineUserQueue queueUser = null;
int currentTime = (int) (System.currentTimeMillis() / 1000);
List<DrawlineUserQueue> drawList = new ArrayList<DrawlineUserQueue>();
for (DrawlineActivity activity : activityList) {
whiteStr = whileMap.get(activity.getId());
if (StringUtils.isEmpty(whiteStr)) {
... ... @@ -268,12 +199,17 @@ public class AddVirtualUser {
queueUser.setStatus((byte) 1);
queueUser.setCreateTime(currentTime);
queueUser.setUpdateTime(currentTime);
//插入排队表
drawlineUserQueueDAO.insertAutoSort(queueUser);
queueUser.setSort(redisCache.getSortId(activity.getId()));
//放入list,准备插入
drawList.add(queueUser);
//一次只插入一条白名单
break;
}
}
//批量插入
if (drawList != null && drawList.size() > 0) {
drawlineUserQueueDAO.insertBatch(drawList);
}
}
/**
... ... @@ -298,23 +234,6 @@ public class AddVirtualUser {
return false;
}
/**
* 检查是否还有剩余的马甲
* @return
*/
private boolean checkVirtual() {
if (waitAddUserMap == null || waitAddUserMap.size() == 0) {
return false;
}
List<DrawlineVirtualUser> l = null;
for (Entry<Integer, List<DrawlineVirtualUser>> entry : waitAddUserMap.entrySet()) {
l = entry.getValue();
if (l != null && l.size() > 0) {
return true;
}
}
return false;
}
/**
* 查询该活动下,这个马甲有没有排队过
... ...
... ... @@ -142,7 +142,10 @@
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-redis</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
... ...
... ... @@ -13,12 +13,13 @@ import com.yoho.service.model.activity.drawline.response.UserBaseRspBO;
public class DrawlineUserQueueConvert {
public static DrawlineUserQueue toDOBy(int uid, int activityId, byte userType) {
public static DrawlineUserQueue toDOBy(int uid, int activityId, byte userType, int sort) {
int currentTime = (int) (System.currentTimeMillis() / 1000);
DrawlineUserQueue drawlineUserQueue = new DrawlineUserQueue();
drawlineUserQueue.setUid(uid);
drawlineUserQueue.setActivityId(activityId);
drawlineUserQueue.setSort(sort);
drawlineUserQueue.setUserType(userType);
drawlineUserQueue.setStatus((byte) 1);
drawlineUserQueue.setCreateTime(currentTime);
... ...
... ... @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yoho.activity.common.cache.RedisCache;
import com.yoho.activity.queue.convert.DrawlineUserQueueConvert;
import com.yoho.activity.queue.service.IDrawlineList2MapService;
import com.yoho.activity.queue.service.IDrawlineUserQueueService;
... ... @@ -35,6 +36,9 @@ public class DrawlineUserQueueServiceImpl implements IDrawlineUserQueueService {
@Autowired
private IDrawlineList2MapService drawlineList2MapService;
@Autowired
private RedisCache redisCache;
public int addDrawlineUserQueue(DrawlineUserQueueReqBO drawlineUserQueueReqBO) throws ServiceException {
logger.info("Enter addDrawlineUserQueue. param drawlineUserQueueReqBO is {}", drawlineUserQueueReqBO);
... ... @@ -78,25 +82,111 @@ public class DrawlineUserQueueServiceImpl implements IDrawlineUserQueueService {
}
// (5)组装数据,新增排队
drawlineUserQueue = DrawlineUserQueueConvert.toDOBy(uid, activityId, userType);
int rows = drawlineUserQueueDAO.insertAutoSort(drawlineUserQueue);
int sort = redisCache.getSortId(activityId);
drawlineUserQueue = DrawlineUserQueueConvert.toDOBy(uid, activityId, userType, sort);
int rows = drawlineUserQueueDAO.insertSelective(drawlineUserQueue);
logger.debug("addDrawlineUserQueue: drawlineUserQueueDAO.insertAutoSort. drawlineUserQueue is {}, rows is {}", drawlineUserQueue, rows);
if (1 > rows) {
logger.warn("addDrawlineUserQueue: fail to addDrawlineUserQueue. uid is {}, activityId is {}, userType is {}", uid, activityId, userType);
throw new ServiceException(900001007, "参加活动排队失败");
}
// (6)查询排队序号
drawlineUserQueue = drawlineUserQueueDAO.selectByUidAndActivityIdAndUserType(uid, activityId, userType);
logger.debug("addDrawlineUserQueue: drawlineUserQueueDAO.selectByUidAndActivityIdAndUserType. uid is {}, activityId is {}, userType is {}, drawlineUserQueue is {}", uid, activityId, userType,
drawlineUserQueue);
// (7)返回
int sort = null == drawlineUserQueue.getSort() ? 0 : drawlineUserQueue.getSort().intValue();
// // (6)查询排队序号
// drawlineUserQueue =
// drawlineUserQueueDAO.selectByUidAndActivityIdAndUserType(uid,
// activityId, userType);
// logger.debug("addDrawlineUserQueue: drawlineUserQueueDAO.selectByUidAndActivityIdAndUserType. uid is {}, activityId is {}, userType is {}, drawlineUserQueue is {}",
// uid, activityId, userType,
// drawlineUserQueue);
//
// // (7)返回
// int sort = null == drawlineUserQueue.getSort() ? 0 :
// drawlineUserQueue.getSort().intValue();
logger.info("addDrawlineUserQueue: return. drawlineUserQueueReqBO is {}, sort is {}", drawlineUserQueueReqBO, sort);
return sort;
}
// public int addDrawlineUserQueue(DrawlineUserQueueReqBO
// drawlineUserQueueReqBO) throws ServiceException {
// logger.info("Enter addDrawlineUserQueue. param drawlineUserQueueReqBO is {}",
// drawlineUserQueueReqBO);
//
// // (1)获取参数
// int uid = 0, activityId = 0;
// byte userType = 1;
// if (null != drawlineUserQueueReqBO) {
// uid = null == drawlineUserQueueReqBO.getUid() ? 0 :
// drawlineUserQueueReqBO.getUid().intValue();
// activityId = null == drawlineUserQueueReqBO.getActivityId() ? 0 :
// drawlineUserQueueReqBO.getActivityId().intValue();
// userType = null == drawlineUserQueueReqBO.getUserType() ? 1 :
// drawlineUserQueueReqBO.getUserType().byteValue();
// }
//
// // (2)校验
// validAddDrawlineUserQueue(uid, activityId);
//
// // (3)判断活动是否存在,是否过期
// DrawlineActivity drawlineActivity =
// drawlineActivityDAO.selectByPrimaryKey(activityId);
// if (null == drawlineActivity) {
// logger.warn("addDrawlineUserQueue: drawlineActivity is not exists");
// throw new ServiceException(900001003, "活动已不存在");
// }
// int beginTime = null == drawlineActivity.getBeginTime() ? 0 :
// drawlineActivity.getBeginTime().intValue();
// int endTime = null == drawlineActivity.getEndTime() ? 0 :
// drawlineActivity.getEndTime().intValue();
// int currentTime = (int) (System.currentTimeMillis() / 1000);
// if (0 < beginTime && currentTime < beginTime) {
// logger.warn("addDrawlineUserQueue: drawlineActivity is not begin");
// throw new ServiceException(900001004, "活动尚未开始");
// }
// if (0 < endTime && currentTime > endTime) {
// logger.warn("addDrawlineUserQueue: drawlineActivity is already end");
// throw new ServiceException(900001005, "活动已结束");
// }
//
// // (4)判断是否已经参加排队
// DrawlineUserQueue drawlineUserQueue =
// drawlineUserQueueDAO.selectByUidAndActivityIdAndUserType(uid, activityId,
// userType);
// logger.debug("addDrawlineUserQueue: drawlineUserQueueDAO.selectByUidAndActivityIdAndUserType. uid is {}, activityId is {}, userType is {}, drawlineUserQueue is {}",
// uid, activityId, userType,
// drawlineUserQueue);
// if (null != drawlineUserQueue) {
// logger.warn("addDrawlineUserQueue: drawlineUserQueue is exists already");
// throw new ServiceException(900001006, "您已经参加过排队,无需再次排队");
// }
//
// // (5)组装数据,新增排队
// drawlineUserQueue = DrawlineUserQueueConvert.toDOBy(uid, activityId,
// userType);
// int rows = drawlineUserQueueDAO.insertAutoSort(drawlineUserQueue);
// logger.debug("addDrawlineUserQueue: drawlineUserQueueDAO.insertAutoSort. drawlineUserQueue is {}, rows is {}",
// drawlineUserQueue, rows);
// if (1 > rows) {
// logger.warn("addDrawlineUserQueue: fail to addDrawlineUserQueue. uid is {}, activityId is {}, userType is {}",
// uid, activityId, userType);
// throw new ServiceException(900001007, "参加活动排队失败");
// }
//
// // (6)查询排队序号
// drawlineUserQueue =
// drawlineUserQueueDAO.selectByUidAndActivityIdAndUserType(uid, activityId,
// userType);
// logger.debug("addDrawlineUserQueue: drawlineUserQueueDAO.selectByUidAndActivityIdAndUserType. uid is {}, activityId is {}, userType is {}, drawlineUserQueue is {}",
// uid, activityId, userType,
// drawlineUserQueue);
//
// // (7)返回
// int sort = null == drawlineUserQueue.getSort() ? 0 :
// drawlineUserQueue.getSort().intValue();
// logger.info("addDrawlineUserQueue: return. drawlineUserQueueReqBO is {}, sort is {}",
// drawlineUserQueueReqBO, sort);
// return sort;
// }
public List<DrawlineUserQueueRespBO> getDrawlineUserQueueList(DrawlineUserQueueReqBO drawlineUserQueueReqBO) throws ServiceException {
logger.info("Enter getDrawlineUserQueueList. param drawlineUserQueueReqBO is {}", drawlineUserQueueReqBO);
... ...
... ... @@ -9,9 +9,11 @@ import org.springframework.stereotype.Service;
import com.netflix.config.DynamicLongProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.yoho.activity.common.utils.PropertiesUtil;
import com.yoho.activity.queue.constant.Const;
import com.yoho.activity.queue.service.ITimeTaskService;
import com.yoho.core.common.utils.LocalIp;
@Service
public class LuckyDrawService {
... ... @@ -21,23 +23,34 @@ public class LuckyDrawService {
private ITimeTaskService timeTaskService;
public LuckyDrawService() {
// 服务启动后5分钟执行任务,起一个每天(默认间隔)执行一次的线程,对活动进行抽奖
DynamicLongProperty interval = DynamicPropertyFactory.getInstance().getLongProperty("drawline.luckydraw.interval", 1440);
Const.LUCKYDRAW_EXE.scheduleAtFixedRate(new Runnable() {
public void run() {
logger.info("Enter LuckyDrawService thread run.");
// 判断定时任务配置,true时执行定时任务,false不执行
if (!PropertiesUtil.getExecuteTask()) {
return;
}
timeTaskService.luckyDraw();
logger.info("Outer LuckyDrawService thread run.");
}
}, 5, interval.get(), TimeUnit.MINUTES);
//本次定时抽奖不执行
return;
// // 服务启动后5分钟执行任务,起一个每天(默认间隔)执行一次的线程,对活动进行抽奖
// DynamicLongProperty interval = DynamicPropertyFactory.getInstance().getLongProperty("drawline.luckydraw.interval", 1440);
//
// Const.LUCKYDRAW_EXE.scheduleAtFixedRate(new Runnable() {
//
// public void run() {
// logger.info("Enter LuckyDrawService thread run.");
//
// DynamicStringProperty taskHost = DynamicPropertyFactory.getInstance().getStringProperty("execute.timetask.host", "192.168.90.9");
// String localIP = LocalIp.getLocalIp();
// if (null == localIP || !localIP.equals(taskHost.get())) {
// logger.info("LuckyDrawService: not bind execute.timetask.host. localIP is {}, taskHost is {}", localIP, taskHost.get());
// return;
// }
//
// // 判断定时任务配置,true时执行定时任务,false不执行
// if (!PropertiesUtil.getExecuteTask()) {
// logger.info("LuckyDrawService: execute task false.");
// return;
// }
//
// timeTaskService.luckyDraw();
//
// logger.info("Outer LuckyDrawService thread run.");
// }
// }, 5, interval.get(), TimeUnit.MINUTES);
}
}
... ...
/**
*
*/
package com;
import javax.annotation.Resource;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.yoho.activity.common.cache.RedisCache;
/**
* 描述:
*
* @author ping.huang
* 2016年3月15日
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath*:META-INF/spring/spring*.xml"})
public class Test {
@Resource
RedisCache redisCache;
@org.junit.Test
public void t() {
System.out.println(redisCache.getSortId(-1));
System.out.println(redisCache.getSortId(-1));
System.out.println(redisCache.getSortId(-1));
}
}
... ...