// Code generated by goctl. DO NOT EDIT. // versions: // goctl version: 1.9.2 package model import ( "context" "database/sql" "fmt" "strings" "time" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stringx" ) var ( sysOperationLogFieldNames = builder.RawFieldNames(&SysOperationLog{}) sysOperationLogRows = strings.Join(sysOperationLogFieldNames, ",") sysOperationLogRowsExpectAutoSet = strings.Join(stringx.Remove(sysOperationLogFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") sysOperationLogRowsWithPlaceHolder = strings.Join(stringx.Remove(sysOperationLogFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" cacheSysOperationLogIdPrefix = "cache:sysOperationLog:id:" cacheSysOperationLogLogIdPrefix = "cache:sysOperationLog:logId:" ) type ( sysOperationLogModel interface { Insert(ctx context.Context, data *SysOperationLog) (sql.Result, error) FindOne(ctx context.Context, id int64) (*SysOperationLog, error) FindOneByLogId(ctx context.Context, logId string) (*SysOperationLog, error) Update(ctx context.Context, data *SysOperationLog) error Delete(ctx context.Context, id int64) error } defaultSysOperationLogModel struct { sqlc.CachedConn table string } SysOperationLog struct { Id int64 `db:"id"` LogId string `db:"log_id"` UserId string `db:"user_id"` Username string `db:"username"` Module string `db:"module"` Operation string `db:"operation"` Method string `db:"method"` Path string `db:"path"` RequestBody sql.NullString `db:"request_body"` ResponseBody sql.NullString `db:"response_body"` Ip string `db:"ip"` UserAgent string `db:"user_agent"` Duration int64 `db:"duration"` // milliseconds Status int64 `db:"status"` // 1:success 0:fail CreatedAt time.Time `db:"created_at"` } ) func newSysOperationLogModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultSysOperationLogModel { return &defaultSysOperationLogModel{ CachedConn: sqlc.NewConn(conn, c, opts...), table: "`sys_operation_log`", } } func (m *defaultSysOperationLogModel) Delete(ctx context.Context, id int64) error { data, err := m.FindOne(ctx, id) if err != nil { return err } sysOperationLogIdKey := fmt.Sprintf("%s%v", cacheSysOperationLogIdPrefix, id) sysOperationLogLogIdKey := fmt.Sprintf("%s%v", cacheSysOperationLogLogIdPrefix, data.LogId) _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("delete from %s where `id` = ?", m.table) return conn.ExecCtx(ctx, query, id) }, sysOperationLogIdKey, sysOperationLogLogIdKey) return err } func (m *defaultSysOperationLogModel) FindOne(ctx context.Context, id int64) (*SysOperationLog, error) { sysOperationLogIdKey := fmt.Sprintf("%s%v", cacheSysOperationLogIdPrefix, id) var resp SysOperationLog err := m.QueryRowCtx(ctx, &resp, sysOperationLogIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysOperationLogRows, m.table) return conn.QueryRowCtx(ctx, v, query, id) }) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultSysOperationLogModel) FindOneByLogId(ctx context.Context, logId string) (*SysOperationLog, error) { sysOperationLogLogIdKey := fmt.Sprintf("%s%v", cacheSysOperationLogLogIdPrefix, logId) var resp SysOperationLog err := m.QueryRowIndexCtx(ctx, &resp, sysOperationLogLogIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { query := fmt.Sprintf("select %s from %s where `log_id` = ? limit 1", sysOperationLogRows, m.table) if err := conn.QueryRowCtx(ctx, &resp, query, logId); err != nil { return nil, err } return resp.Id, nil }, m.queryPrimary) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultSysOperationLogModel) Insert(ctx context.Context, data *SysOperationLog) (sql.Result, error) { sysOperationLogIdKey := fmt.Sprintf("%s%v", cacheSysOperationLogIdPrefix, data.Id) sysOperationLogLogIdKey := fmt.Sprintf("%s%v", cacheSysOperationLogLogIdPrefix, data.LogId) ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, sysOperationLogRowsExpectAutoSet) return conn.ExecCtx(ctx, query, data.LogId, data.UserId, data.Username, data.Module, data.Operation, data.Method, data.Path, data.RequestBody, data.ResponseBody, data.Ip, data.UserAgent, data.Duration, data.Status) }, sysOperationLogIdKey, sysOperationLogLogIdKey) return ret, err } func (m *defaultSysOperationLogModel) Update(ctx context.Context, newData *SysOperationLog) error { data, err := m.FindOne(ctx, newData.Id) if err != nil { return err } sysOperationLogIdKey := fmt.Sprintf("%s%v", cacheSysOperationLogIdPrefix, data.Id) sysOperationLogLogIdKey := fmt.Sprintf("%s%v", cacheSysOperationLogLogIdPrefix, data.LogId) _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, sysOperationLogRowsWithPlaceHolder) return conn.ExecCtx(ctx, query, newData.LogId, newData.UserId, newData.Username, newData.Module, newData.Operation, newData.Method, newData.Path, newData.RequestBody, newData.ResponseBody, newData.Ip, newData.UserAgent, newData.Duration, newData.Status, newData.Id) }, sysOperationLogIdKey, sysOperationLogLogIdKey) return err } func (m *defaultSysOperationLogModel) formatPrimary(primary any) string { return fmt.Sprintf("%s%v", cacheSysOperationLogIdPrefix, primary) } func (m *defaultSysOperationLogModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysOperationLogRows, m.table) return conn.QueryRowCtx(ctx, v, query, primary) } func (m *defaultSysOperationLogModel) tableName() string { return m.table }