zs 2 år sedan
förälder
incheckning
bb96846abf

+ 4 - 9
src/controller/team.controller.ts

@@ -14,7 +14,6 @@ import {
   CDTO_team,
   CVO_team,
   FVO_team,
-  QDTO_team,
   QVO_team,
   UDTO_team,
   UVAO_team,
@@ -38,18 +37,14 @@ export class TeamController extends BaseController {
   @Get('/')
   @ApiQuery({ name: 'query' })
   @ApiResponse({ type: QVO_team })
-  async query(
-    @Query() filter: QDTO_team,
-    @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.data) {
       const newData = new QVO_team(i);
       data.push(newData);
     }
-    const total = await this.service.count(filter);
+    const total = list.total;
     return { data, total };
   }
 

+ 11 - 1
src/controller/teamApply.controller.ts

@@ -31,6 +31,8 @@ export class TeamApplyController extends BaseController {
   @Validate()
   @ApiResponse({ type: CVO_teamApply })
   async create(@Body() data: CDTO_teamApply) {
+    // 检查账号
+    await this.service.checkApply(data);
     const dbData = await this.service.create(data);
     const result = new CVO_teamApply(dbData);
     return result;
@@ -65,10 +67,18 @@ export class TeamApplyController extends BaseController {
   @Validate()
   @ApiResponse({ type: UVAO_teamApply })
   async update(@Param('id') id: string, @Body() body: UDTO_teamApply) {
-    const result = await this.service.updateOne(id, body);
+    const result = await this.service.update(id, body);
     return result;
   }
 
+  @Get('/out')
+  @ApiQuery({ name: 'query' })
+  @ApiResponse({ type: FVO_teamApply })
+  async out(@Query() query: any) {
+    await this.service.out(query);
+    return 'ok';
+  }
+
   @Del('/:id')
   @Validate()
   async delete(@Param('id') id: string) {

+ 9 - 0
src/service/team.service.ts

@@ -8,4 +8,13 @@ type modelType = ReturnModelType<typeof Team>;
 export class TeamService extends BaseService<modelType> {
   @InjectEntityModel(Team)
   model: modelType;
+
+  async specialQuery(filter) {
+    const { skip = 0, limit = 0, ...info } = filter;
+    if (info.user) info.member = { $elemMatch: { _id: info.user } };
+    delete info.user;
+    const data: any = await this.model.find(info).skip(skip).limit(limit);
+    const total = await this.model.count(info);
+    return { data, total };
+  }
 }

+ 69 - 1
src/service/teamApply.service.ts

@@ -1,11 +1,79 @@
 import { Provide } from '@midwayjs/decorator';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
-import { BaseService } from 'free-midway-component';
 import { TeamApply } from '../entity/teamApply.entity';
+import {
+  BaseService,
+  FrameworkErrorEnum,
+  ServiceError,
+} from 'free-midway-component';
+import { CDTO_teamApply } from '../interface/teamApply.interface';
+import { QVO_user } from '../interface/user.interface';
+import { Team } from '../entity/team.entity';
+import { User } from '../entity/user.entity';
 type modelType = ReturnModelType<typeof TeamApply>;
 @Provide()
 export class TeamApplyService extends BaseService<modelType> {
   @InjectEntityModel(TeamApply)
   model: modelType;
+
+  @InjectEntityModel(Team)
+  TeamModel: ReturnModelType<typeof Team>;
+
+  @InjectEntityModel(User)
+  UserModel: ReturnModelType<typeof User>;
+
+  /**
+   * 检查是否有申请记录
+   * @param data 参数
+   */
+  async checkApply(data: CDTO_teamApply) {
+    if (data.apply_id) {
+      // 检查申请记录
+      const apply = await this.model.count({
+        apply_id: data.apply_id,
+        status: '0',
+      });
+      if (apply > 0) {
+        throw new ServiceError(
+          '已有申请记录 请等待团队管理人员处理!',
+          FrameworkErrorEnum.BAD_BODY
+        );
+      }
+    }
+  }
+  // 修改
+  async update(id, body) {
+    const { status } = body;
+    // 查询报名申请信息
+    const teamApply = await this.model.findById(id);
+    // 查询个人信息
+    let user = await this.UserModel.findById(teamApply.apply_id);
+    if (user) user = JSON.parse(JSON.stringify(user));
+    const newData: any = new QVO_user(user);
+    delete newData.password;
+    const team: any = await this.TeamModel.findById(teamApply.team_id);
+    const memberList = team.member;
+    if (status === '1') {
+      memberList.push(newData);
+      const number = memberList.length.toString();
+      await this.TeamModel.updateOne(
+        { _id: team._id },
+        { member: memberList, number }
+      );
+    } else {
+      const member = memberList.filter(i => i._id !== newData._id);
+      const number = member.length.toString();
+      await this.TeamModel.updateOne({ _id: team._id }, { member, number });
+    }
+    await this.model.updateOne({ _id: id }, body);
+  }
+  // 退出团队删除记录
+  async out(query) {
+    const { apply_id } = query;
+    const res: any = await this.model.find({ apply_id });
+    for (const val of res) {
+      await this.model.deleteOne(val._id);
+    }
+  }
 }