muyu-apiserver/model/invproductmodel_gen.go

184 lines
7.4 KiB
Go
Raw Normal View History

// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: 1.9.2
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 (
invProductFieldNames = builder.RawFieldNames(&InvProduct{})
invProductRows = strings.Join(invProductFieldNames, ",")
invProductRowsExpectAutoSet = strings.Join(stringx.Remove(invProductFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
invProductRowsWithPlaceHolder = strings.Join(stringx.Remove(invProductFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
cacheInvProductIdPrefix = "cache:invProduct:id:"
cacheInvProductProductIdPrefix = "cache:invProduct:productId:"
cacheInvProductProductNamePrefix = "cache:invProduct:productName:"
)
type (
invProductModel interface {
Insert(ctx context.Context, data *InvProduct) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*InvProduct, error)
FindOneByProductId(ctx context.Context, productId string) (*InvProduct, error)
FindOneByProductName(ctx context.Context, productName string) (*InvProduct, error)
Update(ctx context.Context, data *InvProduct) error
Delete(ctx context.Context, id int64) error
}
defaultInvProductModel struct {
sqlc.CachedConn
table string
}
InvProduct struct {
Id int64 `db:"id"`
ProductId string `db:"product_id"`
ProductName string `db:"product_name"`
ImageUrl string `db:"image_url"`
Spec string `db:"spec"`
Color string `db:"color"`
SalesPrice float64 `db:"sales_price"`
CostPrice float64 `db:"cost_price"`
Remark sql.NullString `db:"remark"`
Status int64 `db:"status"`
TenantId string `db:"tenant_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
DeletedAt sql.NullTime `db:"deleted_at"`
}
)
func newInvProductModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultInvProductModel {
return &defaultInvProductModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`inv_product`",
}
}
func (m *defaultInvProductModel) Delete(ctx context.Context, id int64) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
invProductIdKey := fmt.Sprintf("%s%v", cacheInvProductIdPrefix, id)
invProductProductIdKey := fmt.Sprintf("%s%v", cacheInvProductProductIdPrefix, data.ProductId)
invProductProductNameKey := fmt.Sprintf("%s%v", cacheInvProductProductNamePrefix, data.ProductName)
_, 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)
}, invProductIdKey, invProductProductIdKey, invProductProductNameKey)
return err
}
func (m *defaultInvProductModel) FindOne(ctx context.Context, id int64) (*InvProduct, error) {
invProductIdKey := fmt.Sprintf("%s%v", cacheInvProductIdPrefix, id)
var resp InvProduct
err := m.QueryRowCtx(ctx, &resp, invProductIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", invProductRows, 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 *defaultInvProductModel) FindOneByProductId(ctx context.Context, productId string) (*InvProduct, error) {
invProductProductIdKey := fmt.Sprintf("%s%v", cacheInvProductProductIdPrefix, productId)
var resp InvProduct
err := m.QueryRowIndexCtx(ctx, &resp, invProductProductIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `product_id` = ? limit 1", invProductRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, productId); 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 *defaultInvProductModel) FindOneByProductName(ctx context.Context, productName string) (*InvProduct, error) {
invProductProductNameKey := fmt.Sprintf("%s%v", cacheInvProductProductNamePrefix, productName)
var resp InvProduct
err := m.QueryRowIndexCtx(ctx, &resp, invProductProductNameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `product_name` = ? limit 1", invProductRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, productName); 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 *defaultInvProductModel) Insert(ctx context.Context, data *InvProduct) (sql.Result, error) {
invProductIdKey := fmt.Sprintf("%s%v", cacheInvProductIdPrefix, data.Id)
invProductProductIdKey := fmt.Sprintf("%s%v", cacheInvProductProductIdPrefix, data.ProductId)
invProductProductNameKey := fmt.Sprintf("%s%v", cacheInvProductProductNamePrefix, data.ProductName)
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, invProductRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.ProductId, data.ProductName, data.ImageUrl, data.Spec, data.Color, data.SalesPrice, data.CostPrice, data.Remark, data.Status, data.TenantId, data.DeletedAt)
}, invProductIdKey, invProductProductIdKey, invProductProductNameKey)
return ret, err
}
func (m *defaultInvProductModel) Update(ctx context.Context, newData *InvProduct) error {
data, err := m.FindOne(ctx, newData.Id)
if err != nil {
return err
}
invProductIdKey := fmt.Sprintf("%s%v", cacheInvProductIdPrefix, data.Id)
invProductProductIdKey := fmt.Sprintf("%s%v", cacheInvProductProductIdPrefix, data.ProductId)
invProductProductNameKey := fmt.Sprintf("%s%v", cacheInvProductProductNamePrefix, data.ProductName)
_, 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, invProductRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, newData.ProductId, newData.ProductName, newData.ImageUrl, newData.Spec, newData.Color, newData.SalesPrice, newData.CostPrice, newData.Remark, newData.Status, newData.TenantId, newData.DeletedAt, newData.Id)
}, invProductIdKey, invProductProductIdKey, invProductProductNameKey)
return err
}
func (m *defaultInvProductModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheInvProductIdPrefix, primary)
}
func (m *defaultInvProductModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", invProductRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultInvProductModel) tableName() string {
return m.table
}