product.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package base
  2. import (
  3. "context"
  4. "dashoo.cn/common_definition/comm_def"
  5. "dashoo.cn/opms_libary/myerrors"
  6. "github.com/gogf/gf/errors/gerror"
  7. "github.com/gogf/gf/frame/g"
  8. "github.com/gogf/gf/util/gvalid"
  9. model "dashoo.cn/micro/app/model/base"
  10. server "dashoo.cn/micro/app/service/base"
  11. )
  12. type ProductHandler struct{}
  13. // GetList 获取列表
  14. func (h *ProductHandler) GetList(ctx context.Context, req *model.ProductSearchReq, rsp *comm_def.CommonMsg) error {
  15. s, err := server.NewProductService(ctx)
  16. if err != nil {
  17. g.Log().Error(err)
  18. return err
  19. }
  20. total, list, err := s.GetList(req)
  21. _, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
  22. if err != nil {
  23. g.Log().Error(err)
  24. return err
  25. }
  26. rsp.Data = g.Map{"list": list, "total": total}
  27. return nil
  28. }
  29. // Create 添加产品信息
  30. func (h *ProductHandler) Create(ctx context.Context, req *model.AddProductReq, rsp *comm_def.CommonMsg) error {
  31. // 参数校验
  32. if err := gvalid.CheckStruct(ctx, req, nil); err != nil {
  33. return err
  34. }
  35. s, err := server.NewProductService(ctx)
  36. if err != nil {
  37. g.Log().Error(err)
  38. return err
  39. }
  40. _, err = s.Create(req)
  41. _, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
  42. if err != nil {
  43. g.Log().Error(err)
  44. return err
  45. }
  46. return nil
  47. }
  48. //DeleteByIds 批量删除数据
  49. func (h *ProductHandler) DeleteByIds(ctx context.Context, req *model.DeIds, rsp *comm_def.CommonMsg) error {
  50. // 参数校验
  51. if len(req.Ids) == 0 {
  52. return gerror.New("参数为空,操作失败")
  53. }
  54. s, err := server.NewProductService(ctx)
  55. if err != nil {
  56. g.Log().Error(err)
  57. return err
  58. }
  59. err = s.DeleteByIds(req.Ids)
  60. _, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
  61. if err != nil {
  62. g.Log().Error(err)
  63. return err
  64. }
  65. return nil
  66. }
  67. // GetEntityById 根据ID查询详细信息
  68. func (h *ProductHandler) GetEntityById(ctx context.Context, req *comm_def.IdReq, rsp *comm_def.CommonMsg) error {
  69. // 参数校验
  70. if req.Id == 0 {
  71. return gerror.New("参数有误!")
  72. }
  73. s, err := server.NewProductService(ctx)
  74. if err != nil {
  75. g.Log().Error(err)
  76. return err
  77. }
  78. product, err := s.GetEntityById(req.Id)
  79. _, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
  80. if err != nil {
  81. g.Log().Error(err)
  82. return err
  83. }
  84. rsp.Data = product
  85. return nil
  86. }
  87. //UpdateById 更新信息
  88. func (h *ProductHandler) UpdateById(ctx context.Context, req *model.UpdateProductReq, rsp *comm_def.CommonMsg) error {
  89. s, err := server.NewProductService(ctx)
  90. if err != nil {
  91. g.Log().Error(err)
  92. return err
  93. }
  94. err = s.UpdateById(req)
  95. _, err, rsp.Code, rsp.Msg = myerrors.CheckError(err)
  96. if err != nil {
  97. g.Log().Error(err)
  98. return err
  99. }
  100. return nil
  101. }