Browse Source

巡检模板 api 查询

master
巴林闲侠 2 years ago
parent
commit
f1c05301cc
  1. 22
      api/app/lib/controllers/patrolManage/patrolTemplate.js
  2. 91
      api/app/lib/index.js
  3. 9
      api/app/lib/models/patrol_template.js
  4. 17
      api/log/development.log
  5. 19
      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) {
try {
const models = ctx.fs.dc.models;
const { limit, page, userId } = ctx.query;
let userWhere = {};
const { userId } = ctx.fs.api
const { limit, page } = ctx.query;
let options = {
include: [{
required: true,
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'],
}]
},]
};
if (limit) {
options.limit = Number(limit);
@ -28,9 +18,6 @@ async function getPatrolTemplate (ctx, next) {
if (page && limit) {
options.offset = Number(page) * Number(limit);
}
if (userId) {
userWhere.id = userId;
}
let res = await models.PatrolTemplate.findAndCountAll(options);
ctx.status = 200;
ctx.body = res;
@ -47,11 +34,12 @@ async function createPatrolTemplate (ctx, next) {
const transaction = await ctx.fs.dc.orm.transaction();
try {
const models = ctx.fs.dc.models;
const { userId } = ctx.fs.api
const data = ctx.request.body;
const { name, describe, checkItems = [] } = data;
let Template = {
name, describe
name, describe, createUserId: userId
};
const templateRes = await models.PatrolTemplate.create(

91
api/app/lib/index.js

@ -13,69 +13,72 @@ const schedule = require('./schedule')
// const apiLog = require('./middlewares/api-log');
module.exports.entry = function (app, router, opts) {
app.fs.logger.log('info', '[FS-AUTH]', 'Inject auth and api mv into router.');
app.fs.logger.log('info', '[FS-AUTH]', 'Inject auth and api mv into router.');
app.fs.api = app.fs.api || {};
app.fs.opts = opts || {};
app.fs.utils = app.fs.utils || {};
app.fs.api.authAttr = app.fs.api.authAttr || {};
app.fs.api.logAttr = app.fs.api.logAttr || {};
app.fs.api = app.fs.api || {};
app.fs.opts = opts || {};
app.fs.utils = app.fs.utils || {};
app.fs.api.authAttr = app.fs.api.authAttr || {};
app.fs.api.logAttr = app.fs.api.logAttr || {};
// 顺序固定 ↓
//redisConnect(app, opts)
socketConect(app, opts)
// 顺序固定 ↓
//redisConnect(app, opts)
socketConect(app, opts)
// 实例其他平台请求方法
//paasRequest(app, opts)
// 实例其他平台请求方法
//paasRequest(app, opts)
// clickHouse 数据库 client
//clickHouseClient(app, opts)
// clickHouse 数据库 client
//clickHouseClient(app, opts)
// 工具类函数
utils(app, opts)
// 工具类函数
utils(app, opts)
// 定时任务
schedule(app, opts)
// 定时任务
schedule(app, opts)
//鉴权中间件
router.use(authenticator(app, opts));
//鉴权中间件
router.use(authenticator(app, opts));
// 日志记录
// router.use(apiLog(app, opts));
// 日志记录
// router.use(apiLog(app, opts));
router = routes(app, router, opts);
router = routes(app, router, opts);
};
module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Sequelize, models: {} }
// 模型关系摘出来 初始化之后再定义关系才行
fs.readdirSync(path.join(__dirname, '/models')).forEach((filename) => {
require(`./models/${filename}`)(dc)
});
// 模型关系摘出来 初始化之后再定义关系才行
fs.readdirSync(path.join(__dirname, '/models')).forEach((filename) => {
require(`./models/${filename}`)(dc)
});
const { Department, User, UserResource, Resource, Project, Point, PatrolPlan,
CheckItems, CheckItemsGroup
} = dc.models;
const { Department, User, UserResource, Resource, Project, Point, PatrolPlan,
CheckItems, CheckItemsGroup, PatrolTemplate
} = dc.models;
UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' });
UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' });
UserResource.belongsTo(Resource, { foreignKey: 'resourceId', targetKey: 'code' });
Resource.hasMany(UserResource, { foreignKey: 'resourceId', sourceKey: 'code' });
Resource.hasMany(Resource, { foreignKey: 'parentResource', sourceKey: 'code' });
PatrolTemplate.belongsTo(User, { foreignKey: 'createUserId', targetKey: 'id' });
User.hasMany(PatrolTemplate, { foreignKey: 'createUserId', sourceKey: 'id' });
User.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' });
Department.hasMany(User, { foreignKey: 'departmentId', sourceKey: 'id' });
UserResource.belongsTo(Resource, { foreignKey: 'resourceId', targetKey: 'code' });
Resource.hasMany(UserResource, { foreignKey: 'resourceId', sourceKey: 'code' });
Resource.hasMany(Resource, { foreignKey: 'parentResource', sourceKey: 'code' });
Point.belongsTo(Project, { foreignKey: 'projectId', targetKey: 'id' });
Project.hasMany(Point, { foreignKey: 'projectId', sourceKey: 'id' });
User.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' });
Department.hasMany(User, { foreignKey: 'departmentId', sourceKey: 'id' });
PatrolPlan.belongsTo(Project, { foreignKey: 'structureId', targetKey: 'id' });
Project.hasMany(PatrolPlan, { foreignKey: 'structureId', sourceKey: 'id' });
Point.belongsTo(Project, { foreignKey: 'projectId', targetKey: 'id' });
Project.hasMany(Point, { foreignKey: 'projectId', sourceKey: 'id' });
PatrolPlan.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(PatrolPlan, { foreignKey: 'userId', sourceKey: 'id' });
PatrolPlan.belongsTo(Project, { foreignKey: 'structureId', targetKey: 'id' });
Project.hasMany(PatrolPlan, { foreignKey: 'structureId', sourceKey: 'id' });
CheckItems.belongsTo(CheckItemsGroup, { foreignKey: 'groupId', targetKey: 'id' });
CheckItemsGroup.hasMany(CheckItems, { foreignKey: 'groupId', sourceKey: 'id' });
PatrolPlan.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(PatrolPlan, { foreignKey: 'userId', sourceKey: 'id' });
CheckItems.belongsTo(CheckItemsGroup, { foreignKey: 'groupId', targetKey: 'id' });
CheckItemsGroup.hasMany(CheckItems, { foreignKey: 'groupId', sourceKey: 'id' });
};

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

@ -32,6 +32,15 @@ module.exports = dc => {
primaryKey: false,
field: "describe",
autoIncrement: false
},
createUserId: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "create_user_id",
autoIncrement: false
}
}, {
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.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-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

19
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,
name varchar(128) not null,
describe varchar(1024)
id serial not null
constraint patrol_template_pk
primary key,
name varchar(128) not null,
describe varchar(1024),
create_user integer not null
);
create unique index patrol_template_id_uindex
on patrol_template (id);
create unique index if not exists patrol_template_id_uindex
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

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

@ -1,12 +1,13 @@
import { Button, Form, Input, Modal, Select, DatePicker } from 'antd';
import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import { createPatrolTemplate, delPatrolTemplate, updatePatrolTemplate, getPatrolTemplate } from '../actions/template';
import moment from 'moment';
const { RangePicker } = DatePicker;
const { TextArea } = Input;
const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) => {
const PlanModal = ({ visible, onCancel, dispatch, type, curRecord, tableRef }) => {
const [form] = Form.useForm();
return (
@ -20,19 +21,34 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
onCancel();
}}
onOk={() => {
if (type === 'view') {
form.resetFields();
onCancel();
return;
}
form
.validateFields()
.then((values) => {
form.resetFields();
const params = {
...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) => {
console.log('Validate Failed:', info);
@ -46,7 +62,6 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
initialValues={{
...curRecord,
}}
disabled={type === 'view'}
>
<Form.Item
name="name"
@ -55,14 +70,14 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
{ required: true, message: '请输入巡检模板名称' },
]}
>
<Input disabled={type === 'view'} />
<Input />
</Form.Item>
<Form.Item
name="describe"
label="描述"
rules={[]}
>
<TextArea rows={4} disabled={type === 'view'} />
<TextArea rows={4} />
</Form.Item>
<Form.Item
name="checkItems"
@ -82,11 +97,11 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
options: [
{
label: 'Jack',
value: 'jack',
value: 1,
},
{
label: 'Lucy',
value: 'lucy',
value: 2,
},
],
},
@ -95,7 +110,7 @@ const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) =
options: [
{
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 ProTable from '@ant-design/pro-table';
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';
function PatrolTemplate (props) {
@ -14,47 +14,32 @@ function PatrolTemplate (props) {
const [type, setType] = 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 = [{
title: '模板名称',
dataIndex: 'struName',
key: 'struName',
dataIndex: 'name',
key: 'name',
ellipsis: true,
render: (_, record) => {
return <div>{record?.project?.name}</div>
}
}, {
title: '模板描述',
dataIndex: 'name',
key: 'name',
dataIndex: 'describe',
key: 'describe',
ellipsis: true
}, {
title: '操作人',
dataIndex: 'frequency',
key: 'frequency',
dataIndex: 'user.name',
key: 'user.name',
ellipsis: true,
render: (t, r, i) => {
return r.user ? r.user.name : '-'
}
}, {
title: '检查项',
dataIndex: 'patrolPoints',
key: 'patrolPoints',
dataIndex: 'checkItems',
key: 'checkItems',
ellipsis: true,
render: (_, record) => <div>{record?.points?.length ? record?.points?.map(p => p.name).join() : '-'}</div>
render: (_, r) => {
return
}
}, {
title: '操作',
dataIndex: 'action',
@ -68,11 +53,6 @@ function PatrolTemplate (props) {
setCurRecord(record)
setVisible(true)
}}>修改</Button>
<Button type="link" onClick={() => {
setType('view')
setCurRecord(record)
setVisible(true)
}}>查看</Button>
<Popconfirm
title="确认删除?"
onConfirm={() => {
@ -119,12 +99,12 @@ function PatrolTemplate (props) {
visible ?
<PlanTemplateModal
visible={visible}
onCreate={onCreate}
type={type}
curRecord={curRecord}
onCancel={() => {
setVisible(false);
}}
tableRef={tableRef}
/> : null
}
</>

Loading…
Cancel
Save