...
|
...
|
@@ -3,13 +3,37 @@ |
|
|
*/
|
|
|
package com.yoho.activity.other.timer;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
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.alibaba.fastjson.JSON;
|
|
|
import com.yoho.activity.common.enums.UserTypeEnum;
|
|
|
import com.yoho.activity.common.utils.MapUtil;
|
|
|
import com.yoho.activity.common.utils.MapUtil.FunctionExPlus;
|
|
|
import com.yoho.queue.dal.IDrawlineActivityDAO;
|
|
|
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.DrawlineUserQueue;
|
|
|
import com.yoho.queue.dal.model.DrawlineVirtualUser;
|
|
|
|
|
|
/**
|
|
|
* 描述:
|
|
|
* 描述:自动插入马甲
|
|
|
* 一分钟执行一次
|
|
|
* 规则
|
|
|
* 第一次判断,该活动只要有一个真实用户参加,就直接插入100个马甲;如果该活动还没有任何用户参加,则不增加马甲
|
|
|
* 从第二分钟开始,每次插入的马甲数,是前一分钟内,新增的真实用户数的5倍
|
|
|
*
|
|
|
* @author ping.huang 2016年3月5日
|
|
|
*/
|
...
|
...
|
@@ -17,9 +41,163 @@ import org.springframework.stereotype.Component; |
|
|
public class AddVirtualUserTimer {
|
|
|
|
|
|
static Logger log = LoggerFactory.getLogger(AddVirtualUserTimer.class);
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
IDrawlineVirtualUserDAO drawlineVirtualUserDAO;
|
|
|
@Resource
|
|
|
IDrawlineActivityDAO drawlineActivityDAO;
|
|
|
@Resource
|
|
|
IDrawlineUserQueueDAO drawlineUserQueueDAO;
|
|
|
|
|
|
//各个活动下,待进入排队的马甲
|
|
|
Map<Integer, List<DrawlineVirtualUser>> waitAddUserMap = new HashMap<Integer, List<DrawlineVirtualUser>>();
|
|
|
|
|
|
//各个活动下,已经进入排队的马甲(该变量可以去掉了)
|
|
|
// Map<Integer, DrawlineVirtualUser> alreadyAddUserMap = null;
|
|
|
|
|
|
//所有的活动
|
|
|
List<DrawlineActivity> activityList = null;
|
|
|
|
|
|
//所有的活动ID
|
|
|
List<Integer> activityIdList = new ArrayList<Integer>();
|
|
|
|
|
|
//是否第一次
|
|
|
Map<Integer, Boolean> first = new HashMap<Integer, Boolean>();
|
|
|
|
|
|
/**
|
|
|
* 初始化一些参数
|
|
|
*/
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
//初始化所有活动
|
|
|
activityList = drawlineActivityDAO.selectAllActivity();
|
|
|
|
|
|
if (activityList == null || activityList.size() == 0) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
for (DrawlineActivity activity : activityList) {
|
|
|
activityIdList.add(activity.getId());
|
|
|
}
|
|
|
//查询这些活动下,已经进入排队的马甲
|
|
|
List<DrawlineUserQueue> queueList = drawlineUserQueueDAO.selectByTypeAndActivityIds(StringUtils.join(activityIdList, ","));
|
|
|
|
|
|
//初始化所有的马甲用户
|
|
|
List<DrawlineVirtualUser> userList = drawlineVirtualUserDAO.selectWaitQueueVirtualUser();
|
|
|
|
|
|
List<DrawlineVirtualUser> waitList = null;
|
|
|
for (DrawlineActivity activity : activityList) {
|
|
|
for (DrawlineVirtualUser user : userList) {
|
|
|
//如果该马甲已经在该活动下排队过,则排除
|
|
|
if (userIsExists(activity.getId().intValue(), user.getUid().intValue(), queueList)) {
|
|
|
continue;
|
|
|
}
|
|
|
//没有排队过,则进入等待排队的队列
|
|
|
waitList = waitAddUserMap.get(activity.getId());
|
|
|
if (waitList == null) {
|
|
|
waitList = new ArrayList<DrawlineVirtualUser>();
|
|
|
}
|
|
|
waitList.add(user);
|
|
|
waitAddUserMap.put(activity.getId(), waitList);
|
|
|
}
|
|
|
}
|
|
|
System.out.println("result= " + JSON.toJSONString(waitAddUserMap));
|
|
|
}
|
|
|
|
|
|
@Scheduled(fixedDelay = 60 * 1000)
|
|
|
public void test() {
|
|
|
log.info("------------");
|
|
|
public void run() {
|
|
|
log.info("start add virtuals");
|
|
|
|
|
|
//没有活动,直接退出
|
|
|
if (activityList == null || activityList.size() == 0) {
|
|
|
log.info("activityList is empty over");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
//查询所有的活动,前一分钟内实际增加的真实用户数
|
|
|
List<Map<String, Object>> realUserCountList = drawlineUserQueueDAO.selectRealUserCountByActivityIds(StringUtils.join(activityIdList, ","));
|
|
|
//前一分钟没有真实用户参加
|
|
|
if (realUserCountList == null || realUserCountList.size() == 0) {
|
|
|
log.info("realUser add is empty");
|
|
|
return;
|
|
|
}
|
|
|
//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());
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
//活动下,等待排队的马甲
|
|
|
List<DrawlineVirtualUser> waitList = null;
|
|
|
//需要加入的马甲数量
|
|
|
int num = 0, n = 0;
|
|
|
DrawlineUserQueue queueUser = null;
|
|
|
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");
|
|
|
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);
|
|
|
//插入排队表,因为排队字段sort必须要连续,为了避免锁表到导致真实用户插入问题,这里需要在循环里面插入数据库
|
|
|
drawlineUserQueueDAO.insertAutoSort(queueUser);
|
|
|
//插入成功后,需要从等待排队的马甲中删除
|
|
|
waitList.remove(waitList.get(i));
|
|
|
i--;
|
|
|
}
|
|
|
}
|
|
|
log.info("end add virtuals");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询该活动下,这个马甲有没有排队过
|
|
|
* @param activityId
|
|
|
* @param uid
|
|
|
* @param queueList
|
|
|
* @return
|
|
|
*/
|
|
|
private boolean userIsExists(int activityId, int uid, List<DrawlineUserQueue> queueList) {
|
|
|
if (queueList == null || queueList.size() == 0) {
|
|
|
return false;
|
|
|
}
|
|
|
for (DrawlineUserQueue user : queueList) {
|
|
|
if (user.getActivityId() != null && user.getActivityId().intValue() == activityId && user.getUid() != null && user.getUid().intValue() == uid) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
} |
...
|
...
|
|