Browse Source

巡检计划-巡检人员多选功能

master
liujiangyong 2 years ago
parent
commit
b87740767a
  1. 58
      api/app/lib/controllers/patrolManage/patrolPlan.js
  2. 6
      api/app/lib/index.js
  3. 9
      api/app/lib/models/patrol_plan.js
  4. 50
      api/app/lib/models/patrol_plan_user.js
  5. 2
      script/1.0.5.1/schema/1.update_partol_plan.sql
  6. 10
      script/1.0.5.1/schema/2.create_patrol_plan_user.sql
  7. 4
      weapp/package/inspectionInput/inspectionInput.js
  8. 12
      weapp/package/startInspection/startInspection.js
  9. 6
      weapp/package/startInspection/startInspection.wxml
  10. 23
      web/client/src/sections/patrolManage/components/planModal.js

58
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);
@ -44,17 +44,28 @@ async function getPatrolPlan (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;
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 };
await models.PatrolPlan.create(plan);
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 }

6
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' });

9
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,

50
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;
};

2
script/1.0.5.1/schema/1.update_partol_plan.sql

@ -0,0 +1,2 @@
ALTER TABLE "public"."patrol_plan"
DROP COLUMN "user_id";

10
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")
)
;

4
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,

12
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();

6
weapp/package/startInspection/startInspection.wxml

@ -29,11 +29,11 @@
</view>
<view class="txt">
<view style="float: left;font-weight: bold;">巡检人</view>
<view style="float:left;width:70%;margin-left:40rpx;">{{dataList.user.name}}</view>
<view style="float:left;width:70%;margin-left:40rpx;">{{dataList.showUsers}}</view>
</view>
<view class="txt">
<view style="float: left;font-weight: bold;">巡检单位</view>
<view style="float:left;width:70%;margin-left:40rpx;">{{dataList.user.department.name}}</view>
<view style="float:left;width:70%;margin-left:40rpx;">{{dataList.showDepts}}</view>
</view>
<view class="txt">
<view style="float: left;font-weight: bold;">巡检点位</view>
@ -54,7 +54,7 @@
</view>
<view class="txt" style="margin-bottom: 20rpx;">
<view style="float: left;font-weight: bold;">巡检人</view>
<view style="float:left;width:55%;margin-left:40rpx;">{{dataList.user.name}}</view>
<view style="float:left;width:55%;margin-left:40rpx;">{{dataList.showUsers}}</view>
</view>
<view class="txt" style="margin-bottom: 20rpx;">
<view style="float: left;font-weight: bold;">本次巡检日期</view>

23
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 })))
}} />
</Form.Item>
@ -180,19 +189,17 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, userLoading, userLis
<Select mode="multiple" options={pointOpt} disabled={!pointOpt || type === 'view'} />
</Form.Item>
<Form.Item
name="userId"
name="userIds"
label="巡检人员"
rules={[
{ required: true, message: '请选择巡检人员' },
]}
>
<Select disabled={type === 'view'} options={userOpt} loading={userLoading} onChange={(value) => {
const curUser = userList.filter(u => u.id == value)
if (curUser.length) {
<Select mode="multiple" disabled={type === 'view'} options={userOpt} loading={userLoading} onChange={(values) => {
const selectUser = userList.filter(u => values?.includes(u.id))
form.setFieldsValue({
userDept: curUser[0].department.name
userDept: [...new Set(selectUser.map(u => u.department.name))].join()
});
}
}} />
</Form.Item>
<Form.Item

Loading…
Cancel
Save