// Code generated by goctl. DO NOT EDIT!

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 (
	sysDictionaryFieldNames          = builder.RawFieldNames(&SysDictionary{})
	sysDictionaryRows                = strings.Join(sysDictionaryFieldNames, ",")
	sysDictionaryRowsExpectAutoSet   = strings.Join(stringx.Remove(sysDictionaryFieldNames, "`id`", "`create_time`", "`update_time`", "`create_at`", "`update_at`"), ",")
	sysDictionaryRowsWithPlaceHolder = strings.Join(stringx.Remove(sysDictionaryFieldNames, "`id`", "`create_time`", "`update_time`", "`create_at`", "`update_at`"), "=?,") + "=?"

	cacheArkAdminSysDictionaryIdPrefix        = "cache:arkAdmin:sysDictionary:id:"
	cacheArkAdminSysDictionaryUniqueKeyPrefix = "cache:arkAdmin:sysDictionary:uniqueKey:"
)

type (
	sysDictionaryModel interface {
		Insert(ctx context.Context, data *SysDictionary) (sql.Result, error)
		FindOne(ctx context.Context, id int64) (*SysDictionary, error)
		FindOneByUniqueKey(ctx context.Context, uniqueKey string) (*SysDictionary, error)
		Update(ctx context.Context, data *SysDictionary) error
		Delete(ctx context.Context, id int64) error
	}

	defaultSysDictionaryModel struct {
		sqlc.CachedConn
		table string
	}

	SysDictionary struct {
		Id         int64     `db:"id"`          // 编号
		ParentId   int64     `db:"parent_id"`   // 0=配置集 !0=父级id
		Name       string    `db:"name"`        // 名称
		Type       int64     `db:"type"`        // 1文本 2数字 3数组 4单选 5多选 6下拉 7日期 8时间 9单图 10多图 11单文件 12多文件
		UniqueKey  string    `db:"unique_key"`  // 唯一值
		Value      string    `db:"value"`       // 配置值
		Status     int64     `db:"status"`      // 0=禁用 1=开启
		OrderNum   int64     `db:"order_num"`   // 排序值
		Remark     string    `db:"remark"`      // 备注
		CreateTime time.Time `db:"create_time"` // 创建时间
		UpdateTime time.Time `db:"update_time"` // 更新时间
	}
)

func newSysDictionaryModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultSysDictionaryModel {
	return &defaultSysDictionaryModel{
		CachedConn: sqlc.NewConn(conn, c),
		table:      "`sys_dictionary`",
	}
}

func (m *defaultSysDictionaryModel) Delete(ctx context.Context, id int64) error {
	data, err := m.FindOne(ctx, id)
	if err != nil {
		return err
	}

	arkAdminSysDictionaryIdKey := fmt.Sprintf("%s%v", cacheArkAdminSysDictionaryIdPrefix, id)
	arkAdminSysDictionaryUniqueKeyKey := fmt.Sprintf("%s%v", cacheArkAdminSysDictionaryUniqueKeyPrefix, data.UniqueKey)
	_, 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)
	}, arkAdminSysDictionaryIdKey, arkAdminSysDictionaryUniqueKeyKey)
	return err
}

func (m *defaultSysDictionaryModel) FindOne(ctx context.Context, id int64) (*SysDictionary, error) {
	arkAdminSysDictionaryIdKey := fmt.Sprintf("%s%v", cacheArkAdminSysDictionaryIdPrefix, id)
	var resp SysDictionary
	err := m.QueryRowCtx(ctx, &resp, arkAdminSysDictionaryIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysDictionaryRows, 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 *defaultSysDictionaryModel) FindOneByUniqueKey(ctx context.Context, uniqueKey string) (*SysDictionary, error) {
	arkAdminSysDictionaryUniqueKeyKey := fmt.Sprintf("%s%v", cacheArkAdminSysDictionaryUniqueKeyPrefix, uniqueKey)
	var resp SysDictionary
	err := m.QueryRowIndexCtx(ctx, &resp, arkAdminSysDictionaryUniqueKeyKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
		query := fmt.Sprintf("select %s from %s where `unique_key` = ? limit 1", sysDictionaryRows, m.table)
		if err := conn.QueryRowCtx(ctx, &resp, query, uniqueKey); 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 *defaultSysDictionaryModel) Insert(ctx context.Context, data *SysDictionary) (sql.Result, error) {
	arkAdminSysDictionaryIdKey := fmt.Sprintf("%s%v", cacheArkAdminSysDictionaryIdPrefix, data.Id)
	arkAdminSysDictionaryUniqueKeyKey := fmt.Sprintf("%s%v", cacheArkAdminSysDictionaryUniqueKeyPrefix, data.UniqueKey)
	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, sysDictionaryRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.ParentId, data.Name, data.Type, data.UniqueKey, data.Value, data.Status, data.OrderNum, data.Remark)
	}, arkAdminSysDictionaryIdKey, arkAdminSysDictionaryUniqueKeyKey)
	return ret, err
}

func (m *defaultSysDictionaryModel) Update(ctx context.Context, newData *SysDictionary) error {
	data, err := m.FindOne(ctx, newData.Id)
	if err != nil {
		return err
	}

	arkAdminSysDictionaryIdKey := fmt.Sprintf("%s%v", cacheArkAdminSysDictionaryIdPrefix, data.Id)
	arkAdminSysDictionaryUniqueKeyKey := fmt.Sprintf("%s%v", cacheArkAdminSysDictionaryUniqueKeyPrefix, data.UniqueKey)
	_, 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, sysDictionaryRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, newData.ParentId, newData.Name, newData.Type, newData.UniqueKey, newData.Value, newData.Status, newData.OrderNum, newData.Remark, newData.Id)
	}, arkAdminSysDictionaryIdKey, arkAdminSysDictionaryUniqueKeyKey)
	return err
}

func (m *defaultSysDictionaryModel) formatPrimary(primary interface{}) string {
	return fmt.Sprintf("%s%v", cacheArkAdminSysDictionaryIdPrefix, primary)
}

func (m *defaultSysDictionaryModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysDictionaryRows, m.table)
	return conn.QueryRowCtx(ctx, v, query, primary)
}

func (m *defaultSysDictionaryModel) tableName() string {
	return m.table
}
