|
|
|
|
@ -1,7 +1,9 @@
|
|
|
|
|
// 引用用户模版数据
|
|
|
|
|
const TeamUser = require('../model/teamUser');
|
|
|
|
|
const User = require('../model/user');
|
|
|
|
|
|
|
|
|
|
const Course = require('../model/xah/course');
|
|
|
|
|
const Task = require('../model/xah/task');
|
|
|
|
|
const Work = require('../model/work');
|
|
|
|
|
const teamUserController = {
|
|
|
|
|
|
|
|
|
|
//新增团队成员
|
|
|
|
|
@ -45,7 +47,7 @@ const teamUserController = {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getTeamMembers: async function (req, res, next) {
|
|
|
|
|
try {
|
|
|
|
|
const teamId = req.query.teamId; // 从请求参数中获取团队ID
|
|
|
|
|
@ -126,9 +128,107 @@ const teamUserController = {
|
|
|
|
|
data: e
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
getFreeTime: async function (req, res, next) {
|
|
|
|
|
try {
|
|
|
|
|
const teamId = req.query.teamId; // 从请求参数中获取团队ID
|
|
|
|
|
const teamMembers = await TeamUser.getById('teamId', teamId);
|
|
|
|
|
if (teamMembers.length > 0) {
|
|
|
|
|
const userIds = teamMembers.map(member => member.userId);
|
|
|
|
|
const teamIds = new Set();
|
|
|
|
|
for (const userId of userIds) {
|
|
|
|
|
const TeamIdList = await TeamUser.getTeamIdByUserId('userId', userId);
|
|
|
|
|
for (const teamId of TeamIdList)
|
|
|
|
|
teamIds.add(teamId.teamId);
|
|
|
|
|
}
|
|
|
|
|
let timeList = [];
|
|
|
|
|
// 逐个查询course信息
|
|
|
|
|
for (const userId of userIds) {
|
|
|
|
|
const courseData = await Course.getById('userId', userId);
|
|
|
|
|
if (courseData.length > 0) {
|
|
|
|
|
//将courseData数组中的每个元素添加到timeList数组中
|
|
|
|
|
timeList.push(...courseData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 逐个查询task信息
|
|
|
|
|
for (const userId of userIds) {
|
|
|
|
|
const taskData = await Task.getById('userId', userId);
|
|
|
|
|
if (taskData.length > 0) {
|
|
|
|
|
timeList.push(...taskData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 查所有团队的work
|
|
|
|
|
for (const teamId of teamIds) {
|
|
|
|
|
const workData = await Work.getById('teamId', teamId);
|
|
|
|
|
if (workData.length > 0) {
|
|
|
|
|
timeList.push(...workData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const dateTimeMap = new Map();
|
|
|
|
|
for (const time of timeList) {
|
|
|
|
|
const date = time.startTime.split('T')[0];
|
|
|
|
|
if (!dateTimeMap[date]) {
|
|
|
|
|
dateTimeMap[date] = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dateTimeMap[date].push(time);
|
|
|
|
|
}
|
|
|
|
|
const freeTimeMap = {}; // 存放空闲时间数据的对象
|
|
|
|
|
|
|
|
|
|
for (const date in dateTimeMap) {
|
|
|
|
|
const workTimeRanges = dateTimeMap[date];
|
|
|
|
|
|
|
|
|
|
// 插入一个工作时间段,表示0点到7点的工作时间
|
|
|
|
|
workTimeRanges.push({startTime: date + 'T00:00:00.000', endTime: date+'T07:00:00.000'});
|
|
|
|
|
// 插入一个工作时间段,表示晚上11点到0点的工作时间
|
|
|
|
|
workTimeRanges.push({startTime: date + 'T23:00:00.000', endTime: date + 'T00:00:00.000'});
|
|
|
|
|
|
|
|
|
|
// 对工作时间段按开始时间进行排序
|
|
|
|
|
workTimeRanges.sort((a, b) => new Date(a.startTime) > new Date(b.startTime)? 1 : -1);
|
|
|
|
|
|
|
|
|
|
let remainingFreeTime = [];
|
|
|
|
|
|
|
|
|
|
// 计算空闲时间
|
|
|
|
|
workTimeRanges.forEach((workTime, index) => {
|
|
|
|
|
const nextWorkTime = workTimeRanges[index + 1];
|
|
|
|
|
if (nextWorkTime) {
|
|
|
|
|
const freeTime = {startTime: workTime.endTime, endTime: nextWorkTime.startTime};
|
|
|
|
|
if (freeTime.startTime < freeTime.endTime) {
|
|
|
|
|
remainingFreeTime.push(freeTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 如果还有剩余空闲时间,将其保存到freeTimeMap中
|
|
|
|
|
if (remainingFreeTime.length > 0) {
|
|
|
|
|
if (!freeTimeMap[date]) {
|
|
|
|
|
freeTimeMap[date] = [];
|
|
|
|
|
}
|
|
|
|
|
freeTimeMap[date] = freeTimeMap[date].concat(remainingFreeTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const freeTimeList = [];
|
|
|
|
|
|
|
|
|
|
for (const date in freeTimeMap) {
|
|
|
|
|
freeTimeList.push(...freeTimeMap[date]);
|
|
|
|
|
}
|
|
|
|
|
res.json(
|
|
|
|
|
{
|
|
|
|
|
code: 200,
|
|
|
|
|
message: "成功获取空闲时间",
|
|
|
|
|
data: freeTimeList
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}catch (e)
|
|
|
|
|
{
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
code: 500,
|
|
|
|
|
message: "获取空闲时间失败",
|
|
|
|
|
data: e
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = teamUserController;
|
|
|
|
|
|