diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index 6328daa4..16081b58 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -6,8 +6,12 @@ func (p *FsProductModel) FindOne(ctx context.Context, id int64) (resp *FsProduct err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` = ? ", id).First(&resp).Error return resp, err } -func (p *FsProductModel) FindOneBySn(ctx context.Context, sn string) (resp *FsProduct, err error) { - err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`sn` = ? ", sn).Take(&resp).Error +func (p *FsProductModel) FindOneBySn(ctx context.Context, sn string, fields ...string) (resp *FsProduct, err error) { + db := p.db.WithContext(ctx).Model(&FsProduct{}).Where("`sn` = ? ", sn) + if len(fields) != 0 { + db = db.Select(fields[0]) + } + err = db.Take(&resp).Error return resp, err } func (p *FsProductModel) GetProductListByIds(ctx context.Context, productIds []int64, sort string) (resp []FsProduct, err error) { diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index 4b8ab2b1..fde41568 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -60,11 +60,15 @@ func (d *FsProductModel3dModel) Get3dModelsByParam(ctx context.Context, req Get3 func (d *FsProductModel3dModel) Update(ctx context.Context, id int64, data *FsProductModel3d) error { return d.db.WithContext(ctx).Where("`id` = ? ", id).Updates(&data).Error } -func (d *FsProductModel3dModel) GetAllBySizeIdsTag(ctx context.Context, sizeIds []int64, tag int64) (resp []FsProductModel3d, err error) { +func (d *FsProductModel3dModel) GetAllBySizeIdsTag(ctx context.Context, sizeIds []int64, tag int64, fields ...string) (resp []FsProductModel3d, err error) { if len(sizeIds) == 0 { return } - err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`size_id` in (?) and `tag` = ?", sizeIds, tag).Find(&resp).Error + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`size_id` in (?) and `tag` = ?", sizeIds, tag) + if len(fields) != 0 { + db = db.Select(fields[0]) + } + err = db.Find(&resp).Error return resp, err } func (d *FsProductModel3dModel) GetAll(ctx context.Context) (resp []FsProductModel3d, err error) { diff --git a/model/gmodel/fs_product_size_logic.go b/model/gmodel/fs_product_size_logic.go index 38d108a7..351fa22e 100755 --- a/model/gmodel/fs_product_size_logic.go +++ b/model/gmodel/fs_product_size_logic.go @@ -38,16 +38,19 @@ func (s *FsProductSizeModel) GetGroupProductSizeByStatus(ctx context.Context, pr Find(&resp).Error return resp, err } -func (s *FsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds []int64, sort string) (resp []FsProductSize, err error) { +func (s *FsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds []int64, orderBy string, fields ...string) (resp []FsProductSize, err error) { if len(productIds) == 0 { return nil, nil } db := s.db.WithContext(ctx).Model(&FsProductSize{}).Where("`product_id` in(?) and `status` = ?", productIds, 1) - switch sort { - case "sort-asc": - db = db.Order("`sort` ASC") - case "sort-desc": - db = db.Order("`sort` DESC") + if len(fields) != 0 { + db = db.Select(fields[0]) + } + switch orderBy { + case "": + db = db.Order("`id` ASC") + default: + db = db.Order(orderBy) } err = db.Find(&resp).Error if err != nil { diff --git a/server/product/internal/handler/getmodelbypidhandler.go b/server/product/internal/handler/getmodelbypidhandler.go new file mode 100644 index 00000000..f944508e --- /dev/null +++ b/server/product/internal/handler/getmodelbypidhandler.go @@ -0,0 +1,78 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "fusenapi/server/product/internal/logic" + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" +) + +func GetModelByPidHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + if claims != nil { + // 从token中获取对应的用户信息 + userinfo, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + } else { + // 如果claims为nil,则认为用户身份为白板用户 + userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} + } + + var req types.GetModelByPidReq + // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 + if err := httpx.Parse(r, &req); err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 510, + Message: "parameter error", + }) + logx.Info(err) + return + } + // 创建一个业务逻辑层实例 + l := logic.NewGetModelByPidLogic(r.Context(), svcCtx) + resp := l.GetModelByPid(&req, userinfo) + // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } + } +} diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index a65b0205..43a10ab4 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -67,6 +67,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/render_design", Handler: GetRenderDesignHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/product/get_model_by_pid", + Handler: GetModelByPidHandler(serverCtx), + }, }, ) } diff --git a/server/product/internal/logic/getmodelbypidlogic.go b/server/product/internal/logic/getmodelbypidlogic.go new file mode 100644 index 00000000..51567f54 --- /dev/null +++ b/server/product/internal/logic/getmodelbypidlogic.go @@ -0,0 +1,85 @@ +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) +} diff --git a/server/product/internal/logic/getsizebyproductlogic.go b/server/product/internal/logic/getsizebyproductlogic.go index 644c7f94..3d111822 100644 --- a/server/product/internal/logic/getsizebyproductlogic.go +++ b/server/product/internal/logic/getsizebyproductlogic.go @@ -63,7 +63,7 @@ func (l *GetSizeByProductLogic) GetSizeByProduct(userinfo *auth.UserInfo) (resp productIds = append(productIds, v.Id) } productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn) - productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort-desc") + productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort DESC") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list") diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index c8abf939..cd953a79 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -293,6 +293,10 @@ type GetRenderDesignRsp struct { LogoColor []string `json:"logo_color"` } +type GetModelByPidReq struct { + Pid string `form:"pid"` //实际上是产品sn +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index 9fbf6e05..80a5c41e 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -44,6 +44,9 @@ service product { //获取云渲染设计方案信息 @handler GetRenderDesignHandler get /api/product/render_design(GetRenderDesignReq) returns (response); + //获取产品模型信息 + @handler GetModelByPidHandler + get /api/product/get_model_by_pid(GetModelByPidReq) returns (response); //*********************产品详情分解接口结束*********************** } @@ -309,4 +312,9 @@ type GetRenderDesignRsp { SizeId int64 `json:"size_id"` TemplateId int64 `json:"template_id"` LogoColor []string `json:"logo_color"` +} + +//获取产品模型信息 +type GetModelByPidReq { + Pid string `form:"pid"` //实际上是产品sn } \ No newline at end of file