123 lines
3.1 KiB
Go
123 lines
3.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"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 msgResponse(utils.Ok.Code, 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
|
|
}
|
|
|
|
func toProductItem(row dao.ProductInfo) *product.Item {
|
|
publishTime := ""
|
|
if !row.PublishTime.IsZero() {
|
|
publishTime = row.PublishTime.Format(time.DateTime)
|
|
}
|
|
return &product.Item{
|
|
Id: int64(row.Id),
|
|
Name: row.Name,
|
|
Subhead: row.Subhead,
|
|
Content: row.Content,
|
|
ModelCode: row.ModelCode,
|
|
Price: float64(row.Price),
|
|
StorePrice: float64(row.StorePrice),
|
|
SalePrice: float64(row.SalePrice),
|
|
SharePrice: float64(row.SharePrice),
|
|
AgentPrice: float64(row.AgentPrice),
|
|
SaleReward: row.SaleReward,
|
|
SalesModel: uint32(row.SalesModel),
|
|
Waybill: row.Waybill,
|
|
Status: uint32(row.Status),
|
|
Weight: float64(row.Weight),
|
|
IsIndex: uint32(row.IsIndex),
|
|
IndexImage: row.IndexImage,
|
|
Cubage: row.Cubage,
|
|
Label: row.Label,
|
|
Images: row.Images,
|
|
IsBuy: uint32(row.IsBuy),
|
|
PeriodValidity: int32(row.PeriodValidity),
|
|
PublishTime: publishTime,
|
|
Type: uint32(row.Type),
|
|
NormsNumber: uint32(row.NormsNumber),
|
|
BoxNumber: row.BoxNumber,
|
|
Sort: uint32(row.Sort),
|
|
Number: uint32(row.Number),
|
|
VerifyStatus: uint32(row.VerifyStatus),
|
|
VerifyId: int64(row.VerifyId),
|
|
VerifyName: row.VerifyName,
|
|
VerifySecondId: int64(row.VerifySecondId),
|
|
VerifySecondName: row.VerifySecondName,
|
|
Reason: row.Reason,
|
|
AdminName: row.AdminName,
|
|
Edit: uint32(row.Edit),
|
|
}
|
|
}
|