// Code generated by goctl. DO NOT EDIT.

package models

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 (
	resourcePanelConfigFieldNames          = builder.RawFieldNames(&ResourcePanelConfig{})
	resourcePanelConfigRows                = strings.Join(resourcePanelConfigFieldNames, ",")
	resourcePanelConfigRowsExpectAutoSet   = strings.Join(stringx.Remove(resourcePanelConfigFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	resourcePanelConfigRowsWithPlaceHolder = strings.Join(stringx.Remove(resourcePanelConfigFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmResourcePanelConfigIdPrefix = "cache:pcm:resourcePanelConfig:id:"
)

type (
	resourcePanelConfigModel interface {
		Insert(ctx context.Context, data *ResourcePanelConfig) (sql.Result, error)
		FindOne(ctx context.Context, id int64) (*ResourcePanelConfig, error)
		Update(ctx context.Context, data *ResourcePanelConfig) error
		Delete(ctx context.Context, id int64) error
	}

	defaultResourcePanelConfigModel struct {
		sqlc.CachedConn
		table string
	}

	ResourcePanelConfig struct {
		Id              int64     `db:"id"`
		Title           string    `db:"title"`             // 标题
		TitleColor      string    `db:"title_color"`       // 标题色
		MainColor       string    `db:"main_color"`        // 主色调
		MainColor2      string    `db:"main_color2"`       // 次主色调
		TextColor       string    `db:"text_color"`        // 文字颜色
		BackgroundColor string    `db:"background_color"`  // 背景底色
		Center          string    `db:"center"`            // 中心点
		CenterPosition  string    `db:"center_position"`   // 中心点坐标
		ProvinceBgColor string    `db:"province_bg_color"` // 三级地图底色
		StatusIng       string    `db:"status_ing"`        // 接入中图标
		StatusUn        string    `db:"status_un"`         // 未接入图标
		StatusEnd       string    `db:"status_end"`        // 已接入图标
		TitleIcon       string    `db:"title_icon"`        // 标题底图
		SubTitleIcon    string    `db:"sub_title_icon"`    // 小标题底图
		NumberBg        string    `db:"number_bg"`         // 数字底图
		TaskBg          string    `db:"task_bg"`           // 任务底图
		CreateTime      time.Time `db:"create_time"`       // 创建时间
		UpdateTime      time.Time `db:"update_time"`       // 更新时间
	}
)

func newResourcePanelConfigModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultResourcePanelConfigModel {
	return &defaultResourcePanelConfigModel{
		CachedConn: sqlc.NewConn(conn, c, opts...),
		table:      "`resource_panel_config`",
	}
}

func (m *defaultResourcePanelConfigModel) Delete(ctx context.Context, id int64) error {
	pcmResourcePanelConfigIdKey := fmt.Sprintf("%s%v", cachePcmResourcePanelConfigIdPrefix, 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)
	}, pcmResourcePanelConfigIdKey)
	return err
}

func (m *defaultResourcePanelConfigModel) FindOne(ctx context.Context, id int64) (*ResourcePanelConfig, error) {
	pcmResourcePanelConfigIdKey := fmt.Sprintf("%s%v", cachePcmResourcePanelConfigIdPrefix, id)
	var resp ResourcePanelConfig
	err := m.QueryRowCtx(ctx, &resp, pcmResourcePanelConfigIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", resourcePanelConfigRows, 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 *defaultResourcePanelConfigModel) Insert(ctx context.Context, data *ResourcePanelConfig) (sql.Result, error) {
	pcmResourcePanelConfigIdKey := fmt.Sprintf("%s%v", cachePcmResourcePanelConfigIdPrefix, 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, resourcePanelConfigRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Title, data.TitleColor, data.MainColor, data.MainColor2, data.TextColor, data.BackgroundColor, data.Center, data.CenterPosition, data.ProvinceBgColor, data.StatusIng, data.StatusUn, data.StatusEnd, data.TitleIcon, data.SubTitleIcon, data.NumberBg, data.TaskBg)
	}, pcmResourcePanelConfigIdKey)
	return ret, err
}

func (m *defaultResourcePanelConfigModel) Update(ctx context.Context, data *ResourcePanelConfig) error {
	pcmResourcePanelConfigIdKey := fmt.Sprintf("%s%v", cachePcmResourcePanelConfigIdPrefix, 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, resourcePanelConfigRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.Title, data.TitleColor, data.MainColor, data.MainColor2, data.TextColor, data.BackgroundColor, data.Center, data.CenterPosition, data.ProvinceBgColor, data.StatusIng, data.StatusUn, data.StatusEnd, data.TitleIcon, data.SubTitleIcon, data.NumberBg, data.TaskBg, data.Id)
	}, pcmResourcePanelConfigIdKey)
	return err
}

func (m *defaultResourcePanelConfigModel) formatPrimary(primary any) string {
	return fmt.Sprintf("%s%v", cachePcmResourcePanelConfigIdPrefix, primary)
}

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

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