// 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/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/stringx"
)

var (
	fsProductDesignFieldNames          = builder.RawFieldNames(&FsProductDesign{})
	fsProductDesignRows                = strings.Join(fsProductDesignFieldNames, ",")
	fsProductDesignRowsExpectAutoSet   = strings.Join(stringx.Remove(fsProductDesignFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	fsProductDesignRowsWithPlaceHolder = strings.Join(stringx.Remove(fsProductDesignFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultFsProductDesignModel struct {
		conn  sqlx.SqlConn
		table string
	}

	FsProductDesign struct {
		Id         int64          `db:"id"`
		Sn         string         `db:"sn"`          // 唯一标识
		UserId     int64          `db:"user_id"`     // 用户ID
		ProductId  int64          `db:"product_id"`  // 产品ID
		TemplateId int64          `db:"template_id"` // 模型ID
		MaterialId int64          `db:"material_id"` // 材质ID
		SizeId     int64          `db:"size_id"`     // 尺寸ID
		OptionalId int64          `db:"optional_id"` // 选项ID
		Cover      string         `db:"cover"`       // 封面图
		Info       sql.NullString `db:"info"`        // 保留的设计信息
		Utime      time.Time      `db:"utime"`       // 更新时间
		Status     int64          `db:"status"`      // 状态
		IsDel      int64          `db:"is_del"`      // 是否删除 0未删除 1删除
		IsPay      int64          `db:"is_pay"`      // 是否已有支付 0 未 1 有
		LogoColor  sql.NullString `db:"logo_color"`  // logo图片备选项
		PageGuid   string         `db:"page_guid"`   // 页面识别id
	}
)

func newFsProductDesignModel(conn sqlx.SqlConn) *defaultFsProductDesignModel {
	return &defaultFsProductDesignModel{
		conn:  conn,
		table: "`fs_product_design`",
	}
}

func (m *defaultFsProductDesignModel) Delete(ctx context.Context, id int64) error {
	query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
	_, err := m.conn.ExecCtx(ctx, query, id)
	return err
}

func (m *defaultFsProductDesignModel) FindOne(ctx context.Context, id int64) (*FsProductDesign, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsProductDesignRows, m.table)
	var resp FsProductDesign
	err := m.conn.QueryRowCtx(ctx, &resp, query, id)
	switch err {
	case nil:
		return &resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *defaultFsProductDesignModel) Insert(ctx context.Context, data *FsProductDesign) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, fsProductDesignRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.Sn, data.UserId, data.ProductId, data.TemplateId, data.MaterialId, data.SizeId, data.OptionalId, data.Cover, data.Info, data.Utime, data.Status, data.IsDel, data.IsPay, data.LogoColor, data.PageGuid)
	return ret, err
}

func (m *defaultFsProductDesignModel) Update(ctx context.Context, data *FsProductDesign) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsProductDesignRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.Sn, data.UserId, data.ProductId, data.TemplateId, data.MaterialId, data.SizeId, data.OptionalId, data.Cover, data.Info, data.Utime, data.Status, data.IsDel, data.IsPay, data.LogoColor, data.PageGuid, data.Id)
	return err
}

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