|
|
@ -1,12 +1,15 @@ |
|
|
|
'use strict'; |
|
|
|
const moment = require('moment'); |
|
|
|
|
|
|
|
async function getPatrolPlan (ctx, next) { |
|
|
|
try { |
|
|
|
const models = ctx.fs.dc.models; |
|
|
|
const { limit, page, userId } = ctx.query; |
|
|
|
let where = {} |
|
|
|
let userWhere = {}; |
|
|
|
let options = { |
|
|
|
order: [['id', 'desc']], |
|
|
|
where, |
|
|
|
include: [{ |
|
|
|
required: userId ? true : false, |
|
|
|
model: models.User, |
|
|
@ -30,6 +33,9 @@ async function getPatrolPlan (ctx, next) { |
|
|
|
} |
|
|
|
if (userId) { |
|
|
|
userWhere.id = userId; |
|
|
|
where.startTime = { $lte: moment().format('YYYY-MM-DD') + ' 23:59:59' } |
|
|
|
where.endTime = { $gte: moment().format('YYYY-MM-DD') + ' 00:00:00' } |
|
|
|
where.nextTime = { $lte: moment().format('YYYY-MM-DD HH:mm:ss') } |
|
|
|
} |
|
|
|
let res = await models.PatrolPlan.findAndCountAll(options); |
|
|
|
ctx.status = 200; |
|
|
@ -52,7 +58,7 @@ async function createPatrolPlan(ctx, next) { |
|
|
|
|
|
|
|
let plan = { name, way, structureId, startTime, endTime, frequency, points, templateId }; |
|
|
|
|
|
|
|
const patrolPlanRes = await models.PatrolPlan.create(plan, { transaction }); |
|
|
|
const patrolPlanRes = await models.PatrolPlan.create({ ...plan, nextTime: moment().format() }, { transaction }); |
|
|
|
await models.PatrolPlanUser.bulkCreate( |
|
|
|
userIds.map(uid => { |
|
|
|
return { |
|
|
@ -82,7 +88,20 @@ async function updatePatrolPlan (ctx, next) { |
|
|
|
const data = ctx.request.body; |
|
|
|
const { name, way, structureId, startTime, endTime, frequency, points, userIds, templateId } = data; |
|
|
|
|
|
|
|
let plan = { name, way, structureId, startTime, endTime, frequency, points, templateId }; |
|
|
|
const frequencyNum = Number(frequency.split('次')[0]); |
|
|
|
const unit = frequency.split('/')[1]; |
|
|
|
const sTime = moment().startOf(unit === '天' ? 'day' : unit === '周' ? 'isoWeek' : 'month'); |
|
|
|
const eTime = moment().endOf(unit === '天' ? 'day' : unit === '周' ? 'isoWeek' : 'month'); |
|
|
|
const curPlanRecord = await models.PatrolRecord.findAll({ |
|
|
|
where: { |
|
|
|
patrolPlanId: data.id, |
|
|
|
inspectionTime: { $between: [sTime.format(), eTime.format()] }, |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
let plan = { name, way, structureId, startTime, endTime, frequency, points, templateId, |
|
|
|
nextTime: curPlanRecord.length >= frequencyNum ? eTime.format() : sTime.format() |
|
|
|
}; |
|
|
|
|
|
|
|
if (data && data.id) { |
|
|
|
await models.PatrolPlan.update(plan, { |
|
|
|