fusenapi/model/fsproductsizemodel.go

70 lines
2.3 KiB
Go
Raw Normal View History

2023-06-05 09:13:05 +00:00
package model
2023-06-06 09:36:11 +00:00
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
2023-06-07 09:27:17 +00:00
"strings"
2023-06-06 09:36:11 +00:00
)
2023-06-05 09:13:05 +00:00
var _ FsProductSizeModel = (*customFsProductSizeModel)(nil)
type (
// FsProductSizeModel is an interface to be customized, add more methods here,
// and implement the added methods in customFsProductSizeModel.
FsProductSizeModel interface {
fsProductSizeModel
2023-06-06 09:36:11 +00:00
CountByStatus(ctx context.Context, status int) (total int, err error)
2023-06-09 04:07:54 +00:00
GetAllByProductIds(ctx context.Context, productIds []string, sort string) (resp []FsProductSize, err error)
2023-06-09 08:45:29 +00:00
GetAllByIds(ctx context.Context, ids []string, sort string) (resp []FsProductSize, err error)
2023-06-05 09:13:05 +00:00
}
customFsProductSizeModel struct {
*defaultFsProductSizeModel
}
)
// NewFsProductSizeModel returns a model for the database table.
func NewFsProductSizeModel(conn sqlx.SqlConn) FsProductSizeModel {
return &customFsProductSizeModel{
defaultFsProductSizeModel: newFsProductSizeModel(conn),
}
}
2023-06-06 09:36:11 +00:00
func (m *defaultFsProductSizeModel) CountByStatus(ctx context.Context, status int) (total int, err error) {
query := fmt.Sprintf("select %s from %s where `status` = ? limit 1", "count(*) as num", m.table)
err = m.conn.QueryRowCtx(ctx, &total, query, status)
if err != nil {
return 0, err
}
return
}
2023-06-09 04:07:54 +00:00
func (m *defaultFsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds []string, sort string) (resp []FsProductSize, err error) {
2023-06-07 09:27:17 +00:00
query := fmt.Sprintf("select %s from %s where `product_id` in(?) and `status` = ? ", fsProductSizeRows, m.table)
2023-06-06 09:36:11 +00:00
switch sort {
2023-06-09 04:07:54 +00:00
case "sort-asc":
2023-06-06 09:36:11 +00:00
query = fmt.Sprintf("%s order by `sort` ASC", query)
2023-06-09 04:07:54 +00:00
case "sort-desc":
2023-06-06 09:36:11 +00:00
query = fmt.Sprintf("%s order by `sort` DESC", query)
}
2023-06-07 09:27:17 +00:00
err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(productIds, ","), 1)
2023-06-06 09:36:11 +00:00
if err != nil {
return nil, err
}
return
}
2023-06-09 04:07:54 +00:00
2023-06-09 08:45:29 +00:00
func (m *defaultFsProductSizeModel) GetAllByIds(ctx context.Context, ids []string, sort string) (resp []FsProductSize, err error) {
query := fmt.Sprintf("select %s from %s where `id` in (?) and `status` = ? ", fsProductSizeRows, m.table)
2023-06-09 04:07:54 +00:00
switch sort {
case "sort-asc":
query = fmt.Sprintf("%s order by `sort` ASC", query)
case "sort-desc":
query = fmt.Sprintf("%s order by `sort` DESC", query)
}
err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(ids, ","), 1)
if err != nil {
return nil, err
}
return
}