// 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 (
	scParticipantLabelInfoFieldNames          = builder.RawFieldNames(&ScParticipantLabelInfo{})
	scParticipantLabelInfoRows                = strings.Join(scParticipantLabelInfoFieldNames, ",")
	scParticipantLabelInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(scParticipantLabelInfoFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	scParticipantLabelInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(scParticipantLabelInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmScParticipantLabelInfoIdPrefix = "cache:pcm:scParticipantLabelInfo:id:"
)

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

	defaultScParticipantLabelInfoModel struct {
		sqlc.CachedConn
		table string
	}

	ScParticipantLabelInfo struct {
		Id            int64          `db:"id"`             // id
		Key           string         `db:"key"`            // key
		Value         string         `db:"value"`          // value
		ParticipantId int64          `db:"participant_id"` // participant id
		Metadata      sql.NullString `db:"metadata"`       // 元数据
		DeletedFlag   int64          `db:"deleted_flag"`   // 是否删除
		CreatedBy     sql.NullInt64  `db:"created_by"`     // 创建人
		CreatedTime   time.Time      `db:"created_time"`   // 创建时间
		UpdatedBy     sql.NullInt64  `db:"updated_by"`     // 更新人
		UpdatedTime   sql.NullTime   `db:"updated_time"`   // 更新时间
	}
)

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

func (m *defaultScParticipantLabelInfoModel) withSession(session sqlx.Session) *defaultScParticipantLabelInfoModel {
	return &defaultScParticipantLabelInfoModel{
		CachedConn: m.CachedConn.WithSession(session),
		table:      "`sc_participant_label_info`",
	}
}

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

func (m *defaultScParticipantLabelInfoModel) FindOne(ctx context.Context, id int64) (*ScParticipantLabelInfo, error) {
	pcmScParticipantLabelInfoIdKey := fmt.Sprintf("%s%v", cachePcmScParticipantLabelInfoIdPrefix, id)
	var resp ScParticipantLabelInfo
	err := m.QueryRowCtx(ctx, &resp, pcmScParticipantLabelInfoIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", scParticipantLabelInfoRows, 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 *defaultScParticipantLabelInfoModel) Insert(ctx context.Context, data *ScParticipantLabelInfo) (sql.Result, error) {
	pcmScParticipantLabelInfoIdKey := fmt.Sprintf("%s%v", cachePcmScParticipantLabelInfoIdPrefix, 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, scParticipantLabelInfoRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.Key, data.Value, data.ParticipantId, data.Metadata, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime)
	}, pcmScParticipantLabelInfoIdKey)
	return ret, err
}

func (m *defaultScParticipantLabelInfoModel) Update(ctx context.Context, data *ScParticipantLabelInfo) error {
	pcmScParticipantLabelInfoIdKey := fmt.Sprintf("%s%v", cachePcmScParticipantLabelInfoIdPrefix, 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, scParticipantLabelInfoRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.Key, data.Value, data.ParticipantId, data.Metadata, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime, data.Id)
	}, pcmScParticipantLabelInfoIdKey)
	return err
}

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

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

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