selection_find_all.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package handler
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/sirupsen/logrus"
  7. "youngee_b_api/consts"
  8. "youngee_b_api/middleware"
  9. "youngee_b_api/model/http_model"
  10. "youngee_b_api/pack"
  11. "youngee_b_api/service/selection_service"
  12. "youngee_b_api/util"
  13. )
  14. func WrapFindAllSelectionHandler(ctx *gin.Context) {
  15. handler := newFindAllSelection(ctx)
  16. baseRun(handler)
  17. }
  18. type FindAllSelectionHandler struct {
  19. ctx *gin.Context
  20. req *http_model.FindAllSelectionRequest
  21. resp *http_model.CommonResponse
  22. }
  23. func (f FindAllSelectionHandler) getContext() *gin.Context {
  24. return f.ctx
  25. }
  26. func (f FindAllSelectionHandler) getResponse() interface{} {
  27. return f.resp
  28. }
  29. func (f FindAllSelectionHandler) getRequest() interface{} {
  30. return f.req
  31. }
  32. func (f FindAllSelectionHandler) run() {
  33. enterpriseID := middleware.GetSessionAuth(f.ctx).EnterpriseID
  34. fmt.Printf("2企业ID2 %+v", enterpriseID)
  35. condition := pack.HttpFindAllSelectionRequestToCondition(f.req)
  36. data, err := selection_service.Selection.GetAllSelection(f.ctx, enterpriseID, f.req.PageSize, f.req.PageNum, condition)
  37. if err != nil {
  38. logrus.WithContext(f.ctx).Errorf("[FindAllSelectionHandler] error GetAllSelection, err:%+v", err)
  39. util.HandlerPackErrorResp(f.resp, consts.ErrorInternal, consts.DefaultToast)
  40. return
  41. }
  42. f.resp.Data = data
  43. }
  44. func (f FindAllSelectionHandler) checkParam() error {
  45. var errs []error
  46. if f.req.PageNum < 0 || f.req.PageSize <= 0 {
  47. errs = append(errs, errors.New("page param error"))
  48. }
  49. f.req.PageNum--
  50. if len(errs) != 0 {
  51. return fmt.Errorf("check param errs:%+v", errs)
  52. }
  53. return nil
  54. }
  55. func newFindAllSelection(ctx *gin.Context) *FindAllSelectionHandler {
  56. return &FindAllSelectionHandler{
  57. ctx: ctx,
  58. req: http_model.NewFindAllSelectionRequest(),
  59. resp: http_model.NewFindAllSelectionResponse(),
  60. }
  61. }