match.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // 基础模块 user表文档
  23. const conn = this.app.mongooseDB.get('base');
  24. const schema = _.get(this.ctx.model, 'Base.User.schema');
  25. const m = conn.model('User', schema);
  26. data = JSON.parse(JSON.stringify(data));
  27. const match_id = data._id;
  28. let groups = await this.matchGroupModel.find({ match_id });
  29. groups = JSON.parse(JSON.stringify(groups));
  30. for (const group of groups) {
  31. const { _id: group_id } = group;
  32. let projects = await this.matchProjectModel.find({ match_id, group_id });
  33. projects = JSON.parse(JSON.stringify(projects));
  34. for (const project of projects) {
  35. const { _id: project_id } = project;
  36. let signs = await this.matchSignModel.find({ match_id, group_id, project_id }).populate({
  37. path: 'user_id',
  38. populate: {
  39. path: 'user_id',
  40. model: m,
  41. },
  42. });
  43. signs = JSON.parse(JSON.stringify(signs));
  44. const users = [];
  45. for (const sign of signs) {
  46. const userObj = _.get(sign, 'user_id.user_id');
  47. const user = _.pick(userObj, [ 'icon', 'name' ]);
  48. user._id = _.get(sign, 'user_id._id');
  49. users.push(user);
  50. }
  51. project.user = users;
  52. }
  53. group.project = projects;
  54. }
  55. data.group = groups;
  56. return data;
  57. }
  58. }
  59. module.exports = MatchService;