'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); // class MatchService extends CrudService { constructor(ctx) { super(ctx, 'match'); this.model = this.ctx.model.Race.Match; this.matchGroupModel = this.ctx.model.Race.MatchGroup; this.matchProjectModel = this.ctx.model.Race.MatchProject; this.matchSignModel = this.ctx.model.Race.MatchSign; } /** * 查询这个赛事下所有的东西 * @param {Object} params 地址栏路径参数 */ async getAll({ id }) { let data = await this.model.findById(id); if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到赛事信息'); data = JSON.parse(JSON.stringify(data)); const match_id = data._id; let groups = await this.matchGroupModel.find({ match_id }); groups = JSON.parse(JSON.stringify(groups)); for (const group of groups) { const { _id: group_id } = group; let projects = await this.matchProjectModel.find({ match_id, group_id }); projects = JSON.parse(JSON.stringify(projects)); for (const project of projects) { const { _id: project_id } = project; let signs = await this.matchSignModel.find({ match_id, group_id, project_id }).populate({ path: 'user_id', model: this.ctx.model.Race.User, populate: { path: 'user_id', model: this.ctx.model.Base.User, }, }); signs = JSON.parse(JSON.stringify(signs)); const users = []; for (const sign of signs) { const userObj = _.get(sign, 'user_id.user_id'); const user = _.pick(userObj, [ 'icon', 'name' ]); user._id = _.get(sign, 'user_id._id'); users.push(user); } project.user = users; } group.project = projects; } data.group = groups; return data; } async getSchedule(query) { const groupRace = await this.ctx.service.matchSmallGroupSchedule.query(query); const eliminateRace = await this.ctx.service.eliminate.query(query); return [ ...groupRace, ...eliminateRace ]; } } module.exports = MatchService;