From e303a4a6cb9fcbda32f1fa9bc9af7ded39ae05e8 Mon Sep 17 00:00:00 2001 From: linfeng <2445465217@qq.com> Date: Sun, 22 May 2022 22:48:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=B6=88=E6=81=AF=E6=B8=85?= =?UTF-8?q?=E7=90=86=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/linfeng/common/utils/DateUtils.java | 8 +++- .../modules/admin/service/MessageService.java | 1 + .../service/impl/MessageServiceImpl.java | 18 +++++++-- .../linfeng/modules/job/task/MessageTask.java | 37 +++++++++++++++++++ 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/linfeng/modules/job/task/MessageTask.java diff --git a/src/main/java/io/linfeng/common/utils/DateUtils.java b/src/main/java/io/linfeng/common/utils/DateUtils.java index e006e08..498116d 100644 --- a/src/main/java/io/linfeng/common/utils/DateUtils.java +++ b/src/main/java/io/linfeng/common/utils/DateUtils.java @@ -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); } /** diff --git a/src/main/java/io/linfeng/modules/admin/service/MessageService.java b/src/main/java/io/linfeng/modules/admin/service/MessageService.java index 678a76f..a539999 100644 --- a/src/main/java/io/linfeng/modules/admin/service/MessageService.java +++ b/src/main/java/io/linfeng/modules/admin/service/MessageService.java @@ -31,5 +31,6 @@ public interface MessageService extends IService { Boolean status(Integer type, Integer uid); + void deleteMessageByMonth(Integer integer); } diff --git a/src/main/java/io/linfeng/modules/admin/service/impl/MessageServiceImpl.java b/src/main/java/io/linfeng/modules/admin/service/impl/MessageServiceImpl.java index d3c7392..6e058be 100644 --- a/src/main/java/io/linfeng/modules/admin/service/impl/MessageServiceImpl.java +++ b/src/main/java/io/linfeng/modules/admin/service/impl/MessageServiceImpl.java @@ -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 i return update(updateWrapper); } + /** + * 清除几个月前的消息数据 + * @param month + */ + @Override + public void deleteMessageByMonth(Integer month) { + String s = DateUtils.addDateMonths(new Date(),-month); + LambdaQueryWrapper wrapper=new LambdaQueryWrapper<>(); + wrapper.le(MessageEntity::getCreateTime,s); + this.remove(wrapper); + } } \ No newline at end of file diff --git a/src/main/java/io/linfeng/modules/job/task/MessageTask.java b/src/main/java/io/linfeng/modules/job/task/MessageTask.java new file mode 100644 index 0000000..4b40c23 --- /dev/null +++ b/src/main/java/io/linfeng/modules/job/task/MessageTask.java @@ -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)); + } + } +} \ No newline at end of file