Browse Source

待巡检判断

master
liujiangyong 2 years ago
parent
commit
bb67894ec6
  1. 23
      api/app/lib/controllers/patrolManage/patrolPlan.js
  2. 13
      api/app/lib/controllers/patrolManage/patrolRecord.js
  3. 11
      api/app/lib/models/patrol_plan.js
  4. 6
      script/1.0.5.1/schema/3.update_partol_plan.sql

23
api/app/lib/controllers/patrolManage/patrolPlan.js

@ -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, {

13
api/app/lib/controllers/patrolManage/patrolRecord.js

@ -228,9 +228,18 @@ async function addPatrolRecord (ctx, next) {
const curPlanRecord = await models.PatrolRecord.findAndCountAll({
where: { patrolPlanId: patrolPlanId }
});
const patrolCount = curPlanRecord.count + 1;
await models.PatrolPlan.update({ patrolCount }, {
// 更新下次巡检时间
const curPatrolPlan = await models.PatrolPlan.findOne({
where: { id: patrolPlanId }
});
const frequency = Number(curPatrolPlan.frequency.split('次')[0]);
const unit = curPatrolPlan.frequency.split('/')[1];
const sTime = moment().startOf(unit === '天' ? 'day' : unit === '周' ? 'isoWeek' : 'month');
const eTime = moment().endOf(unit === '天' ? 'day' : unit === '周' ? 'isoWeek' : 'month');
const filterRecord = curPlanRecord.rows.filter(r => moment(r.inspectionTime).diff(sTime) >= 0 && moment(r.inspectionTime).diff(eTime) <= 0);
await models.PatrolPlan.update({ patrolCount, nextTime: filterRecord.length + 1 >= frequency ? eTime.format() : sTime.format() }, {
where: { id: patrolPlanId },
transaction
})

11
api/app/lib/models/patrol_plan.js

@ -94,7 +94,16 @@ module.exports = dc => {
primaryKey: false,
field: "template_id",
autoIncrement: false
}
},
nextTime: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: null,
comment: null,
primaryKey: false,
field: "next_time",
autoIncrement: false
},
}, {
tableName: "patrol_plan",
comment: "",

6
script/1.0.5.1/schema/3.update_partol_plan.sql

@ -0,0 +1,6 @@
ALTER TABLE "public"."patrol_plan"
ADD COLUMN "next_time" timestamp;
COMMENT ON COLUMN "public"."patrol_plan"."next_time" IS '下次巡检时间';
update "public"."patrol_plan" set next_time = '2023-03-20 00:00:00'
Loading…
Cancel
Save