history.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package http
  2. import (
  3. "net/http"
  4. "strconv"
  5. "time"
  6. "go-common/library/ecode"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. func dmHistory(c *bm.Context) {
  10. var (
  11. p = c.Request.Form
  12. contextType = "text/xml"
  13. )
  14. tp, err := strconv.ParseInt(p.Get("type"), 10, 64)
  15. if err != nil {
  16. c.AbortWithStatus(http.StatusBadRequest)
  17. return
  18. }
  19. oid, err := strconv.ParseInt(p.Get("oid"), 10, 64)
  20. if err != nil {
  21. c.AbortWithStatus(http.StatusBadRequest)
  22. return
  23. }
  24. date, err := time.Parse("2006-01-02", p.Get("date"))
  25. if err != nil {
  26. c.AbortWithStatus(http.StatusBadRequest)
  27. return
  28. }
  29. // convert 2006-01-02-->2016-01-02 23:59:59
  30. tm := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, time.Local)
  31. data, err := dmSvc.SearchDMHistory(c, int32(tp), oid, tm.Unix())
  32. if err != nil {
  33. c.AbortWithStatus(httpCode(err))
  34. return
  35. }
  36. c.Writer.Header().Set("Content-Encoding", "deflate")
  37. c.Bytes(200, contextType, data)
  38. }
  39. func dmHistoryV2(c *bm.Context) {
  40. p := c.Request.Form
  41. tp, err := strconv.ParseInt(p.Get("type"), 10, 64)
  42. if err != nil {
  43. c.JSON(nil, ecode.RequestErr)
  44. return
  45. }
  46. oid, err := strconv.ParseInt(p.Get("oid"), 10, 64)
  47. if err != nil {
  48. c.JSON(nil, ecode.RequestErr)
  49. return
  50. }
  51. date, err := time.Parse("2006-01-02", p.Get("date"))
  52. if err != nil {
  53. c.JSON(nil, ecode.RequestErr)
  54. return
  55. }
  56. // convert 2006-01-02-->2016-01-02 23:59:59
  57. tm := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, time.Local)
  58. c.JSON(dmSvc.SearchDMHistoryV2(c, int32(tp), oid, tm.Unix()))
  59. }
  60. func dmHistoryIndex(c *bm.Context) {
  61. var (
  62. p = c.Request.Form
  63. now = time.Now()
  64. )
  65. tp, err := strconv.ParseInt(p.Get("type"), 10, 64)
  66. if err != nil {
  67. c.JSON(nil, ecode.RequestErr)
  68. return
  69. }
  70. oid, err := strconv.ParseInt(p.Get("oid"), 10, 64)
  71. if err != nil {
  72. c.JSON(nil, ecode.RequestErr)
  73. return
  74. }
  75. month := p.Get("month")
  76. date, err := time.Parse("2006-01", month)
  77. if err != nil {
  78. c.JSON(nil, ecode.RequestErr)
  79. return
  80. }
  81. // only allow recent one year query
  82. if now.Year()-date.Year() >= 1 && now.Month()-date.Month() > 12 {
  83. c.JSON(nil, ecode.RequestErr)
  84. return
  85. }
  86. data, err := dmSvc.SearchDMHisIndex(c, int32(tp), oid, month)
  87. if err != nil {
  88. c.JSON(nil, err)
  89. return
  90. }
  91. c.JSON(data, nil)
  92. }