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
@@ -0,0 +1,59 @@
package logic
import (
"context"
"strconv"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/rpc/order/pb"
"lone-services/services/order/internal/dao"
"lone-services/services/order/internal/model"
"lone-services/services/order/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type ExistByStoreProductLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewExistByStoreProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExistByStoreProductLogic {
return &ExistByStoreProductLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// ExistByStoreProduct 门店是否存在指定产品的已支付/待支付/支付中订单(内部)
func (l *ExistByStoreProductLogic) ExistByStoreProduct(in *order.ExistByStoreProductReq) (*order.ExistByStoreProductResp, error) {
if in.StoreId < utils.NumberOne || in.ProductId < utils.NumberOne {
return nil, status.Error(codes.InvalidArgument, utils.ErrorParams.Msg)
}
var info dao.OrderInfo
err := model.OrderModel{}.Init().GetOne(modelbase.Params{
Eq: map[string]string{
"product_ids": strconv.FormatInt(in.ProductId, utils.NumberTen),
"store_id": strconv.FormatInt(in.StoreId, utils.NumberTen),
},
In: map[string][]string{
"status": {
utils.StringStatusOk,
strconv.Itoa(dao.OrderPayStatusWait),
strconv.Itoa(dao.OrderPayStatusIn),
},
},
}, &info)
if err != nil {
l.Errorf("exist by store product: %v", err)
return nil, status.Error(codes.Internal, utils.Fail.Msg)
}
return &order.ExistByStoreProductResp{Exist: info.Id >= utils.NumberOne}, nil
}
@@ -1,5 +1,5 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.10.2
// goctl 1.10.1
// Source: order.proto
package server
@@ -95,6 +95,12 @@ func (s *OrderServer) UpdateOrderPayStatus(ctx context.Context, in *order.Update
return l.UpdateOrderPayStatus(in)
}
// 门店是否存在指定产品的有效/待支付订单(内部)
func (s *OrderServer) ExistByStoreProduct(ctx context.Context, in *order.ExistByStoreProductReq) (*order.ExistByStoreProductResp, error) {
l := logic.NewExistByStoreProductLogic(ctx, s.svcCtx)
return l.ExistByStoreProduct(in)
}
// API
func (s *OrderServer) ApiSupply(ctx context.Context, in *order.ApiSupplyRequest) (*order.Response, error) {
l := logic.NewApiSupplyLogic(ctx, s.svcCtx)
+11 -1
View File
@@ -1,5 +1,5 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.10.2
// goctl 1.10.1
// Source: order.proto
package orderClient
@@ -26,6 +26,8 @@ type (
EditCartRequest = order.EditCartRequest
EmptyRequest = order.EmptyRequest
ExcelExportResponse = order.ExcelExportResponse
ExistByStoreProductReq = order.ExistByStoreProductReq
ExistByStoreProductResp = order.ExistByStoreProductResp
GetOrderInfoRequest = order.GetOrderInfoRequest
IdAndTypeRequest = order.IdAndTypeRequest
IdRequest = order.IdRequest
@@ -64,6 +66,8 @@ type (
GetOrderInfo(ctx context.Context, in *GetOrderInfoRequest, opts ...grpc.CallOption) (*OrderInfoResponse, error)
// status 1 为已支付 4 支付失败,其它的返回错误
UpdateOrderPayStatus(ctx context.Context, in *UpdateOrderPayStatusRequest, opts ...grpc.CallOption) (*UpdateOrderPayStatusResponse, error)
// 门店是否存在指定产品的有效/待支付订单(内部)
ExistByStoreProduct(ctx context.Context, in *ExistByStoreProductReq, opts ...grpc.CallOption) (*ExistByStoreProductResp, error)
// API
ApiSupply(ctx context.Context, in *ApiSupplyRequest, opts ...grpc.CallOption) (*Response, error)
ApiSupplyInfo(ctx context.Context, in *ApiSupplyInfoRequest, opts ...grpc.CallOption) (*Response, error)
@@ -164,6 +168,12 @@ func (m *defaultOrder) UpdateOrderPayStatus(ctx context.Context, in *UpdateOrder
return client.UpdateOrderPayStatus(ctx, in, opts...)
}
// 门店是否存在指定产品的有效/待支付订单(内部)
func (m *defaultOrder) ExistByStoreProduct(ctx context.Context, in *ExistByStoreProductReq, opts ...grpc.CallOption) (*ExistByStoreProductResp, error) {
client := order.NewOrderClient(m.cli.Conn())
return client.ExistByStoreProduct(ctx, in, opts...)
}
// API
func (m *defaultOrder) ApiSupply(ctx context.Context, in *ApiSupplyRequest, opts ...grpc.CallOption) (*Response, error) {
client := order.NewOrderClient(m.cli.Conn())
@@ -78,7 +78,6 @@ func (l *RefundLogic) Refund(in *payment.RefundReq) (*payment.Response, error) {
TotalFen: totalFen,
Currency: "CNY",
})
fmt.Println("outTradeNo", outTradeNo, "outRefundNo", outRefundNo)
if err != nil {
l.Errorf("refund: %v", err)
return outResponse(utils.Fail, err.Error()), nil
+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,
}
}
+32 -13
View File
@@ -16,6 +16,9 @@ import (
)
type (
ApiInfoReq = product.ApiInfoReq
ApiItem = product.ApiItem
ApiItemsReq = product.ApiItemsReq
CreateReq = product.CreateReq
EditApplyPassReq = product.EditApplyPassReq
EditApplyReq = product.EditApplyReq
@@ -64,14 +67,18 @@ type (
EditSusceptible(ctx context.Context, in *EditSusceptibleReq, opts ...grpc.CallOption) (*Response, error)
// TODO: 临时接口,企微编辑申请审批对接后删除
EditApplyPass(ctx context.Context, in *EditApplyPassReq, opts ...grpc.CallOption) (*Response, error)
// 增减库存
Number(ctx context.Context, in *NumberReq, opts ...grpc.CallOption) (*emptypb.Empty, error)
// 批量增减库存(服务内按 id 排序后在同一事务中执行,防死锁)
NumberItems(ctx context.Context, in *NumberItemsReq, opts ...grpc.CallOption) (*emptypb.Empty, error)
InfoById(ctx context.Context, in *InfoByIdReq, opts ...grpc.CallOption) (*Item, error)
ItemsByIds(ctx context.Context, in *ItemsByIdsReq, opts ...grpc.CallOption) (*ItemsByIdsData, error)
// 增加库存
NumberAdd(ctx context.Context, in *NumberAddReq, opts ...grpc.CallOption) (*Response, error)
ApiItems(ctx context.Context, in *ApiItemsReq, opts ...grpc.CallOption) (*Response, error)
ApiInfo(ctx context.Context, in *ApiInfoReq, opts ...grpc.CallOption) (*Response, error)
// 增减库存
Number(ctx context.Context, in *NumberReq, opts ...grpc.CallOption) (*emptypb.Empty, error)
// 批量增减库存
NumberItems(ctx context.Context, in *NumberItemsReq, opts ...grpc.CallOption) (*emptypb.Empty, error)
// 获取单个产品详情
InfoById(ctx context.Context, in *InfoByIdReq, opts ...grpc.CallOption) (*Item, error)
// 获取多个产品详情
ItemsByIds(ctx context.Context, in *ItemsByIdsReq, opts ...grpc.CallOption) (*ItemsByIdsData, error)
}
defaultProduct struct {
@@ -157,30 +164,42 @@ func (m *defaultProduct) EditApplyPass(ctx context.Context, in *EditApplyPassReq
return client.EditApplyPass(ctx, in, opts...)
}
// 增加库存
func (m *defaultProduct) NumberAdd(ctx context.Context, in *NumberAddReq, opts ...grpc.CallOption) (*Response, error) {
client := product.NewProductClient(m.cli.Conn())
return client.NumberAdd(ctx, in, opts...)
}
func (m *defaultProduct) ApiItems(ctx context.Context, in *ApiItemsReq, opts ...grpc.CallOption) (*Response, error) {
client := product.NewProductClient(m.cli.Conn())
return client.ApiItems(ctx, in, opts...)
}
func (m *defaultProduct) ApiInfo(ctx context.Context, in *ApiInfoReq, opts ...grpc.CallOption) (*Response, error) {
client := product.NewProductClient(m.cli.Conn())
return client.ApiInfo(ctx, in, opts...)
}
// 增减库存
func (m *defaultProduct) Number(ctx context.Context, in *NumberReq, opts ...grpc.CallOption) (*emptypb.Empty, error) {
client := product.NewProductClient(m.cli.Conn())
return client.Number(ctx, in, opts...)
}
// 批量增减库存(服务内按 id 排序后在同一事务中执行,防死锁)
// 批量增减库存
func (m *defaultProduct) NumberItems(ctx context.Context, in *NumberItemsReq, opts ...grpc.CallOption) (*emptypb.Empty, error) {
client := product.NewProductClient(m.cli.Conn())
return client.NumberItems(ctx, in, opts...)
}
// 获取单个产品详情
func (m *defaultProduct) InfoById(ctx context.Context, in *InfoByIdReq, opts ...grpc.CallOption) (*Item, error) {
client := product.NewProductClient(m.cli.Conn())
return client.InfoById(ctx, in, opts...)
}
// 获取多个产品详情
func (m *defaultProduct) ItemsByIds(ctx context.Context, in *ItemsByIdsReq, opts ...grpc.CallOption) (*ItemsByIdsData, error) {
client := product.NewProductClient(m.cli.Conn())
return client.ItemsByIds(ctx, in, opts...)
}
// 增加库存
func (m *defaultProduct) NumberAdd(ctx context.Context, in *NumberAddReq, opts ...grpc.CallOption) (*Response, error) {
client := product.NewProductClient(m.cli.Conn())
return client.NumberAdd(ctx, in, opts...)
}
+27 -6
View File
@@ -64,6 +64,27 @@ func (p ProductInfoValidator) GetMessage() validate.ValidatorMessages {
}
}
type ApiProductInfoValidator struct {
Id int64 `validate:"required,gt=0"`
}
func (p ApiProductInfoValidator) GetMessage() validate.ValidatorMessages {
return validate.ValidatorMessages{
"Id.required": "ID不能为空",
"Id.gt": "ID必须大于0",
}
}
type ApiProductItemsValidator struct {
Name string `validate:"max=256"`
}
func (p ApiProductItemsValidator) GetMessage() validate.ValidatorMessages {
return validate.ValidatorMessages{
"Name.max": "产品名长度不能超过256",
}
}
type ProductItemsValidator struct {
Name string `validate:"max=256"`
Page uint32 `validate:"omitempty,min=1"`
@@ -163,12 +184,12 @@ func (p ProductEditApplyPassValidator) GetMessage() validate.ValidatorMessages {
}
type ProductEditBaseValidator struct {
Id int64 `validate:"required,gt=0"`
Name string `validate:"required,max=256"`
Subhead string `validate:"required,max=255"`
Content string `validate:"required"`
Waybill string `validate:"required,max=255"`
Weight float64 `validate:"required"`
Id int64 `validate:"required,gt=0"`
Name string `validate:"required,max=256"`
Subhead string `validate:"required,max=255"`
Content string `validate:"required"`
Waybill string `validate:"required,max=255"`
Weight float64 `validate:"required"`
Cubage string `validate:"required,max=255"`
Images []string `validate:"required,min=1,dive,required"`
PeriodValidity int32 `validate:"required"`
@@ -68,5 +68,7 @@ func (l *GetInfoByIdLogic) GetInfoById(in *store.IdRequest) (*store.InfoResponse
DistrictIds: info.District,
Area: info.Area,
BankInfo: info.BankInfo,
BuyAmount: info.BuyAmount,
ResidualNumber: info.ResidualNumber,
}, nil
}
@@ -67,6 +67,8 @@ func (l *GetInfoByIdsLogic) GetInfoByIds(in *store.IdsRequest) (*store.ItemsResp
DistrictIds: item.District,
Area: item.Area,
BankInfo: item.BankInfo,
BuyAmount: item.BuyAmount,
ResidualNumber: item.ResidualNumber,
})
}