| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | package logic | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-06-07 18:09:54 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"fusenapi/constants" | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	"fusenapi/model/gmodel" | 
					
						
							|  |  |  | 	"fusenapi/utils/auth" | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	"fusenapi/utils/basic" | 
					
						
							|  |  |  | 	"fusenapi/utils/format" | 
					
						
							| 
									
										
										
										
											2023-06-13 17:47:48 +08:00
										 |  |  | 	"fusenapi/utils/step_price" | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 11:03:20 +08:00
										 |  |  | 	"fusenapi/server/product/internal/svc" | 
					
						
							|  |  |  | 	"fusenapi/server/product/internal/types" | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/zeromicro/go-zero/core/logx" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type GetSizeByProductLogic struct { | 
					
						
							|  |  |  | 	logx.Logger | 
					
						
							|  |  |  | 	ctx    context.Context | 
					
						
							|  |  |  | 	svcCtx *svc.ServiceContext | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewGetSizeByProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSizeByProductLogic { | 
					
						
							|  |  |  | 	return &GetSizeByProductLogic{ | 
					
						
							|  |  |  | 		Logger: logx.WithContext(ctx), | 
					
						
							|  |  |  | 		ctx:    ctx, | 
					
						
							|  |  |  | 		svcCtx: svcCtx, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 获取分类下的产品以及尺寸 | 
					
						
							| 
									
										
										
										
											2023-07-21 15:20:18 +08:00
										 |  |  | func (l *GetSizeByProductLogic) GetSizeByProduct(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { | 
					
						
							| 
									
										
										
										
											2023-06-15 12:00:32 +08:00
										 |  |  | 	if userinfo.GetIdType() != auth.IDTYPE_User { | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	//获取所有网站目录 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	tagsModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn) | 
					
						
							| 
									
										
										
										
											2023-06-09 20:06:24 +08:00
										 |  |  | 	tagsList, err := tagsModel.GetAllByLevel(l.ctx, constants.TYPE_WEBSITE) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get website tags") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(tagsList) == 0 { | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeOK, "tag list is null") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	tagIds := make([]int64, 0, len(tagsList)) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	for _, v := range tagsList { | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		tagIds = append(tagIds, v.Id) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	//获取这些类型的产品 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn) | 
					
						
							|  |  |  | 	productList, err := productModel.GetProductListByIds(l.ctx, tagIds, "sort-desc") | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get tag product list") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	productIds := make([]int64, 0, len(productList)) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	for _, v := range productList { | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		productIds = append(productIds, v.Id) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn) | 
					
						
							| 
									
										
										
										
											2023-07-14 16:48:04 +08:00
										 |  |  | 	productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort DESC") | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	sizeIds := make([]int64, 0, len(productSizeList)) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	for _, v := range productSizeList { | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		sizeIds = append(sizeIds, v.Id) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	//获取价格列表 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	productPriceList, err := productPriceModel.GetPriceListBySizeIds(l.ctx, sizeIds) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product proce list") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	mapProductPrice := make(map[int64]gmodel.FsProductPrice) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	for _, v := range productPriceList { | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		mapProductPrice[*v.SizeId] = v | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	//组装返回 | 
					
						
							|  |  |  | 	list := make([]types.GetSizeByProductRsp, 0, len(tagsList)) | 
					
						
							|  |  |  | 	for _, tag := range tagsList { | 
					
						
							|  |  |  | 		//获取第一层子类 | 
					
						
							|  |  |  | 		firstChildrenList, err := l.GetFirstChildrenList(tag, productList, productSizeList, mapProductPrice) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logx.Error(err) | 
					
						
							|  |  |  | 			return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get first level children  list") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		data := types.GetSizeByProductRsp{ | 
					
						
							|  |  |  | 			Id:       tag.Id, | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 			Name:     *tag.Title, | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 			Children: firstChildrenList, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		list = append(list, data) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return resp.SetStatusWithMessage(basic.CodeOK, "success", list) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 第一层子层 | 
					
						
							| 
									
										
										
										
											2023-06-29 11:43:07 +08:00
										 |  |  | func (l *GetSizeByProductLogic) GetFirstChildrenList(tag gmodel.FsTags, productList []gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenList []types.Children, err error) { | 
					
						
							|  |  |  | 	childrenList = make([]types.Children, 0, len(productList)) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	for _, product := range productList { | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		if *product.Type != tag.Id { | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		childrenObjList, err := l.GetSecondChildrenList(product, productSizeList, mapProductPrice) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		//获取第二层子类 | 
					
						
							| 
									
										
										
										
											2023-06-29 11:43:07 +08:00
										 |  |  | 		data := types.Children{ | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 			Id:           product.Id, | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 			Name:         *product.Title, | 
					
						
							|  |  |  | 			Cycle:        int(*product.DeliveryDays + *product.ProduceDays), | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 			ChildrenList: childrenObjList, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		childrenList = append(childrenList, data) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 第2层子层 | 
					
						
							| 
									
										
										
										
											2023-06-29 11:43:07 +08:00
										 |  |  | func (l *GetSizeByProductLogic) GetSecondChildrenList(product gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) { | 
					
						
							|  |  |  | 	childrenObjList = make([]types.ChildrenObj, 0, len(productSizeList)) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 	for _, productSize := range productSizeList { | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		if product.Id != *productSize.ProductId { | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-06-29 11:43:07 +08:00
										 |  |  | 		priceList := make([]types.PriceObj, 0, len(productSizeList)) | 
					
						
							| 
									
										
										
										
											2023-06-07 18:09:54 +08:00
										 |  |  | 		price, ok := mapProductPrice[productSize.Id] | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 		//无对应尺寸价格 | 
					
						
							| 
									
										
										
										
											2023-06-07 18:09:54 +08:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2023-06-29 17:48:07 +08:00
										 |  |  | 			priceList = []types.PriceObj{ | 
					
						
							|  |  |  | 				{Num: 1, Price: 0}, | 
					
						
							|  |  |  | 				{Num: 1, Price: 0}, | 
					
						
							|  |  |  | 				{Num: 1, Price: 0}, | 
					
						
							| 
									
										
										
										
											2023-06-20 18:37:56 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-06-29 11:43:07 +08:00
										 |  |  | 			childrenObjList = append(childrenObjList, types.ChildrenObj{ | 
					
						
							| 
									
										
										
										
											2023-06-20 18:37:56 +08:00
										 |  |  | 				Id:        productSize.Id, | 
					
						
							|  |  |  | 				Name:      *productSize.Capacity, | 
					
						
							|  |  |  | 				PriceList: priceList, | 
					
						
							| 
									
										
										
										
											2023-06-07 18:09:54 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		if price.StepNum == nil || price.StepPrice == nil { | 
					
						
							| 
									
										
										
										
											2023-06-07 18:09:54 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		*price.StepNum = strings.Trim(*price.StepNum, " ") | 
					
						
							|  |  |  | 		*price.StepPrice = strings.Trim(*price.StepPrice, " ") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-07 18:09:54 +08:00
										 |  |  | 		//阶梯数量切片 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		stepNum, err := format.StrSlicToIntSlice(strings.Split(*price.StepNum, ",")) | 
					
						
							| 
									
										
										
										
											2023-06-07 18:09:54 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		//阶梯价格切片 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		stepPrice, err := format.StrSlicToIntSlice(strings.Split(*price.StepPrice, ",")) | 
					
						
							| 
									
										
										
										
											2023-06-07 18:09:54 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-06-08 10:41:57 +08:00
										 |  |  | 		if len(stepNum) == 0 || len(stepPrice) == 0 { | 
					
						
							|  |  |  | 			return nil, errors.New(fmt.Sprintf("stepNum count or stepPrice count is empty: product size id :%d ,product price id :%d", productSize.Id, price.Id)) | 
					
						
							| 
									
										
										
										
											2023-06-07 18:09:54 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-06-08 10:41:57 +08:00
										 |  |  | 		index := 0 | 
					
						
							|  |  |  | 		// 最小购买数量小于 最大阶梯数量+5 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 		for int(*price.MinBuyNum) < (stepNum[len(stepNum)-1]+5) && index < 3 { | 
					
						
							| 
									
										
										
										
											2023-06-29 11:43:07 +08:00
										 |  |  | 			priceList = append(priceList, types.PriceObj{ | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 				Num:   int(*price.MinBuyNum * *price.EachBoxNum), | 
					
						
							| 
									
										
										
										
											2023-06-13 17:47:48 +08:00
										 |  |  | 				Price: step_price.GetStepPrice(int(*price.MinBuyNum), stepNum, stepPrice), | 
					
						
							| 
									
										
										
										
											2023-06-08 10:41:57 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 			*price.MinBuyNum++ | 
					
						
							| 
									
										
										
										
											2023-06-08 10:41:57 +08:00
										 |  |  | 			index++ | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-06-29 11:43:07 +08:00
										 |  |  | 		data := types.ChildrenObj{ | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 			Id:        productSize.Id, | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 			Name:      *productSize.Capacity, | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 			PriceList: priceList, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		childrenObjList = append(childrenObjList, data) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } |