feat: product items info

This commit is contained in:
zzw
2026-10-08 09:25:41 +08:00
parent 5fdf23beed
commit cfe88e0f56
27 changed files with 1213 additions and 224 deletions
+22
View File
@@ -182,6 +182,28 @@ type VerifyFirstEditStatus struct {
VerifyStatus uint8 `gorm:"column:verify_status" json:"verify_status"`
}
// WebInfo C 端列表/详情投影(对齐老 ProductWebInfo)
type WebInfo struct {
Id int `gorm:"column:id"`
Name string `gorm:"column:name"`
Subhead string `gorm:"column:subhead"`
Content string `gorm:"column:content"`
ModelCode string `gorm:"column:model_code"`
Price float32 `gorm:"column:price"`
StorePrice float32 `gorm:"column:store_price"`
SalePrice float32 `gorm:"column:sale_price"`
Images string `gorm:"column:images"`
Label string `gorm:"column:label"`
IsBuy uint8 `gorm:"column:is_buy"`
PeriodValidity int16 `gorm:"column:period_validity"`
IsIndex uint8 `gorm:"column:is_index"`
IndexImage string `gorm:"column:index_image"`
Type uint8 `gorm:"column:type"`
NormsNumber uint8 `gorm:"column:norms_number"`
Number uint `gorm:"column:number"`
IsPermit uint8 `gorm:"-"`
}
// Info 列表/详情查询投影
type Info struct {
Id int `gorm:"column:id"`
@@ -0,0 +1,120 @@
package logic
import (
"context"
"strconv"
"lone-services/pkg/modelbase"
"lone-services/pkg/rpcclient"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
orderpb "lone-services/rpc/order/pb"
product "lone-services/rpc/product/pb"
storepb "lone-services/rpc/store/pb"
"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 ApiInfoLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewApiInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApiInfoLogic {
return &ApiInfoLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *ApiInfoLogic) ApiInfo(in *product.ApiInfoReq) (*product.Response, error) {
var req validator.ApiProductInfoValidator
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
userInfo := utils.GetUserFromCtx(l.ctx)
var info dao.WebInfo
err := model.ProductModel{}.Init().GetOne(modelbase.Params{
Eq: map[string]string{
"id": strconv.FormatInt(req.Id, utils.NumberTen),
"status": utils.StringStatusOk,
},
Other: map[string]string{"publish_time < ? ": utils.Now().String()},
}, &info)
if err != nil {
l.Errorf("api product info: %v", err)
return failResponse(utils.Fail), nil
}
if info.Id < utils.NumberOne {
return failResponse(utils.ErrorNotFund), nil
}
// 试用资格仅门店登录时计算
if userInfo.StoreId >= utils.NumberOne {
if err := l.fillStorePermit(&info, userInfo.StoreId); err != nil {
l.Errorf("api product info permit: %v", err)
return failResponse(utils.Fail), nil
}
}
return okResponse(toApiItem(info)), nil
}
func (l *ApiInfoLogic) fillStorePermit(info *dao.WebInfo, storeId int64) error {
switch info.Type {
case dao.ProductFirstTry:
exist, err := existStoreProductOrder(l.ctx, l.svcCtx.OrderSvcName, storeId, int64(info.Id))
if err != nil {
l.Errorf("api product info first-try order: %v", err)
return nil
}
if !exist {
info.IsPermit = utils.NumberOne
}
case dao.ProductTry:
storeInfo, err := getStoreInfoById(l.ctx, l.svcCtx.StoreSvcName, storeId)
if err != nil {
return err
}
tryAmount := float64(utils.GetConfigInt("try.try_amount"))
tryError := utils.GetConfigInt("try.try_error")
if storeInfo.BuyAmount > tryAmount && int(storeInfo.ResidualNumber) < tryError {
info.IsPermit = utils.NumberOne
}
}
return nil
}
func getStoreInfoById(ctx context.Context, storeSvcName string, storeId int64) (*storepb.InfoResponse, error) {
cli, err := rpcclient.Get(storeSvcName)
if err != nil {
return nil, err
}
return storepb.NewStoreClient(cli.Conn()).GetInfoById(ctx, &storepb.IdRequest{Id: storeId})
}
func existStoreProductOrder(ctx context.Context, orderSvcName string, storeId, productId int64) (bool, error) {
cli, err := rpcclient.Get(orderSvcName)
if err != nil {
return false, err
}
resp, err := orderpb.NewOrderClient(cli.Conn()).ExistByStoreProduct(ctx, &orderpb.ExistByStoreProductReq{
StoreId: storeId,
ProductId: productId,
})
if err != nil {
return false, err
}
if resp == nil {
return false, nil
}
return resp.Exist, nil
}
@@ -0,0 +1,72 @@
package logic
import (
"context"
"strconv"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
product "lone-services/rpc/product/pb"
"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 ApiItemsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewApiItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApiItemsLogic {
return &ApiItemsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *ApiItemsLogic) ApiItems(in *product.ApiItemsReq) (*product.Response, error) {
var req validator.ApiProductItemsValidator
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
userInfo := utils.GetUserFromCtx(l.ctx)
if !userInfo.Valid || userInfo.ID < utils.NumberOne {
return failResponse(utils.ErrorNoLoginInfo), nil
}
w := modelbase.Params{
Eq: map[string]string{
"status": utils.StringStatusOk,
"sales_model": strconv.Itoa(dao.ProductSalesModelOffline),
},
Other: map[string]string{
"publish_time < ? ": utils.Now().String(),
},
Order: "sort DESC, id DESC",
}
if len(req.Name) > utils.NumberZero {
w.Like = map[string]string{
"name LIKE ? ": "%%" + req.Name + "%%",
}
}
var rows []dao.WebInfo
err := model.ProductModel{}.Init().Items(w, &rows)
if err != nil {
l.Errorf("api product items: %v", err)
return failResponse(utils.Fail), nil
}
items := make([]*product.ApiItem, 0, len(rows))
for _, row := range rows {
items = append(items, toApiItem(row))
}
return okResponse(items), nil
}
@@ -11,6 +11,29 @@ import (
jsoniter "github.com/json-iterator/go"
)
func toApiItem(row dao.WebInfo) *product.ApiItem {
return &product.ApiItem{
Id: int64(row.Id),
Name: row.Name,
Subhead: row.Subhead,
Content: row.Content,
ModelCode: row.ModelCode,
Price: utils.RoundFloat(float64(row.Price)),
StorePrice: utils.RoundFloat(float64(row.StorePrice)),
SalePrice: utils.RoundFloat(float64(row.SalePrice)),
Images: splitImages(row.Images),
Label: row.Label,
IsBuy: uint32(row.IsBuy),
PeriodValidity: int32(row.PeriodValidity),
IsIndex: uint32(row.IsIndex),
IndexImage: row.IndexImage,
Type: uint32(row.Type),
NormsNumber: uint32(row.NormsNumber),
Number: uint32(row.Number),
IsPermit: uint32(row.IsPermit),
}
}
func toProductItem(row dao.Info) *product.Item {
publishTime := ""
if !row.PublishTime.IsZero() {
+4 -1
View File
@@ -7,8 +7,11 @@ import (
jsoniter "github.com/json-iterator/go"
)
// 关闭 EscapeHTML,避免 content 中 <p> 被编成 \u003cp\u003e
var jsonAPI = jsoniter.Config{EscapeHTML: false}.Froze()
func okResponse(data any) *product.Response {
buf, _ := jsoniter.Marshal(data)
buf, _ := jsonAPI.Marshal(data)
return &product.Response{
Code: utils.Ok.Code,
Msg: utils.Ok.Msg,
@@ -96,30 +96,42 @@ func (s *ProductServer) EditApplyPass(ctx context.Context, in *product.EditApply
return l.EditApplyPass(in)
}
// 增加库存
func (s *ProductServer) NumberAdd(ctx context.Context, in *product.NumberAddReq) (*product.Response, error) {
l := logic.NewNumberAddLogic(ctx, s.svcCtx)
return l.NumberAdd(in)
}
func (s *ProductServer) ApiItems(ctx context.Context, in *product.ApiItemsReq) (*product.Response, error) {
l := logic.NewApiItemsLogic(ctx, s.svcCtx)
return l.ApiItems(in)
}
func (s *ProductServer) ApiInfo(ctx context.Context, in *product.ApiInfoReq) (*product.Response, error) {
l := logic.NewApiInfoLogic(ctx, s.svcCtx)
return l.ApiInfo(in)
}
// 增减库存
func (s *ProductServer) Number(ctx context.Context, in *product.NumberReq) (*emptypb.Empty, error) {
l := logic.NewNumberLogic(ctx, s.svcCtx)
return l.Number(in)
}
// 批量增减库存(服务内按 id 排序后在同一事务中执行,防死锁)
// 批量增减库存
func (s *ProductServer) NumberItems(ctx context.Context, in *product.NumberItemsReq) (*emptypb.Empty, error) {
l := logic.NewNumberItemsLogic(ctx, s.svcCtx)
return l.NumberItems(in)
}
// 获取单个产品详情
func (s *ProductServer) InfoById(ctx context.Context, in *product.InfoByIdReq) (*product.Item, error) {
l := logic.NewInfoByIdLogic(ctx, s.svcCtx)
return l.InfoById(in)
}
// 获取多个产品详情
func (s *ProductServer) ItemsByIds(ctx context.Context, in *product.ItemsByIdsReq) (*product.ItemsByIdsData, error) {
l := logic.NewItemsByIdsLogic(ctx, s.svcCtx)
return l.ItemsByIds(in)
}
// 增加库存
func (s *ProductServer) NumberAdd(ctx context.Context, in *product.NumberAddReq) (*product.Response, error) {
l := logic.NewNumberAddLogic(ctx, s.svcCtx)
return l.NumberAdd(in)
}
@@ -14,6 +14,8 @@ type ServiceContext struct {
Prefix string
ChoreSvcName string
WecomSvcName string
StoreSvcName string
OrderSvcName string
}
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
@@ -25,11 +27,21 @@ func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
if wecomSvc == utils.StringEmpty {
logx.Error("config services.wecom empty")
}
storeSvc := utils.GetConfigString("services.store")
if storeSvc == utils.StringEmpty {
logx.Error("config services.store empty")
}
orderSvc := utils.GetConfigString("services.order")
if orderSvc == utils.StringEmpty {
logx.Error("config services.order empty")
}
return &ServiceContext{
Config: c,
DB: db,
Prefix: utils.GetConfigString("mysql.prefix"),
ChoreSvcName: choreSvc,
WecomSvcName: wecomSvc,
StoreSvcName: storeSvc,
OrderSvcName: orderSvc,
}
}