request_base.go 1.4 KB

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