eliminate.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const { ObjectId } = require('mongoose').Types;
  7. class EliminateService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'eliminate');
  10. this.model = this.ctx.model.Race.Eliminate;
  11. this.matchModel = this.ctx.model.Race.Match;
  12. this.matchGroupModel = this.ctx.model.Race.MatchTeamGroup;
  13. this.baseUserModel = this.ctx.model.Base.User;
  14. this.userModel = this.ctx.model.Race.User;
  15. this.teamApplyModel = this.ctx.model.Race.TeamApply;
  16. }
  17. /**
  18. * 查询淘汰赛的排名
  19. * @param {Object} query 查询参数
  20. */
  21. async ranking({ match_id, group_id, project_id }) {
  22. let raceList = await this.model.find({ match_id, group_id, project_id });
  23. if (raceList.length <= 0) return [];
  24. raceList = JSON.parse(JSON.stringify(raceList));
  25. const rarr = [];
  26. for (const i of raceList) {
  27. const d = await this.getPlayerName(i);
  28. rarr.push(d);
  29. }
  30. const { edges, nodes } = await this.graphData({ match_id, group_id, project_id });
  31. const topEdges = this.getTopNodes(edges);
  32. const topNodes = nodes.filter(f => topEdges.find(ff => ff.target === f.id));
  33. // 然后将顶点的节点排序,根据标记进行排序
  34. // 第一名一定是 w, 胜者组: w开头,l越多,越优先; 败者组: l越少,越优先
  35. let winGroup = topNodes.filter(f => f.mark.includes('w'));
  36. winGroup = winGroup.map(i => {
  37. const markArr = i.mark.split('');
  38. i.sort = markArr.length;
  39. return i;
  40. });
  41. winGroup = _.orderBy(winGroup, [ 'sort' ], [ 'asc' ]);
  42. let loseGroup = topNodes.filter(f => !f.mark.includes('w'));
  43. loseGroup = loseGroup.map(i => {
  44. const markArr = i.mark.split('');
  45. i.sort = markArr.length;
  46. return i;
  47. });
  48. loseGroup = _.orderBy(loseGroup, [ 'sort' ], [ 'asc' ]);
  49. const orderNodes = [ ...winGroup, ...loseGroup ];
  50. const arr = [];
  51. let num = 1;
  52. for (const n of orderNodes) {
  53. const { id } = n;
  54. const es = topEdges.filter(f => f.target === id);
  55. const s1 = _.head(es);
  56. const s2 = _.last(es);
  57. const r = rarr.find(f => (f.player_one_node === s1.source && f.player_two_node === s2.source) || (f.player_one_node === s2.source && f.player_two_node === s1.source));
  58. if (!r) continue;
  59. const { player_one_score, player_two_score, player_one_name, player_two_name } = r;
  60. let win,
  61. lose;
  62. if (player_one_score > player_two_score) {
  63. win = player_one_name;
  64. lose = player_two_name;
  65. } else {
  66. win = player_two_name;
  67. lose = player_one_name;
  68. }
  69. arr.push({ name: win, num });
  70. if (num < 5) num++;
  71. arr.push({ name: lose, num });
  72. if (num < 5) num++;
  73. }
  74. return arr;
  75. }
  76. getTopNodes(edges) {
  77. const list = edges.filter(f => !edges.find(ff => ff.source === f.target));
  78. return list;
  79. }
  80. /**
  81. * 获取流程图数据
  82. * @param {Object} param 比赛项目的参数
  83. * @return {Array} 选手列表
  84. */
  85. async playerList({ match_id, group_id, project_id }) {
  86. assert(match_id, '缺少赛事信息');
  87. assert(group_id, '缺少组别信息');
  88. assert(project_id, '缺少项目信息');
  89. const match = await this.matchModel.findById(match_id);
  90. if (!match) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到赛事信息');
  91. const { format } = match;
  92. // 0为小组赛,不需要这里处理
  93. if (format === '0') return;
  94. let players = [];
  95. // 1.为淘汰赛,直接找项目的报名选手进行安排
  96. // 2.为小组后淘汰,先找项目的小组再给小组排名;然后根据每组的取前几,决定淘汰赛选手
  97. const groupResult = await this.ctx.model.Race.MatchSmallGroupSchedule.find({ project_id, status: '2' });
  98. const r = await this.groupRanking(groupResult);
  99. for (const team_id in r) {
  100. const list = r[team_id];
  101. for (const p of list) {
  102. players.push(p.player);
  103. }
  104. }
  105. const head = _.head(groupResult);
  106. const player_type = _.get(head, 'player_type');
  107. if (player_type === 'Race.TeamApply') {
  108. const list = await this.ctx.service.teamApply.query({ _id: players });
  109. players = list.map(i => {
  110. const { one_member_name, two_member_name, _id } = i;
  111. const obj = { _id, name: `${one_member_name}-${two_member_name}` };
  112. return obj;
  113. });
  114. }
  115. return players;
  116. }
  117. /**
  118. * 获取流程图数据
  119. * @param {Object} param 比赛项目的参数
  120. * @return {Object} {nodes,edges} 流程图数据
  121. */
  122. async graphData({ match_id, group_id, project_id }) {
  123. assert(match_id, '缺少赛事信息');
  124. assert(group_id, '缺少组别信息');
  125. assert(project_id, '缺少项目信息');
  126. const match = await this.matchModel.findById(match_id);
  127. if (!match) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到赛事信息');
  128. const { format } = match;
  129. // 0为小组赛,不需要这里处理
  130. if (format === '0') return;
  131. // 1.为淘汰赛,直接找项目的报名选手进行安排
  132. // 2.为小组后淘汰,先找项目的小组再给小组排名;然后根据每组的取前几,决定淘汰赛选手
  133. // 总之第一步是将选手取出来
  134. const groupResult = await this.ctx.model.Race.MatchSmallGroupSchedule.find({ project_id, status: '2' });
  135. const r = await this.groupRanking(groupResult);
  136. const players = [];
  137. for (const team_id in r) {
  138. const list = r[team_id];
  139. for (const p of list) {
  140. players.push(p.player);
  141. }
  142. }
  143. const level = this.getLevel(players.length);
  144. const chartData = this.getChartData(level);
  145. return chartData;
  146. }
  147. /**
  148. * 获取赛事流程数据(在matchTeamGroup的service中使用的)
  149. * 利用边关系,2个节点指向同一节点,即为一场比赛
  150. * @param {Object} data 流程图数据
  151. * @property {Array} edges 流程图数据的边关系
  152. * @return {Array} 赛事流程数据
  153. */
  154. getEliminateData(data) {
  155. const { edges } = data;
  156. let stop = false;
  157. const schList = [];
  158. let dupAll = JSON.parse(JSON.stringify(edges));
  159. while (!stop) {
  160. const dup = JSON.parse(JSON.stringify(dupAll));
  161. // 取出第一个关系
  162. const head = dup[0];
  163. // 删除第一个关系
  164. dup.shift();
  165. // 查看是否有与第一个关系指向一个目标的地方
  166. const r = dup.find(f => f.target === head.target);
  167. if (r) {
  168. // 有,则将这俩关系组为一个比赛,然后在dup中删除
  169. const sch = { player_one_node: head.source, player_two_node: r.source };
  170. schList.push(sch);
  171. const ri = dup.find(f => f.target === head.target);
  172. dup.splice(ri, 1);
  173. }
  174. dupAll = dup;
  175. if (dupAll.length <= 0) stop = true;
  176. }
  177. return schList;
  178. }
  179. // 获取表单数据
  180. getChartData(level) {
  181. let returnData = {};
  182. const nodes = [];
  183. const edges = [];
  184. // 生成基数层
  185. const allPersonNumber = 2 ** level;
  186. for (let i = 1; i <= allPersonNumber; i++) {
  187. const level = 1; // 基数层固定为第一层
  188. const node = this.getNode(level, i);
  189. nodes.push(node);
  190. }
  191. // 生成 胜败组
  192. const winnerTeamLine = allPersonNumber / 2;
  193. for (let i = 1; i <= allPersonNumber; i++) {
  194. const level = 2; // 胜负组固定为第二层
  195. const mark = i <= winnerTeamLine ? 'w' : 'l';
  196. const node = this.getNode(level, i, mark);
  197. nodes.push(node);
  198. // 但是只有 胜者组线 以前会产生 边关系; 胜者组线以后的数据为败者组,不与基数层产生关系线
  199. if (i <= winnerTeamLine) {
  200. const { level, pos, id } = node;
  201. const source1 = `${level - 1}-${2 * pos - 1}`;
  202. const source2 = `${level - 1}-${2 * pos}`;
  203. const target = id;
  204. const edge1 = this.getEdge(source1, target);
  205. const edge2 = this.getEdge(source2, target);
  206. edges.push(edge1, edge2);
  207. }
  208. }
  209. // 接下来开始进行赛事统一处理
  210. returnData = this.nextProcess(3, nodes, edges);
  211. return returnData;
  212. }
  213. nextProcess(startLevel, nodes, edges) {
  214. // 1.正常进行安排:level向前进
  215. const lastLevel = nodes.filter(f => f.level === startLevel - 1);
  216. const group = _.chunk(lastLevel, 2);
  217. const thisLevelNodes = [];
  218. const thisLevelEdges = [];
  219. for (let i = 1; i <= group.length; i++) {
  220. const e = group[i - 1];
  221. const source1 = _.head(e);
  222. const source2 = _.last(e);
  223. const mark1 = _.get(source1, 'mark');
  224. const mark2 = _.get(source2, 'mark');
  225. // 先检查mark是否是一致的,如果不一致,就不需要处理了
  226. if (mark1 !== mark2) continue;
  227. const nl = startLevel;
  228. const pos = i;
  229. const node = this.getNode(nl, pos, mark1);
  230. const edge1 = this.getEdge(source1.id, node.id);
  231. const edge2 = this.getEdge(source2.id, node.id);
  232. thisLevelNodes.push(node);
  233. thisLevelEdges.push(edge1, edge2);
  234. }
  235. // 2.检查当前层级是否应该加入败者的比赛(例如:4进2, 剩余2个人要决出名次,就需要加比赛)
  236. const thisLevelNodesGroup = _.chunk(thisLevelNodes, 2);
  237. for (let i = 0; i < thisLevelNodesGroup.length; i++) {
  238. const e = thisLevelNodesGroup[i];
  239. // 先检查mark是否是一致的,如果不一致,就不需要处理了
  240. const s1 = _.head(e);
  241. const s2 = _.last(e);
  242. if (s1.mark !== s2.mark) continue;
  243. // 获取最后一个节点,为了下面计算新节点的pos
  244. const last = _.last(thisLevelNodes);
  245. const groupLast = _.last(e);
  246. const mark = `${_.get(groupLast, 'mark')}l`;
  247. const pos = _.get(last, 'pos') + 1;
  248. const level = startLevel;
  249. // 这地方应该生成的是
  250. const obj1 = this.getNode(level, pos, mark);
  251. const obj2 = this.getNode(level, pos + 1, mark);
  252. thisLevelNodes.push(obj1, obj2);
  253. }
  254. nodes.push(...thisLevelNodes);
  255. edges.push(...thisLevelEdges);
  256. if (thisLevelNodes.length > 0) {
  257. const { nodes: sn, edges: se } = this.nextProcess(startLevel + 1, nodes, edges);
  258. nodes = sn;
  259. edges = se;
  260. }
  261. return { nodes, edges };
  262. }
  263. getNode(level, pos, mark, name = '选手') {
  264. const id = `${mark ? `${mark}-` : ''}${level}-${pos}`;
  265. const nName = `${name}${id}`;
  266. return { id, name: nName, level, pos, mark };
  267. }
  268. getEdge(source, target) {
  269. return { source, target };
  270. }
  271. async getPersonNumber({ match_id, group_id, project_id }) {
  272. const list = await this.matchGroupModel.find({ match_id, group_id, project_id }, { rise: 1 });
  273. const number = list.reduce((p, n) => p + parseInt(n.rise) || 0, 0);
  274. return number;
  275. }
  276. /**
  277. * 返回按比赛结果,将各个小组晋级的选手及成绩
  278. * @param {Array} list 某项目的所有比赛
  279. * @return {Array} {team_id:[{player,win,score}]}
  280. */
  281. async groupRanking(list) {
  282. list = JSON.parse(JSON.stringify(list));
  283. // 先看每场比赛有没有胜者,有胜者就过,没有就设置下
  284. list = list.map(i => {
  285. const { winner, player_one, player_one_score, player_two, player_two_score } = i;
  286. if (winner) return i;
  287. if (player_one_score > player_two_score) i.winner = player_one;
  288. else i.winner = player_two;
  289. return i;
  290. });
  291. list = _.groupBy(list, 'team_id');
  292. for (const team_id in list) {
  293. const groups = list[team_id];
  294. // 过滤出选手
  295. let players = [];
  296. const p1s = groups.map(i => i.player_one);
  297. const p2s = groups.map(i => i.player_two);
  298. players.push(...p1s, ...p2s);
  299. players = _.uniq(players);
  300. // 转换成object[], player:选手; win: 胜场; score:净胜球
  301. players = players.map(i => ({ player: i, win: 0, score: 0 }));
  302. for (const g of groups) {
  303. const { winner, player_one, player_one_score, player_two, player_two_score } = g;
  304. const p1RoundScore = this.getScore(player_one_score, player_two_score);
  305. const p2RoundScore = this.getScore(player_two_score, player_one_score);
  306. const p1 = players.find(f => f.player === player_one);
  307. const p2 = players.find(f => f.player === player_two);
  308. // p1,p2谁没有就不行
  309. if (!p1 || !p2) continue;
  310. p1.score = p1.score + p1RoundScore;
  311. p2.score = p2.score + p2RoundScore;
  312. if (winner === player_one) p1.win += 1;
  313. else p2.win += 1;
  314. }
  315. players = _.orderBy(players, [ 'win', 'score' ], [ 'desc', 'desc' ]);
  316. const team = await this.ctx.model.Race.MatchTeamGroup.findById(team_id);
  317. if (!team) continue;
  318. const { rise } = team;
  319. const toNext = _.head(_.chunk(players, rise));
  320. list[team_id] = toNext;
  321. }
  322. return list;
  323. }
  324. /**
  325. * 计算净胜分
  326. * @param {String|Number} s1 分数1
  327. * @param {String|Number} s2 分数2
  328. * @return {Number} 净胜分
  329. */
  330. getScore(s1, s2) {
  331. return parseInt(s1) - parseInt(s2);
  332. }
  333. /**
  334. * 计算比赛轮数
  335. * @param {Number} number 淘汰赛总人数
  336. * @return {Number} 比赛轮数
  337. */
  338. getLevel(number) {
  339. let i = 1;
  340. let stop = false;
  341. while (!stop) {
  342. const num = 2 ** i;
  343. if (num === number) stop = true;
  344. else if (i > 10) stop = true;
  345. else i++;
  346. }
  347. if (i > 10) throw new BusinessError(ErrorCode.SERVICE_FAULT, '人数过多,请使用小组赛+淘汰赛赛制');
  348. return i;
  349. }
  350. async fetch(filter, { sort, desc, projection } = {}) {
  351. assert(filter);
  352. filter = await this.beforeFetch(filter);
  353. const { _id, id } = filter;
  354. if (_id || id) filter = { _id: ObjectId(_id || id) };
  355. // 处理排序
  356. if (sort && _.isString(sort)) {
  357. sort = { [sort]: desc ? -1 : 1 };
  358. } else if (sort && _.isArray(sort)) {
  359. sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  360. }
  361. // 过滤出ref字段
  362. const { populate } = this.getRefMods();
  363. let res = await this.model.findOne(filter, projection).populate(populate).exec();
  364. res = JSON.parse(JSON.stringify(res));
  365. res = await this.getOtherName(res);
  366. res = await this.getPlayerName(res);
  367. res = await this.afterFetch(filter, res);
  368. return res;
  369. }
  370. // 换选手名称
  371. async getPlayerName(data) {
  372. if (!data) return data;
  373. data = JSON.parse(JSON.stringify(data));
  374. const { player_type, player_one, player_two } = data;
  375. if (!player_type) return data;
  376. // 人处理
  377. if (player_type === 'Race.User') {
  378. if (player_one) {
  379. const p1 = await this.userModel.findById(player_one, { user_id: 1 }).populate({ path: 'user_id', model: this.baseUserModel, select: 'name' });
  380. data.player_one_name = _.get(p1, 'user_id.name');
  381. }
  382. if (player_two) {
  383. const p2 = await this.userModel.findById(player_two, { user_id: 1 }).populate({ path: 'user_id', model: this.baseUserModel, select: 'name' });
  384. data.player_two_name = _.get(p2, 'user_id.name');
  385. }
  386. } else if (player_type === 'Race.TeamApply') {
  387. if (player_one) {
  388. const p1 = await this.teamApplyModel.findById(player_one, { one_member_name: 1, two_member_name: 1 });
  389. data.player_one_name = `${_.get(p1, 'one_member_name')}-${_.get(p1, 'two_member_name')}`;
  390. }
  391. if (player_two) {
  392. const p2 = await this.teamApplyModel.findById(player_two, { one_member_name: 1, two_member_name: 1 });
  393. data.player_two_name = `${_.get(p2, 'one_member_name')}-${_.get(p2, 'two_member_name')}`;
  394. }
  395. }
  396. return data;
  397. }
  398. // 获取其他名称
  399. async getOtherName(data) {
  400. if (!data) return data;
  401. data = JSON.parse(JSON.stringify(data));
  402. const { referee_id, match_id, group_id, project_id, address_id } = data;
  403. if (match_id) {
  404. data.match_id_name = _.get(data, 'match_id.name');
  405. data.match_id = _.get(data, 'match_id._id');
  406. }
  407. if (group_id) {
  408. data.group_id_name = _.get(data, 'group_id.name');
  409. data.group_id = _.get(data, 'group_id._id');
  410. }
  411. if (project_id) {
  412. data.project_id_name = _.get(data, 'project_id.name');
  413. data.project_id = _.get(data, 'project_id._id');
  414. }
  415. if (address_id) {
  416. data.address_id_name = _.get(data, 'address_id.name');
  417. data.address_id = _.get(data, 'address_id._id');
  418. }
  419. if (referee_id) {
  420. data.referee_id_name = _.get(data, 'referee_id.user_id.name');
  421. data.referee_id = _.get(data, 'referee_id._id');
  422. }
  423. return data;
  424. }
  425. async afterQuery(filter, data) {
  426. const arr = [];
  427. for (let d of data) {
  428. d = await this.getOtherName(d);
  429. d = await this.getPlayerName(d);
  430. arr.push(d);
  431. }
  432. return arr;
  433. }
  434. // 如果打完比赛,则自动推进
  435. async afterUpdate(filter, update, data) {
  436. const { status } = data;
  437. if (status === '2') await this.toNextRace(data);
  438. const { _id } = data;
  439. const nd = await this.fetch({ _id });
  440. return nd;
  441. }
  442. // 自动推进至下一场
  443. async toNextRace(data) {
  444. const { match_id, group_id, project_id, player_one, player_one_score, player_one_node, player_two, player_two_node, player_two_score } = data;
  445. const number = await this.getPersonNumber({ match_id, group_id, project_id });
  446. const chartLevel = this.getLevel(number);
  447. const chartData = this.getChartData(chartLevel);
  448. // 通过整图关系,找到下一步
  449. const { edges, nodes } = chartData;
  450. let winner = _.get(data, 'winner');
  451. let looser;
  452. let looserNode;
  453. // 找到败者
  454. if (!winner) {
  455. if (player_one_score > player_two_score) winner = player_one;
  456. else winner = player_two;
  457. }
  458. // 设置败者并获取败者节点
  459. if (winner === player_one) {
  460. looser = player_two;
  461. looserNode = nodes.find(f => f.id === player_two_node);
  462. } else {
  463. looser = player_one;
  464. looserNode = nodes.find(f => f.id === player_one_node);
  465. }
  466. const looserNextNodeMark = `${looserNode.mark || ''}l`;
  467. // 通过边关系找到下一节点
  468. const list = edges.filter(f => f.source === player_one_node || f.source === player_two_node);
  469. const getTarget = nodeEdge => _.get(nodeEdge, 'target');
  470. if (getTarget(_.head(list)) !== getTarget(_.last(list))) throw new BusinessError(ErrorCode.DATA_INVALID, '两个选手的图位置错误');
  471. // 确定了胜者晋级的节点id
  472. const target = getTarget(_.head(list));
  473. const node = nodes.find(f => f.id === target);
  474. const { id } = node;
  475. const wQeury = {
  476. $or: [{ player_one_node: id }, { player_two_node: id }],
  477. };
  478. const wEData = await this.model.findOne(wQeury);
  479. if (!wEData) return data;
  480. if (id === wEData.player_one_node) wEData.player_one = winner;
  481. else if (id === wEData.player_two_node) wEData.player_two = winner;
  482. await wEData.save();
  483. // 再得出败者的节点id
  484. const { level } = node;
  485. let midArr = nodes.filter(f => f.level === level);
  486. // 先过滤掉胜者节点的mark类型
  487. midArr = midArr.filter(f => f.mark === looserNextNodeMark);
  488. console.log(midArr);
  489. // 再依次找,哪个节点没有人,没有人就安排上
  490. midArr = _.orderBy(midArr, [ 'pos' ], [ 'asc' ]);
  491. for (const n of midArr) {
  492. const { id } = n;
  493. const query = {
  494. $or: [{ player_one_node: id }, { player_two_node: id }],
  495. };
  496. const eliminate = await this.model.findOne(query);
  497. if (!eliminate) continue;
  498. if (!ObjectId.isValid(eliminate.player_one)) eliminate.player_one = looser;
  499. else if (!ObjectId.isValid(eliminate.player_two)) eliminate.player_two = looser;
  500. else continue;
  501. await eliminate.save();
  502. break;
  503. }
  504. }
  505. async beforeQuery(filter) {
  506. const user_name = _.get(filter, 'user_name');
  507. let user_id = _.get(filter, 'user_id');
  508. // 没有user_id就不需要处理查询条件
  509. if (!user_id && !user_name) return filter;
  510. const usualCondition = _.pick(filter, [ 'match_id', 'group_id', 'project_id' ]);
  511. if (user_name) {
  512. // 要先找比赛用户模块的数据id
  513. const baseUser = await this.baseUserModel.findOne({ name: new RegExp(user_name) }, { _id: 1 });
  514. if (!baseUser) delete filter.user_name;
  515. delete filter.user_name;
  516. const baseUserId = baseUser._id;
  517. const raceUser = await this.userModel.findOne({ user_id: baseUserId, type: '0' });
  518. if (raceUser) user_id = raceUser._id;
  519. }
  520. if (user_id) {
  521. // 需要查:该用户是 单打 和 双打 的情况
  522. // 单打,直接user_id 为player_one/two 就可以,双打需要查teamApply
  523. // 所以先把user_id添加进查询范围里
  524. let probablyList = [ user_id ];
  525. // 尽可能的缩小查询范围
  526. // 接着找组队申请中和该用户有关的人
  527. const teamApplyList = await this.teamApplyModel.find({ ...usualCondition, status: '1', $or: [{ one_member_id: user_id }, { two_member_id: user_id }] }, { _id: 1 });
  528. const teamApplyIds = teamApplyList.map(i => i._id);
  529. probablyList.push(...teamApplyIds);
  530. // 删除user_id.会造成错误
  531. delete filter.user_id;
  532. // 添加该用户正确的范围条件
  533. probablyList = probablyList.map(i => ObjectId(i).toString());
  534. filter.$or = [{ player_one: { $in: probablyList } }, { player_two: { $in: probablyList } }];
  535. }
  536. return filter;
  537. }
  538. }
  539. module.exports = EliminateService;