|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- // Code generated by goctl. DO NOT EDIT.
-
- package models
-
- import (
- "context"
- "database/sql"
- "fmt"
- "strings"
-
- "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 (
- dictFieldNames = builder.RawFieldNames(&Dict{})
- dictRows = strings.Join(dictFieldNames, ",")
- dictRowsExpectAutoSet = strings.Join(stringx.Remove(dictFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
- dictRowsWithPlaceHolder = strings.Join(stringx.Remove(dictFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
-
- cachePcmDictIdPrefix = "cache:pcm:dict:id:"
- )
-
- type (
- dictModel interface {
- Insert(ctx context.Context, data *Dict) (sql.Result, error)
- FindOne(ctx context.Context, id int64) (*Dict, error)
- Update(ctx context.Context, data *Dict) error
- Delete(ctx context.Context, id int64) error
- }
-
- defaultDictModel struct {
- sqlc.CachedConn
- table string
- }
-
- Dict struct {
- Id int64 `db:"id"` // id
- DictName string `db:"dict_name"` // 字典名称
- DictCode string `db:"dict_code"` // 字典编号
- DictValue string `db:"dict_value"` // 字典值
- Source string `db:"source"` // 来源
- Description sql.NullString `db:"description"` // 描述
- CreatedBy sql.NullInt64 `db:"created_by"` // 创建人
- CreatedTime sql.NullTime `db:"created_time"` // 创建时间
- UpdatedBy sql.NullInt64 `db:"updated_by"` // 更新人
- UpdatedTime sql.NullTime `db:"updated_time"` // 更新时间
- DeletedFlag int64 `db:"deleted_flag"` // 是否删除(0-否,1-是)
- }
- )
-
- func newDictModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultDictModel {
- return &defaultDictModel{
- CachedConn: sqlc.NewConn(conn, c),
- table: "`dict`",
- }
- }
-
- func (m *defaultDictModel) Delete(ctx context.Context, id int64) error {
- pcmDictIdKey := fmt.Sprintf("%s%v", cachePcmDictIdPrefix, id)
- _, 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)
- }, pcmDictIdKey)
- return err
- }
-
- func (m *defaultDictModel) FindOne(ctx context.Context, id int64) (*Dict, error) {
- pcmDictIdKey := fmt.Sprintf("%s%v", cachePcmDictIdPrefix, id)
- var resp Dict
- err := m.QueryRowCtx(ctx, &resp, pcmDictIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
- query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", dictRows, 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 *defaultDictModel) Insert(ctx context.Context, data *Dict) (sql.Result, error) {
- pcmDictIdKey := fmt.Sprintf("%s%v", cachePcmDictIdPrefix, data.Id)
- 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, dictRowsExpectAutoSet)
- return conn.ExecCtx(ctx, query, data.DictName, data.DictCode, data.DictValue, data.Source, data.Description, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime, data.DeletedFlag)
- }, pcmDictIdKey)
- return ret, err
- }
-
- func (m *defaultDictModel) Update(ctx context.Context, data *Dict) error {
- pcmDictIdKey := fmt.Sprintf("%s%v", cachePcmDictIdPrefix, data.Id)
- _, 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, dictRowsWithPlaceHolder)
- return conn.ExecCtx(ctx, query, data.DictName, data.DictCode, data.DictValue, data.Source, data.Description, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime, data.DeletedFlag, data.Id)
- }, pcmDictIdKey)
- return err
- }
-
- func (m *defaultDictModel) formatPrimary(primary interface{}) string {
- return fmt.Sprintf("%s%v", cachePcmDictIdPrefix, primary)
- }
-
- func (m *defaultDictModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
- query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", dictRows, m.table)
- return conn.QueryRowCtx(ctx, v, query, primary)
- }
-
- func (m *defaultDictModel) tableName() string {
- return m.table
- }
|