2023-06-01 07:32:28 +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-01 07:32:28 +00:00
|
|
|
|
|
|
|
var _ FsProductModel = (*customFsProductModel)(nil)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// FsProductModel is an interface to be customized, add more methods here,
|
|
|
|
// and implement the added methods in customFsProductModel.
|
|
|
|
FsProductModel interface {
|
|
|
|
fsProductModel
|
2023-06-07 09:27:17 +00:00
|
|
|
GetProductListByConditions(ctx context.Context, productTypes []string, sort string) ([]FsProduct, error)
|
2023-06-06 10:17:03 +00:00
|
|
|
GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error)
|
2023-06-09 04:07:54 +00:00
|
|
|
GetProductListByIds(ctx context.Context, ids []string, sort string) (resp []FsProduct, err error)
|
2023-06-01 07:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
customFsProductModel struct {
|
|
|
|
*defaultFsProductModel
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewFsProductModel returns a model for the database table.
|
|
|
|
func NewFsProductModel(conn sqlx.SqlConn) FsProductModel {
|
|
|
|
return &customFsProductModel{
|
|
|
|
defaultFsProductModel: newFsProductModel(conn),
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 09:27:17 +00:00
|
|
|
func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context, productTypes []string, sort string) (resp []FsProduct, err error) {
|
|
|
|
query := fmt.Sprintf("select %s from %s where `type` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?",
|
2023-06-06 09:36:11 +00:00
|
|
|
fsProductRows, m.table)
|
|
|
|
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)
|
|
|
|
default:
|
|
|
|
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(productTypes, ","), 0, 1, 1)
|
2023-06-06 09:36:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-06-06 10:17:03 +00:00
|
|
|
func (m *defaultFsProductModel) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) {
|
|
|
|
query := fmt.Sprintf("select %s from %s where `is_del` =? and `is_shelf` = ? order by RAND() limit ?",
|
2023-06-06 09:36:11 +00:00
|
|
|
fsProductRows, m.table)
|
2023-06-06 10:17:03 +00:00
|
|
|
err = m.conn.QueryRowsCtx(ctx, &resp, query, 0, 1, limit)
|
2023-06-06 09:36:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-06-09 04:07:54 +00:00
|
|
|
func (m *defaultFsProductModel) GetProductListByIds(ctx context.Context, ids []string, sort string) (resp []FsProduct, err error) {
|
|
|
|
query := fmt.Sprintf("select %s from %s where `id` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?",
|
|
|
|
fsProductRows, m.table)
|
|
|
|
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)
|
|
|
|
default:
|
|
|
|
query = fmt.Sprintf("%s order by sort DESC", query)
|
|
|
|
}
|
|
|
|
err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(ids, ","), 0, 1, 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|