score.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package http
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "go-common/app/admin/main/up-rating/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/render"
  11. )
  12. func scoreList(ctx *blademaster.Context) {
  13. arg := new(model.RatingListArg)
  14. if err := ctx.Bind(arg); err != nil {
  15. log.Error("scoreList ctx.Bind error(%v)", err)
  16. return
  17. }
  18. date := time.Now()
  19. if arg.ScoreDate != "" {
  20. var err error
  21. if date, err = time.ParseInLocation("2006-01", arg.ScoreDate, time.Local); err != nil {
  22. log.Error("date(%s) parse error", arg.ScoreDate)
  23. err = ecode.RequestErr
  24. return
  25. }
  26. }
  27. res, total, err := svr.RatingList(ctx, arg, date)
  28. if err != nil {
  29. log.Error("svr.RatingList error(%v) arg(%v)", err, arg)
  30. ctx.JSON(nil, err)
  31. return
  32. }
  33. ctx.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  34. "code": 0,
  35. "message": "0",
  36. "data": res,
  37. "paging": &model.Paging{
  38. Ps: arg.Limit,
  39. Total: total,
  40. },
  41. }))
  42. }
  43. func scoreCurrent(ctx *blademaster.Context) {
  44. arg := new(struct {
  45. MID int64 `form:"mid" validate:"required"`
  46. })
  47. if err := ctx.Bind(arg); err != nil {
  48. log.Error("ctx.Bind error(%v)", err)
  49. return
  50. }
  51. res, err := svr.ScoreCurrent(ctx, arg.MID)
  52. if err != nil {
  53. log.Error("svr.ScoreCurrent error(%v) arg(%v)", err, arg)
  54. ctx.JSON(nil, err)
  55. return
  56. }
  57. ctx.JSON(res, nil)
  58. }
  59. func scoreHistory(ctx *blademaster.Context) {
  60. arg := new(model.UpRatingHistoryArg)
  61. if err := ctx.Bind(arg); err != nil {
  62. log.Error("ctx.Bind error(%v)", err)
  63. return
  64. }
  65. types := []model.ScoreType{
  66. model.Creativity,
  67. model.Influence,
  68. model.Credit,
  69. }
  70. if arg.ScoreType != model.Magnetic {
  71. types = []model.ScoreType{arg.ScoreType}
  72. }
  73. res, err := svr.ScoreHistory(ctx, types, arg.Mid, arg.Month == 0, arg.Month)
  74. if err != nil {
  75. log.Error("svr.RatingList error(%v) arg(%v)", err, arg)
  76. ctx.JSON(nil, err)
  77. return
  78. }
  79. ctx.JSON(&model.UpRatingHistoryResp{Data: res}, nil)
  80. }
  81. func scoreExport(ctx *blademaster.Context) {
  82. arg := new(model.RatingListArg)
  83. if err := ctx.Bind(arg); err != nil {
  84. log.Error("scoreList ctx.Bind error(%v)", err)
  85. return
  86. }
  87. date := time.Now()
  88. if arg.ScoreDate != "" {
  89. var err error
  90. if date, err = time.ParseInLocation("2006-01", arg.ScoreDate, time.Local); err != nil {
  91. log.Error("date(%s) parse error", arg.ScoreDate)
  92. err = ecode.RequestErr
  93. return
  94. }
  95. }
  96. content, err := svr.ExportScores(ctx, arg, date)
  97. if err != nil {
  98. ctx.JSON(nil, err)
  99. log.Error("up-rating svr.ExportScores error(%v)", err)
  100. return
  101. }
  102. ctx.Render(http.StatusOK, CSV{
  103. Content: content,
  104. Title: fmt.Sprintf("%s-%s", time.Now().Format("2006-01-02"), "scores"),
  105. })
  106. }