90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package model
|
||
|
||
import (
|
||
"admin/internal/db"
|
||
"admin/internal/errcode"
|
||
"admin/internal/global"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
func init() {
|
||
db.RegisterTableModels(History{})
|
||
}
|
||
|
||
// History 用户执行历史
|
||
type History struct {
|
||
ID int `gorm:"primarykey" readonly:"true"`
|
||
UserId int `gorm:"index"`
|
||
UserName string `gorm:"index"`
|
||
OpResourceType string `gorm:"type:varchar(100);index"` // 操作资源类型:项目、角色、用户、封禁等
|
||
OpResourceGroup string `gorm:"type:varchar(100);index"` // 操作资源组名:系统、系统、系统、神魔大陆主播服等
|
||
OpResourceKey string `gorm:"type:varchar(100);index"` // 操作对象名:神魔大陆主播服、qa、chenshun、account123
|
||
Method string `gorm:"type:varchar(100);index"` // 操作方法:新增、修改、删除、一键停服
|
||
DetailInfo string `gorm:"type:longtext"` // 详情,涉及到的对象列表,例如新增就给表行数据
|
||
CreatedAt time.Time
|
||
}
|
||
|
||
func (m *History) TableName() string {
|
||
return "history"
|
||
}
|
||
|
||
func (m *History) GetId() int {
|
||
return m.ID
|
||
}
|
||
|
||
func (m *History) Create() error {
|
||
return global.GLOB_DB.Create(m).Error
|
||
}
|
||
|
||
func (m *History) List(pageNo, pageLen int, userId int, opType, opGroup, opKey, method string) ([]*History, int, error) {
|
||
if pageNo <= 0 || pageLen <= 0 || pageLen > 1000 {
|
||
return nil, 0, errcode.New(errcode.ParamsInvalid, "pageNo or pageLen invalid:%v,%v", pageNo, pageLen)
|
||
}
|
||
|
||
list := make([]*History, 0)
|
||
|
||
whereSqls := make([]string, 0)
|
||
whereArgs := make([]any, 0)
|
||
|
||
if userId != 0 {
|
||
whereSqls = append(whereSqls, "user_id=?")
|
||
whereArgs = append(whereArgs, userId)
|
||
}
|
||
if opType != "" {
|
||
whereSqls = append(whereSqls, "op_resource_type=?")
|
||
whereArgs = append(whereArgs, opType)
|
||
}
|
||
if opGroup != "" {
|
||
whereSqls = append(whereSqls, "op_resource_group=?")
|
||
whereArgs = append(whereArgs, opGroup)
|
||
}
|
||
if opKey != "" {
|
||
whereSqls = append(whereSqls, "op_resource_key=?")
|
||
whereArgs = append(whereArgs, opKey)
|
||
}
|
||
if method != "" {
|
||
whereSqls = append(whereSqls, "method=?")
|
||
whereArgs = append(whereArgs, method)
|
||
}
|
||
|
||
tx := global.GLOB_DB
|
||
txCount := global.GLOB_DB.Model(new(History))
|
||
if len(whereSqls) != 0 {
|
||
tx = tx.Where(strings.Join(whereSqls, " and "), whereArgs...)
|
||
txCount = txCount.Where(strings.Join(whereSqls, " and "), whereArgs...)
|
||
}
|
||
|
||
limitStart := (pageNo - 1) * pageLen
|
||
err := tx.Offset(limitStart).Limit(pageLen).Order("created_at desc").Find(&list).Error
|
||
if err != nil {
|
||
return nil, 0, errcode.New(errcode.DBError, "list error:%v", err)
|
||
}
|
||
totalCount := int64(0)
|
||
err = txCount.Count(&totalCount).Error
|
||
if err != nil {
|
||
return nil, 0, errcode.New(errcode.DBError, "count error:%v", err)
|
||
}
|
||
return list, int(totalCount), nil
|
||
}
|