request_base.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package model
  2. import (
  3. "math"
  4. "strconv"
  5. )
  6. // const .
  7. const (
  8. // dateFmt = "20060102"
  9. dateTimeFmt = "20060102_150405"
  10. ResponeModelJSON = "json"
  11. ResponeModelCSV = "csv"
  12. )
  13. func round(num float64) int {
  14. return int(num + math.Copysign(0.5, num))
  15. }
  16. //ToFixed fix float precision
  17. func ToFixed(num float64, precision int) float64 {
  18. output := math.Pow(10, float64(precision))
  19. return float64(round(num*output)) / output // since go 1.9 doesn't have a math.Round function...
  20. }
  21. // floatFormat format float to string
  22. func floatFormat(f float64) string {
  23. return strconv.FormatFloat(f, 'f', 2, 64)
  24. }
  25. // intFormat format int to string
  26. func intFormat(i int64) string {
  27. return strconv.Itoa(int(i))
  28. }
  29. //PageArg page arg
  30. type PageArg struct {
  31. Page int `form:"page" default:"1"`
  32. Size int `form:"size" default:"20"`
  33. }
  34. //PageResult page result
  35. type PageResult struct {
  36. Page int `json:"page"`
  37. TotalCount int `json:"total_count"`
  38. }
  39. //CheckPageValidation check the page validte, return limit offset
  40. func (arg *PageArg) CheckPageValidation() (limit, offset int) {
  41. if arg.Page < 1 {
  42. arg.Page = 1
  43. }
  44. if arg.Size > 100 || arg.Size <= 0 {
  45. arg.Size = 10
  46. }
  47. limit = arg.Size
  48. offset = (arg.Page - 1) * limit
  49. return
  50. }
  51. //ToPageResult cast to page result
  52. func (arg *PageArg) ToPageResult(total int) (res PageResult) {
  53. res.TotalCount = total
  54. res.Page = arg.Page
  55. return
  56. }
  57. //ExportArg export arg
  58. type ExportArg struct {
  59. Export string `form:"export"`
  60. }
  61. //ExportFormat export format
  62. func (e *ExportArg) ExportFormat() string {
  63. return e.Export
  64. }