zs 2 anni fa
parent
commit
7807f86478

+ 7 - 9
src/controller/application.controller.ts

@@ -14,7 +14,7 @@ import {
   CDTO_application,
   CVO_application,
   FVO_application,
-  QDTO_application,
+  // QDTO_application,
   QVO_application,
   UDTO_application,
   UVAO_application,
@@ -31,6 +31,8 @@ export class ApplicationController extends BaseController {
   @Validate()
   @ApiResponse({ type: CVO_application })
   async create(@Body() data: CDTO_application) {
+    // 检查申请
+    await this.service.checkApply(data);
     const dbData = await this.service.create(data);
     const result = new CVO_application(dbData);
     return result;
@@ -38,18 +40,14 @@ export class ApplicationController extends BaseController {
   @Get('/')
   @ApiQuery({ name: 'query' })
   @ApiResponse({ type: QVO_application })
-  async query(
-    @Query() filter: QDTO_application,
-    @Query('skip') skip: number,
-    @Query('limit') limit: number
-  ) {
-    const list = await this.service.query(filter, { skip, limit });
+  async query(@Query() filter: any) {
+    const list = await this.service.specialQuery(filter);
     const data = [];
-    for (const i of list) {
+    for (const i of list.list) {
       const newData = new QVO_application(i);
       data.push(newData);
     }
-    const total = await this.service.count(filter);
+    const total = list.total;
     return { data, total };
   }
 

+ 7 - 0
src/controller/course.controller.ts

@@ -53,6 +53,13 @@ export class CourseController extends BaseController {
     return { data, total };
   }
 
+  @Get('/ranking')
+  @ApiQuery({ name: 'query' })
+  async rank(@Query() filter: any) {
+    const result = await this.service.rankQuery(filter);
+    return result;
+  }
+
   @Get('/:id')
   @ApiResponse({ type: FVO_course })
   async fetch(@Param('id') id: string) {

+ 79 - 1
src/service/application.service.ts

@@ -1,11 +1,89 @@
 import { Provide } from '@midwayjs/decorator';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
-import { BaseService } from 'free-midway-component';
+import {
+  BaseService,
+  FrameworkErrorEnum,
+  ServiceError,
+} from 'free-midway-component';
 import { Application } from '../entity/application.entity';
+import { CDTO_application } from '../interface/application.interface';
+import { Match } from '../entity/match.entity';
+import { User } from '../entity/user.entity';
+import { Team } from '../entity/team.entity';
+const moment = require('moment');
 type modelType = ReturnModelType<typeof Application>;
 @Provide()
 export class ApplicationService extends BaseService<modelType> {
   @InjectEntityModel(Application)
   model: modelType;
+
+  @InjectEntityModel(Match)
+  MatchModel: ReturnModelType<typeof Match>;
+
+  @InjectEntityModel(User)
+  UserModel: ReturnModelType<typeof User>;
+
+  @InjectEntityModel(Team)
+  TeamModel: ReturnModelType<typeof Team>;
+  /**
+   * 检查是否有申请记录
+   * @param data 参数
+   */
+  async checkApply(data: CDTO_application) {
+    const res: any = await this.MatchModel.findById(data.match_id);
+    if (res) {
+      const flag = moment(moment().format('YYYY-MM-DD HH:mm:ss')).isBefore(
+        res.sign_deadline
+      );
+      if (!flag) {
+        throw new ServiceError(
+          '报名时间已过 无法报名!',
+          FrameworkErrorEnum.BAD_BODY
+        );
+      }
+    }
+    if (data.team_id) {
+      // 检查申请记录
+      const apply = await this.model.count({
+        match_id: data.match_id,
+        team_id: data.team_id,
+      });
+      if (apply > 0) {
+        throw new ServiceError(
+          '已有申请记录 请等待管理人员处理!',
+          FrameworkErrorEnum.BAD_BODY
+        );
+      }
+    }
+  }
+
+  async specialQuery(filter) {
+    const { skip = 0, limit = 0, ...info } = filter;
+    if (info.user) {
+      const user = await this.UserModel.findById(info.user);
+      if (user.type === '0') {
+        info.user_id = { $elemMatch: { _id: info.user } };
+        delete info.user;
+        const list: any = await this.model.find(info).skip(skip).limit(limit);
+        const total = await this.model.count(info);
+        return { list, total };
+      } else if (user.type === '1') {
+        const list = [];
+        const user_id = info.user;
+        delete info.user;
+        const data: any = await this.model.find(info).skip(skip).limit(limit);
+        for (const val of data) {
+          const team = await this.TeamModel.findById(val.team_id);
+          if (team.administrator === user_id) list.push(val);
+        }
+        const total = list.length;
+        return { list, total };
+      }
+    } else {
+      const list: any = await this.model.find(info).skip(skip).limit(limit);
+      const total = await this.model.count(info);
+      return { list, total };
+    }
+  }
 }

+ 68 - 0
src/service/course.service.ts

@@ -4,8 +4,76 @@ import { ReturnModelType } from '@typegoose/typegoose';
 import { BaseService } from 'free-midway-component';
 import { Course } from '../entity/course.entity';
 type modelType = ReturnModelType<typeof Course>;
+import _ = require('lodash');
 @Provide()
 export class CourseService extends BaseService<modelType> {
   @InjectEntityModel(Course)
   model: modelType;
+
+  // 排名
+  async rankQuery(filter) {
+    const { match_id } = filter;
+    const projecList = ['team_id', 'team_name', 'score'];
+    const groups = ['red', 'blue'];
+    const projection = {};
+    for (const g of groups) {
+      for (const prop of projecList) {
+        projection[`${g}_${prop}`] = 1;
+      }
+    }
+    // 查出所有的比赛
+    const list: any = await this.model.find({ match_id }, projection);
+    // 统计出所有队伍(去重)
+    const rts = list.map(f => f.red_team_id);
+    const bts = list.map(f => f.blue_team_id);
+    const teamList = _.uniq(_.compact([...rts, ...bts]));
+    const rList = [];
+    for (const team_id of teamList) {
+      // 获得这支队伍在所有比赛中的胜负情况及积分
+      const res = this.computedTeam(list, team_id);
+      const info = this.getOtherInfoFromList(list, team_id);
+      rList.push({ ...info, ...res });
+    }
+    const scoreList = _.orderBy(rList, 'score', ['desc']);
+    return { scoreList };
+  }
+  /**
+   * 根据队伍id,算出在范围内队伍的分数
+   * @param {Array} list 比赛列表
+   * @param {String} team_id 队伍id
+   */
+  computedTeam(list, team_id) {
+    let score = 0; // 分数
+    for (const match of list) {
+      const { red_team_id, blue_team_id, red_score, blue_score } = match;
+      if (!red_score && !blue_score) continue;
+      let teamInMatch = 'blue';
+      if (red_team_id === team_id) teamInMatch = 'red';
+      else if (blue_team_id === team_id) teamInMatch = 'blue';
+      else {
+        // 该队伍不是红蓝任意一方,说明不是这的,下一场
+        continue;
+      }
+      // 加分数
+      score += parseInt(match[`${teamInMatch}_score`]) || 0;
+    }
+    return { score };
+  }
+  /**
+   * 根据比赛列表获取队伍信息
+   * @param {Array} list 比赛列表
+   * @param {String} team_id 队伍id
+   */
+  getOtherInfoFromList(list, team_id) {
+    const match = list.find(
+      f => f.red_team_id === team_id || f.blue_team_id === team_id
+    );
+    let name = '';
+    if (match) {
+      const { red_team_id } = match;
+      if (red_team_id === team_id) name = match.red_team_name;
+      else name = match.blue_team_name;
+    }
+    return { name };
+  }
 }