新增消息清理定时任务
This commit is contained in:
parent
28dd5323c9
commit
e303a4a6cb
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,5 +31,6 @@ public interface MessageService extends IService<MessageEntity> {
|
||||
|
||||
Boolean status(Integer type, Integer uid);
|
||||
|
||||
void deleteMessageByMonth(Integer integer);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
37
src/main/java/io/linfeng/modules/job/task/MessageTask.java
Normal file
37
src/main/java/io/linfeng/modules/job/task/MessageTask.java
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user