const router = require('koa-router')();
const initCtx = require('../../util/initCtx');
const Team = require('../../models/team');
const Competition = require('../../models/competition');
const TeamUser = require('../../models/teamUser');
const User = require('../../models/user');
// const { chTeam } = require('../../util/checkParams');
const codeList = require('../../enum/codeList');
// 新增团队
router.post('/addTeam', async ctx => {
const { teamName, managerId, membersId, teacherId, teamState, joinCondition, competitionId } = ctx.request.body;
const team = new Team({
teamName,
managerId,
teacherId,
teamState,
joinCondition,
competitionId
})
// const check = new chTeam(ctx, teamName);
// check.chTeamFun();
const data = await Team.find({'teamName': teamName})
if(data.length) {
new initCtx(ctx).fail('团队名称已经存在', 200, codeList.repeat);
} else {
const teamInfo = await team.save()
// 团队添加成功后,向团队成员表中依次填入负责人和团队成员
const teamUserList = [];
const teamId = teamInfo._id;
// 添加负责人
teamUserList.push({
teamId,
numberId: managerId,
isManager: true,
competitionId
})
if(membersId && membersId.length) {
membersId.forEach( item => {
teamUserList.push({
teamId,
numberId: item,
isManager: false,
competitionId
})
})
}
// 将数据加入数据库中
await TeamUser.insertMany(teamUserList).then( res => {
new initCtx(ctx, '添加团队成功').success();
})
.catch( err => {
console.log(err);
new initCtx(ctx).fail('添加团队失败', 500);
})
}
})
// 获取团队列表
router.get('/queryTeamList', async ctx => {
const { pageNum, pageCount, keyword, competitionId } = ctx.query;
let listdata = [];
if(pageNum == -1 && pageCount == -1) {
listdata = await Team.find({"teamName" : {$regex: keyword ? keyword : ''}, "competitionId" : competitionId})
.sort({'createTime': -1})
} else {
listdata = await Team.find({"teamName" : {$regex: keyword ? keyword : ''}, "competitionId" : competitionId})
.sort({'createTime': -1})
.skip((pageCount-1)*pageNum)
.limit(Number(pageNum));
}
new initCtx(ctx, 'SUCCESS', listdata).success();
})
// 根据teamId获取单个团队信息
router.get('/getTeamInfo/:teamId', async ctx => {
const teamId = ctx.params.teamId;
const teamInfo = await Team.findOne({_id: teamId});
let membersInfo = [];
let teacherInfo = {};
if(teamInfo) {
teacherInfo = await User.findOne({numberId: teamInfo.teacherId})
const memberInfo = await TeamUser.find({
competitionId: teamInfo.competitionId,
teamId: teamInfo._id,
isManager: false
})
for(let i = 0; i < memberInfo.length; i++) {
const userInfo = await User.find({numberId: memberInfo[i].numberId});
membersInfo = membersInfo.concat(userInfo)
}
}
new initCtx(ctx, 'SUCCESS', { ...teamInfo._doc, membersInfo, teacherInfo}).success()
})
// 用户获取某一赛事中我的团队
router.get('/getMyTeamInfo', async ctx => {
const { teamId, numberId, competitionId } = ctx.query;
const teamUserInfo = await TeamUser.findOne({teamId : {$regex: teamId ? teamId : ''}, numberId, competitionId});
let myTeamInfo = {};
if(teamUserInfo) {
const teamId = teamUserInfo.teamId
// console.log(teamId);
myTeamInfo = await Team.findOne({_id: teamId});
}
new initCtx(ctx, 'SUCCESS', myTeamInfo).success();
})
// 根据赛事名称获取团队列表
router.get('/getTeamListByCompName', async ctx => {
const { pageNum, pageCount, keyword, compName } = ctx.query;
const comp = await Competition.findOne({name : compName});
let teamList = [];
if(comp) {
teamList = await Team.find({competitionId : {$in : comp['_id']}, "teamName" : {$regex: keyword ? keyword : ''}})
.sort({'createTime': -1})
.skip((pageCount-1)*pageNum)
.limit(Number(pageNum));
}
new initCtx(ctx, 'SUCCESS', teamList).success()
})
// 根据teamName修改某个团队信息
router.put('/updateTeamInfo/:teamName', async ctx => {
const teamName = ctx.params.teamName;
const { teacherId, joinCondition, teamState, membersId, teamId, competitionId } = ctx.request.body;
await Team.updateOne({teamName}, { teacherId, joinCondition, teamState })
if(membersId && membersId.length) {
for(let i = 0; i < membersId.length; i++) {
const newMember = {
teamId,
numberId: membersId[i],
isManager: false,
competitionId
};
const result = await TeamUser.findOne(newMember)
if(!result) {
await (new TeamUser(newMember)).save()
}
}
}
new initCtx(ctx, '修改团队信息成功').success();
})
// 管理员根据teamName删除某团队
router.delete('/delTeam/:teamName', async (ctx) => {
const teamName = ctx.params.teamName;
await Team.deleteOne({teamName}).then( res => {
new initCtx(ctx, '删除成功').success()
}).catch( err => {
console.log(err);
new initCtx(ctx).fail('团队删除失败,请重试', 500);
})
})
module.exports = router.routes();
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 25 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/e908d3f559a440a89ffab26bc7ee7a71_weixin_38343072.jpg!1)
马coder
- 粉丝: 1218
- 资源: 6602
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)