| 
									
										
										
										
											2023-07-17 11:28:59 +08:00
										 |  |  | package logic | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fusenapi/constants" | 
					
						
							|  |  |  | 	"fusenapi/utils/auth" | 
					
						
							|  |  |  | 	"fusenapi/utils/basic" | 
					
						
							| 
									
										
										
										
											2023-08-08 16:27:42 +08:00
										 |  |  | 	"fusenapi/utils/format" | 
					
						
							| 
									
										
										
										
											2023-07-17 11:28:59 +08:00
										 |  |  | 	"gorm.io/gorm" | 
					
						
							| 
									
										
										
										
											2023-08-08 16:27:42 +08:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2023-07-17 11:28:59 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"fusenapi/server/product/internal/svc" | 
					
						
							|  |  |  | 	"fusenapi/server/product/internal/types" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/zeromicro/go-zero/core/logx" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type GetSizeByPidLogic struct { | 
					
						
							|  |  |  | 	logx.Logger | 
					
						
							|  |  |  | 	ctx    context.Context | 
					
						
							|  |  |  | 	svcCtx *svc.ServiceContext | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewGetSizeByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSizeByPidLogic { | 
					
						
							|  |  |  | 	return &GetSizeByPidLogic{ | 
					
						
							|  |  |  | 		Logger: logx.WithContext(ctx), | 
					
						
							|  |  |  | 		ctx:    ctx, | 
					
						
							|  |  |  | 		svcCtx: svcCtx, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (l *GetSizeByPidLogic) GetSizeByPid(req *types.GetSizeByPidReq, 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") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	//获取产品尺寸列表(需要正序排序) | 
					
						
							| 
									
										
										
										
											2023-08-18 12:06:59 +08:00
										 |  |  | 	sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "is_hot DESC,sort ASC") | 
					
						
							| 
									
										
										
										
											2023-07-17 11:28:59 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sizeIds := make([]int64, 0, len(sizeList)) | 
					
						
							| 
									
										
										
										
											2023-08-08 16:27:42 +08:00
										 |  |  | 	productIds := make([]int64, 0, len(sizeList)) | 
					
						
							| 
									
										
										
										
											2023-07-17 11:28:59 +08:00
										 |  |  | 	for _, v := range sizeList { | 
					
						
							|  |  |  | 		sizeIds = append(sizeIds, v.Id) | 
					
						
							| 
									
										
										
										
											2023-08-08 16:27:42 +08:00
										 |  |  | 		productIds = append(productIds, *v.ProductId) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	//获取产品价格列表 | 
					
						
							|  |  |  | 	productPriceList, err := l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price list") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	mapProductMinPrice := make(map[int64]int64) | 
					
						
							|  |  |  | 	//存储产品最小价格 | 
					
						
							|  |  |  | 	for _, v := range productPriceList { | 
					
						
							|  |  |  | 		priceStrSlic := strings.Split(v.Price, ",") | 
					
						
							|  |  |  | 		priceSlice, err := format.StrSlicToIntSlice(priceStrSlic) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logx.Error(err) | 
					
						
							|  |  |  | 			return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if len(priceSlice) == 0 { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		//正序排序价格(注意排序后的阶梯价格不能用作阶梯数量价格计算) | 
					
						
							|  |  |  | 		sort.Ints(priceSlice) | 
					
						
							|  |  |  | 		if min, ok := mapProductMinPrice[v.ProductId]; ok { | 
					
						
							|  |  |  | 			if min > int64(priceSlice[0]) { | 
					
						
							|  |  |  | 				mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-07-17 11:28:59 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	//获取对应模型数据 | 
					
						
							|  |  |  | 	modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id,size_id") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	mapSizeModel := make(map[int64]int) //size id为key | 
					
						
							|  |  |  | 	for k, v := range modelList { | 
					
						
							|  |  |  | 		mapSizeModel[*v.SizeId] = k | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	//处理 | 
					
						
							|  |  |  | 	listRsp := make([]types.GetSizeByPidRsp, 0, len(sizeList)) | 
					
						
							|  |  |  | 	for _, sizeInfo := range sizeList { | 
					
						
							|  |  |  | 		//没有模型的不能使用 | 
					
						
							|  |  |  | 		modelIndex, ok := mapSizeModel[sizeInfo.Id] | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		var title interface{} | 
					
						
							|  |  |  | 		_ = json.Unmarshal([]byte(*sizeInfo.Title), &title) | 
					
						
							| 
									
										
										
										
											2023-08-08 16:27:42 +08:00
										 |  |  | 		minPrice := int64(0) | 
					
						
							|  |  |  | 		if price, ok := mapProductMinPrice[*sizeInfo.ProductId]; ok { | 
					
						
							|  |  |  | 			minPrice = price | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-07-17 11:28:59 +08:00
										 |  |  | 		listRsp = append(listRsp, types.GetSizeByPidRsp{ | 
					
						
							|  |  |  | 			Id:              sizeInfo.Id, | 
					
						
							|  |  |  | 			Title:           title, | 
					
						
							|  |  |  | 			Capacity:        *sizeInfo.Capacity, | 
					
						
							|  |  |  | 			Cover:           *sizeInfo.Cover, | 
					
						
							|  |  |  | 			PartsCanDeleted: *sizeInfo.PartsCanDeleted > 0, | 
					
						
							|  |  |  | 			ModelId:         modelList[modelIndex].Id, | 
					
						
							| 
									
										
										
										
											2023-08-18 11:43:50 +08:00
										 |  |  | 			IsPopular:       *sizeInfo.IsHot > 0, | 
					
						
							| 
									
										
										
										
											2023-08-08 16:27:42 +08:00
										 |  |  | 			MinPrice:        float64(minPrice) / 100, | 
					
						
							| 
									
										
										
										
											2023-07-17 11:28:59 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp) | 
					
						
							|  |  |  | } |