fusenapi/server/product/internal/logic/getpricebypidlogic.go
laodaming edd3d7353d fix
2023-07-14 19:25:52 +08:00

79 lines
2.6 KiB
Go

package logic
import (
"errors"
"fmt"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
"gorm.io/gorm"
"strings"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetPriceByPidLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetPriceByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPriceByPidLogic {
return &GetPriceByPidLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.Pid = strings.Trim(req.Pid, " ")
if req.Pid == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty")
}
//获取产品信息(只是获取id)
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
}
//查询产品价格
priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIds(l.ctx, []int64{productInfo.Id})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list")
}
//处理价格信息
mapRsp := make(map[string]interface{})
for _, v := range priceList {
mapKey := fmt.Sprintf("_%d", v.Id)
stepNum, err := format.StrSlicToIntSlice(strings.Split(*v.StepNum, ","))
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", v.Id))
}
/*$price['step_num'] = explode(',', $price['step_num']);
$price['step_price'] = explode(',', $price['step_price']);
while ($price['min_buy_num'] < end($price['step_num']) + 5) {
$outData["{$price['size_id']}"]['items'][] = [
'num' => intval($price['min_buy_num']),
'total_num' => $price['min_buy_num'] * $price['each_box_num'],
'price' => ProductPriceService::getPrice($price['min_buy_num'], $price['step_num'], $price['step_price'])
];
$price['min_buy_num'] += 1;
}
$outData["{$price['size_id']}"]['min_price'] = floatval(end($price['step_price']) / 100);
$outData["{$price['size_id']}"]['max_price'] = floatval(reset($price['step_price']) / 100);*/
}
return resp.SetStatus(basic.CodeOK)
}