| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | package logic | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 	"fusenapi/constants" | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | 	"fusenapi/utils/auth" | 
					
						
							|  |  |  | 	"fusenapi/utils/basic" | 
					
						
							|  |  |  | 	"gorm.io/gorm" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"fusenapi/server/product/internal/svc" | 
					
						
							|  |  |  | 	"fusenapi/server/product/internal/types" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/zeromicro/go-zero/core/logx" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type GetTemplateByPidLogic struct { | 
					
						
							|  |  |  | 	logx.Logger | 
					
						
							|  |  |  | 	ctx    context.Context | 
					
						
							|  |  |  | 	svcCtx *svc.ServiceContext | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewGetTemplateByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTemplateByPidLogic { | 
					
						
							|  |  |  | 	return &GetTemplateByPidLogic{ | 
					
						
							|  |  |  | 		Logger: logx.WithContext(ctx), | 
					
						
							|  |  |  | 		ctx:    ctx, | 
					
						
							|  |  |  | 		svcCtx: svcCtx, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, 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") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-08-18 10:26:33 +08:00
										 |  |  | 	if req.TemplateTag == "" { | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:template_tag") | 
					
						
							| 
									
										
										
										
											2023-08-08 17:03:54 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-08-11 14:52:16 +08:00
										 |  |  | 	//获取产品信息(只获取id) | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | 	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-11 18:59:03 +08:00
										 |  |  | 	//没有指定物料 | 
					
						
							|  |  |  | 	sizeIds := make([]int64, 0, 10) | 
					
						
							|  |  |  | 	if req.ProductSizeId <= 0 { | 
					
						
							|  |  |  | 		//获取产品所有物料 | 
					
						
							|  |  |  | 		sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "id") | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logx.Error(err) | 
					
						
							|  |  |  | 			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "failed to get product  size list") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if len(sizeList) == 0 { | 
					
						
							|  |  |  | 			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product size list is empty") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for _, v := range sizeList { | 
					
						
							|  |  |  | 			sizeIds = append(sizeIds, v.Id) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { //指定物料 | 
					
						
							|  |  |  | 		sizeIds = append(sizeIds, req.ProductSizeId) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-08-14 10:51:39 +08:00
										 |  |  | 	//根据尺寸id获取模型 | 
					
						
							|  |  |  | 	modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL) | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 	if len(modelList) == 0 { | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "model list is empty") | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 	modelIds := make([]int64, 0, len(modelList)) | 
					
						
							| 
									
										
										
										
											2023-08-14 10:51:39 +08:00
										 |  |  | 	mapModel := make(map[int64]int) | 
					
						
							|  |  |  | 	for k, v := range modelList { | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 		modelIds = append(modelIds, v.Id) | 
					
						
							| 
									
										
										
										
											2023-08-14 10:51:39 +08:00
										 |  |  | 		mapModel[v.Id] = k | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 	//查询模型ids下对应tag标签的模板 | 
					
						
							| 
									
										
										
										
											2023-08-18 10:26:33 +08:00
										 |  |  | 	templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByModelIdsTemplateTag(l.ctx, modelIds, req.TemplateTag, "") | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template list") | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-08-14 14:21:14 +08:00
										 |  |  | 	rsp := make(map[string]interface{}) | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 	for _, templateInfo := range templateList { | 
					
						
							|  |  |  | 		//没有设置模板据不要 | 
					
						
							|  |  |  | 		if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-08-14 10:51:39 +08:00
										 |  |  | 		modelIndex, ok := mapModel[*templateInfo.ModelId] | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 		//基础模板信息 | 
					
						
							| 
									
										
										
										
											2023-08-14 15:52:10 +08:00
										 |  |  | 		var info interface{} | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 		if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &info); err != nil { | 
					
						
							|  |  |  | 			logx.Error(err) | 
					
						
							| 
									
										
										
										
											2023-08-14 10:51:39 +08:00
										 |  |  | 			return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse json product template info(may be old data):%d", templateInfo.Id)) | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-08-18 12:30:53 +08:00
										 |  |  | 		//后台隐藏/显示信息(现在下面是写死了) | 
					
						
							|  |  |  | 		/*var switchInfo interface{} | 
					
						
							| 
									
										
										
										
											2023-08-11 18:59:03 +08:00
										 |  |  | 		if templateInfo.SwitchInfo != nil && *templateInfo.SwitchInfo != "" { | 
					
						
							|  |  |  | 			_ = json.Unmarshal([]byte(*templateInfo.SwitchInfo), &switchInfo) | 
					
						
							| 
									
										
										
										
											2023-08-18 12:30:53 +08:00
										 |  |  | 		}*/ | 
					
						
							| 
									
										
										
										
											2023-08-14 10:51:39 +08:00
										 |  |  | 		modelInfo := modelList[modelIndex] | 
					
						
							|  |  |  | 		mapKey := fmt.Sprintf("_%d", *modelInfo.SizeId) | 
					
						
							| 
									
										
										
										
											2023-08-14 14:21:14 +08:00
										 |  |  | 		rsp[mapKey] = map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2023-08-18 12:30:53 +08:00
										 |  |  | 			"id":       templateInfo.Id, | 
					
						
							|  |  |  | 			"material": *templateInfo.MaterialImg, | 
					
						
							| 
									
										
										
										
											2023-08-18 14:57:21 +08:00
										 |  |  | 			//写死的数据 | 
					
						
							| 
									
										
										
										
											2023-08-18 12:30:53 +08:00
										 |  |  | 			"material_data": map[string]interface{}{ | 
					
						
							|  |  |  | 				"QRcode": map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2023-08-21 15:27:53 +08:00
										 |  |  | 					"if_show":       true, | 
					
						
							| 
									
										
										
										
											2023-08-29 16:36:36 +08:00
										 |  |  | 					"text":          "qrcode", | 
					
						
							|  |  |  | 					"default_value": "default qrcode", | 
					
						
							| 
									
										
										
										
											2023-08-18 12:30:53 +08:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2023-08-21 15:27:53 +08:00
										 |  |  | 				"Website": map[string]interface{}{ | 
					
						
							|  |  |  | 					"if_show":       true, | 
					
						
							| 
									
										
										
										
											2023-08-29 16:36:36 +08:00
										 |  |  | 					"text":          "website", | 
					
						
							|  |  |  | 					"default_value": "default website", | 
					
						
							| 
									
										
										
										
											2023-08-18 12:30:53 +08:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2023-08-21 15:27:53 +08:00
										 |  |  | 				"Address": map[string]interface{}{ | 
					
						
							|  |  |  | 					"if_show":       true, | 
					
						
							|  |  |  | 					"text":          "address", | 
					
						
							| 
									
										
										
										
											2023-08-29 16:36:36 +08:00
										 |  |  | 					"default_value": "default address", | 
					
						
							| 
									
										
										
										
											2023-08-18 12:30:53 +08:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2023-08-21 15:27:53 +08:00
										 |  |  | 				"Phone": map[string]interface{}{ | 
					
						
							|  |  |  | 					"if_show":       true, | 
					
						
							|  |  |  | 					"text":          "phone", | 
					
						
							| 
									
										
										
										
											2023-08-29 16:36:36 +08:00
										 |  |  | 					"default_value": "17557283679", | 
					
						
							| 
									
										
										
										
											2023-08-18 12:30:53 +08:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2023-08-21 15:27:53 +08:00
										 |  |  | 				"Logo": map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2023-08-18 12:30:53 +08:00
										 |  |  | 					"material": "/image/logo/aHnT1_rzubdwax_scale.png", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2023-08-14 14:21:14 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-08-14 10:51:39 +08:00
										 |  |  | 	return resp.SetStatusWithMessage(basic.CodeOK, "success", rsp) | 
					
						
							| 
									
										
										
										
											2023-07-17 14:40:24 +08:00
										 |  |  | } |