product_create.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package handler
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/sirupsen/logrus"
  6. log "github.com/sirupsen/logrus"
  7. "youngee_m_api/consts"
  8. "youngee_m_api/db"
  9. "youngee_m_api/model/http_model"
  10. "youngee_m_api/service"
  11. "youngee_m_api/util"
  12. )
  13. func WrapCreateProductHandler(ctx *gin.Context) {
  14. handler := newCreateProductHandler(ctx)
  15. BaseRun(handler)
  16. }
  17. func newCreateProductHandler(ctx *gin.Context) *CreateProductHandler {
  18. return &CreateProductHandler{
  19. req: http_model.NewCreateProductRequest(),
  20. resp: http_model.NewCreateProductResponse(),
  21. ctx: ctx,
  22. }
  23. }
  24. type CreateProductHandler struct {
  25. req *http_model.CreateProductRequest
  26. resp *http_model.CommonResponse
  27. ctx *gin.Context
  28. }
  29. func (h *CreateProductHandler) getRequest() interface{} {
  30. return h.req
  31. }
  32. func (h *CreateProductHandler) getContext() *gin.Context {
  33. return h.ctx
  34. }
  35. func (h *CreateProductHandler) getResponse() interface{} {
  36. return h.resp
  37. }
  38. func (h *CreateProductHandler) run() {
  39. data := http_model.CreateProductRequest{}
  40. data = *h.req
  41. fmt.Println("ProductId:", h.req.ProductId)
  42. enterpriseID := db.GetEnterpriseIDByProductID(h.ctx, h.req.ProductId)
  43. fmt.Println("enterpriseID:", enterpriseID)
  44. // 根据品牌名和商品名查询商品是否存在,若存在则更新,否则新增
  45. product, err := service.Product.FindByID(h.ctx, data.ProductId)
  46. if err != nil {
  47. logrus.Errorf("[CreateProductHandler] call FindByID err:%+v\n", err)
  48. util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
  49. log.Info("CreateProduct fail,req:%+v", h.req)
  50. return
  51. } else {
  52. if product != nil {
  53. // 该商品存在,更新
  54. fmt.Println("该商品存在,更新")
  55. fmt.Println("商品价格:", data.ProductPrice)
  56. fmt.Println("商品公开佣金:", data.PublicCommission)
  57. data.ProductId = product.ProductID
  58. res, err := service.Product.Update(h.ctx, data, enterpriseID)
  59. if err != nil {
  60. logrus.Errorf("[CreateProductHandler] call Update err:%+v\n", err)
  61. util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
  62. log.Info("CreateProduct fail,req:%+v", h.req)
  63. return
  64. }
  65. h.resp.Message = "成功更新商品信息"
  66. h.resp.Data = res
  67. } else {
  68. // 商品不存在,新增
  69. fmt.Println("商品不存在,新增")
  70. fmt.Println("data.EnterpriseId:", data.EnterpriseId)
  71. res, err := service.Product.Create(h.ctx, data, data.EnterpriseId)
  72. if err != nil {
  73. logrus.Errorf("[CreateProductHandler] call Create err:%+v\n", err)
  74. util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
  75. log.Info("CreateProduct fail,req:%+v", h.req)
  76. return
  77. }
  78. h.resp.Message = "成功新增商品信息"
  79. h.resp.Data = res
  80. }
  81. }
  82. }
  83. func (h *CreateProductHandler) checkParam() error {
  84. return nil
  85. }