72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
|
|
package model
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
||
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||
|
|
)
|
||
|
|
|
||
|
|
var _ SysOperationLogModel = (*customSysOperationLogModel)(nil)
|
||
|
|
|
||
|
|
type (
|
||
|
|
SysOperationLogModel interface {
|
||
|
|
sysOperationLogModel
|
||
|
|
FindList(ctx context.Context, page, pageSize int64, username, module, operation, startTime, endTime string) ([]*SysOperationLog, int64, error)
|
||
|
|
}
|
||
|
|
|
||
|
|
customSysOperationLogModel struct {
|
||
|
|
*defaultSysOperationLogModel
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func NewSysOperationLogModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) SysOperationLogModel {
|
||
|
|
return &customSysOperationLogModel{
|
||
|
|
defaultSysOperationLogModel: newSysOperationLogModel(conn, c, opts...),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customSysOperationLogModel) FindList(ctx context.Context, page, pageSize int64, username, module, operation, startTime, endTime string) ([]*SysOperationLog, int64, error) {
|
||
|
|
where := "WHERE 1=1"
|
||
|
|
args := make([]interface{}, 0)
|
||
|
|
|
||
|
|
if username != "" {
|
||
|
|
where += " AND username LIKE ?"
|
||
|
|
args = append(args, "%"+username+"%")
|
||
|
|
}
|
||
|
|
if module != "" {
|
||
|
|
where += " AND module = ?"
|
||
|
|
args = append(args, module)
|
||
|
|
}
|
||
|
|
if operation != "" {
|
||
|
|
where += " AND operation LIKE ?"
|
||
|
|
args = append(args, "%"+operation+"%")
|
||
|
|
}
|
||
|
|
if startTime != "" {
|
||
|
|
where += " AND created_at >= ?"
|
||
|
|
args = append(args, startTime)
|
||
|
|
}
|
||
|
|
if endTime != "" {
|
||
|
|
where += " AND created_at <= ?"
|
||
|
|
args = append(args, endTime)
|
||
|
|
}
|
||
|
|
|
||
|
|
var total int64
|
||
|
|
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s %s", m.table, where)
|
||
|
|
err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, args...)
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, err
|
||
|
|
}
|
||
|
|
|
||
|
|
var list []*SysOperationLog
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ? OFFSET ?", sysOperationLogRows, m.table, where)
|
||
|
|
args = append(args, pageSize, (page-1)*pageSize)
|
||
|
|
err = m.QueryRowsNoCacheCtx(ctx, &list, query, args...)
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return list, total, nil
|
||
|
|
}
|