From e1bc5fbe51d488e439d393240082254530373fa7 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 17 Jul 2023 17:08:45 +0800 Subject: [PATCH] fix --- model/gmodel/fs_order_logic.go | 2 +- .../handler/getrendersettingbypidhandler.go | 78 +++++++++ server/product/internal/handler/routes.go | 5 + .../logic/getrendersettingbypidlogic.go | 152 ++++++++++++++++++ server/product/internal/types/types.go | 19 +++ server_api/product.api | 21 +++ 6 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 server/product/internal/handler/getrendersettingbypidhandler.go create mode 100644 server/product/internal/logic/getrendersettingbypidlogic.go diff --git a/model/gmodel/fs_order_logic.go b/model/gmodel/fs_order_logic.go index 5ae29381..ef332996 100755 --- a/model/gmodel/fs_order_logic.go +++ b/model/gmodel/fs_order_logic.go @@ -48,6 +48,6 @@ func (o *FsOrderModel) FindOneAndCreateServiceContact(ctx context.Context, userI // 获取用户最近下单成功的订单 func (o *FsOrderModel) FindLastSuccessOneOrder(ctx context.Context, userId int64, statusGt int64) (order *FsOrder, err error) { - err = o.db.WithContext(ctx).Model(&FsOrder{}).Where("`user_id` = ? and `status` > ?", userId, statusGt).Order("id DESC").Find(&order).Error + err = o.db.WithContext(ctx).Model(&FsOrder{}).Where("`user_id` = ? and `status` > ?", userId, statusGt).Order("id DESC").Take(&order).Error return order, err } diff --git a/server/product/internal/handler/getrendersettingbypidhandler.go b/server/product/internal/handler/getrendersettingbypidhandler.go new file mode 100644 index 00000000..dcf7faee --- /dev/null +++ b/server/product/internal/handler/getrendersettingbypidhandler.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 GetRenderSettingByPidHandler(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.GetRenderSettingByPidReq + // 如果端点有请求结构体,则使用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.NewGetRenderSettingByPidLogic(r.Context(), svcCtx) + resp := l.GetRenderSettingByPid(&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 279bd2ac..6b060a10 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -97,6 +97,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/get_light_by_pid", Handler: GetLightByPidHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/product/get_render_setting_by_pid", + Handler: GetRenderSettingByPidHandler(serverCtx), + }, }, ) } diff --git a/server/product/internal/logic/getrendersettingbypidlogic.go b/server/product/internal/logic/getrendersettingbypidlogic.go new file mode 100644 index 00000000..b4cbdfda --- /dev/null +++ b/server/product/internal/logic/getrendersettingbypidlogic.go @@ -0,0 +1,152 @@ +package logic + +import ( + "errors" + "fusenapi/constants" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/color_list" + "gorm.io/gorm" + "strings" + + "context" + + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetRenderSettingByPidLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetRenderSettingByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRenderSettingByPidLogic { + return &GetRenderSettingByPidLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetRenderSettingByPidLogic) GetRenderSettingByPid(req *types.GetRenderSettingByPidReq, 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") + } + //获取产品信息 + productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid) + 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") + } + //获取产品分类 + tagInfo, err := l.svcCtx.AllModels.FsTags.FindOne(l.ctx, *productInfo.Type) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product tag is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get tag info") + } + //是否低效果渲染 + isLowRendering := false + //是否移除背景 + isRemoveBg := false + //是否存在最新设计 + lastDesign := false + //是否拥有云渲染设计方案 + renderDesign := false + renderDesign, err = l.checkRenderDesign(req.ClientNo) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to checkRenderDesign") + } + if userinfo.UserId != 0 { + user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusAddMessage(basic.CodeDbRecordNotFoundErr, "user info is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user info") + } + isLowRendering = *user.IsLowRendering > 0 + isRemoveBg = *user.IsRemoveBg > 0 + lastDesign, err = l.checkLastDesignExists(user.Id) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to check if exists last design ") + } + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetRenderSettingByPidRsp{ + Id: productInfo.Id, + Type: *productInfo.Type, + Title: *productInfo.Title, + IsEnv: *productInfo.IsProtection, + IsMicro: *productInfo.IsMicrowave, + TypeName: *tagInfo.Title, + IsLowRendering: isLowRendering, + IsRemoveBg: isRemoveBg, + RenderDesign: renderDesign, + LastDesign: lastDesign, + Colors: color_list.GetColor(), + }) +} + +// 查询是否存在渲染设计 +func (l *GetRenderSettingByPidLogic) checkRenderDesign(clientNo string) (bool, error) { + if clientNo == "" { + return false, nil + } + renderDesign, err := l.svcCtx.AllModels.FsProductRenderDesign.FindOneRenderDesignByParams(l.ctx, gmodel.FindOneRenderDesignByParamsReq{ + ClientNo: &clientNo, + Fields: "id,info,material_id,optional_id,size_id,template_id", + OrderBy: "`id` DESC", + }) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return false, nil + } + return false, err + } + if renderDesign.Info != nil && *renderDesign.Info != "" { + return true, nil + } + return false, nil +} + +// 查询是否存在最新设计 +func (l *GetRenderSettingByPidLogic) checkLastDesignExists(userId int64) (bool, error) { + //查询用户最近下单成功的数据 + orderInfo, err := l.svcCtx.AllModels.FsOrder.FindLastSuccessOneOrder(l.ctx, userId, int64(constants.STATUS_NEW_NOT_PAY)) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return false, nil + } + return false, err + } + //获取该订单相关设计信息 + orderDetail, err := l.svcCtx.AllModels.FsOrderDetail.GetOneOrderDetailByOrderId(l.ctx, orderInfo.Id) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return false, nil + } + return false, err + } + //获取设计模板详情,便于获得design_id + orderDetailTemplate, err := l.svcCtx.AllModels.FsOrderDetailTemplate.FindOne(l.ctx, *orderDetail.OrderDetailTemplateId) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return false, nil + } + return false, err + } + return *orderDetailTemplate.DesignId > 0, nil +} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index e4c4f524..0188b4cb 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -347,6 +347,25 @@ type GetLightByPidReq struct { Pid string `form:"pid"` } +type GetRenderSettingByPidReq struct { + Pid string `form:"pid"` + ClientNo string `form:"client_no,optional"` +} + +type GetRenderSettingByPidRsp struct { + Id int64 `json:"id"` //产品id + Type int64 `json:"type"` //产品typeid + Title string `json:"title"` //产品名称 + IsEnv int64 `json:"isEnv"` //产品标签之一 + IsMicro int64 `json:"isMicro"` //产品标签之一 + TypeName string `json:"typeName"` //产品类型名称 + IsLowRendering bool `json:"is_low_rendering"` //低质量画质渲染开关 + IsRemoveBg bool `json:"is_remove_bg"` //logo上传是否去背景 + RenderDesign bool `json:"render_design"` //是否拥有云渲染设计方案 + LastDesign bool `json:"last_lesign"` //是否拥有千人千面设计方案 + Colors interface{} `json:"colors"` +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index a822a9aa..a6faef19 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -62,6 +62,9 @@ service product { //获取产品灯光数据 @handler GetLightByPidHandler get /api/product/get_light_by_pid(GetLightByPidReq) returns (response); + //获取产品渲染设置 + @handler GetRenderSettingByPidHandler + get /api/product/get_render_setting_by_pid(GetRenderSettingByPidReq) returns (response); //*********************产品详情分解接口结束*********************** } @@ -378,4 +381,22 @@ type GetFittingByPidRsp { //获取产品灯光数据 type GetLightByPidReq { Pid string `form:"pid"` +} +//获取产品渲染设置 +type GetRenderSettingByPidReq { + Pid string `form:"pid"` + ClientNo string `form:"client_no,optional"` +} +type GetRenderSettingByPidRsp { + Id int64 `json:"id"` //产品id + Type int64 `json:"type"` //产品typeid + Title string `json:"title"` //产品名称 + IsEnv int64 `json:"isEnv"` //产品标签之一 + IsMicro int64 `json:"isMicro"` //产品标签之一 + TypeName string `json:"typeName"` //产品类型名称 + IsLowRendering bool `json:"is_low_rendering"` //低质量画质渲染开关 + IsRemoveBg bool `json:"is_remove_bg"` //logo上传是否去背景 + RenderDesign bool `json:"render_design"` //是否拥有云渲染设计方案 + LastDesign bool `json:"last_lesign"` //是否拥有千人千面设计方案 + Colors interface{} `json:"colors"` } \ No newline at end of file