You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
4.6 KiB
113 lines
4.6 KiB
'use strict';
|
|
|
|
async function findPatrolRecord(ctx, next) {
|
|
let rslt = [];
|
|
let error = { name: 'FindError', message: '获取巡检记录失败' };
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const { startTime, endTime, alarm, patrolPlanId, pointId } = ctx.params;
|
|
// patrolPlanId传all查所有
|
|
if (patrolPlanId == 'all') {
|
|
/* 如果有startTime && endTime,查询所有符合条件的数据 */
|
|
if (startTime !== 'null' && endTime !== 'null') {
|
|
if (pointId !== 'null') {
|
|
if (alarm == 'null') {
|
|
rslt = await models.PatrolRecord.findAll({
|
|
where: { inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } },
|
|
});
|
|
} else {
|
|
rslt = await models.PatrolRecord.findAll({
|
|
where: { alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } },
|
|
});
|
|
}
|
|
} else {
|
|
if (alarm == 'null') {
|
|
rslt = await models.PatrolRecord.findAll({
|
|
where: { inspectionTime: { $between: [startTime, endTime] } },
|
|
});
|
|
} else {
|
|
rslt = await models.PatrolRecord.findAll({
|
|
where: { alarm, inspectionTime: { $between: [startTime, endTime] } },
|
|
});
|
|
}
|
|
}
|
|
} else {
|
|
/* 如果没有startTime && endTime,查询每个点位最新一条符合条件的数据 */
|
|
let a = []
|
|
if (pointId !== 'null') {
|
|
a = await models.PatrolRecord.findAll({
|
|
where: { pointId: { $in: pointId.split(',') } },
|
|
});
|
|
}
|
|
rslt = pointId.split(',').map(i => {
|
|
return a.filter(t => t.pointId == i).sort((a, b) => b.id - a.id)[0] || null
|
|
})
|
|
}
|
|
} else {
|
|
if (startTime !== 'null' && endTime !== 'null') {
|
|
if (pointId !== 'null') {
|
|
rslt = await models.PatrolRecord.findAll({
|
|
where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } },
|
|
});
|
|
} else {
|
|
rslt = await models.PatrolRecord.findAll({
|
|
where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] } },
|
|
});
|
|
}
|
|
|
|
} else {
|
|
let a = []
|
|
/* 如果没有startTime && endTime,查询每个点位最新一条符合条件的数据 */
|
|
if (pointId !== 'null') {
|
|
a = await models.PatrolRecord.findAll({
|
|
where: { patrolPlanId: { $in: patrolPlanId.split(',') }, pointId: { $in: pointId.split(',') } },
|
|
});
|
|
} else {
|
|
a = await models.PatrolRecord.findAll({
|
|
where: { patrolPlanId: { $in: patrolPlanId.split(',') } },
|
|
});
|
|
}
|
|
|
|
rslt = pointId.split(',').map(i => {
|
|
return a.filter(t => t.pointId == i).sort((a, b) => b.id - a.id)[0] || null
|
|
})
|
|
}
|
|
}
|
|
|
|
ctx.status = 200;
|
|
ctx.body = rslt;
|
|
error = null
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
"message": "获取巡检记录失败"
|
|
}
|
|
}
|
|
}
|
|
|
|
async function addPatrolRecord(ctx, next) {
|
|
let error = { name: 'addError', message: '新增巡检记录失败' };
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const data = ctx.request.body;
|
|
let { patrolPlanId, lastInspectionTime, inspectionTime, points, alarm, pointId } = data
|
|
let record = { patrolPlanId, lastInspectionTime, inspectionTime, points, alarm, pointId }
|
|
|
|
await models.PatrolRecord.create(record);
|
|
|
|
ctx.status = 204;
|
|
error = null
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
"message": '新增巡检计划失败'
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
findPatrolRecord,
|
|
addPatrolRecord,
|
|
}
|