1
This commit is contained in:
@@ -206,6 +206,83 @@ module.exports = {
|
||||
console.error('获取投递记录详情失败:', error);
|
||||
return ctx.fail('获取投递记录详情失败: ' + error.message);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/apply/trend:
|
||||
* get:
|
||||
* summary: 获取近7天投递趋势数据
|
||||
* description: 根据设备SN码获取近7天的投递数量趋势
|
||||
* tags: [前端-投递管理]
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: sn_code
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* description: 设备SN码
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 获取成功
|
||||
*/
|
||||
'GET /apply/trend': async (ctx) => {
|
||||
try {
|
||||
const models = Framework.getModels();
|
||||
const { apply_records, op } = models;
|
||||
const { sn_code } = ctx.query;
|
||||
|
||||
if (!sn_code) {
|
||||
return ctx.fail('请提供设备SN码');
|
||||
}
|
||||
|
||||
// 计算近7天的日期范围
|
||||
const today = new Date();
|
||||
today.setHours(23, 59, 59, 999);
|
||||
const sevenDaysAgo = new Date();
|
||||
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 6);
|
||||
sevenDaysAgo.setHours(0, 0, 0, 0);
|
||||
|
||||
// 查询近7天的投递记录
|
||||
const records = await apply_records.findAll({
|
||||
where: {
|
||||
sn_code: sn_code,
|
||||
applyTime: {
|
||||
[op.gte]: sevenDaysAgo,
|
||||
[op.lte]: today
|
||||
}
|
||||
},
|
||||
attributes: ['applyTime'],
|
||||
raw: true
|
||||
});
|
||||
|
||||
// 生成7天的日期数组
|
||||
const trendData = [];
|
||||
for (let i = 6; i >= 0; i--) {
|
||||
const date = new Date();
|
||||
date.setDate(date.getDate() - i);
|
||||
date.setHours(0, 0, 0, 0);
|
||||
|
||||
const dateStr = `${date.getMonth() + 1}/${date.getDate()}`;
|
||||
|
||||
// 统计当天的投递数量
|
||||
const count = records.filter(record => {
|
||||
const recordDate = new Date(record.applyTime);
|
||||
recordDate.setHours(0, 0, 0, 0);
|
||||
return recordDate.getTime() === date.getTime();
|
||||
}).length;
|
||||
|
||||
trendData.push({
|
||||
date: dateStr,
|
||||
value: count
|
||||
});
|
||||
}
|
||||
|
||||
return ctx.success(trendData);
|
||||
} catch (error) {
|
||||
console.error('获取投递趋势数据失败:', error);
|
||||
return ctx.fail('获取投递趋势数据失败: ' + error.message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user