1
This commit is contained in:
@@ -326,62 +326,76 @@ module.exports = {
|
||||
return ctx.fail('请提供设备SN码');
|
||||
}
|
||||
|
||||
const { task_status, task_commands, op } = await Framework.getModels();
|
||||
const { task_commands } = await Framework.getModels();
|
||||
const sequelize = task_commands.sequelize;
|
||||
const Sequelize = require('sequelize');
|
||||
|
||||
// 先查当前账号对应任务,再按 task_id 关联查指令
|
||||
const task_rows = await task_status.findAll({
|
||||
where: { sn_code },
|
||||
attributes: ['id'],
|
||||
order: [['id', 'DESC']],
|
||||
limit: 2000
|
||||
});
|
||||
const task_ids = task_rows.map((row) => row.id).filter((id) => id != null);
|
||||
if (!task_ids.length) {
|
||||
return ctx.success({
|
||||
list: [],
|
||||
total: 0,
|
||||
page,
|
||||
page_size
|
||||
});
|
||||
}
|
||||
|
||||
const where = {
|
||||
task_id: { [op.in]: task_ids }
|
||||
};
|
||||
if (body.command_type) {
|
||||
where.command_type = String(body.command_type).trim();
|
||||
}
|
||||
if (body.status) {
|
||||
where.status = String(body.status).trim();
|
||||
}
|
||||
|
||||
const { rows, count } = await task_commands.findAndCountAll({
|
||||
where,
|
||||
attributes: [
|
||||
'id',
|
||||
'task_id',
|
||||
'command_type',
|
||||
'command_name',
|
||||
'command_params',
|
||||
'status',
|
||||
'priority',
|
||||
'sequence',
|
||||
'retry_count',
|
||||
'max_retries',
|
||||
'start_time',
|
||||
'end_time',
|
||||
'duration',
|
||||
'result',
|
||||
'error_message',
|
||||
'progress'
|
||||
],
|
||||
order: [['id', 'DESC']],
|
||||
const conditions = ['ts.sn_code = :sn_code'];
|
||||
const replacements = {
|
||||
sn_code,
|
||||
limit: page_size,
|
||||
offset
|
||||
});
|
||||
};
|
||||
const command_type = body.command_type != null ? String(body.command_type).trim() : '';
|
||||
const status = body.status != null ? String(body.status).trim() : '';
|
||||
if (command_type) {
|
||||
conditions.push('tc.command_type = :command_type');
|
||||
replacements.command_type = command_type;
|
||||
}
|
||||
if (status) {
|
||||
conditions.push('tc.status = :status');
|
||||
replacements.status = status;
|
||||
}
|
||||
// 仅保留真实下发到客户端的指令,过滤服务端汇总类记录
|
||||
conditions.push("tc.command_type <> 'job_filter_summary'");
|
||||
const where_sql = conditions.join(' AND ');
|
||||
|
||||
// 用 JOIN + 覆盖索引,避免大 IN 列表导致慢查询
|
||||
const count_sql = `
|
||||
SELECT COUNT(*) AS count
|
||||
FROM task_commands tc
|
||||
INNER JOIN task_status ts ON ts.id = tc.task_id
|
||||
WHERE ${where_sql}
|
||||
`;
|
||||
const data_sql = `
|
||||
SELECT
|
||||
tc.id,
|
||||
tc.task_id,
|
||||
tc.command_type,
|
||||
tc.command_name,
|
||||
tc.command_params,
|
||||
tc.status,
|
||||
tc.priority,
|
||||
tc.sequence,
|
||||
tc.retry_count,
|
||||
tc.max_retries,
|
||||
tc.start_time,
|
||||
tc.end_time,
|
||||
tc.duration,
|
||||
tc.result,
|
||||
tc.error_message,
|
||||
tc.progress
|
||||
FROM task_commands tc
|
||||
INNER JOIN task_status ts ON ts.id = tc.task_id
|
||||
WHERE ${where_sql}
|
||||
ORDER BY tc.id DESC
|
||||
LIMIT :limit OFFSET :offset
|
||||
`;
|
||||
|
||||
const [count_result, rows] = await Promise.all([
|
||||
sequelize.query(count_sql, {
|
||||
replacements,
|
||||
type: Sequelize.QueryTypes.SELECT
|
||||
}),
|
||||
sequelize.query(data_sql, {
|
||||
replacements,
|
||||
type: Sequelize.QueryTypes.SELECT
|
||||
})
|
||||
]);
|
||||
const count = Number(count_result && count_result[0] ? count_result[0].count : 0) || 0;
|
||||
|
||||
const list = rows.map((r) => {
|
||||
const j = r.toJSON ? r.toJSON() : r;
|
||||
const j = r && typeof r.toJSON === 'function' ? r.toJSON() : r;
|
||||
return {
|
||||
...j,
|
||||
// 前端直接展示,避免超长字段压垮 UI
|
||||
@@ -400,6 +414,41 @@ module.exports = {
|
||||
console.error('[任务管理] 获取指令分页失败:', error);
|
||||
return ctx.fail('获取指令分页失败: ' + (error.message || '未知错误'));
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 按设备 SN 重试单条指令
|
||||
*/
|
||||
'POST /task/command/retry': async (ctx) => {
|
||||
try {
|
||||
const body = ctx.getBody() || {};
|
||||
const query = typeof ctx.getQuery === 'function' ? (ctx.getQuery() || {}) : {};
|
||||
const sn_code = body.sn_code || query.sn_code;
|
||||
const command_id = Number(body.command_id || body.commandId || 0);
|
||||
if (!sn_code) {
|
||||
return ctx.fail('请提供设备SN码');
|
||||
}
|
||||
if (!command_id) {
|
||||
return ctx.fail('请提供指令ID');
|
||||
}
|
||||
|
||||
const { task_commands, task_status } = await Framework.getModels();
|
||||
const command_row = await task_commands.findByPk(command_id);
|
||||
if (!command_row) {
|
||||
return ctx.fail('指令不存在');
|
||||
}
|
||||
const task_row = await task_status.findByPk(command_row.task_id);
|
||||
if (!task_row || String(task_row.sn_code || '') !== String(sn_code)) {
|
||||
return ctx.fail('无权重试该指令');
|
||||
}
|
||||
|
||||
const plaAccountService = require('../services/pla_account_service.js');
|
||||
const result = await plaAccountService.retryCommand({ commandId: command_id });
|
||||
return ctx.success(result);
|
||||
} catch (error) {
|
||||
console.error('[任务管理] 重试指令失败:', error);
|
||||
return ctx.fail('重试指令失败: ' + (error.message || '未知错误'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user