123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package http
- import (
- "encoding/csv"
- "io/ioutil"
- "strings"
- "go-common/app/admin/main/esports/conf"
- "go-common/app/admin/main/esports/model"
- "go-common/library/ecode"
- "go-common/library/log"
- bm "go-common/library/net/http/blademaster"
- "go-common/library/xstr"
- "strconv"
- )
- func arcList(c *bm.Context) {
- var (
- list []*model.ArcResult
- cnt int
- err error
- )
- v := new(model.ArcListParam)
- if err = c.Bind(v); err != nil {
- return
- }
- if v.Pn == 0 {
- v.Pn = 1
- }
- if v.Ps == 0 {
- v.Ps = 20
- }
- if list, cnt, err = esSvc.ArcList(c, v); err != nil {
- c.JSON(nil, err)
- return
- }
- data := make(map[string]interface{}, 2)
- page := map[string]int{
- "num": v.Pn,
- "size": v.Ps,
- "total": cnt,
- }
- data["page"] = page
- data["list"] = list
- c.JSON(data, nil)
- }
- func batchAddArc(c *bm.Context) {
- v := new(model.ArcAddParam)
- if err := c.Bind(v); err != nil {
- return
- }
- if len(v.Aids) > conf.Conf.Rule.MaxBatchArcLimit {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- c.JSON(nil, esSvc.BatchAddArc(c, v))
- }
- func batchEditArc(c *bm.Context) {
- v := new(model.ArcAddParam)
- if err := c.Bind(v); err != nil {
- return
- }
- if len(v.Aids) > conf.Conf.Rule.MaxBatchArcLimit {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- c.JSON(nil, esSvc.BatchEditArc(c, v))
- }
- func editArc(c *bm.Context) {
- v := new(model.ArcImportParam)
- if err := c.Bind(v); err != nil {
- return
- }
- c.JSON(nil, esSvc.EditArc(c, v))
- }
- func arcImportCSV(c *bm.Context) {
- var (
- err error
- data []byte
- )
- file, _, err := c.Request.FormFile("file")
- if err != nil {
- log.Error("arcImportCSV upload err(%v)", err)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- defer file.Close()
- data, err = ioutil.ReadAll(file)
- if err != nil {
- log.Error("arcImportCSV ioutil.ReadAll err(%v)", err)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- r := csv.NewReader(strings.NewReader(string(data)))
- records, err := r.ReadAll()
- if err != nil {
- log.Error("r.ReadAll() err(%v)", err)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if l := len(records); l > conf.Conf.Rule.MaxCSVRows || l <= 1 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- var arcs []*model.ArcImportParam
- aidMap := make(map[int64]int64, len(arcs))
- for _, v := range records {
- arc := new(model.ArcImportParam)
- // aid
- if aid, err := strconv.ParseInt(v[0], 10, 64); err != nil || aid <= 0 {
- log.Warn("arcImportCSV strconv.ParseInt(%s) error(%v)", v[0], err)
- continue
- } else {
- if _, ok := aidMap[aid]; ok {
- continue
- }
- arc.Aid = aid
- aidMap[aid] = aid
- }
- // gids
- if gids, err := xstr.SplitInts(v[1]); err != nil {
- log.Warn("arcImportCSV gids xstr.SplitInts(%s) aid(%d) error(%v)", v[1], arc.Aid, err)
- } else {
- for _, id := range gids {
- if id > 0 {
- arc.Gids = append(arc.Gids, id)
- }
- }
- }
- // match ids
- if matchIDs, err := xstr.SplitInts(v[2]); err != nil {
- log.Warn("arcImportCSV match xstr.SplitInts(%s) aid(%d) error(%v)", v[2], arc.Aid, err)
- } else {
- for _, id := range matchIDs {
- if id > 0 {
- arc.MatchIDs = append(arc.MatchIDs, id)
- }
- }
- }
- // team ids
- if teamIDs, err := xstr.SplitInts(v[3]); err != nil {
- log.Warn("arcImportCSV team xstr.SplitInts(%s) aid(%d) error(%v)", v[3], arc.Aid, err)
- } else {
- for _, id := range teamIDs {
- if id > 0 {
- arc.TeamIDs = append(arc.TeamIDs, id)
- }
- }
- }
- // tag ids
- if tagIDs, err := xstr.SplitInts(v[4]); err != nil {
- log.Warn("arcImportCSV tag xstr.SplitInts(%s) aid(%d) error(%v)", v[4], arc.Aid, err)
- } else {
- for _, id := range tagIDs {
- if id > 0 {
- arc.TagIDs = append(arc.TagIDs, id)
- }
- }
- }
- // years
- if years, err := xstr.SplitInts(v[5]); err != nil {
- log.Warn("arcImportCSV year xstr.SplitInts(%s) aid(%d) error(%v)", v[5], arc.Aid, err)
- } else {
- for _, id := range years {
- if id > 0 {
- arc.Years = append(arc.Years, id)
- }
- }
- }
- arcs = append(arcs, arc)
- }
- if len(arcs) == 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- c.JSON(nil, esSvc.ArcImportCSV(c, arcs))
- }
- func batchDelArc(c *bm.Context) {
- v := new(struct {
- Aids []int64 `form:"aids,split" validate:"dive,gt=1,required"`
- })
- if err := c.Bind(v); err != nil {
- return
- }
- c.JSON(nil, esSvc.BatchDelArc(c, v.Aids))
- }
|