|
|
package com.yoho.activity.queue.service.impl;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Timer;
|
|
|
import java.util.TimerTask;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.activity.queue.convert.DrawlineActivityLuckydrawConvert;
|
|
|
import com.yoho.activity.queue.convert.DrawlineLuckyUserConvert;
|
|
|
import com.yoho.activity.queue.service.ITimeTaskService;
|
|
|
import com.yoho.core.common.restbean.ResponseBean;
|
|
|
import com.yoho.core.rest.client.ServiceCaller;
|
|
|
import com.yoho.queue.dal.IDrawlineActivityDAO;
|
|
|
import com.yoho.queue.dal.IDrawlineActivityLuckydrawDAO;
|
|
|
import com.yoho.queue.dal.IDrawlineLuckyUserDAO;
|
|
|
import com.yoho.queue.dal.IDrawlinePrizeSettingDAO;
|
|
|
import com.yoho.queue.dal.IDrawlineUserQueueDAO;
|
|
|
import com.yoho.queue.dal.model.DrawlineActivity;
|
|
|
import com.yoho.queue.dal.model.DrawlineActivityLuckydraw;
|
|
|
import com.yoho.queue.dal.model.DrawlineLuckyUser;
|
|
|
import com.yoho.queue.dal.model.DrawlinePrizeSetting;
|
|
|
import com.yoho.queue.dal.model.DrawlineUserQueue;
|
|
|
import com.yoho.service.model.activity.drawline.request.InboxReqBO;
|
|
|
|
|
|
@Service
|
|
|
public class TimeTaskServiceImpl implements ITimeTaskService {
|
|
|
private Logger logger = LoggerFactory.getLogger(TimeTaskServiceImpl.class);
|
|
|
|
|
|
private static final String SEND_INBOX_SERVICE_URL = "message.saveInbox";
|
|
|
|
|
|
private static final String LIMITCODE_MSG_TITLE = "【限购码】您获得了1个限购码";
|
|
|
|
|
|
private static final String LIMITCODE_MSG_CONTENT = "尊敬的客户:恭喜您成功获得1个限购码!您可以在个人中心查看详情和使用。";
|
|
|
|
|
|
@Autowired
|
|
|
private IDrawlineActivityDAO drawlineActivityDAO;
|
|
|
@Autowired
|
|
|
private IDrawlinePrizeSettingDAO drawlinePrizeSettingDAO;
|
|
|
@Autowired
|
|
|
private IDrawlineUserQueueDAO drawlineUserQueueDAO;
|
|
|
@Autowired
|
|
|
private IDrawlineLuckyUserDAO drawlineLuckyUserDAO;
|
|
|
@Autowired
|
|
|
private IDrawlineActivityLuckydrawDAO drawlineActivityLuckydrawDAO;
|
|
|
|
|
|
@Autowired
|
|
|
private IDrawlineLuckyUserDAO luckyUserDAO;
|
|
|
@Autowired
|
|
|
private IDrawlinePrizeSettingDAO prizeSettingDAO;
|
|
|
|
|
|
@Autowired
|
|
|
ServiceCaller serviceCaller;
|
|
|
|
|
|
private static final List<Integer> TIMETASK_LUCKYDRAW_LIST = new ArrayList<Integer>();
|
|
|
|
|
|
public JSONArray luckyDraw() {
|
|
|
logger.info("Enter luckyDraw.");
|
|
|
|
|
|
// (1)查询未成功抽奖的活动
|
|
|
List<DrawlineActivity> drawlineActivityList = drawlineActivityDAO.selectByNotLuckydraw();
|
|
|
logger.debug("luckyDraw: drawlineActivityDAO.selectByNotLuckydraw.");
|
|
|
if (null == drawlineActivityList || 0 == drawlineActivityList.size()) {
|
|
|
return null;
|
|
|
}
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
|
|
|
// (2)遍历未完成抽奖活动,进行抽奖动作(1.对过期活动立即抽奖;2.对未过期活动新建延迟任务进行抽奖)
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
Timer timer = new Timer();
|
|
|
for (DrawlineActivity drawlineActivity : drawlineActivityList) {
|
|
|
if (null == drawlineActivity || null == drawlineActivity.getId() || null == drawlineActivity.getEndTime()) {
|
|
|
continue;
|
|
|
}
|
|
|
Integer id = drawlineActivity.getId();
|
|
|
long endTime = drawlineActivity.getEndTime().longValue() * 1000;
|
|
|
|
|
|
// 判断活动是否过期,过期立即执行抽奖,否则延迟到活动结束后两分钟执行抽奖
|
|
|
if (currentTime > endTime) {
|
|
|
jsonArray.add(luckyDraw(id, endTime));
|
|
|
} else {
|
|
|
if (TIMETASK_LUCKYDRAW_LIST.contains(id)) {
|
|
|
continue;
|
|
|
}
|
|
|
TIMETASK_LUCKYDRAW_LIST.add(id);
|
|
|
|
|
|
timer.schedule(new TimerTask() {
|
|
|
|
|
|
@Override
|
|
|
public void run() {
|
|
|
luckyDraw(id, endTime);
|
|
|
}
|
|
|
}, endTime - currentTime + 120000);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return jsonArray;
|
|
|
}
|
|
|
|
|
|
private JSONObject luckyDraw(Integer id, long endTime) {
|
|
|
logger.info("luckyDraw. param id is {}, endTiem is {}", id, endTime);
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
jsonObject.put("id", id);
|
|
|
jsonObject.put("endTime", endTime);
|
|
|
|
|
|
// (1)查询奖品设置信息
|
|
|
DrawlinePrizeSetting drawlinePrizeSetting = drawlinePrizeSettingDAO.selectByActivityId(id);
|
|
|
logger.debug("luckyDraw: drawlinePrizeSettingDAO.selectByActivityId. activityId is {}, drawlinePrizeSetting is {}", id, drawlinePrizeSetting);
|
|
|
if (null == drawlinePrizeSetting || null == drawlinePrizeSetting.getRealUserNum() || null == drawlinePrizeSetting.getSockUserNum()) {
|
|
|
return jsonObject;
|
|
|
}
|
|
|
|
|
|
// (2)获取白名单排队列表,并用作最终中奖的排队列表
|
|
|
String whileList = drawlinePrizeSetting.getWhiteList();
|
|
|
List<DrawlineUserQueue> drawlineUserQueueList = drawlineUserQueueDAO.selectRealUserQueueByActIdAndUids(id, whileList);
|
|
|
logger.debug("luckyDraw: drawlineUserQueueDAO.selectRealUserQueueByActIdAndUids. activityId is {}, uids is {}", id, whileList);
|
|
|
if (null == drawlineUserQueueList) {
|
|
|
drawlineUserQueueList = new ArrayList<DrawlineUserQueue>();
|
|
|
}
|
|
|
|
|
|
// (3)获取真实用户中奖数量,中奖排队列表,并加入到最终中奖的排队列表中
|
|
|
int realUserNum = drawlinePrizeSetting.getRealUserNum().intValue() - drawlineUserQueueList.size();
|
|
|
if (0 < realUserNum) {
|
|
|
List<DrawlineUserQueue> realUserDrawlineUserQueueList = drawlineUserQueueDAO.selectRealUserQueueByRand(id, whileList, realUserNum);
|
|
|
logger.debug("luckyDraw: drawlineUserQueueDAO.selectRealUserQueueByRand. activityId is {}, uids is {}, limit is {}", id, whileList, realUserNum);
|
|
|
if (null != realUserDrawlineUserQueueList) {
|
|
|
drawlineUserQueueList.addAll(realUserDrawlineUserQueueList);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// (4)获取马甲用户中奖数量,中奖排队列表,并加入到最终中奖的排队列表当中
|
|
|
int sockUserNum = drawlinePrizeSetting.getSockUserNum().intValue();
|
|
|
if (0 < sockUserNum) {
|
|
|
List<DrawlineUserQueue> sockUserDrawlineUserQueueList = drawlineUserQueueDAO.selectByRand(id, (byte) 2, sockUserNum);
|
|
|
logger.debug("luckyDraw: drawlineUserQueueDAO.selectByRand. activityId is {}, userType is 2, limit is {}", id, sockUserNum);
|
|
|
if (null != sockUserDrawlineUserQueueList) {
|
|
|
drawlineUserQueueList.addAll(sockUserDrawlineUserQueueList);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// (5)如果中奖排队列表为空,说明无人中奖,终止任务
|
|
|
if (0 == drawlineUserQueueList.size()) {
|
|
|
return jsonObject;
|
|
|
}
|
|
|
|
|
|
// (6)把中奖排队列表插入到中奖用户表中
|
|
|
List<DrawlineLuckyUser> drawlineLuckyUserList = new ArrayList<DrawlineLuckyUser>();
|
|
|
int currTime = (int) (System.currentTimeMillis() / 1000);
|
|
|
for (DrawlineUserQueue drawlineUserQueue : drawlineUserQueueList) {
|
|
|
if (null == drawlineUserQueue || null == drawlineUserQueue.getUid() || null == drawlineUserQueue.getUserType() || null == drawlineUserQueue.getCreateTime()) {
|
|
|
continue;
|
|
|
}
|
|
|
drawlineLuckyUserList.add(DrawlineLuckyUserConvert.toDOBy(id, currTime - drawlineUserQueue.getCreateTime(), currTime, drawlineUserQueue.getUid(), drawlineUserQueue.getUserType()));
|
|
|
}
|
|
|
|
|
|
// (7)删除该活动中奖用户数据数据
|
|
|
int deleteDrawlineLuckyUserRows = drawlineLuckyUserDAO.deleteByActivityId(id);
|
|
|
logger.debug("luckyDraw: drawlineLuckyUserDAO.deleteByActivityId. activityId is {}, deleteDrawlineLuckyUserRows is {}", id, deleteDrawlineLuckyUserRows);
|
|
|
|
|
|
// (8)插入中奖用户数据
|
|
|
int drawlineLuckyUserRows = drawlineLuckyUserDAO.insertBatch(drawlineLuckyUserList);
|
|
|
logger.info("luckyDraw: drawlineLuckyUserDAO.insertBatch. drawlineLuckyUserList size is {}, drawlineLuckyUserRows is {}", drawlineLuckyUserList.size(), drawlineLuckyUserRows);
|
|
|
|
|
|
// (9)删除活动抽奖状态数据
|
|
|
int deleteDrawlineActivityLuckydrawRows = drawlineActivityLuckydrawDAO.deleteByPrimaryKey(id);
|
|
|
logger.debug("luckyDraw: drawlineActivityLuckydrawDAO.deleteByPrimaryKey. id is {}, deleteDrawlineActivityLuckydrawRows is {}", id, deleteDrawlineActivityLuckydrawRows);
|
|
|
|
|
|
// (10)插入活动抽奖状态数据
|
|
|
DrawlineActivityLuckydraw drawlineActivityLuckydraw = DrawlineActivityLuckydrawConvert.toDOBy(id, (int) (endTime / 1000), currTime);
|
|
|
int drawlineActivityLuckydrawRows = drawlineActivityLuckydrawDAO.insert(drawlineActivityLuckydraw);
|
|
|
logger.debug("luckyDraw: drawlineActivityLuckydrawDAO.insert. drawlineActivityLuckydraw is {}, drawlineActivityLuckydrawRows is {}", drawlineActivityLuckydraw, drawlineActivityLuckydrawRows);
|
|
|
|
|
|
// (11)返回
|
|
|
jsonObject.put("deleteDrawlineLuckyUserRows", deleteDrawlineLuckyUserRows);
|
|
|
jsonObject.put("drawlineLuckyUserRows", drawlineLuckyUserRows);
|
|
|
jsonObject.put("deleteDrawlineActivityLuckydrawRows", deleteDrawlineActivityLuckydrawRows);
|
|
|
jsonObject.put("drawlineActivityLuckydrawRows", drawlineActivityLuckydrawRows);
|
|
|
return jsonObject;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public JSONArray luckyUserNotice() {
|
|
|
logger.info("Enter luckyUserNotice service.");
|
|
|
|
|
|
JSONArray res = new JSONArray();
|
|
|
//(1) 查询未发送站内信中奖用户参与活动的ID
|
|
|
List<Integer> actIdList = luckyUserDAO.selectUnNoticeActivityIds();
|
|
|
logger.debug("luckyUserDAO.selectUnNoticeActivityIds: result is {}", actIdList);
|
|
|
if(null == actIdList || 0 == actIdList.size()){
|
|
|
return res;
|
|
|
}
|
|
|
for(Integer actId : actIdList){
|
|
|
//(2) 查询活动奖品设置
|
|
|
DrawlinePrizeSetting prizeSetting = prizeSettingDAO.selectByActivityId(actId);
|
|
|
logger.debug("prizeSettingDAO.selectByActivityId: articleId is {}", actId);
|
|
|
if(null == prizeSetting){
|
|
|
logger.warn("prize is not setted artivityId is {}", actId);
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
//(3) 查询中奖用户列表
|
|
|
List<DrawlineLuckyUser> luckyUserList = luckyUserDAO.selectNotcieUserByActId(actId);
|
|
|
logger.debug("luckyUserDAO.selectNotcieUserByActId: articleId is {}", actId);
|
|
|
if(null == luckyUserList || 0 == luckyUserList.size()){
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
//(4) 遍历中奖用户列表发送站内信
|
|
|
for(DrawlineLuckyUser luckyUser : luckyUserList){
|
|
|
if(null == luckyUser || null == luckyUser.getUid() || 0 == luckyUser.getUid()){
|
|
|
continue;
|
|
|
}
|
|
|
int luckyUserId = luckyUser.getUid();
|
|
|
InboxReqBO inboxReqBO = new InboxReqBO();
|
|
|
inboxReqBO.setUid(luckyUserId);
|
|
|
inboxReqBO.setType("1");
|
|
|
inboxReqBO.setTitle(LIMITCODE_MSG_TITLE);
|
|
|
inboxReqBO.setContent(LIMITCODE_MSG_CONTENT);
|
|
|
try{
|
|
|
//(4.1) 调用发送站内信服务
|
|
|
ResponseBean resBean = serviceCaller.call(SEND_INBOX_SERVICE_URL, inboxReqBO, ResponseBean.class);
|
|
|
if(null != resBean && resBean.getCode().equals("200")){
|
|
|
//(4.2) 更新中奖表站内信发送状态
|
|
|
int num = luckyUserDAO.updateByActIdAndUid(actId, luckyUserId);
|
|
|
logger.debug("luckyUserDAO.updateByActIdAndUids, activityId is {}, uid is {}", actId, luckyUserId);
|
|
|
if(num > 0){
|
|
|
JSONObject userObj = new JSONObject();
|
|
|
userObj.put("luckyUserId", luckyUserId);
|
|
|
res.add(userObj);
|
|
|
logger.info("send lucky user inbox success, uid is {}", luckyUserId);
|
|
|
}
|
|
|
}
|
|
|
}catch(Exception e){
|
|
|
logger.error("send luckyUser inbox failed", e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
logger.info("Leave LuckyUserNotice service: result is{}", res);
|
|
|
return res;
|
|
|
}
|
|
|
} |
...
|
...
|
|