🎉 初始化项目

This commit is contained in:
2026-03-03 06:05:51 +08:00
commit e1c70fe218
241 changed files with 148285 additions and 0 deletions

52
server/task/clearTable.go Normal file
View File

@@ -0,0 +1,52 @@
package task
import (
"errors"
"fmt"
"time"
"git.echol.cn/loser/ai_proxy/server/model/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",
})
ClearTableDetail = append(ClearTableDetail, common.ClearDB{
TableName: "jwt_blacklists",
CompareField: "created_at",
Interval: "168h",
})
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
}