fusenapi/server/product-model/internal/logic/getmodeldetaillogic.go

69 lines
2.1 KiB
Go
Raw Normal View History

2023-06-25 09:11:42 +00:00
package logic
import (
"encoding/json"
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/basic"
"gorm.io/gorm"
"net/http"
"context"
"fusenapi/server/product-model/internal/svc"
"fusenapi/server/product-model/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetModelDetailLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetModelDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetModelDetailLogic {
return &GetModelDetailLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetModelDetailLogic) GetModelDetail(req *types.GetModelDetailReq, r *http.Request) (resp *basic.Response) {
authKey := r.Header.Get("Auth-Key")
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
_, err := genentModel.Find(l.ctx, authKey)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
}
if req.ModelId <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param model_id is required")
}
//获取产品模型信息
productModel3dInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, req.ModelId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product model info is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product model info")
}
//解析模型json数据
var modelInfoRsp interface{}
if productModel3dInfo.ModelInfo != nil && *productModel3dInfo.ModelInfo != "" {
if err = json.Unmarshal([]byte(*productModel3dInfo.ModelInfo), &modelInfoRsp); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse model info")
}
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetModelDetailRsp{
Tag: *productModel3dInfo.Tag,
ProductModelInfo: modelInfoRsp,
})
}