This commit is contained in:
张成
2025-12-18 17:02:54 +08:00
parent 70281b5444
commit bad9978f53
2 changed files with 155 additions and 4 deletions

View File

@@ -109,7 +109,7 @@ module.exports = {
* /api/apply/statistics: * /api/apply/statistics:
* get: * get:
* summary: 获取投递统计 * summary: 获取投递统计
* description: 根据设备SN码获取投递统计数据 * description: 根据设备SN码获取投递统计数据(包含今日、本周、本月统计)
* tags: [前端-投递管理] * tags: [前端-投递管理]
* parameters: * parameters:
* - in: query * - in: query
@@ -124,7 +124,7 @@ module.exports = {
*/ */
'GET /apply/statistics': async (ctx) => { 'GET /apply/statistics': async (ctx) => {
const models = Framework.getModels(); const models = Framework.getModels();
const { apply_records } = models; const { apply_records, op } = models;
const { sn_code } = ctx.query; const { sn_code } = ctx.query;
const final_sn_code = sn_code; const final_sn_code = sn_code;
@@ -132,18 +132,61 @@ module.exports = {
return ctx.fail('请提供设备SN码'); return ctx.fail('请提供设备SN码');
} }
// 计算时间范围
const now = new Date();
// 今天的开始时间00:00:00
const todayStart = new Date(now);
todayStart.setHours(0, 0, 0, 0);
// 本周的开始时间周一00:00:00
const weekStart = new Date(now);
const dayOfWeek = weekStart.getDay();
const diff = dayOfWeek === 0 ? 6 : dayOfWeek - 1; // 周日算作上周
weekStart.setDate(weekStart.getDate() - diff);
weekStart.setHours(0, 0, 0, 0);
// 本月的开始时间1号00:00:00
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
monthStart.setHours(0, 0, 0, 0);
const [ const [
totalCount, totalCount,
successCount, successCount,
failedCount, failedCount,
pendingCount, pendingCount,
interviewCount interviewCount,
todayCount,
weekCount,
monthCount
] = await Promise.all([ ] = await Promise.all([
// 总计
apply_records.count({ where: { sn_code: final_sn_code } }), apply_records.count({ where: { sn_code: final_sn_code } }),
apply_records.count({ where: { sn_code: final_sn_code, applyStatus: 'success' } }), apply_records.count({ where: { sn_code: final_sn_code, applyStatus: 'success' } }),
apply_records.count({ where: { sn_code: final_sn_code, applyStatus: 'failed' } }), apply_records.count({ where: { sn_code: final_sn_code, applyStatus: 'failed' } }),
apply_records.count({ where: { sn_code: final_sn_code, applyStatus: 'pending' } }), apply_records.count({ where: { sn_code: final_sn_code, applyStatus: 'pending' } }),
apply_records.count({ where: { sn_code: final_sn_code, feedbackStatus: 'interview' } }) apply_records.count({ where: { sn_code: final_sn_code, feedbackStatus: 'interview' } }),
// 今日
apply_records.count({
where: {
sn_code: final_sn_code,
applyTime: { [op.gte]: todayStart }
}
}),
// 本周
apply_records.count({
where: {
sn_code: final_sn_code,
applyTime: { [op.gte]: weekStart }
}
}),
// 本月
apply_records.count({
where: {
sn_code: final_sn_code,
applyTime: { [op.gte]: monthStart }
}
})
]); ]);
return ctx.success({ return ctx.success({
@@ -152,6 +195,9 @@ module.exports = {
failedCount, failedCount,
pendingCount, pendingCount,
interviewCount, interviewCount,
todayCount,
weekCount,
monthCount,
successRate: totalCount > 0 ? ((successCount / totalCount) * 100).toFixed(2) : 0, successRate: totalCount > 0 ? ((successCount / totalCount) * 100).toFixed(2) : 0,
interviewRate: totalCount > 0 ? ((interviewCount / totalCount) * 100).toFixed(2) : 0 interviewRate: totalCount > 0 ? ((interviewCount / totalCount) * 100).toFixed(2) : 0
}); });

View File

@@ -206,6 +206,111 @@ module.exports = {
console.error('[任务管理] 获取待执行任务失败:', error); console.error('[任务管理] 获取待执行任务失败:', error);
return ctx.fail('获取待执行任务失败: ' + (error.message || '未知错误')); return ctx.fail('获取待执行任务失败: ' + (error.message || '未知错误'));
} }
},
/**
* @swagger
* /api/task/statistics:
* get:
* summary: 获取任务统计
* description: 根据设备SN码获取任务统计数据包含今日、本周、本月统计
* tags: [前端-任务管理]
* parameters:
* - in: query
* name: sn_code
* required: true
* schema:
* type: string
* description: 设备SN码
* responses:
* 200:
* description: 获取成功
*/
'GET /task/statistics': async (ctx) => {
try {
const { sn_code } = ctx.query || {};
if (!sn_code) {
return ctx.fail('请提供设备SN码');
}
const { task_status, op } = await Framework.getModels();
// 计算时间范围
const now = new Date();
// 今天的开始时间00:00:00
const todayStart = new Date(now);
todayStart.setHours(0, 0, 0, 0);
// 本周的开始时间周一00:00:00
const weekStart = new Date(now);
const dayOfWeek = weekStart.getDay();
const diff = dayOfWeek === 0 ? 6 : dayOfWeek - 1; // 周日算作上周
weekStart.setDate(weekStart.getDate() - diff);
weekStart.setHours(0, 0, 0, 0);
// 本月的开始时间1号00:00:00
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
monthStart.setHours(0, 0, 0, 0);
const [
totalCount,
completedCount,
runningCount,
pendingCount,
failedCount,
todayCount,
weekCount,
monthCount
] = await Promise.all([
// 总计
task_status.count({ where: { sn_code: sn_code } }),
task_status.count({ where: { sn_code: sn_code, status: 'completed' } }),
task_status.count({ where: { sn_code: sn_code, status: 'running' } }),
task_status.count({ where: { sn_code: sn_code, status: 'pending' } }),
task_status.count({ where: { sn_code: sn_code, status: 'failed' } }),
// 今日完成
task_status.count({
where: {
sn_code: sn_code,
status: 'completed',
updated_time: { [op.gte]: todayStart }
}
}),
// 本周完成
task_status.count({
where: {
sn_code: sn_code,
status: 'completed',
updated_time: { [op.gte]: weekStart }
}
}),
// 本月完成
task_status.count({
where: {
sn_code: sn_code,
status: 'completed',
updated_time: { [op.gte]: monthStart }
}
})
]);
return ctx.success({
totalCount,
completedCount,
runningCount,
pendingCount,
failedCount,
todayCount,
weekCount,
monthCount,
completionRate: totalCount > 0 ? ((completedCount / totalCount) * 100).toFixed(2) : 0
});
} catch (error) {
console.error('[任务管理] 获取任务统计失败:', error);
return ctx.fail('获取任务统计失败: ' + (error.message || '未知错误'));
}
} }
}; };