新增消息清理定时任务

This commit is contained in:
linfeng 2022-05-22 22:48:10 +08:00
parent 28dd5323c9
commit e303a4a6cb
4 changed files with 58 additions and 6 deletions

View File

@ -132,6 +132,7 @@ public class DateUtils {
return dateTime.plusWeeks(weeks).toDate();
}
/**
* 对日期的进行加/
*
@ -139,9 +140,12 @@ public class DateUtils {
* @param months 月数负数为减
* @return /减几月后的日期
*/
public static Date addDateMonths(Date date, int months) {
public static String addDateMonths(Date date, int months) {
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
DateTime dateTime = new DateTime(date);
return dateTime.plusMonths(months).toDate();
Date date1 = dateTime.plusMonths(months).toDate();
return simpleDateFormat.format(date1);
}
/**

View File

@ -31,5 +31,6 @@ public interface MessageService extends IService<MessageEntity> {
Boolean status(Integer type, Integer uid);
void deleteMessageByMonth(Integer integer);
}

View File

@ -1,19 +1,18 @@
package io.linfeng.modules.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.linfeng.common.utils.Constant;
import io.linfeng.common.utils.DateUtil;
import io.linfeng.common.utils.*;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.linfeng.common.utils.PageUtils;
import io.linfeng.common.utils.Query;
import io.linfeng.modules.admin.dao.MessageDao;
import io.linfeng.modules.admin.entity.MessageEntity;
@ -104,5 +103,16 @@ public class MessageServiceImpl extends ServiceImpl<MessageDao, MessageEntity> i
return update(updateWrapper);
}
/**
* 清除几个月前的消息数据
* @param month
*/
@Override
public void deleteMessageByMonth(Integer month) {
String s = DateUtils.addDateMonths(new Date(),-month);
LambdaQueryWrapper<MessageEntity> wrapper=new LambdaQueryWrapper<>();
wrapper.le(MessageEntity::getCreateTime,s);
this.remove(wrapper);
}
}

View File

@ -0,0 +1,37 @@
package io.linfeng.modules.job.task;
import cn.hutool.core.util.NumberUtil;
import io.linfeng.modules.admin.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
*
* 定期清理所有消息定时任务
*
*
* @author linfeng
* @date 2022/5/22 22:43
*/
@Component("messageTask")
public class MessageTask implements ITask {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
@Lazy
private MessageService messageService;
@Override
public void run(String params){
logger.debug("messageTask定时任务正在执行参数为{}", params);
//必须是整数而且为正数代表清除几个月前的数据
if(NumberUtil.isInteger(params)&&Integer.valueOf(params)>0){
messageService.deleteMessageByMonth(Integer.valueOf(params));
}
}
}