From 29154e835e58fcef81671bdbf330bb23ccfdd4ba Mon Sep 17 00:00:00 2001 From: liujiangyong Date: Mon, 20 Mar 2023 16:42:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=85=E5=B7=A1=E6=A3=80=E6=8C=89=E7=82=B9?= =?UTF-8?q?=E4=BD=8D=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/patrolManage/patrolPlan.js | 30 +++++++++++- .../controllers/patrolManage/patrolRecord.js | 46 +++++++++++++++---- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/api/app/lib/controllers/patrolManage/patrolPlan.js b/api/app/lib/controllers/patrolManage/patrolPlan.js index 5bc1050..9678b68 100644 --- a/api/app/lib/controllers/patrolManage/patrolPlan.js +++ b/api/app/lib/controllers/patrolManage/patrolPlan.js @@ -98,9 +98,17 @@ async function updatePatrolPlan (ctx, next) { inspectionTime: { $between: [sTime.format(), eTime.format()] }, } }); + const groupRecord = curPlanRecord.group(({ pointId }) => pointId); + let isComplete = true; + points.forEach(p => { + if (!groupRecord[p.id] || groupRecord[p.id].length < frequencyNum) { + isComplete = false; + return; + } + }); let plan = { name, way, structureId, startTime, endTime, frequency, points, templateId, - nextTime: curPlanRecord.length >= frequencyNum ? eTime.format() : sTime.format() + nextTime: isComplete ? eTime.format() : sTime.format() }; if (data && data.id) { @@ -174,6 +182,26 @@ async function delPatrolPlan (ctx, next) { } } +Array.prototype.group = function (callback, thisArg = null) { + // 参数合法性判断 + if (typeof callback !== "function") { + throw new TypeError(`${callback} is not a function`); + } + const arr = this; + const length = this.length; + const grouper = Object.create(null); + + for (let i = 0; i < length; i++) { + const key = callback.call(thisArg, arr[i], i, arr) + if (!grouper[key]) { + grouper[key] = [arr[i]] + } else { + grouper[key].push(arr[i]) + } + } + return grouper; +}; + module.exports = { getPatrolPlan, createPatrolPlan, diff --git a/api/app/lib/controllers/patrolManage/patrolRecord.js b/api/app/lib/controllers/patrolManage/patrolRecord.js index 7bd238e..3833aac 100644 --- a/api/app/lib/controllers/patrolManage/patrolRecord.js +++ b/api/app/lib/controllers/patrolManage/patrolRecord.js @@ -2,7 +2,7 @@ const moment = require("moment"); -async function findPatrolRecord (ctx, next) { +async function findPatrolRecord(ctx, next) { let rslt = []; let error = { name: 'FindError', message: '获取巡检记录失败' }; try { @@ -142,7 +142,7 @@ async function findPatrolRecord (ctx, next) { } } -async function findPatrolRecordUnlicensed (ctx, next) { +async function findPatrolRecordUnlicensed(ctx, next) { let rslt = []; let error = { name: 'FindError', message: '获取巡检记录失败' }; try { @@ -166,7 +166,7 @@ async function findPatrolRecordUnlicensed (ctx, next) { } } -async function findPointCurPatrolRecord (ctx, next) { +async function findPointCurPatrolRecord(ctx, next) { let rslt = []; let error = { name: 'FindError', message: '获取巡检记录失败' }; try { @@ -189,7 +189,7 @@ async function findPointCurPatrolRecord (ctx, next) { } } -async function addPatrolRecord (ctx, next) { +async function addPatrolRecord(ctx, next) { let error = { name: 'addError', message: '新增巡检记录失败' }; const transaction = await ctx.fs.dc.orm.transaction(); try { @@ -238,8 +238,15 @@ async function addPatrolRecord (ctx, next) { 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() }, { + const groupRecord = filterRecord.group(({ pointId }) => pointId); + let isComplete = true; + curPatrolPlan.points.forEach(p => { + if (!groupRecord[p.id] || (pointId === p.id ? groupRecord[p.id].length + 1 : groupRecord[p.id].length) < frequency) { + isComplete = false; + return; + } + }); + await models.PatrolPlan.update({ patrolCount, nextTime: isComplete ? eTime.format() : sTime.format() }, { where: { id: patrolPlanId }, transaction }) @@ -257,7 +264,7 @@ async function addPatrolRecord (ctx, next) { } } -async function getPatrolRecordIssueHandle (ctx) { +async function getPatrolRecordIssueHandle(ctx) { try { const { models } = ctx.fs.dc; const { userId, userInfo } = ctx.fs.api @@ -330,7 +337,7 @@ async function getPatrolRecordIssueHandle (ctx) { } } -async function getPatrolRecordIssueHandleById (ctx) { +async function getPatrolRecordIssueHandleById(ctx) { try { const { models } = ctx.fs.dc; const { id } = ctx.params @@ -356,7 +363,7 @@ async function getPatrolRecordIssueHandleById (ctx) { } // 新建维修处理计划成功 -function addPatrolRecordIssueHandle (opts) { +function addPatrolRecordIssueHandle(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; @@ -375,7 +382,7 @@ function addPatrolRecordIssueHandle (opts) { // 修改维修处理计划 -function editPatrolRecordIssueHandle (opts) { +function editPatrolRecordIssueHandle(opts) { return async function (ctx, next) { try { @@ -396,6 +403,25 @@ function editPatrolRecordIssueHandle (opts) { } } +Array.prototype.group = function (callback, thisArg = null) { + // 参数合法性判断 + if (typeof callback !== "function") { + throw new TypeError(`${callback} is not a function`); + } + const arr = this; + const length = this.length; + const grouper = Object.create(null); + + for (let i = 0; i < length; i++) { + const key = callback.call(thisArg, arr[i], i, arr) + if (!grouper[key]) { + grouper[key] = [arr[i]] + } else { + grouper[key].push(arr[i]) + } + } + return grouper; +}; module.exports = { findPatrolRecord,