diff --git a/server/product/internal/handler/getthousandfacedesignbypidhandler.go b/server/product/internal/handler/getlastproductdesignhandler.go similarity index 88% rename from server/product/internal/handler/getthousandfacedesignbypidhandler.go rename to server/product/internal/handler/getlastproductdesignhandler.go index 4635c7a9..22e73dd2 100644 --- a/server/product/internal/handler/getthousandfacedesignbypidhandler.go +++ b/server/product/internal/handler/getlastproductdesignhandler.go @@ -15,7 +15,7 @@ import ( "fusenapi/server/product/internal/types" ) -func GetThousandFaceDesignByPidHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { +func GetLastProductDesignHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var ( @@ -53,7 +53,7 @@ func GetThousandFaceDesignByPidHandler(svcCtx *svc.ServiceContext) http.HandlerF userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} } - var req types.GetThousandFaceDesignByPidReq + var req types.Request // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 if err := httpx.Parse(r, &req); err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ @@ -64,8 +64,8 @@ func GetThousandFaceDesignByPidHandler(svcCtx *svc.ServiceContext) http.HandlerF return } // 创建一个业务逻辑层实例 - l := logic.NewGetThousandFaceDesignByPidLogic(r.Context(), svcCtx) - resp := l.GetThousandFaceDesignByPid(&req, userinfo) + l := logic.NewGetLastProductDesignLogic(r.Context(), svcCtx) + resp := l.GetLastProductDesign(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index 9c510df5..669d7cef 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -104,8 +104,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodGet, - Path: "/api/product/get_thousand_face_design_by_pid", - Handler: GetThousandFaceDesignByPidHandler(serverCtx), + Path: "/api/product/get_last_product_design", + Handler: GetLastProductDesignHandler(serverCtx), }, }, ) diff --git a/server/product/internal/logic/getlastproductdesignlogic.go b/server/product/internal/logic/getlastproductdesignlogic.go new file mode 100644 index 00000000..b99f2a90 --- /dev/null +++ b/server/product/internal/logic/getlastproductdesignlogic.go @@ -0,0 +1,110 @@ +package logic + +import ( + "context" + "encoding/json" + "errors" + "fusenapi/constants" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "gorm.io/gorm" + + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetLastProductDesignLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetLastProductDesignLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLastProductDesignLogic { + return &GetLastProductDesignLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetLastProductDesignLogic) GetLastProductDesign(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { + if !userinfo.IsUser() { + return resp.SetStatusAddMessage(basic.CodeUnAuth, "please login") + } + //获取用户信息 + user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "user not found") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user info") + } + //若没打开了个性化渲染按钮 + if *user.IsOpenRender != 1 { + return resp.SetStatusWithMessage(basic.CodeOK, "success:IsOpenRender switch is closed") + } + //查询用户最近下单成功的数据 + orderInfo, err := l.svcCtx.AllModels.FsOrder.FindLastSuccessOneOrder(l.ctx, user.Id, int64(constants.STATUS_NEW_NOT_PAY)) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "none of order is found") + } + logx.Error(err) + return resp.SetStatusAddMessage(basic.CodeDbSqlErr, "failed to get your last order") + } + //获取该订单相关设计信息 + orderDetail, err := l.svcCtx.AllModels.FsOrderDetail.GetOneOrderDetailByOrderId(l.ctx, orderInfo.Id) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order detail is not found") + } + logx.Error(err) + return resp.SetStatusAddMessage(basic.CodeDbSqlErr, "failed to get order detail") + } + //获取设计模板详情,便于获得design_id + orderDetailTemplate, err := l.svcCtx.AllModels.FsOrderDetailTemplate.FindOne(l.ctx, *orderDetail.OrderDetailTemplateId) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order detail template is not found") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get order detail template") + } + //最后一次设计不存在,则不返回该设计相关数据 + if *orderDetailTemplate.DesignId <= 0 { + return resp.SetStatusWithMessage(basic.CodeOK, "success:last design id is not set") + } + //获取设计数据 + productDesign, err := l.svcCtx.AllModels.FsProductDesign.FindOne(l.ctx, *orderDetailTemplate.DesignId, user.Id) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product design is not found") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product design") + } + var info interface{} + if productDesign.Info != nil && *productDesign.Info != "" { + if err := json.Unmarshal([]byte(*productDesign.Info), &info); err != nil { + logx.Error(err) + return nil + } + } + var logoColor interface{} + if productDesign.LogoColor != nil && *productDesign.LogoColor != "" { + if err := json.Unmarshal([]byte(*productDesign.LogoColor), &logoColor); err != nil { + logx.Error(err) + return nil + } + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetLastProductDesignRsp{ + Id: productDesign.Id, + OptionalId: *productDesign.OptionalId, + SizeId: *productDesign.SizeId, + LogoColor: logoColor, + Info: info, + }) +} diff --git a/server/product/internal/logic/getthousandfacedesignbypidlogic.go b/server/product/internal/logic/getthousandfacedesignbypidlogic.go deleted file mode 100644 index 1670de08..00000000 --- a/server/product/internal/logic/getthousandfacedesignbypidlogic.go +++ /dev/null @@ -1,47 +0,0 @@ -package logic - -import ( - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "strings" - - "context" - - "fusenapi/server/product/internal/svc" - "fusenapi/server/product/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type GetThousandFaceDesignByPidLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewGetThousandFaceDesignByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetThousandFaceDesignByPidLogic { - return &GetThousandFaceDesignByPidLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -func (l *GetThousandFaceDesignByPidLogic) GetThousandFaceDesignByPid(req *types.GetThousandFaceDesignByPidReq, 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") - }*/ - //获取产品设计 - - return resp.SetStatus(basic.CodeOK) -} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 33e927ef..926bc0e5 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -366,15 +366,11 @@ type GetRenderSettingByPidRsp struct { Colors interface{} `json:"colors"` } -type GetThousandFaceDesignByPidReq struct { - Pid string `form:"pid"` -} - -type GetThousandFaceDesignByPidRsp struct { +type GetLastProductDesignRsp struct { Id int64 `json:"id"` OptionalId int64 `json:"optional_id"` SizeId int64 `json:"size_id"` - LogoColor []string `json:"logo_color"` + LogoColor interface{} `json:"logo_color"` Info interface{} `json:"info"` } diff --git a/server_api/product.api b/server_api/product.api index d036cbc4..96b2b5d7 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -66,8 +66,8 @@ service product { @handler GetRenderSettingByPidHandler get /api/product/get_render_setting_by_pid(GetRenderSettingByPidReq) returns (response); //获取产品千人千面设计方案 - @handler GetThousandFaceDesignByPidHandler - get /api/product/get_thousand_face_design_by_pid(GetThousandFaceDesignByPidReq) returns (response); + @handler GetLastProductDesignHandler + get /api/product/get_last_product_design(request) returns (response); //*********************产品详情分解接口结束*********************** } @@ -404,13 +404,10 @@ type GetRenderSettingByPidRsp { Colors interface{} `json:"colors"` } //获取产品千人千面设计方案 -type GetThousandFaceDesignByPidReq { - Pid string `form:"pid"` -} -type GetThousandFaceDesignByPidRsp { +type GetLastProductDesignRsp { Id int64 `json:"id"` OptionalId int64 `json:"optional_id"` SizeId int64 `json:"size_id"` - LogoColor []string `json:"logo_color"` + LogoColor interface{} `json:"logo_color"` Info interface{} `json:"info"` } \ No newline at end of file