93 lines
3.0 KiB
Go
93 lines
3.0 KiB
Go
package goods_status
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"strings"
|
|
)
|
|
|
|
var _ GoodsStatusModel = (*customGoodsStatusModel)(nil)
|
|
|
|
type (
|
|
// GoodsStatusModel is an interface to be customized, add more methods here,
|
|
// and implement the added methods in customGoodsStatusModel.
|
|
GoodsStatusModel interface {
|
|
goodsStatusModel
|
|
withSession(session sqlx.Session) GoodsStatusModel
|
|
DeleteByApiId(ctx context.Context, id int64) error
|
|
FindUpList(ctx context.Context, apiId int64) ([]int64, error)
|
|
UpdateDownByIds(ctx context.Context, ids []int64) error
|
|
FindNotIdOne(ctx context.Context, apiId int64) (int64, error)
|
|
}
|
|
|
|
customGoodsStatusModel struct {
|
|
*defaultGoodsStatusModel
|
|
}
|
|
)
|
|
|
|
func (m *customGoodsStatusModel) FindNotIdOne(ctx context.Context, apiId int64) (int64, error) {
|
|
query := fmt.Sprintf("select gs.`api_product_id` from %s gs left join `goods_info` gi on gs.`api_id` = gi.`api_id` where gs.`api_id` = ? and gs.`api_product_id` not in (select gs.api_product_id from `goods_status` gs join `goods_info` gi on gs.`api_id` = gi.`api_id` and gi.api_product_id = gs.api_product_id where gs.`api_id` = ?) limit 1", m.table)
|
|
var resp int64
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, apiId, apiId)
|
|
switch err {
|
|
case nil:
|
|
return resp, nil
|
|
case sqlx.ErrNotFound:
|
|
return 0, ErrNotFound
|
|
default:
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
func (m *customGoodsStatusModel) UpdateDownByIds(ctx context.Context, ids []int64) error {
|
|
// 动态构造 IN 子句中的占位符
|
|
placeholders := make([]string, len(ids))
|
|
for i := range ids {
|
|
placeholders[i] = "?"
|
|
}
|
|
query := fmt.Sprintf("update %s set `status` = 0 where `id` in (%s)", m.table, strings.Join(placeholders, ","))
|
|
_, err := m.conn.ExecCtx(ctx, query, convertToInterface(ids)...)
|
|
return err
|
|
}
|
|
|
|
func (m *customGoodsStatusModel) FindUpList(ctx context.Context, apiId int64) ([]int64, error) {
|
|
query := fmt.Sprintf("select g.`id` from %s g join `config_api` c on g.`api_id` = c.id where g.status = 0 and c.id = ?", m.table)
|
|
var resp []int64
|
|
err := m.conn.QueryRowsCtx(ctx, &resp, query, apiId)
|
|
switch err {
|
|
case nil:
|
|
return resp, nil
|
|
case sqlx.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *customGoodsStatusModel) DeleteByApiId(ctx context.Context, id int64) error {
|
|
query := fmt.Sprintf("delete from %s where `api_id` = ?", m.table)
|
|
_, err := m.conn.ExecCtx(ctx, query, id)
|
|
return err
|
|
}
|
|
|
|
// NewGoodsStatusModel returns a model for the database table.
|
|
func NewGoodsStatusModel(conn sqlx.SqlConn) GoodsStatusModel {
|
|
return &customGoodsStatusModel{
|
|
defaultGoodsStatusModel: newGoodsStatusModel(conn),
|
|
}
|
|
}
|
|
|
|
func (m *customGoodsStatusModel) withSession(session sqlx.Session) GoodsStatusModel {
|
|
return NewGoodsStatusModel(sqlx.NewSqlConnFromSession(session))
|
|
}
|
|
|
|
// convertToInterface 将 int64 切片转换为接口切片
|
|
func convertToInterface(ids []int64) []interface{} {
|
|
result := make([]interface{}, len(ids))
|
|
for i, id := range ids {
|
|
result[i] = id
|
|
}
|
|
return result
|
|
}
|