diff --git a/api/app/lib/controllers/patrolManage/patrolPlan.js b/api/app/lib/controllers/patrolManage/patrolPlan.js index 81441dc..8159bfb 100644 --- a/api/app/lib/controllers/patrolManage/patrolPlan.js +++ b/api/app/lib/controllers/patrolManage/patrolPlan.js @@ -6,21 +6,21 @@ async function getPatrolPlan (ctx, next) { const { limit, page, userId } = ctx.query; let userWhere = {}; let options = { + order: [['id', 'desc']], include: [{ - required: true, + required: userId ? true : false, model: models.User, attributes: ['id', 'name'], where: userWhere, include: [{ - required: true, model: models.Department, attributes: ['id', 'name'], }] }, { - required: true, model: models.Project, attributes: ['id', 'name'], - }] + }], + distinct: true, }; if (limit) { options.limit = Number(limit); @@ -43,18 +43,29 @@ async function getPatrolPlan (ctx, next) { } } -async function createPatrolPlan (ctx, next) { +async function createPatrolPlan(ctx, next) { + const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const data = ctx.request.body; - const { name, way, structureId, startTime, endTime, frequency, points, userId, templateId } = data; - - let plan = { name, way, structureId, startTime, endTime, frequency, points, userId, templateId }; - - await models.PatrolPlan.create(plan); - + const { name, way, structureId, startTime, endTime, frequency, points, userIds, templateId } = data; + + let plan = { name, way, structureId, startTime, endTime, frequency, points, templateId }; + + const patrolPlanRes = await models.PatrolPlan.create(plan, { transaction }); + await models.PatrolPlanUser.bulkCreate( + userIds.map(uid => { + return { + patrolPlanId: patrolPlanRes.id, + userId: uid + } + }), { transaction } + ) + + await transaction.commit(); ctx.status = 204; } catch (error) { + await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { @@ -64,25 +75,41 @@ async function createPatrolPlan (ctx, next) { } async function updatePatrolPlan (ctx, next) { + const transaction = await ctx.fs.dc.orm.transaction(); try { let errMsg = '修改巡检计划失败'; const models = ctx.fs.dc.models; const data = ctx.request.body; - const { name, way, structureId, startTime, endTime, frequency, points, userId, templateId } = data; + const { name, way, structureId, startTime, endTime, frequency, points, userIds, templateId } = data; - let plan = { name, way, structureId, startTime, endTime, frequency, points, userId, templateId }; + let plan = { name, way, structureId, startTime, endTime, frequency, points, templateId }; if (data && data.id) { await models.PatrolPlan.update(plan, { - where: { id: data.id } + where: { id: data.id }, + transaction }) + await models.PatrolPlanUser.destroy({ + where: { patrolPlanId: data.id }, + transaction + }) + await models.PatrolPlanUser.bulkCreate( + userIds.map(uid => { + return { + patrolPlanId: data.id, + userId: uid + } + }), { transaction } + ) } else { errMsg = '请传入巡检计划id'; throw errMsg; } + await transaction.commit(); ctx.status = 204; } catch (error) { + await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { @@ -92,6 +119,7 @@ async function updatePatrolPlan (ctx, next) { } async function delPatrolPlan (ctx, next) { + const transaction = await ctx.fs.dc.orm.transaction(); try { let errMsg = '删除巡检计划失败'; @@ -107,12 +135,20 @@ async function delPatrolPlan (ctx, next) { throw errMsg; } + await models.PatrolPlanUser.destroy({ + where: { patrolPlanId: id }, + transaction + }) + await models.PatrolPlan.destroy({ - where: { id } + where: { id }, + transaction }) + await transaction.commit(); ctx.status = 204; } catch (error) { + await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: error } diff --git a/api/app/lib/index.js b/api/app/lib/index.js index 97168de..38d2f17 100644 --- a/api/app/lib/index.js +++ b/api/app/lib/index.js @@ -54,7 +54,7 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq }); const { Department, User, UserResource, Resource, Project, Point, - PatrolPlan, PatrolRecord, ReportInfo, + PatrolPlan, PatrolRecord, ReportInfo, PatrolPlanUser, CheckItems, CheckItemsGroup, PatrolTemplate, PatrolTemplateCheckItems, PatrolRecordIssueHandle } = dc.models; @@ -93,8 +93,8 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq PatrolPlan.belongsTo(Project, { foreignKey: 'structureId', targetKey: 'id' }); Project.hasMany(PatrolPlan, { foreignKey: 'structureId', sourceKey: 'id' }); - PatrolPlan.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' }); - User.hasMany(PatrolPlan, { foreignKey: 'userId', sourceKey: 'id' }); + PatrolPlan.belongsToMany(User, { through: PatrolPlanUser, foreignKey: 'patrolPlanId', otherKey: 'userId' }); + User.belongsToMany(PatrolPlan, { through: PatrolPlanUser, foreignKey: 'userId', otherKey: 'patrolPlanId' }); ReportInfo.belongsTo(Project, { foreignKey: 'projectId', targetKey: 'id' }); Project.hasMany(ReportInfo, { foreignKey: 'projectId', sourceKey: 'id' }); diff --git a/api/app/lib/models/patrol_plan.js b/api/app/lib/models/patrol_plan.js index 1bb18ae..8dae174 100644 --- a/api/app/lib/models/patrol_plan.js +++ b/api/app/lib/models/patrol_plan.js @@ -77,15 +77,6 @@ module.exports = dc => { field: "points", autoIncrement: false }, - userId: { - type: DataTypes.INTEGER, - allowNull: true, - defaultValue: null, - comment: null, - primaryKey: false, - field: "user_id", - autoIncrement: false - }, patrolCount: { type: DataTypes.INTEGER, allowNull: false, diff --git a/api/app/lib/models/patrol_plan_user.js b/api/app/lib/models/patrol_plan_user.js new file mode 100644 index 0000000..28d38c6 --- /dev/null +++ b/api/app/lib/models/patrol_plan_user.js @@ -0,0 +1,50 @@ +/* eslint-disable*/ +'use strict'; + +module.exports = dc => { + const DataTypes = dc.ORM; + const sequelize = dc.orm; + const PatrolPlanUser = sequelize.define("patrolPlanUser", { + id: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: true, + field: "id", + autoIncrement: true, + }, + patrolPlanId: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: false, + field: "patrol_plan_id", + autoIncrement: false, + references: { + key: "id", + model: "patrolPlan" + } + }, + userId: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: false, + field: "user_id", + autoIncrement: false, + references: { + key: "id", + model: "user" + } + } + }, { + tableName: "patrol_plan_user", + comment: "", + indexes: [] + }); + dc.models.PatrolPlanUser = PatrolPlanUser; + return PatrolPlanUser; +}; \ No newline at end of file diff --git a/script/1.0.5.1/schema/1.update_partol_plan.sql b/script/1.0.5.1/schema/1.update_partol_plan.sql new file mode 100644 index 0000000..d1663b1 --- /dev/null +++ b/script/1.0.5.1/schema/1.update_partol_plan.sql @@ -0,0 +1,2 @@ +ALTER TABLE "public"."patrol_plan" + DROP COLUMN "user_id"; \ No newline at end of file diff --git a/script/1.0.5.1/schema/2.create_patrol_plan_user.sql b/script/1.0.5.1/schema/2.create_patrol_plan_user.sql new file mode 100644 index 0000000..6e8f51e --- /dev/null +++ b/script/1.0.5.1/schema/2.create_patrol_plan_user.sql @@ -0,0 +1,10 @@ +DROP TABLE IF EXISTS "public"."patrol_plan_user"; +CREATE TABLE "public"."patrol_plan_user" ( + "id" serial, + "patrol_plan_id" int4 NOT NULL, + "user_id" int4 NOT NULL, + PRIMARY KEY ("id"), + CONSTRAINT "patrol_plan_user_patrol_plan_id_fk" FOREIGN KEY ("patrol_plan_id") REFERENCES "public"."patrol_plan" ("id"), + CONSTRAINT "patrol_plan_user_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user" ("id") +) +; \ No newline at end of file diff --git a/weapp/package/inspectionInput/inspectionInput.js b/weapp/package/inspectionInput/inspectionInput.js index fc22884..717aaab 100644 --- a/weapp/package/inspectionInput/inspectionInput.js +++ b/weapp/package/inspectionInput/inspectionInput.js @@ -343,13 +343,13 @@ Page({ alarm = true; } } - + const { id, name, departmentId, deptName } = wx.getStorageSync('userInfo'); let data = { patrolPlanId: dataList.id, pointId: itemData.id, inspectionTime: moment().format('YYYY-MM-DD HH:mm:ss'), points: { - user: dataList.user, + user: { id, name, department: { id: departmentId, name: deptName } }, project: dataList.project, frequency: dataList.frequency, itemData: itemData, diff --git a/weapp/package/startInspection/startInspection.js b/weapp/package/startInspection/startInspection.js index 53fd816..9e1f594 100644 --- a/weapp/package/startInspection/startInspection.js +++ b/weapp/package/startInspection/startInspection.js @@ -43,7 +43,11 @@ Page({ return e.name }).join('、') this.setData({ - dataList: curPlan, + dataList: { + ...curPlan, + showUsers: curPlan.users.map(u => u.name).join(), + showDepts: [...new Set(curPlan?.users?.map(u => u.department.name))].join(), + }, points, // showModal: true, // itemData: curPlan.points.find(p => p.id == this.data.scenePointId) @@ -482,7 +486,11 @@ Page({ return e.name }).join('、') that.setData({ - dataList: data, + dataList: { + ...data, + showUsers: data.users.map(u => u.name).join(), + showDepts: [...new Set(data?.users?.map(u => u.department.name))].join(), + }, points }) that.getPatrolRecord(); diff --git a/weapp/package/startInspection/startInspection.wxml b/weapp/package/startInspection/startInspection.wxml index f234771..979e8e7 100644 --- a/weapp/package/startInspection/startInspection.wxml +++ b/weapp/package/startInspection/startInspection.wxml @@ -29,11 +29,11 @@ 巡检人 - {{dataList.user.name}} + {{dataList.showUsers}} 巡检单位 - {{dataList.user.department.name}} + {{dataList.showDepts}} 巡检点位 @@ -54,7 +54,7 @@ 巡检人 - {{dataList.user.name}} + {{dataList.showUsers}} 本次巡检日期 diff --git a/web/client/src/sections/patrolManage/components/planModal.js b/web/client/src/sections/patrolManage/components/planModal.js index e159193..a500647 100644 --- a/web/client/src/sections/patrolManage/components/planModal.js +++ b/web/client/src/sections/patrolManage/components/planModal.js @@ -34,6 +34,14 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, userLoading, userLis useEffect(() => { if (userList.length) { setUserOpt(userList?.filter(f => f.structure?.includes(curRecord?.project?.id))?.map(u => ({ label: u.name, value: u.id }))) + // 如果巡检人员用户被删除,筛掉被删除的用户 + const userListIds = userList?.map(u => u.id); + const filterUsers = curRecord?.users?.filter(u => userListIds.includes(u.id)); + const nextUserIds = filterUsers.map(u => u.id); + form.setFieldsValue({ + userIds: nextUserIds, + userDept: [...new Set(filterUsers.map(u => u.department?.name))].join() + }); } }, [userList]) @@ -92,7 +100,8 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, userLoading, userLis ...curRecord, time: [moment(curRecord?.startTime), moment(curRecord?.endTime)], points: curRecord?.points?.map(p => p.id), - userDept: curRecord?.user?.department?.name, + userIds: curRecord?.users?.map(u => u.id), + userDept: [...new Set(curRecord?.users?.map(u => u.department?.name))].join(), frequency: curRecord?.frequency?.split('次')[0] }} disabled={type === 'view'} @@ -111,7 +120,7 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, userLoading, userLis setPointOpt(res.payload.data?.rows[0]?.points?.map(p => ({ label: p.name, value: p.id }))) } }) - form.setFieldsValue({ userId: null }); + form.setFieldsValue({ userIds: [], userDept: [] }); setUserOpt(userList?.filter(f => f.structure?.includes(projectId))?.map(u => ({ label: u.name, value: u.id }))) }} /> @@ -180,19 +189,17 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, userLoading, userLis { - const curUser = userList.filter(u => u.id == value) - if (curUser.length) { - form.setFieldsValue({ - userDept: curUser[0].department.name - }); - } +