84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
product "lone-services/rpc/product/pb"
|
|
"lone-services/services/product/internal/apply"
|
|
"lone-services/services/product/internal/dao"
|
|
"lone-services/services/product/internal/model"
|
|
"lone-services/services/product/internal/svc"
|
|
"lone-services/services/product/validator"
|
|
|
|
"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 := validate.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.Info
|
|
result, err := model.ProductModel{}.Init().Page(w, &info)
|
|
if err != nil {
|
|
l.Errorf("product items: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
return failResponse(utils.ErrorNoLoginInfo), nil
|
|
}
|
|
adminId := int(adminInfo.ID)
|
|
|
|
items := make([]*product.Item, 0, len(info))
|
|
for _, row := range info {
|
|
if applyData, err := apply.Get(l.ctx, adminId, row.Id); err == nil {
|
|
row.Edit = applyData.Status
|
|
}
|
|
items = append(items, toProductItem(row))
|
|
}
|
|
|
|
return okResponse(&product.ItemsData{
|
|
Count: result.Count,
|
|
Items: items,
|
|
}), nil
|
|
}
|