diff --git a/api/controller_front/apply.js b/api/controller_front/apply.js index 6c88be0..bf6d58a 100644 --- a/api/controller_front/apply.js +++ b/api/controller_front/apply.js @@ -109,7 +109,7 @@ module.exports = { * /api/apply/statistics: * get: * summary: 获取投递统计 - * description: 根据设备SN码获取投递统计数据 + * description: 根据设备SN码获取投递统计数据(包含今日、本周、本月统计) * tags: [前端-投递管理] * parameters: * - in: query @@ -124,7 +124,7 @@ module.exports = { */ 'GET /apply/statistics': async (ctx) => { const models = Framework.getModels(); - const { apply_records } = models; + const { apply_records, op } = models; const { sn_code } = ctx.query; const final_sn_code = sn_code; @@ -132,18 +132,61 @@ module.exports = { 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 [ totalCount, successCount, failedCount, pendingCount, - interviewCount + interviewCount, + todayCount, + weekCount, + monthCount ] = await Promise.all([ + // 总计 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: 'failed' } }), 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({ @@ -152,6 +195,9 @@ module.exports = { failedCount, pendingCount, interviewCount, + todayCount, + weekCount, + monthCount, successRate: totalCount > 0 ? ((successCount / totalCount) * 100).toFixed(2) : 0, interviewRate: totalCount > 0 ? ((interviewCount / totalCount) * 100).toFixed(2) : 0 }); diff --git a/api/controller_front/task.js b/api/controller_front/task.js index c70b73a..abaa8d4 100644 --- a/api/controller_front/task.js +++ b/api/controller_front/task.js @@ -206,6 +206,111 @@ module.exports = { console.error('[任务管理] 获取待执行任务失败:', error); 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 || '未知错误')); + } } };