product_create.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package handler
  2. import (
  3. "youngee_b_api/consts"
  4. "youngee_b_api/middleware"
  5. "youngee_b_api/model/http_model"
  6. "youngee_b_api/service"
  7. "youngee_b_api/util"
  8. "github.com/gin-gonic/gin"
  9. "github.com/sirupsen/logrus"
  10. log "github.com/sirupsen/logrus"
  11. )
  12. // WrapCreateProductHandler
  13. // @BasePath /youngee/m/
  14. // SendCode godoc
  15. // @Summary CreateProduct 创建商品
  16. // @Schemes
  17. // @Description 企业创建商品,添加到商品库
  18. // @Accept json
  19. // @Produce json
  20. // @Param Authorization header string true "登录TOKEN信息"
  21. // @Param req body http_model.CreateProductRequest true "创建商品请求结构体"
  22. // @Success 200 {object} http_model.CommonResponse{data=http_model.CreateProductData} "创建商品相应结构体"
  23. // @Router /product/create [post]
  24. func WrapCreateProductHandler(ctx *gin.Context) {
  25. handler := newCreateProductHandler(ctx)
  26. baseRun(handler)
  27. }
  28. func newCreateProductHandler(ctx *gin.Context) *CreateProductHandler {
  29. return &CreateProductHandler{
  30. req: http_model.NewCreateProductRequest(),
  31. resp: http_model.NewCreateProductResponse(),
  32. ctx: ctx,
  33. }
  34. }
  35. type CreateProductHandler struct {
  36. req *http_model.CreateProductRequest
  37. resp *http_model.CommonResponse
  38. ctx *gin.Context
  39. }
  40. func (h *CreateProductHandler) getRequest() interface{} {
  41. return h.req
  42. }
  43. func (h *CreateProductHandler) getContext() *gin.Context {
  44. return h.ctx
  45. }
  46. func (h *CreateProductHandler) getResponse() interface{} {
  47. return h.resp
  48. }
  49. func (h *CreateProductHandler) run() {
  50. data := http_model.CreateProductRequest{}
  51. data = *h.req
  52. auth := middleware.GetSessionAuth(h.ctx)
  53. enterpriseID := auth.EnterpriseID
  54. //根据品牌名和商品名查询商品是否存在,若存在则更新,否则新增
  55. product, err := service.Product.FindByID(h.ctx, data.ProductId)
  56. if err != nil {
  57. logrus.Errorf("[CreateProductHandler] call FindByID err:%+v\n", err)
  58. util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
  59. log.Info("CreateProduct fail,req:%+v", h.req)
  60. return
  61. } else {
  62. if product != nil {
  63. // 该商品存在,更新
  64. data.ProductId = product.ProductID
  65. res, err := service.Product.Update(h.ctx, data, enterpriseID)
  66. if err != nil {
  67. logrus.Errorf("[CreateProductHandler] call Update err:%+v\n", err)
  68. util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
  69. log.Info("CreateProduct fail,req:%+v", h.req)
  70. return
  71. }
  72. h.resp.Message = "成功更新商品信息"
  73. h.resp.Data = res
  74. } else {
  75. // 商品不存在,新增
  76. res, err := service.Product.Create(h.ctx, data, enterpriseID)
  77. if err != nil {
  78. logrus.Errorf("[CreateProductHandler] call Create err:%+v\n", err)
  79. util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
  80. log.Info("CreateProduct fail,req:%+v", h.req)
  81. return
  82. }
  83. h.resp.Message = "成功新增商品信息"
  84. h.resp.Data = res
  85. }
  86. }
  87. }
  88. func (h *CreateProductHandler) checkParam() error {
  89. return nil
  90. }