// 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 (
	centerTagFieldNames          = builder.RawFieldNames(&CenterTag{})
	centerTagRows                = strings.Join(centerTagFieldNames, ",")
	centerTagRowsExpectAutoSet   = strings.Join(stringx.Remove(centerTagFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	centerTagRowsWithPlaceHolder = strings.Join(stringx.Remove(centerTagFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmCenterTagIdPrefix = "cache:pcm:centerTag:id:"
)

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

	defaultCenterTagModel struct {
		sqlc.CachedConn
		table string
	}

	CenterTag struct {
		Id   int64         `db:"id"`   // 平台唯一id
		Ydyl sql.NullInt64 `db:"ydyl"` // 一带一路标识
		Edwc sql.NullInt64 `db:"edwc"` // 东数西算标识
	}
)

func newCenterTagModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultCenterTagModel {
	return &defaultCenterTagModel{
		CachedConn: sqlc.NewConn(conn, c),
		table:      "`center_tag`",
	}
}

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

func (m *defaultCenterTagModel) FindOne(ctx context.Context, id int64) (*CenterTag, error) {
	pcmCenterTagIdKey := fmt.Sprintf("%s%v", cachePcmCenterTagIdPrefix, id)
	var resp CenterTag
	err := m.QueryRowCtx(ctx, &resp, pcmCenterTagIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", centerTagRows, 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 *defaultCenterTagModel) Insert(ctx context.Context, data *CenterTag) (sql.Result, error) {
	pcmCenterTagIdKey := fmt.Sprintf("%s%v", cachePcmCenterTagIdPrefix, 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, centerTagRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.Ydyl, data.Edwc)
	}, pcmCenterTagIdKey)
	return ret, err
}

func (m *defaultCenterTagModel) Update(ctx context.Context, data *CenterTag) error {
	pcmCenterTagIdKey := fmt.Sprintf("%s%v", cachePcmCenterTagIdPrefix, 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, centerTagRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.Ydyl, data.Edwc, data.Id)
	}, pcmCenterTagIdKey)
	return err
}

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

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

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