match.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. //
  7. class MatchService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'match');
  10. this.model = this.ctx.model.Race.Match;
  11. this.matchGroupModel = this.ctx.model.Race.MatchGroup;
  12. this.matchProjectModel = this.ctx.model.Race.MatchProject;
  13. this.matchSignModel = this.ctx.model.Race.MatchSign;
  14. }
  15. /**
  16. * 查询这个赛事下所有的东西
  17. * @param {Object} params 地址栏路径参数
  18. */
  19. async getAll({ id }) {
  20. let data = await this.model.findById(id);
  21. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到赛事信息');
  22. data = JSON.parse(JSON.stringify(data));
  23. const match_id = data._id;
  24. let groups = await this.matchGroupModel.find({ match_id });
  25. groups = JSON.parse(JSON.stringify(groups));
  26. for (const group of groups) {
  27. const { _id: group_id } = group;
  28. let projects = await this.matchProjectModel.find({ match_id, group_id });
  29. projects = JSON.parse(JSON.stringify(projects));
  30. for (const project of projects) {
  31. const { _id: project_id } = project;
  32. let signs = await this.matchSignModel.find({ match_id, group_id, project_id }).populate({
  33. path: 'user_id',
  34. model: this.ctx.model.Race.User,
  35. populate: {
  36. path: 'user_id',
  37. model: this.ctx.model.Base.User,
  38. },
  39. });
  40. signs = JSON.parse(JSON.stringify(signs));
  41. const users = [];
  42. for (const sign of signs) {
  43. const userObj = _.get(sign, 'user_id.user_id');
  44. const user = _.pick(userObj, [ 'icon', 'name' ]);
  45. user._id = _.get(sign, 'user_id._id');
  46. users.push(user);
  47. }
  48. project.user = users;
  49. }
  50. group.project = projects;
  51. }
  52. data.group = groups;
  53. return data;
  54. }
  55. async getSchedule(query) {
  56. const groupRace = await this.ctx.service.matchSmallGroupSchedule.query(query);
  57. const eliminateRace = await this.ctx.service.eliminate.query(query);
  58. return [ ...groupRace, ...eliminateRace ];
  59. }
  60. }
  61. module.exports = MatchService;