...
|
...
|
@@ -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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询该活动下,这个马甲有没有排队过
|
...
|
...
|
|