add.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. const app = getApp()
  2. Page({
  3. query: {},
  4. data: {
  5. view: 'graph',
  6. frameStyle: { useTop: true, name: '赛程管理', leftArrow: true, useBar: false },
  7. canvasWidth: 375,
  8. canvasHeight: 600,
  9. pixelRatio: 1,
  10. forceMini: false,
  11. // 数据
  12. data: undefined,
  13. riseList: [],
  14. arrangeList: [],
  15. raceList: [],
  16. groundList: [],
  17. refereeList: [],
  18. arrangeData: {},
  19. // picker
  20. show: false,
  21. personList: [],
  22. selectNode: undefined,
  23. loading: true
  24. },
  25. // 计算晋级图
  26. computedTreeData() {
  27. const getWinnerNumList = this.data.riseList; // 将每个小组取多少人收到1个纯数字的数组中
  28. console.log(getWinnerNumList)
  29. const getWinnerNum = getWinnerNumList.reduce((p, n) => p + n, 0); // 小组赛选取人数: 应该是该比赛项目的 各组选取人数加和
  30. console.log(getWinnerNum)
  31. let level = 1;
  32. let loop = true;
  33. while (loop) {
  34. const needUserNum = 2 ** (level - 1);
  35. // 相等,说明已经计算出结果
  36. if (needUserNum === getWinnerNum) loop = false;
  37. // 小于,说明还可以继续往后计算
  38. else if (needUserNum < getWinnerNum) level++;
  39. else {
  40. // 大于,说明不符合淘汰赛安排,需要手动定位
  41. loop = false;
  42. level = NaN
  43. }
  44. }
  45. if (!level) {
  46. console.log('不继续,返回了,编不了数据')
  47. return;
  48. }
  49. const levelList = [];
  50. // 1:16;2:8;3:4;4:2
  51. for (let i = 0; i < level - 1; i++) {
  52. const t = level - i - 1;
  53. // 计算:该层的位置数量
  54. const number = 2 ** t;
  55. const arr = [];
  56. for (let j = 1; j <= number; j++) {
  57. const obj = { id: `${i}-${j}`, num: `${i}-${j}`, pos: [i, j] }
  58. arr.push(obj);
  59. }
  60. levelList.push(arr)
  61. }
  62. // 进行数据组合,从最少的入手,依次取回,然后将取出来的数据删除掉
  63. let getLevelIndex = levelList.length - 1;
  64. const data = this.resetData(levelList, getLevelIndex)
  65. let obj = { id: `${levelList.length}`, num: `${levelList.length}`, children: data }
  66. return obj;
  67. },
  68. resetData(dataList, index = 0) {
  69. const thisLevelList = dataList[index]
  70. // 取出2条数据
  71. const p1 = thisLevelList[0]
  72. const p2 = thisLevelList[1]
  73. // 删除取出的2条数据
  74. dataList[index].shift()
  75. dataList[index].shift()
  76. if (index - 1 < 0) return [p1, p2]
  77. const p1c = this.resetData(dataList, index - 1)
  78. const p2c = this.resetData(dataList, index - 1)
  79. p1.children = p1c
  80. p2.children = p2c;
  81. return [p1, p2]
  82. },
  83. // 点击节点事件
  84. nodeTap(e) {
  85. const node = e.detail;
  86. if (!node) return;
  87. this.setData({ selectNode: node })
  88. this.toOpen();
  89. },
  90. // 获取最基础的那一层节点数据
  91. getFirstLevelNodes(node) {
  92. if (!node.children) return node;
  93. const res = [];
  94. const nodes = node.children;
  95. for (const node of nodes) {
  96. if (node?.children) {
  97. let list = this.getFirstLevelNodes(node);
  98. res.push(list);
  99. } else {
  100. res.push(node)
  101. }
  102. }
  103. return res.flat();
  104. },
  105. // 选择人员的处理
  106. selectChange(changeNode, selectNode) {
  107. const obj = { player_name: changeNode.label, player_id: changeNode.value, node_id: selectNode.id, label: 'player_name', pos: selectNode.pos }
  108. const arrangeList = this.data.arrangeList;
  109. const i = arrangeList.findIndex(f => f.player_id === obj.player_id && f.pos && obj.pos && f.pos[0] === obj.pos[0])
  110. if (i >= 0) arrangeList.splice(i, 1)
  111. arrangeList.push(obj)
  112. this.setData({ arrangeList })
  113. // 并非使用数据重组的方式,将选择的数据放入创建晋级图的数据中心,而是使用一维数组,以节点id对应的方式进行数据更新,更为灵活
  114. // 接下来需要生成比赛信息的数据
  115. this.getRaceList();
  116. },
  117. // 设置比赛数据
  118. getRaceList() {
  119. const arrangeList = this.data.arrangeList;
  120. arrangeList.sort((a, b) => {
  121. if (!a.pos) return b - a
  122. else if (!b.pos) return a - b
  123. else {
  124. // 根据 晋级节点位置,小组内节点位置进行排序
  125. const a1 = a.pos[0]
  126. const b1 = b.pos[0]
  127. if (a1 - b1 !== 0) return a1 - b1
  128. else {
  129. const a2 = a.pos[1]
  130. const b2 = b.pos[1]
  131. return a2 - b2
  132. }
  133. }
  134. })
  135. const raceList = [];
  136. for (let i = 0; i < arrangeList.length; i++) {
  137. const d1 = arrangeList[i]
  138. const d2 = arrangeList[i + 1]
  139. console.log(d1, d2)
  140. // 前后缺项,没法比赛
  141. if (!(d1 && d2)) continue;
  142. const { pos: d1pos } = d1
  143. const { pos: d2pos } = d2;
  144. // 前后两项的层级不一样,不能形成比赛
  145. if (d1pos[0] !== d2pos[0]) continue
  146. // 前后两项层级一样,但是不是连续的数字,例如:1-1与1-3,不能进行比赛,必须是 前+1=后
  147. if (d1pos[1] + 1 !== d2pos[1]) continue
  148. // d2pos[1] 必须是双数,如果不是双数,则不能进行排列
  149. if (d2pos[1] % 2 !== 0) continue;
  150. const { player_id: player_one, player_name: player_one_name, node_id: node_id_one } = d1
  151. const { player_id: player_two, player_name: player_two_name, node_id: node_id_two } = d2
  152. raceList.push({ player_one, player_one_name, node_id_one, player_two, player_two_name, node_id_two })
  153. }
  154. // 因为是数组,所以需要将model特殊处理下,按照一定规则处理可以正常赋值
  155. for (let i = 0; i < raceList.length; i++) {
  156. const d = raceList[i];
  157. const { node_id_one, node_id_two } = d
  158. d.groundModelName = `ground_id(${node_id_one}-${node_id_two})`;
  159. d.refereeModelName = `referee_id(${node_id_one}-${node_id_two})`;
  160. d.timeModelName = `match_time(${node_id_one}-${node_id_two})`;
  161. }
  162. const oRaceList = this.data.raceList;
  163. for (const race of raceList) {
  164. const { node_id_one, node_id_two } = race;
  165. const r = oRaceList.find(f => f.node_id_one === node_id_one && f.node_id_two === node_id_two)
  166. if (r) {
  167. const ri = oRaceList.findIndex(f => f.node_id_one === node_id_one && f.node_id_two === node_id_two)
  168. const { groundModelName, refereeModelName, timeModelName } = race
  169. r = { ...r, groundModelName, refereeModelName, timeModelName }
  170. oRaceList.splice(ri, 1, r)
  171. } else {
  172. oRaceList.push(race)
  173. }
  174. }
  175. this.setData({ raceList: oRaceList })
  176. },
  177. async onLoad(options) {
  178. const { match_id, grouping_id, project_id } = options
  179. this.query = { match_id, grouping_id, project_id }
  180. await this.getTeams()
  181. // 设置初始的晋级图
  182. const data = this.computedTreeData();
  183. this.setData({ data })
  184. // 请求 小组赛的 胜者列表
  185. await this.searchWinnerList();
  186. await this.search();
  187. await this.searchOthers()
  188. this.setData({ loading: false })
  189. },
  190. // 查询已安排的数据
  191. async search() {
  192. let res = await app.$get(`/newCourt/api/eliminatArrange/getOne`, this.query)
  193. if (app.$checkRes(res)) {
  194. if (res.data) { const { arrange } = res.data; this.setData({ arrangeData: res.data, arrangeList: arrange }) }
  195. }
  196. res = await app.$get(`/newCourt/api/eliminateRace`, this.query)
  197. if (app.$checkRes(res)) {
  198. this.setData({
  199. raceList: res.data
  200. })
  201. }
  202. // 数据本地化处理下
  203. this.getRaceList();
  204. },
  205. // 获取该项目的小组,形成晋级图
  206. async getTeams() {
  207. const res = await app.$get(`/newCourt/api/raceTeam`, this.query)
  208. if (app.$checkRes(res)) {
  209. const { data } = res
  210. const riseList = data.map(i => i.rise)
  211. this.setData({ riseList })
  212. }
  213. },
  214. // 获取小组赛胜者名单
  215. async searchWinnerList() {
  216. const res = await app.$post(`/newCourt/api/race/getWinnerList`, this.query)
  217. if (app.$checkRes(res)) {
  218. const { data } = res;
  219. const arr = [];
  220. for (const i of data) {
  221. const { personList, name } = i;
  222. for (const person of personList) {
  223. // person.player_name += `(${name})`;
  224. const obj = { label: person.player_name, value: person.player_id }
  225. arr.push(obj)
  226. }
  227. }
  228. this.setData({
  229. personList: arr
  230. })
  231. }
  232. },
  233. /**自动排列最底层,挨个放 */
  234. toAutoInitData() {
  235. const personList = this.data.personList;
  236. const data = this.data.data
  237. const getBaseList = data => {
  238. if (!data.children) return data;
  239. const arr = [];
  240. for (const d of data.children) {
  241. const r = getBaseList(d)
  242. arr.push(r);
  243. }
  244. return arr.flat();
  245. }
  246. const firstList = getBaseList(data);
  247. for (let i = 0; i < firstList.length; i++) {
  248. const f = firstList[i];
  249. const p = personList[i];
  250. this.selectChange(p, f);
  251. }
  252. },
  253. /**查询选项的数据 */
  254. async searchOthers() {
  255. let res;
  256. res = await app.$get(`/newCourt/api/ground`)
  257. if (app.$checkRes(res)) {
  258. this.setData({ groundList: res.data })
  259. }
  260. res = await app.$get(`/newCourt/api/user`, { type: '1' })
  261. if (app.$checkRes(res)) {
  262. this.setData({ refereeList: res.data })
  263. }
  264. },
  265. turnView(e) {
  266. const view = e?.target?.dataset?.view || 'graph';
  267. this.setData({ view })
  268. },
  269. //picker
  270. onChange(event) {
  271. const obj = event?.detail?.value;
  272. this.selectChange(obj, this.data.selectNode);
  273. this.toClose();
  274. },
  275. toOpen() {
  276. this.setData({ show: true })
  277. },
  278. toClose() {
  279. this.setData({ show: false, selectNode: undefined })
  280. },
  281. toSelected(event) {
  282. const data = event?.detail;
  283. const { value, model } = data;
  284. if (!model) return;
  285. const arr = model.split('(')
  286. const modelName = arr[0]
  287. const index = arr[1]
  288. console.log(arr[1])
  289. const raceList = this.data.raceList;
  290. raceList[index][modelName] = value;
  291. if (modelName === 'ground_id') {
  292. const ground = this.data.groundList.find(f => f._id === value)
  293. if (ground && ground.referee_id) raceList[index]['referee_id'] = ground.referee_id
  294. }
  295. this.setData({ raceList })
  296. },
  297. // 跳转菜单
  298. back(e) {
  299. wx.navigateBack({
  300. delta: 1,
  301. })
  302. },
  303. async toSubmit() {
  304. // 生成的比赛信息
  305. let raceList = this.data.raceList;
  306. // 安排信息
  307. let arrangeList = this.data.arrangeList;
  308. // arrangeList:需要将match_id,grouping_id,project_id 也放入,确定是这个项目的淘汰赛
  309. let arrange = this.data.arrangeData;
  310. if (!arrange._id) {
  311. arrange = { arrange: arrangeList, ...this.query }
  312. }
  313. // raceList:淘汰赛的比赛赛程
  314. raceList = raceList.map(i => {
  315. const { groundModelName, refereeModelName, timeModelName, ...others } = i
  316. return { ...others, ...this.query }
  317. })
  318. // 创建安排
  319. let res
  320. if (arrange._id) {
  321. res = await app.$post(`/newCourt/api/eliminatArrange/${arrange._id}`, arrange)
  322. if (app.$checkRes(res)) {
  323. console.log('淘汰赛安排创建成功')
  324. }
  325. } else {
  326. res = await app.$post(`/newCourt/api/eliminatArrange`, arrange)
  327. if (app.$checkRes(res)) {
  328. console.log('淘汰赛安排创建成功')
  329. }
  330. }
  331. res = await app.$post('/newCourt/api/eliminateRace/saveAll', raceList)
  332. wx.showToast({
  333. title: '保存成功',
  334. })
  335. }
  336. });