archive.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package http
  2. import (
  3. "encoding/csv"
  4. "io/ioutil"
  5. "strings"
  6. "go-common/app/admin/main/esports/conf"
  7. "go-common/app/admin/main/esports/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/xstr"
  12. "strconv"
  13. )
  14. func arcList(c *bm.Context) {
  15. var (
  16. list []*model.ArcResult
  17. cnt int
  18. err error
  19. )
  20. v := new(model.ArcListParam)
  21. if err = c.Bind(v); err != nil {
  22. return
  23. }
  24. if v.Pn == 0 {
  25. v.Pn = 1
  26. }
  27. if v.Ps == 0 {
  28. v.Ps = 20
  29. }
  30. if list, cnt, err = esSvc.ArcList(c, v); err != nil {
  31. c.JSON(nil, err)
  32. return
  33. }
  34. data := make(map[string]interface{}, 2)
  35. page := map[string]int{
  36. "num": v.Pn,
  37. "size": v.Ps,
  38. "total": cnt,
  39. }
  40. data["page"] = page
  41. data["list"] = list
  42. c.JSON(data, nil)
  43. }
  44. func batchAddArc(c *bm.Context) {
  45. v := new(model.ArcAddParam)
  46. if err := c.Bind(v); err != nil {
  47. return
  48. }
  49. if len(v.Aids) > conf.Conf.Rule.MaxBatchArcLimit {
  50. c.JSON(nil, ecode.RequestErr)
  51. return
  52. }
  53. c.JSON(nil, esSvc.BatchAddArc(c, v))
  54. }
  55. func batchEditArc(c *bm.Context) {
  56. v := new(model.ArcAddParam)
  57. if err := c.Bind(v); err != nil {
  58. return
  59. }
  60. if len(v.Aids) > conf.Conf.Rule.MaxBatchArcLimit {
  61. c.JSON(nil, ecode.RequestErr)
  62. return
  63. }
  64. c.JSON(nil, esSvc.BatchEditArc(c, v))
  65. }
  66. func editArc(c *bm.Context) {
  67. v := new(model.ArcImportParam)
  68. if err := c.Bind(v); err != nil {
  69. return
  70. }
  71. c.JSON(nil, esSvc.EditArc(c, v))
  72. }
  73. func arcImportCSV(c *bm.Context) {
  74. var (
  75. err error
  76. data []byte
  77. )
  78. file, _, err := c.Request.FormFile("file")
  79. if err != nil {
  80. log.Error("arcImportCSV upload err(%v)", err)
  81. c.JSON(nil, ecode.RequestErr)
  82. return
  83. }
  84. defer file.Close()
  85. data, err = ioutil.ReadAll(file)
  86. if err != nil {
  87. log.Error("arcImportCSV ioutil.ReadAll err(%v)", err)
  88. c.JSON(nil, ecode.RequestErr)
  89. return
  90. }
  91. r := csv.NewReader(strings.NewReader(string(data)))
  92. records, err := r.ReadAll()
  93. if err != nil {
  94. log.Error("r.ReadAll() err(%v)", err)
  95. c.JSON(nil, ecode.RequestErr)
  96. return
  97. }
  98. if l := len(records); l > conf.Conf.Rule.MaxCSVRows || l <= 1 {
  99. c.JSON(nil, ecode.RequestErr)
  100. return
  101. }
  102. var arcs []*model.ArcImportParam
  103. aidMap := make(map[int64]int64, len(arcs))
  104. for _, v := range records {
  105. arc := new(model.ArcImportParam)
  106. // aid
  107. if aid, err := strconv.ParseInt(v[0], 10, 64); err != nil || aid <= 0 {
  108. log.Warn("arcImportCSV strconv.ParseInt(%s) error(%v)", v[0], err)
  109. continue
  110. } else {
  111. if _, ok := aidMap[aid]; ok {
  112. continue
  113. }
  114. arc.Aid = aid
  115. aidMap[aid] = aid
  116. }
  117. // gids
  118. if gids, err := xstr.SplitInts(v[1]); err != nil {
  119. log.Warn("arcImportCSV gids xstr.SplitInts(%s) aid(%d) error(%v)", v[1], arc.Aid, err)
  120. } else {
  121. for _, id := range gids {
  122. if id > 0 {
  123. arc.Gids = append(arc.Gids, id)
  124. }
  125. }
  126. }
  127. // match ids
  128. if matchIDs, err := xstr.SplitInts(v[2]); err != nil {
  129. log.Warn("arcImportCSV match xstr.SplitInts(%s) aid(%d) error(%v)", v[2], arc.Aid, err)
  130. } else {
  131. for _, id := range matchIDs {
  132. if id > 0 {
  133. arc.MatchIDs = append(arc.MatchIDs, id)
  134. }
  135. }
  136. }
  137. // team ids
  138. if teamIDs, err := xstr.SplitInts(v[3]); err != nil {
  139. log.Warn("arcImportCSV team xstr.SplitInts(%s) aid(%d) error(%v)", v[3], arc.Aid, err)
  140. } else {
  141. for _, id := range teamIDs {
  142. if id > 0 {
  143. arc.TeamIDs = append(arc.TeamIDs, id)
  144. }
  145. }
  146. }
  147. // tag ids
  148. if tagIDs, err := xstr.SplitInts(v[4]); err != nil {
  149. log.Warn("arcImportCSV tag xstr.SplitInts(%s) aid(%d) error(%v)", v[4], arc.Aid, err)
  150. } else {
  151. for _, id := range tagIDs {
  152. if id > 0 {
  153. arc.TagIDs = append(arc.TagIDs, id)
  154. }
  155. }
  156. }
  157. // years
  158. if years, err := xstr.SplitInts(v[5]); err != nil {
  159. log.Warn("arcImportCSV year xstr.SplitInts(%s) aid(%d) error(%v)", v[5], arc.Aid, err)
  160. } else {
  161. for _, id := range years {
  162. if id > 0 {
  163. arc.Years = append(arc.Years, id)
  164. }
  165. }
  166. }
  167. arcs = append(arcs, arc)
  168. }
  169. if len(arcs) == 0 {
  170. c.JSON(nil, ecode.RequestErr)
  171. return
  172. }
  173. c.JSON(nil, esSvc.ArcImportCSV(c, arcs))
  174. }
  175. func batchDelArc(c *bm.Context) {
  176. v := new(struct {
  177. Aids []int64 `form:"aids,split" validate:"dive,gt=1,required"`
  178. })
  179. if err := c.Bind(v); err != nil {
  180. return
  181. }
  182. c.JSON(nil, esSvc.BatchDelArc(c, v.Aids))
  183. }