新增一个日志清理接口,可根据需要创建job调用

This commit is contained in:
thinkgem
2023-10-26 11:27:50 +08:00
parent bc8a6d2db7
commit 31e6b01b4e
4 changed files with 50 additions and 1 deletions

View File

@@ -18,4 +18,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ConditionalOnProperty(name="user.enabled", havingValue="true", matchIfMissing=true)
public interface LogDao extends CrudDao<Log> {
/**
* 删除某个日期之前创建的日志
* @param log .createDate
*/
int deleteLogBefore(Log log);
}

View File

@@ -25,5 +25,13 @@ public interface LogService extends CrudServiceApi<Log> {
* 不使用数据库事务,执行插入日志
*/
void insertLog(Log entity);
/**
* 清理指定日期之前的日志可新建job定时调用
* 1、清理1年前的所有日志logService.deleteLogBefore(1, 0, 0);
* 2、清理6个月前的所有日志logService.deleteLogBefore(0, 6, 0);
* 3、清理7天前的所有日志logService.deleteLogBefore(0, 0, 7);
* 4、清理1年6个月前的所有日志logService.deleteLogBefore(1, 6, 0);
*/
void deleteLogBefore(Integer year, Integer months, Integer days);
}

View File

@@ -4,10 +4,13 @@
*/
package com.jeesite.modules.sys.service.support;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;
import com.jeesite.common.datasource.DataSourceHolder;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.sys.dao.LogDao;
import com.jeesite.modules.sys.entity.Log;
@@ -50,4 +53,30 @@ public class LogServiceSupport extends CrudService<LogDao, Log>
dao.insert(entity);
}
/**
* 清理指定日期之前的日志可新建job定时调用
* 1、清理1年前的所有日志logService.deleteLogBefore(1, 0, 0);
* 2、清理6个月前的所有日志logService.deleteLogBefore(0, 6, 0);
* 3、清理7天前的所有日志logService.deleteLogBefore(0, 0, 7);
* 4、清理1年6个月前的所有日志logService.deleteLogBefore(1, 6, 0);
*/
@Override
@Transactional
public void deleteLogBefore(Integer year, Integer months, Integer days) {
Date date = DateUtils.getOfDayLast(new Date());
if (year != null && year != 0) {
date = DateUtils.addYears(date, -year);
}
if (months != null && months != 0) {
date = DateUtils.addMonths(date, -months);
}
if (days != null && days != 0) {
date = DateUtils.addDays(date, -days);
}
Log log = new Log();
log.setCreateDate(date);
dao.deleteLogBefore(log);
}
}

View File

@@ -20,4 +20,10 @@
<if test="global.dbName == 'postgresql'"></if>
</select> -->
<!-- 删除某个日期之前创建的日志 -->
<delete id="deleteLogBefore">
DELETE FROM ${_prefix}sys_log
WHERE create_date &lt; #{createDate}
</delete>
</mapper>