tree.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/admin/main/esports/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. "github.com/jinzhu/gorm"
  10. )
  11. var _emptyTreeList = make([][]*model.TreeList, 0)
  12. // AddTree .
  13. func (s *Service) AddTree(c context.Context, param *model.Tree) (rs model.Tree, err error) {
  14. var rootID int64
  15. db := s.dao.DB.Model(&model.Tree{}).Create(param)
  16. if err = db.Error; err != nil {
  17. log.Error("AddTree s.dao.DB.Model Create(%+v) error(%v)", param, err)
  18. return
  19. }
  20. rootID = (db.Value.(*model.Tree)).RootID
  21. rs.ID = (db.Value.(*model.Tree)).ID
  22. rs.MaID = (db.Value.(*model.Tree)).MaID
  23. rs.MadID = (db.Value.(*model.Tree)).MadID
  24. rs.Pid = (db.Value.(*model.Tree)).Pid
  25. rs.Mid = (db.Value.(*model.Tree)).Mid
  26. // update detail online.
  27. s.UpOnline(c, rs.MadID, _downLine)
  28. // update first root_id.
  29. if param.RootID == 0 || rs.Pid == 0 {
  30. upDB := s.dao.DB.Model(&model.Tree{})
  31. if err = upDB.Where("id=?", rs.ID).Updates(map[string]interface{}{"root_id": rs.ID}).Error; err != nil {
  32. log.Error("AddTree db.Model Updates(%+v) error(%v)", rs.ID, err)
  33. return
  34. }
  35. rootID = (upDB.Value.(*model.Tree)).RootID
  36. }
  37. rs.RootID = rootID
  38. return
  39. }
  40. // EditTree .
  41. func (s *Service) EditTree(c context.Context, param *model.TreeEditParam) (err error) {
  42. var (
  43. paramIDs []int64
  44. trees, paramTree []*model.Tree
  45. delIDs []int64
  46. nodes map[int64]int64
  47. )
  48. if err = json.Unmarshal([]byte(param.Nodes), &nodes); err != nil {
  49. err = ecode.RequestErr
  50. return
  51. }
  52. if len(nodes) == 0 {
  53. err = ecode.EsportsTreeEmptyErr
  54. return
  55. }
  56. for id := range nodes {
  57. if id > 0 {
  58. paramIDs = append(paramIDs, id)
  59. } else {
  60. err = ecode.RequestErr
  61. return
  62. }
  63. }
  64. // check tree maid same .
  65. if err = s.dao.DB.Model(&model.Tree{}).Model(&model.Tree{}).Where("id IN (?)", paramIDs).Find(&paramTree).Error; err != nil {
  66. log.Error("EditTree s.dao.DB.Model Find(%+v) error(%v)", paramIDs, err)
  67. }
  68. for _, tmpTree := range paramTree {
  69. if tmpTree.MadID != param.MadID {
  70. err = ecode.EsportsTreeNodeErr
  71. return
  72. }
  73. }
  74. ParamIDCount := len(paramIDs)
  75. treeDB := s.dao.DB.Model(&model.Tree{})
  76. if err = treeDB.Where("mad_id=?", param.MadID).Where("is_deleted=0").Find(&trees).Error; err != nil {
  77. log.Error("EditTree Error (%v)", err)
  78. return
  79. }
  80. treeCount := len(trees)
  81. if ParamIDCount != treeCount {
  82. for _, tree := range trees {
  83. if _, ok := nodes[tree.ID]; !ok {
  84. delIDs = append(delIDs, tree.ID)
  85. }
  86. }
  87. }
  88. // Prevent multiplayer editing
  89. if ParamIDCount != (treeCount - len(delIDs)) {
  90. err = ecode.EsportsMultiEdit
  91. return
  92. }
  93. tx := s.dao.DB.Begin()
  94. if err = tx.Error; err != nil {
  95. log.Error("s.dao.DB.Begin error(%v)", err)
  96. return
  97. }
  98. if err = upTree(tx, delIDs, nodes, param.MadID); err != nil {
  99. err = tx.Rollback().Error
  100. return
  101. }
  102. if err = tx.Commit().Error; err != nil {
  103. log.Error("tx.Commit error(%v)", err)
  104. return
  105. }
  106. return
  107. }
  108. func upTree(tx *gorm.DB, delIDs []int64, nodes map[int64]int64, madID int64) (err error) {
  109. if err = tx.Model(&model.Tree{}).Where("id IN (?)", delIDs).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  110. log.Error("upTree tx.Model Updates(%+v) error(%v)", delIDs, err)
  111. return
  112. }
  113. if err = tx.Model(&model.Tree{}).Exec(model.BatchEditTreeSQL(nodes)).Error; err != nil {
  114. log.Error("upTree tx.Model Exec(%+v) error(%v)", nodes, err)
  115. }
  116. if err = tx.Model(&model.MatchDetail{}).Where("id=?", madID).Updates(map[string]interface{}{"online": _downLine}).Error; err != nil {
  117. log.Error("upTree MatchDetail tx.Model Updates(%d) error(%v)", madID, err)
  118. }
  119. return
  120. }
  121. // DelTree .
  122. func (s *Service) DelTree(c context.Context, param *model.TreeDelParam) (err error) {
  123. var ids []int64
  124. if ids, err = xstr.SplitInts(param.IDs); err != nil {
  125. err = ecode.RequestErr
  126. return
  127. }
  128. if err = s.dao.DB.Model(&model.Tree{}).Model(&model.Tree{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  129. log.Error("DelTree treeDB.Model Updates(%+v) error(%v)", ids, err)
  130. }
  131. // update detail online.
  132. s.UpOnline(c, param.MadID, _downLine)
  133. return
  134. }
  135. // TreeList .
  136. func (s *Service) TreeList(c context.Context, param *model.TreeListParam) (res *model.TreeDetailList, err error) {
  137. var (
  138. trees []*model.Tree
  139. mids []int64
  140. contests []*model.Contest
  141. mapContests map[int64]*model.ContestInfo
  142. sTree []*model.TreeList
  143. cInfos []*model.ContestInfo
  144. detail model.MatchDetail
  145. rs [][]*model.TreeList
  146. )
  147. db := s.dao.DB.Model(&model.MatchDetail{}).Where("id=?", param.MadID).First(&detail)
  148. if err = db.Error; err != nil {
  149. log.Error("TreeList MatchDetail db.Model Find error(%v)", err)
  150. }
  151. if detail.ID == 0 {
  152. err = ecode.EsportsTreeDetailErr
  153. return
  154. }
  155. treeDB := s.dao.DB.Model(&model.Tree{})
  156. treeDB = treeDB.Where("mad_id=?", param.MadID).Where("is_deleted=0")
  157. if err = treeDB.Limit(s.c.Rule.MaxTreeContests).Order("root_id ASC,pid ASC,game_rank ASC").Find(&trees).Error; err != nil {
  158. log.Error("TreeList Error (%v)", err)
  159. return
  160. }
  161. for _, tree := range trees {
  162. mids = append(mids, tree.Mid)
  163. }
  164. contestsDB := s.dao.DB.Model(&model.Contest{})
  165. contestsDB = contestsDB.Where("id IN (?)", mids)
  166. count := len(mids)
  167. if err = contestsDB.Offset(0).Limit(count).Find(&contests).Error; err != nil {
  168. log.Error("ContestList Error (%v)", err)
  169. return
  170. }
  171. if cInfos, err = s.contestInfos(c, contests, false); err != nil {
  172. log.Error("s.contestInfos Error (%v)", err)
  173. return
  174. }
  175. mapContests = make(map[int64]*model.ContestInfo, count)
  176. for _, info := range cInfos {
  177. mapContests[info.ID] = info
  178. }
  179. for _, tree := range trees {
  180. if tree.Pid == 0 {
  181. if len(sTree) > 0 {
  182. rs = append(rs, sTree)
  183. }
  184. sTree = nil
  185. if cInfo, ok := mapContests[tree.Mid]; ok {
  186. sTree = append(sTree, &model.TreeList{Tree: tree, ContestInfo: cInfo})
  187. } else {
  188. sTree = append(sTree, &model.TreeList{Tree: tree})
  189. }
  190. } else {
  191. if cInfo, ok := mapContests[tree.Mid]; ok {
  192. sTree = append(sTree, &model.TreeList{Tree: tree, ContestInfo: cInfo})
  193. } else {
  194. sTree = append(sTree, &model.TreeList{Tree: tree})
  195. }
  196. }
  197. }
  198. if len(sTree) > 0 {
  199. rs = append(rs, sTree)
  200. }
  201. res = new(model.TreeDetailList)
  202. if len(trees) == 0 {
  203. res.Tree = _emptyTreeList
  204. } else {
  205. res.Tree = rs
  206. }
  207. res.Detail = &detail
  208. return
  209. }