invoiceRecords.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package handler
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "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/pack"
  11. "youngee_m_api/util"
  12. )
  13. func WrapInvoiceRecordsHandler(ctx *gin.Context) {
  14. handler := newInvoiceRecordsHandler(ctx)
  15. BaseRun(handler)
  16. }
  17. type InvoiceRecordsHandler struct {
  18. ctx *gin.Context
  19. req *http_model.InvoiceRecordsRequest
  20. resp *http_model.CommonResponse
  21. }
  22. func (i InvoiceRecordsHandler) getContext() *gin.Context {
  23. return i.ctx
  24. }
  25. func (i InvoiceRecordsHandler) getResponse() interface{} {
  26. return i.resp
  27. }
  28. func (i InvoiceRecordsHandler) getRequest() interface{} {
  29. return i.req
  30. }
  31. func (i InvoiceRecordsHandler) run() {
  32. condition := pack.HttpInvoiceRecordsRequestToCondition(i.req)
  33. data, err := db.GetInvoiceRecords(i.ctx, i.req, condition)
  34. if err != nil {
  35. // 数据库查询失败,返回5001
  36. logrus.Errorf("[InvoiceRecordsHandler] call GetInvoiceRecords err:%+v\n", err)
  37. util.HandlerPackErrorResp(i.resp, consts.ErrorInternal, "")
  38. logrus.Info("GetInvoiceRecords fail,req:%+v", i.req)
  39. return
  40. }
  41. i.resp.Data = data
  42. }
  43. func (i InvoiceRecordsHandler) checkParam() error {
  44. var errs []error
  45. if i.req.PageNum < 0 || i.req.PageSize <= 0 {
  46. errs = append(errs, errors.New("page param error"))
  47. }
  48. i.req.PageNum--
  49. if len(errs) != 0 {
  50. return fmt.Errorf("check param errs:%+v", errs)
  51. }
  52. return nil
  53. }
  54. func newInvoiceRecordsHandler(ctx *gin.Context) *InvoiceRecordsHandler {
  55. return &InvoiceRecordsHandler{
  56. ctx: ctx,
  57. req: http_model.NewInvoiceRecordsRequest(),
  58. resp: http_model.NewInvoiceRecordsResponse(),
  59. }
  60. }