|
@@ -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);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|