Browse Source

巡检模板 api 查询

master
巴林闲侠 2 years ago
parent
commit
f1c05301cc
  1. 22
      api/app/lib/controllers/patrolManage/patrolTemplate.js
  2. 5
      api/app/lib/index.js
  3. 9
      api/app/lib/models/patrol_template.js
  4. 17
      api/log/development.log
  5. 15
      script/1.0.3/schema/1.create_template.sql
  6. 43
      web/client/src/sections/patrolManage/components/planTemplateModal.js
  7. 52
      web/client/src/sections/patrolManage/containers/patrolTemplate.js

22
api/app/lib/controllers/patrolManage/patrolTemplate.js

@ -3,24 +3,14 @@
async function getPatrolTemplate (ctx, next) { async function getPatrolTemplate (ctx, next) {
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { limit, page, userId } = ctx.query; const { userId } = ctx.fs.api
let userWhere = {}; const { limit, page } = ctx.query;
let options = { let options = {
include: [{ include: [{
required: true, required: true,
model: models.User, model: models.User,
attributes: ['id', 'name'], attributes: ['id', 'name'],
where: userWhere, },]
include: [{
required: true,
model: models.Department,
attributes: ['id', 'name'],
}]
}, {
required: true,
model: models.Project,
attributes: ['id', 'name'],
}]
}; };
if (limit) { if (limit) {
options.limit = Number(limit); options.limit = Number(limit);
@ -28,9 +18,6 @@ async function getPatrolTemplate (ctx, next) {
if (page && limit) { if (page && limit) {
options.offset = Number(page) * Number(limit); options.offset = Number(page) * Number(limit);
} }
if (userId) {
userWhere.id = userId;
}
let res = await models.PatrolTemplate.findAndCountAll(options); let res = await models.PatrolTemplate.findAndCountAll(options);
ctx.status = 200; ctx.status = 200;
ctx.body = res; ctx.body = res;
@ -47,11 +34,12 @@ async function createPatrolTemplate (ctx, next) {
const transaction = await ctx.fs.dc.orm.transaction(); const transaction = await ctx.fs.dc.orm.transaction();
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { userId } = ctx.fs.api
const data = ctx.request.body; const data = ctx.request.body;
const { name, describe, checkItems = [] } = data; const { name, describe, checkItems = [] } = data;
let Template = { let Template = {
name, describe name, describe, createUserId: userId
}; };
const templateRes = await models.PatrolTemplate.create( const templateRes = await models.PatrolTemplate.create(

5
api/app/lib/index.js

@ -54,12 +54,15 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq
}); });
const { Department, User, UserResource, Resource, Project, Point, PatrolPlan, const { Department, User, UserResource, Resource, Project, Point, PatrolPlan,
CheckItems, CheckItemsGroup CheckItems, CheckItemsGroup, PatrolTemplate
} = dc.models; } = dc.models;
UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' }); UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' }); User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' });
PatrolTemplate.belongsTo(User, { foreignKey: 'createUserId', targetKey: 'id' });
User.hasMany(PatrolTemplate, { foreignKey: 'createUserId', sourceKey: 'id' });
UserResource.belongsTo(Resource, { foreignKey: 'resourceId', targetKey: 'code' }); UserResource.belongsTo(Resource, { foreignKey: 'resourceId', targetKey: 'code' });
Resource.hasMany(UserResource, { foreignKey: 'resourceId', sourceKey: 'code' }); Resource.hasMany(UserResource, { foreignKey: 'resourceId', sourceKey: 'code' });
Resource.hasMany(Resource, { foreignKey: 'parentResource', sourceKey: 'code' }); Resource.hasMany(Resource, { foreignKey: 'parentResource', sourceKey: 'code' });

9
api/app/lib/models/patrol_template.js

@ -32,6 +32,15 @@ module.exports = dc => {
primaryKey: false, primaryKey: false,
field: "describe", field: "describe",
autoIncrement: false autoIncrement: false
},
createUserId: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "create_user_id",
autoIncrement: false
} }
}, { }, {
tableName: "patrol_template", tableName: "patrol_template",

17
api/log/development.log

@ -4177,3 +4177,20 @@ notNull Violation: PatrolPlan.patrolCount cannot be null
2023-01-31 14:46:10.386 - debug: [FS-LOGGER] Init. 2023-01-31 14:46:10.386 - debug: [FS-LOGGER] Init.
2023-01-31 14:46:10.690 - info: [FS-ATTACHMENT] Inject attachment mw into router. 2023-01-31 14:46:10.690 - info: [FS-ATTACHMENT] Inject attachment mw into router.
2023-01-31 14:46:10.691 - info: [FS-AUTH] Inject auth and api mv into router. 2023-01-31 14:46:10.691 - info: [FS-AUTH] Inject auth and api mv into router.
2023-02-21 16:38:56.761 - debug: [FS-LOGGER] Init.
2023-02-21 16:38:56.844 - info: [FS-ATTACHMENT] Inject attachment mw into router.
2023-02-21 16:38:56.845 - debug: init fs.attachment and inject it into app(app.fs.attachment) and runtime ctx(ctx.fs.attachment)
2023-02-21 16:38:56.845 - info: [FS-AUTH] Inject auth and api mv into router.
2023-02-21 16:39:01.077 - error: path: /patrolTemplate, error: SequelizeEagerLoadingError: user is not associated to patrolTemplate!
2023-02-21 16:42:23.577 - debug: [FS-LOGGER] Init.
2023-02-21 16:42:23.654 - info: [FS-ATTACHMENT] Inject attachment mw into router.
2023-02-21 16:42:23.655 - debug: init fs.attachment and inject it into app(app.fs.attachment) and runtime ctx(ctx.fs.attachment)
2023-02-21 16:42:23.655 - info: [FS-AUTH] Inject auth and api mv into router.
2023-02-21 16:42:30.481 - error: path: /patrolTemplate, error: SequelizeEagerLoadingError: user is not associated to patrolTemplate!
2023-02-21 16:53:23.186 - debug: [FS-LOGGER] Init.
2023-02-21 16:53:23.264 - info: [FS-ATTACHMENT] Inject attachment mw into router.
2023-02-21 16:53:23.265 - debug: init fs.attachment and inject it into app(app.fs.attachment) and runtime ctx(ctx.fs.attachment)
2023-02-21 16:53:23.265 - info: [FS-AUTH] Inject auth and api mv into router.
2023-02-21 17:02:47.295 - error: path: /patrolTemplate, error: ReferenceError: createUser is not defined
2023-02-21 17:08:28.472 - error: path: /patrolTemplate, error: ReferenceError: createUser is not defined
2023-02-21 17:08:39.999 - error: path: /patrolTemplate, error: ReferenceError: createUser is not defined

15
script/1.0.3/schema/1.create_template.sql

@ -1,16 +1,17 @@
create table patrol_template create table if not exists patrol_template
( (
id serial not null, id serial not null
constraint patrol_template_pk
primary key,
name varchar(128) not null, name varchar(128) not null,
describe varchar(1024) describe varchar(1024),
create_user integer not null
); );
create unique index patrol_template_id_uindex create unique index if not exists patrol_template_id_uindex
on patrol_template (id); on patrol_template (id);
alter table patrol_template
add constraint patrol_template_pk
primary key (id);
create table if not exists patrol_template_check_items create table if not exists patrol_template_check_items

43
web/client/src/sections/patrolManage/components/planTemplateModal.js

@ -1,12 +1,13 @@
import { Button, Form, Input, Modal, Select, DatePicker } from 'antd'; import { Button, Form, Input, Modal, Select, DatePicker } from 'antd';
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { createPatrolTemplate, delPatrolTemplate, updatePatrolTemplate, getPatrolTemplate } from '../actions/template';
import moment from 'moment'; import moment from 'moment';
const { RangePicker } = DatePicker; const { RangePicker } = DatePicker;
const { TextArea } = Input; const { TextArea } = Input;
const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) => { const PlanModal = ({ visible, onCancel, dispatch, type, curRecord, tableRef }) => {
const [form] = Form.useForm(); const [form] = Form.useForm();
return ( return (
@ -20,19 +21,34 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
onCancel(); onCancel();
}} }}
onOk={() => { onOk={() => {
if (type === 'view') {
form.resetFields();
onCancel();
return;
}
form form
.validateFields() .validateFields()
.then((values) => { .then((values) => {
form.resetFields();
const params = { const params = {
...values, ...values,
} }
// onCreate(params);
if (type === 'create') {
dispatch(createPatrolTemplate(params)).then(res => {
if (res.success) {
tableRef.current.reload();
form.resetFields();
onCancel();
}
})
} else {
dispatch(updatePatrolTemplate({
...params,
id: curRecord.id
})).then(res => {
if (res.success) {
tableRef.current.reload();
form.resetFields();
onCancel();
}
})
}
}) })
.catch((info) => { .catch((info) => {
console.log('Validate Failed:', info); console.log('Validate Failed:', info);
@ -46,7 +62,6 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
initialValues={{ initialValues={{
...curRecord, ...curRecord,
}} }}
disabled={type === 'view'}
> >
<Form.Item <Form.Item
name="name" name="name"
@ -55,14 +70,14 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
{ required: true, message: '请输入巡检模板名称' }, { required: true, message: '请输入巡检模板名称' },
]} ]}
> >
<Input disabled={type === 'view'} /> <Input />
</Form.Item> </Form.Item>
<Form.Item <Form.Item
name="describe" name="describe"
label="描述" label="描述"
rules={[]} rules={[]}
> >
<TextArea rows={4} disabled={type === 'view'} /> <TextArea rows={4} />
</Form.Item> </Form.Item>
<Form.Item <Form.Item
name="checkItems" name="checkItems"
@ -82,11 +97,11 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
options: [ options: [
{ {
label: 'Jack', label: 'Jack',
value: 'jack', value: 1,
}, },
{ {
label: 'Lucy', label: 'Lucy',
value: 'lucy', value: 2,
}, },
], ],
}, },
@ -95,7 +110,7 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
options: [ options: [
{ {
label: 'yiminghe', label: 'yiminghe',
value: 'Yiminghe', value: 3,
}, },
], ],
}, },

52
web/client/src/sections/patrolManage/containers/patrolTemplate.js

@ -3,7 +3,7 @@ import { connect } from 'react-redux';
import { Button, Popconfirm } from 'antd'; import { Button, Popconfirm } from 'antd';
import ProTable from '@ant-design/pro-table'; import ProTable from '@ant-design/pro-table';
import PlanTemplateModal from '../components/planTemplateModal'; import PlanTemplateModal from '../components/planTemplateModal';
import { createPatrolTemplate, delPatrolTemplate, updatePatrolTemplate, getPatrolTemplate } from '../actions/plan'; import { createPatrolTemplate, delPatrolTemplate, updatePatrolTemplate, getPatrolTemplate } from '../actions/template';
import moment from 'moment'; import moment from 'moment';
function PatrolTemplate (props) { function PatrolTemplate (props) {
@ -14,47 +14,32 @@ function PatrolTemplate (props) {
const [type, setType] = useState(); const [type, setType] = useState();
const [curRecord, setCurRecord] = useState(); const [curRecord, setCurRecord] = useState();
const onCreate = (values) => {
if (type === 'create') {
dispatch(createPatrolTemplate(values)).then(res => {
if (res.success) {
tableRef.current.reload();
}
})
} else {
dispatch(updatePatrolTemplate({ ...values, id: curRecord.id })).then(res => {
if (res.success) {
tableRef.current.reload();
}
})
}
setVisible(false);
};
const columns = [{ const columns = [{
title: '模板名称', title: '模板名称',
dataIndex: 'struName', dataIndex: 'name',
key: 'struName', key: 'name',
ellipsis: true, ellipsis: true,
render: (_, record) => {
return <div>{record?.project?.name}</div>
}
}, { }, {
title: '模板描述', title: '模板描述',
dataIndex: 'name', dataIndex: 'describe',
key: 'name', key: 'describe',
ellipsis: true ellipsis: true
}, { }, {
title: '操作人', title: '操作人',
dataIndex: 'frequency', dataIndex: 'user.name',
key: 'frequency', key: 'user.name',
ellipsis: true, ellipsis: true,
render: (t, r, i) => {
return r.user ? r.user.name : '-'
}
}, { }, {
title: '检查项', title: '检查项',
dataIndex: 'patrolPoints', dataIndex: 'checkItems',
key: 'patrolPoints', key: 'checkItems',
ellipsis: true, ellipsis: true,
render: (_, record) => <div>{record?.points?.length ? record?.points?.map(p => p.name).join() : '-'}</div> render: (_, r) => {
return
}
}, { }, {
title: '操作', title: '操作',
dataIndex: 'action', dataIndex: 'action',
@ -68,11 +53,6 @@ function PatrolTemplate (props) {
setCurRecord(record) setCurRecord(record)
setVisible(true) setVisible(true)
}}>修改</Button> }}>修改</Button>
<Button type="link" onClick={() => {
setType('view')
setCurRecord(record)
setVisible(true)
}}>查看</Button>
<Popconfirm <Popconfirm
title="确认删除?" title="确认删除?"
onConfirm={() => { onConfirm={() => {
@ -119,12 +99,12 @@ function PatrolTemplate (props) {
visible ? visible ?
<PlanTemplateModal <PlanTemplateModal
visible={visible} visible={visible}
onCreate={onCreate}
type={type} type={type}
curRecord={curRecord} curRecord={curRecord}
onCancel={() => { onCancel={() => {
setVisible(false); setVisible(false);
}} }}
tableRef={tableRef}
/> : null /> : null
} }
</> </>

Loading…
Cancel
Save