| 
									
										
										
										
											2023-06-06 15:52:41 +08:00
										 |  |  | package logic | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-07-11 17:08:19 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	"fusenapi/model/gmodel" | 
					
						
							| 
									
										
										
										
											2023-06-08 11:03:20 +08:00
										 |  |  | 	"fusenapi/server/product/internal/svc" | 
					
						
							|  |  |  | 	"fusenapi/server/product/internal/types" | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 	"fusenapi/utils/auth" | 
					
						
							| 
									
										
										
										
											2023-06-07 12:21:07 +08:00
										 |  |  | 	"fusenapi/utils/basic" | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 	"fusenapi/utils/image" | 
					
						
							| 
									
										
										
										
											2023-06-06 15:52:41 +08:00
										 |  |  | 	"github.com/zeromicro/go-zero/core/logx" | 
					
						
							| 
									
										
										
										
											2023-07-11 17:08:19 +08:00
										 |  |  | 	"gorm.io/gorm" | 
					
						
							| 
									
										
										
										
											2023-06-06 15:52:41 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type GetSuccessRecommandLogic struct { | 
					
						
							|  |  |  | 	logx.Logger | 
					
						
							|  |  |  | 	ctx    context.Context | 
					
						
							|  |  |  | 	svcCtx *svc.ServiceContext | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSuccessRecommandLogic { | 
					
						
							|  |  |  | 	return &GetSuccessRecommandLogic{ | 
					
						
							|  |  |  | 		Logger: logx.WithContext(ctx), | 
					
						
							|  |  |  | 		ctx:    ctx, | 
					
						
							|  |  |  | 		svcCtx: svcCtx, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-06 18:17:03 +08:00
										 |  |  | // 获取推荐的产品列表 | 
					
						
							| 
									
										
										
										
											2023-06-13 19:11:30 +08:00
										 |  |  | func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, 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-06 17:18:01 +08:00
										 |  |  | 	//获取用户信息 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn) | 
					
						
							| 
									
										
										
										
											2023-06-16 14:52:45 +08:00
										 |  |  | 	user, err := userModel.FindUserById(l.ctx, userInfo.UserId) | 
					
						
							| 
									
										
										
										
											2023-07-11 17:08:19 +08:00
										 |  |  | 	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 		logx.Error(err) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 		return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get user info") | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-06 18:17:03 +08:00
										 |  |  | 	if req.Num == 0 || req.Num > 500 { | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 		req.Num = 8 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if req.Size > 0 { | 
					
						
							|  |  |  | 		req.Size = image.GetCurrentSize(req.Size) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-06 18:17:03 +08:00
										 |  |  | 	//随机取8个产品 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn) | 
					
						
							| 
									
										
										
										
											2023-06-06 18:17:03 +08:00
										 |  |  | 	productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num)) | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 		return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list") | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	//没有推荐产品就返回 | 
					
						
							|  |  |  | 	if len(productList) == 0 { | 
					
						
							| 
									
										
										
										
											2023-06-07 17:27:17 +08:00
										 |  |  | 		return resp.SetStatusWithMessage(basic.CodeOK, "success") | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-06 18:17:03 +08:00
										 |  |  | 	list := make([]types.GetSuccessRecommandRsp, 0, len(productList)) | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 	for _, v := range productList { | 
					
						
							| 
									
										
										
										
											2023-06-06 18:17:03 +08:00
										 |  |  | 		data := types.GetSuccessRecommandRsp{ | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 			Title: *v.Title, | 
					
						
							|  |  |  | 			Sn:    *v.Sn, | 
					
						
							| 
									
										
										
										
											2023-06-06 19:21:07 +08:00
										 |  |  | 			Id:    v.Id, | 
					
						
							|  |  |  | 			SkuId: 0, //??????? | 
					
						
							| 
									
										
										
										
											2023-06-06 18:17:03 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-06-06 19:21:07 +08:00
										 |  |  | 		//千人千面处理 | 
					
						
							| 
									
										
										
										
											2023-07-11 17:08:19 +08:00
										 |  |  | 		r := image.ThousandFaceImageFormatReq{ | 
					
						
							| 
									
										
										
										
											2023-06-06 19:21:07 +08:00
										 |  |  | 			Size:           int(req.Size), | 
					
						
							| 
									
										
										
										
											2023-07-11 17:08:19 +08:00
										 |  |  | 			IsThousandFace: 0, | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 			Cover:          *v.Cover, | 
					
						
							|  |  |  | 			CoverImg:       *v.CoverImg, | 
					
						
							|  |  |  | 			CoverDefault:   *v.CoverImg, | 
					
						
							| 
									
										
										
										
											2023-06-06 19:21:07 +08:00
										 |  |  | 			ProductId:      v.Id, | 
					
						
							| 
									
										
										
										
											2023-07-11 17:08:19 +08:00
										 |  |  | 			UserId:         user.Id, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if user.Id > 0 { | 
					
						
							|  |  |  | 			r.IsThousandFace = int(*user.IsThousandFace) | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-07-11 17:08:19 +08:00
										 |  |  | 		image.ThousandFaceImageFormat(&r) | 
					
						
							|  |  |  | 		data.Cover = r.Cover | 
					
						
							|  |  |  | 		data.CoverImg = r.CoverImg | 
					
						
							|  |  |  | 		data.CoverDefault = r.CoverDefault | 
					
						
							| 
									
										
										
										
											2023-06-06 18:17:03 +08:00
										 |  |  | 		list = append(list, data) | 
					
						
							| 
									
										
										
										
											2023-06-06 17:18:01 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-07 12:21:07 +08:00
										 |  |  | 	return resp.SetStatusWithMessage(basic.CodeOK, "success", list) | 
					
						
							| 
									
										
										
										
											2023-06-06 15:52:41 +08:00
										 |  |  | } |