package model import ( "database/sql" "fmt" "strings" "git.i2edu.net/i2/go-zero/core/stores/cache" "git.i2edu.net/i2/go-zero/core/stores/sqlc" "git.i2edu.net/i2/go-zero/core/stores/sqlx" "git.i2edu.net/i2/go-zero/core/stringx" "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/builderx" ) var ( excelExportLogFieldNames = builderx.RawFieldNames(&ExcelExportLog{}) excelExportLogRows = strings.Join(excelExportLogFieldNames, ",") excelExportLogRowsExpectAutoSet = strings.Join(stringx.Remove(excelExportLogFieldNames, "`id`", "`create_time`", "`update_time`"), ",") excelExportLogRowsWithPlaceHolder = strings.Join(stringx.Remove(excelExportLogFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?" cacheExcelExportLogIdPrefix = "cache:excelExportLog:id:" ) type ( ExcelExportLogModel interface { Insert(data ExcelExportLog) (sql.Result, error) FindOne(id int64) (*ExcelExportLog, error) Update(data ExcelExportLog) error Delete(id int64) error } defaultExcelExportLogModel struct { sqlc.CachedConn table string } ExcelExportLog struct { Cond string `db:"cond"` CreateTime sql.NullTime `db:"create_time"` LastUpdateTime sql.NullTime `db:"last_update_time"` CreateBy sql.NullString `db:"create_by"` LastUpdateBy sql.NullString `db:"last_update_by"` DelFlag int64 `db:"del_flag"` Id int64 `db:"id"` Module sql.NullString `db:"module"` } ) func NewExcelExportLogModel(conn sqlx.SqlConn, c cache.CacheConf) ExcelExportLogModel { return &defaultExcelExportLogModel{ CachedConn: sqlc.NewConn(conn, c), table: "`excel_export_log`", } } func (m *defaultExcelExportLogModel) Insert(data ExcelExportLog) (sql.Result, error) { query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, excelExportLogRowsExpectAutoSet) ret, err := m.ExecNoCache(query, data.Cond, data.LastUpdateTime, data.CreateBy, data.LastUpdateBy, data.DelFlag, data.Module) return ret, err } func (m *defaultExcelExportLogModel) FindOne(id int64) (*ExcelExportLog, error) { excelExportLogIdKey := fmt.Sprintf("%s%v", cacheExcelExportLogIdPrefix, id) var resp ExcelExportLog err := m.QueryRow(&resp, excelExportLogIdKey, func(conn sqlx.SqlConn, v interface{}) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", excelExportLogRows, m.table) return conn.QueryRow(v, query, id) }) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultExcelExportLogModel) Update(data ExcelExportLog) error { excelExportLogIdKey := fmt.Sprintf("%s%v", cacheExcelExportLogIdPrefix, data.Id) _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, excelExportLogRowsWithPlaceHolder) return conn.Exec(query, data.Cond, data.LastUpdateTime, data.CreateBy, data.LastUpdateBy, data.DelFlag, data.Module, data.Id) }, excelExportLogIdKey) return err } func (m *defaultExcelExportLogModel) Delete(id int64) error { excelExportLogIdKey := fmt.Sprintf("%s%v", cacheExcelExportLogIdPrefix, id) _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("delete from %s where `id` = ?", m.table) return conn.Exec(query, id) }, excelExportLogIdKey) return err } func (m *defaultExcelExportLogModel) formatPrimary(primary interface{}) string { return fmt.Sprintf("%s%v", cacheExcelExportLogIdPrefix, primary) } func (m *defaultExcelExportLogModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", excelExportLogRows, m.table) return conn.QueryRow(v, query, primary) }