|
|
package com.yoho.yhmessage.push.service.impl;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import org.apache.commons.lang.time.DateUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.yoho.core.redis.YHListOperations;
|
|
|
import com.yoho.core.redis.YHRedisTemplate;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.message.dal.IRecycleTaskDefineDao;
|
|
|
import com.yoho.message.dal.IRecycleTaskUserDao;
|
|
|
import com.yoho.message.dal.model.RecycleTaskDefine;
|
|
|
import com.yoho.message.dal.model.RecycleTaskUser;
|
|
|
import com.yoho.service.model.push.RecycleTaskUserBO;
|
|
|
import com.yoho.service.model.push.RecyleTaskForbidUserBO;
|
|
|
import com.yoho.service.model.response.CommonRspBO;
|
|
|
import com.yoho.yhmessage.push.service.IRecycleTaskUserService;
|
|
|
import com.yoho.yhmessage.push.utils.CronUtil;
|
|
|
import com.yoho.yhmessage.push.utils.RedisKeyUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
@Service
|
|
|
public class RecycleTaskUserServiceImpl implements IRecycleTaskUserService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(RecycleTaskUserServiceImpl.class);
|
|
|
|
|
|
@Autowired
|
|
|
private IRecycleTaskDefineDao recycleTaskDefineDao;
|
|
|
@Autowired
|
|
|
private IRecycleTaskUserDao recycleTaskUserDao;
|
|
|
@Resource(name = "yhListOperations")
|
|
|
private YHListOperations<String, String> listOperations;
|
|
|
@Resource(name = "yhRedisTemplate")
|
|
|
private YHRedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
@Override
|
|
|
public CommonRspBO changeTaskPushFlag(RecycleTaskUserBO recycleTaskUserBO) throws ServiceException {
|
|
|
// 1)参数验证
|
|
|
String taskKey = recycleTaskUserBO.getTaskKey();
|
|
|
int userId = recycleTaskUserBO.getUserId();
|
|
|
if (StringUtils.isEmpty(taskKey)) {
|
|
|
throw new ServiceException(ServiceError.PUSH_RECYCLETASK_TASKKEY_EMPTY);
|
|
|
}
|
|
|
if (userId <= 0) {
|
|
|
throw new ServiceException(ServiceError.PUSH_RECYCLETASK_UID_EMPTY);
|
|
|
}
|
|
|
|
|
|
// 2)检查task_key是否存在
|
|
|
RecycleTaskDefine recycleTaskDefine = recycleTaskDefineDao.selectByTaskKey(taskKey, 1);
|
|
|
if (recycleTaskDefine == null) {
|
|
|
throw new ServiceException(ServiceError.PUSH_RECYCLETASK_TASKKEY_NOTEXIST);
|
|
|
}
|
|
|
|
|
|
// 3)根据task_key和uid取数据
|
|
|
int currenSecond = this.getCurrentSecond();
|
|
|
RecycleTaskUser recycleTaskUser = recycleTaskUserDao.selectByUniqueKey(taskKey, userId);
|
|
|
|
|
|
// 4)如果记录不存在,则直接插入
|
|
|
if (recycleTaskUser == null) {
|
|
|
recycleTaskUser = new RecycleTaskUser();
|
|
|
recycleTaskUser.setTaskKey(taskKey);
|
|
|
recycleTaskUser.setUserId(userId);
|
|
|
recycleTaskUser.setStatus(recycleTaskUserBO.getPushFlag());
|
|
|
recycleTaskUser.setCreateTime(currenSecond);
|
|
|
recycleTaskUser.setUpdateTime(currenSecond);
|
|
|
recycleTaskUserDao.insertSelective(recycleTaskUser);
|
|
|
} else {
|
|
|
// 否则直接更新状态
|
|
|
recycleTaskUserDao.updateStatusByPrimaryKey(recycleTaskUser.getId(), recycleTaskUserBO.getPushFlag(), currenSecond);
|
|
|
}
|
|
|
return new CommonRspBO(200, "更新成功");
|
|
|
}
|
|
|
|
|
|
private int getCurrentSecond() {
|
|
|
long nowTs = System.currentTimeMillis();
|
|
|
return (int) (nowTs / 1000L);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public RecycleTaskUserBO getRecycleTaskUser(String taskKey, int userId) throws ServiceException {
|
|
|
// 1)参数验证
|
|
|
if (StringUtils.isEmpty(taskKey)) {
|
|
|
throw new ServiceException(ServiceError.PUSH_RECYCLETASK_TASKKEY_EMPTY);
|
|
|
}
|
|
|
if (userId <= 0) {
|
|
|
throw new ServiceException(ServiceError.PUSH_RECYCLETASK_UID_EMPTY);
|
|
|
}
|
|
|
|
|
|
// 2)取数据
|
|
|
RecycleTaskUser recycleTaskUser = recycleTaskUserDao.selectByUniqueKey(taskKey, userId);
|
|
|
|
|
|
// 3)不存在则直接返回pushFlag = 0的数据
|
|
|
if (recycleTaskUser == null) {
|
|
|
return new RecycleTaskUserBO(taskKey, userId, 0);
|
|
|
}
|
|
|
|
|
|
// 4)根据recycleTaskUser生成RecycleTaskUserBO
|
|
|
return new RecycleTaskUserBO(taskKey, userId, recycleTaskUser.getStatus());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public CommonRspBO addRecycleTaskForbidUser(RecyleTaskForbidUserBO recyleTaskForbidUserBO) throws ServiceException {
|
|
|
// 1)参数验证
|
|
|
String taskKey = recyleTaskForbidUserBO.getTaskKey();
|
|
|
if (StringUtils.isEmpty(recyleTaskForbidUserBO.getTaskKey())) {
|
|
|
throw new ServiceException(ServiceError.PUSH_RECYCLETASK_TASKKEY_EMPTY);
|
|
|
}
|
|
|
int userId = recyleTaskForbidUserBO.getUserId();
|
|
|
if (userId <= 0) {
|
|
|
throw new ServiceException(ServiceError.PUSH_RECYCLETASK_UID_EMPTY);
|
|
|
}
|
|
|
// 2)检查task_key是否存在
|
|
|
RecycleTaskDefine recycleTaskDefine = recycleTaskDefineDao.selectByTaskKey(taskKey, 1);
|
|
|
if (recycleTaskDefine == null) {
|
|
|
throw new ServiceException(ServiceError.PUSH_RECYCLETASK_TASKKEY_NOTEXIST);
|
|
|
}
|
|
|
|
|
|
// 3)如果该任务下次发送时间为空,表明任务无效,则直接返回成功
|
|
|
Date nextSendTime = CronUtil.getNextRunTime(recycleTaskDefine.getCronExpression(), recycleTaskDefine.getBeginTime(), recycleTaskDefine.getEndTime());
|
|
|
if (nextSendTime == null) {
|
|
|
return new CommonRspBO(200, "成功");
|
|
|
}
|
|
|
|
|
|
// 4)如果跨天无效,则直接返回
|
|
|
boolean clearWhenDifferDay = recyleTaskForbidUserBO.isClearWhenDifferDay();
|
|
|
if(clearWhenDifferDay && !isSameDay(nextSendTime, new Date())){
|
|
|
return new CommonRspBO(200, "成功");
|
|
|
}
|
|
|
|
|
|
// 5)将forbidUserId插入redis,并设置sendTime的一个小时后过期(重复设置过期时间也没关系的)
|
|
|
String redisKey = RedisKeyUtils.genRecycleTaskForbidUserKey(taskKey, nextSendTime);
|
|
|
listOperations.rightPush(redisKey, String.valueOf(userId));
|
|
|
long expired = nextSendTime.getTime() - System.currentTimeMillis() + DateUtils.MILLIS_PER_HOUR;
|
|
|
redisTemplate.longExpire(redisKey, expired , TimeUnit.MILLISECONDS);
|
|
|
|
|
|
logger.info("add recycleTaskForbidUser to redis , redisKey is{}, uid is {}",redisKey,userId);
|
|
|
// List<String> allForbidUserIds = listOperations.range(redisKey, 0, -1);
|
|
|
// logger.info("add recycleTaskForbidUser to redis , redisKey is{}, uid is {},allForbidUserIds is {}",redisKey,userId,allForbidUserIds);
|
|
|
return new CommonRspBO(200, "成功");
|
|
|
}
|
|
|
|
|
|
private boolean isSameDay(Date d1,Date d2){
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
String d1DayStr = sdf.format(d1);
|
|
|
String d2DayStr = sdf.format(d2);
|
|
|
if(d1DayStr.equals(d2DayStr)){
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
} |