lrf 2 years ago
parent
commit
bd744af62b

+ 45 - 0
app/controller/config/.matchTeamGroup.js

@@ -0,0 +1,45 @@
+module.exports = {
+  create: {
+    requestBody: ['!match_id', '!group_id', '!project_id', '!name', 'person_type', 'person', 'remark', '!rise'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['match_id', 'group_id', 'project_id', 'name', 'person_type', 'person', 'remark', 'rise'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        match_id: 'match_id',
+        group_id: 'group_id',
+        project_id: 'project_id',
+        name: 'name',
+        rise: 'rise',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+  auto: {
+    requestBody: ['!match_id', '!group_id', '!project_id', '!team_number'],
+  },
+};

+ 18 - 0
app/controller/matchTeamGroup.js

@@ -0,0 +1,18 @@
+'use strict';
+const meta = require('./config/.matchTeamGroup.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+//
+class MatchTeamGroupController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.matchTeamGroup;
+  }
+
+  async saveAll() {
+    const data = await this.service.saveAll();
+    this.ctx.ok({ data });
+  }
+}
+module.exports = CrudController(MatchTeamGroupController, meta);

+ 39 - 0
app/model/race/matchTeamGroup.js

@@ -0,0 +1,39 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+// 小组赛组设置
+const match_team_group = {
+  match_id: { type: String, required: true, zh: '赛事id', ref: 'Match', getProp: [ 'name' ] }, // 比赛信息中的名称
+  group_id: { type: String, required: true, zh: '组别id', ref: 'Group', getProp: [ 'name' ] }, // 赛事组别中的名称
+  project_id: { type: String, required: true, zh: '项目id', ref: 'Project', getProp: [ 'name' ] }, // 组内项目中的名称
+  name: { type: String, required: true, zh: '分组名称' }, //
+  person_type: { type: String, required: false, zh: '人员类型' }, // 单打:user;双打:teamApply
+  person: { type: Array, required: false, zh: '组内人员', refPath: 'person_type' }, //
+  remark: { type: String, required: false, zh: '备注' }, //
+  rise: { type: String, required: true, zh: '取前几' }, //
+};
+const schema = new Schema(match_team_group, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ match_id: 1 });
+schema.index({ group_id: 1 });
+schema.index({ project_id: 1 });
+schema.index({ name: 1 });
+schema.index({ rise: 1 });
+
+schema.plugin(metaPlugin);
+
+const source = 'race';
+module.exports = app => {
+  const is_multiple = app.mongooseDB.clients;
+  let model;
+  if (is_multiple) {
+    const conn = app.mongooseDB.get(source);
+    model = conn.model('MatchTeamGroup', schema, 'MatchTeamGroup');
+  } else {
+    const { mongoose } = app;
+    model = mongoose.model('MatchTeamGroup', schema, 'MatchTeamGroup');
+  }
+  return model;
+};

+ 1 - 0
app/router.js

@@ -31,4 +31,5 @@ module.exports = app => {
   require('./z_router/matchSign')(app); // 赛事报名
   require('./z_router/bill')(app); // 账单
   require('./z_router/payOrder')(app); // 支付订单
+  require('./z_router/matchTeamGroup')(app); // 小组赛设置
 };

+ 77 - 0
app/service/matchTeamGroup.js

@@ -0,0 +1,77 @@
+'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 MatchTeamGroupService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'matchteamgroup');
+    this.model = this.ctx.model.Race.MatchTeamGroup;
+    this.matchProjectModel = this.ctx.model.Race.MatchProject;
+    this.teamApplyModel = this.ctx.model.Race.TeamApply;
+    this.matchSignModel = this.ctx.model.Race.MatchSign;
+  }
+
+  async saveAll(data) {
+    for (const i of data) {
+      const { _id } = i;
+      if (!_id) {
+        // TODO: 创建前:需要检查这些人员是否出现在这个项目中的别的组中
+        await this.model.create(i);
+      } else {
+        await this.model.updateOne({ _id }, i);
+      }
+    }
+  }
+
+  async auto({ match_id, group_id, project_id, team_number = 0 }) {
+    // 找到比赛项目
+    const project = await this.matchProjectModel.findById(project_id);
+    if (!project) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到比赛项目');
+    const { type } = project;
+    // type 为 1 双打,是TeamApply表中的, type 为2 单打,是User表
+    let personList = [];
+    const obj = { match_id, group_id, project_id };
+    if (type === '1') {
+      // 双打,找到这个项目所有的申请,且审核通过的
+      obj.person_type = 'TeamApply';
+      const teamApplys = await this.teamApplyModel.find({ project_id, pay_status: '1' });
+      personList = JSON.parse(JSON.stringify(teamApplys));
+    } else {
+      // 单打,找到这个项目所有申请报名的信息
+      obj.person_type = 'User';
+      const sign = await this.matchSignModel.find({ project_id, pay_status: '1' });
+      personList = JSON.parse(JSON.stringify(sign));
+    }
+    const returnData = [];
+    // 选手数检测
+    const number = _.floor(_.divide(personList.length, team_number));
+    if (number <= 0) throw new BusinessError(ErrorCode.DATA_INVALID, '选手不足以分组,要分组的总人数 至少为 要分的组数');
+    // 选手列表分组
+    const teams = _.chunk(personList, number);
+    // 获取 选手被均分到每个组后的余数
+    const el = personList.length % team_number;
+    // 最后余下的组数据
+    let last = [];
+    if (el !== 0) {
+      // 如果有剩余不能被均分的选手,则将这些选手 放到 last数组中,再将多出来的这个去掉
+      last = _.last(teams);
+      teams.pop();
+    }
+    // 将正常均分的小组先分出来
+    for (let i = 0; i < teams.length; i++) {
+      const t = { name: `小组${i + 1}`, person: teams[i], ...obj };
+      returnData.push(t);
+    }
+    // 循环剩下的选手, 依次放入每个已经分好的组中,尽可能均分人数
+    for (let i = 0; i < last.length; i++) {
+      returnData[i].person.push(last[i]);
+    }
+
+    return returnData;
+  }
+}
+
+module.exports = MatchTeamGroupService;

+ 21 - 0
app/z_router/matchTeamGroup.js

@@ -0,0 +1,21 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'matchTeamGroup';
+const ckey = 'matchTeamGroup';
+const keyZh = '小组赛设置';
+const routes = [
+  { method: 'post', path: `${rkey}/auto`, controller: `${ckey}.auto`, name: `${ckey}auto`, zh: `${keyZh}自动分组` },
+  { method: 'post', path: `${rkey}/saveAll`, controller: `${ckey}.saveAll`, name: `${ckey}saveAll`, zh: `${keyZh}全部保存` },
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
+  { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};