// 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
}
