后端重构
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"kra/internal/service/types/common"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// @author: [songzhibin97](https://github.com/songzhibin97)
|
||||
// @function: ClearTable
|
||||
// @description: 清理数据库表数据
|
||||
// @param: db(数据库对象) *gorm.DB, tableName(表名) string, compareField(比较字段) string, interval(间隔) string
|
||||
// @return: error
|
||||
|
||||
func ClearTable(db *gorm.DB) error {
|
||||
var ClearTableDetail []common.ClearDB
|
||||
|
||||
ClearTableDetail = append(ClearTableDetail, common.ClearDB{
|
||||
TableName: "sys_operation_records",
|
||||
CompareField: "created_at",
|
||||
Interval: "2160h", // 90天
|
||||
})
|
||||
|
||||
ClearTableDetail = append(ClearTableDetail, common.ClearDB{
|
||||
TableName: "jwt_blacklists",
|
||||
CompareField: "created_at",
|
||||
Interval: "168h", // 7天
|
||||
})
|
||||
|
||||
if db == nil {
|
||||
return errors.New("db Cannot be empty")
|
||||
}
|
||||
|
||||
for _, detail := range ClearTableDetail {
|
||||
duration, err := time.ParseDuration(detail.Interval)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if duration < 0 {
|
||||
return errors.New("parse duration < 0")
|
||||
}
|
||||
err = db.Debug().Exec(fmt.Sprintf("DELETE FROM %s WHERE %s < ?", detail.TableName, detail.CompareField), time.Now().Add(-duration)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user