product_create.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. data.ProductId = product.ProductID
  57. res, err := service.Product.Update(h.ctx, data, enterpriseID)
  58. if err != nil {
  59. logrus.Errorf("[CreateProductHandler] call Update err:%+v\n", err)
  60. util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
  61. log.Info("CreateProduct fail,req:%+v", h.req)
  62. return
  63. }
  64. h.resp.Message = "成功更新商品信息"
  65. h.resp.Data = res
  66. } else {
  67. // 商品不存在,新增
  68. fmt.Println("商品不存在,新增")
  69. fmt.Println("data.EnterpriseId:", data.EnterpriseId)
  70. res, err := service.Product.Create(h.ctx, data, data.EnterpriseId)
  71. if err != nil {
  72. logrus.Errorf("[CreateProductHandler] call Create err:%+v\n", err)
  73. util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
  74. log.Info("CreateProduct fail,req:%+v", h.req)
  75. return
  76. }
  77. h.resp.Message = "成功新增商品信息"
  78. h.resp.Data = res
  79. }
  80. }
  81. }
  82. func (h *CreateProductHandler) checkParam() error {
  83. return nil
  84. }