77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"product/internal/dao"
|
|
"product/internal/model"
|
|
"product/internal/svc"
|
|
"product/product"
|
|
"product/validator"
|
|
|
|
"pkg.local/log"
|
|
"pkg.local/modelbase"
|
|
"pkg.local/utils"
|
|
validateService "pkg.local/validate"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type ItemsLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ItemsLogic {
|
|
return &ItemsLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *ItemsLogic) Items(in *product.ItemsReq) (*product.Response, error) {
|
|
var req validator.ProductItemsValidator
|
|
if msg := validateService.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, msg), nil
|
|
}
|
|
|
|
page := int(req.Page)
|
|
size := int(req.PageSize)
|
|
if page < modelbase.DefaultPage {
|
|
page = modelbase.DefaultPage
|
|
}
|
|
if size < modelbase.DefaultPage {
|
|
size = modelbase.DefaultSize
|
|
}
|
|
|
|
w := modelbase.Params{
|
|
Page: page,
|
|
Size: size,
|
|
Order: "sort DESC, id DESC",
|
|
}
|
|
if len(req.Name) > utils.NumberZero {
|
|
w.Like = map[string]string{
|
|
"name LIKE ? ": "%%" + req.Name + "%%",
|
|
}
|
|
}
|
|
|
|
var info []dao.ProductInfo
|
|
result, err := model.ProductModel{}.Init().Page(w, &info)
|
|
if err != nil {
|
|
log.Errorf("product items: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
items := make([]*product.Item, 0, len(info))
|
|
for _, row := range info {
|
|
items = append(items, toProductItem(row))
|
|
}
|
|
|
|
return okResponse(utils.StructToJson(&product.ItemsData{
|
|
Count: result.Count,
|
|
Items: items,
|
|
})), nil
|
|
}
|