86 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			86 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
|  | package logic | ||
|  | 
 | ||
|  | import ( | ||
|  | 	"encoding/json" | ||
|  | 	"errors" | ||
|  | 	"fusenapi/constants" | ||
|  | 	"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 GetModelByPidLogic struct { | ||
|  | 	logx.Logger | ||
|  | 	ctx    context.Context | ||
|  | 	svcCtx *svc.ServiceContext | ||
|  | } | ||
|  | 
 | ||
|  | func NewGetModelByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetModelByPidLogic { | ||
|  | 	return &GetModelByPidLogic{ | ||
|  | 		Logger: logx.WithContext(ctx), | ||
|  | 		ctx:    ctx, | ||
|  | 		svcCtx: svcCtx, | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  | func (l *GetModelByPidLogic) GetModelByPid(req *types.GetModelByPidReq, 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") | ||
|  | 	} | ||
|  | 	//获取产品尺寸列表(只是获取id) | ||
|  | 	sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "id") | ||
|  | 	if err != nil { | ||
|  | 		logx.Error(err) | ||
|  | 		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list") | ||
|  | 	} | ||
|  | 	if len(sizeList) == 0 { | ||
|  | 		return resp.SetStatusWithMessage(basic.CodeOK, "success:size list is empty", []interface{}{}) | ||
|  | 	} | ||
|  | 	sizeIds := make([]int64, 0, len(sizeList)) | ||
|  | 	for _, v := range sizeList { | ||
|  | 		sizeIds = append(sizeIds, v.Id) | ||
|  | 	} | ||
|  | 	//获取模型数据(只是获取model_info) | ||
|  | 	model3dList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id,model_info") | ||
|  | 	if err != nil { | ||
|  | 		logx.Error(err) | ||
|  | 		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") | ||
|  | 	} | ||
|  | 	if len(model3dList) == 0 { | ||
|  | 		return resp.SetStatusWithMessage(basic.CodeOK, "success:model list is empty", []interface{}{}) | ||
|  | 	} | ||
|  | 	//处理数据 | ||
|  | 	mapModel := make(map[int64]interface{}) | ||
|  | 	for _, v := range model3dList { | ||
|  | 		if v.ModelInfo == nil || *v.ModelInfo == "" { | ||
|  | 			mapModel[v.Id] = nil | ||
|  | 			continue | ||
|  | 		} | ||
|  | 		var info interface{} | ||
|  | 		if err = json.Unmarshal([]byte(*v.ModelInfo), &info); err != nil { | ||
|  | 			logx.Error(err) | ||
|  | 			return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse model info json") | ||
|  | 		} | ||
|  | 		mapModel[v.Id] = info | ||
|  | 	} | ||
|  | 	return resp.SetStatusWithMessage(basic.CodeOK, "success", mapModel) | ||
|  | } |