Files
lone-services/services/product/internal/logic/apiInfoLogic.go
T
2026-10-15 09:23:07 +08:00

124 lines
3.3 KiB
Go

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
}
storeId := userInfo.BizId
if storeId < utils.NumberOne {
storeId = userInfo.StoreId
}
if storeId >= utils.NumberOne {
if err := l.fillStorePermit(&info, 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
}