Browse Source

巡检模板 UI 页面

master
巴林闲侠 2 years ago
parent
commit
5f1e7a38d3
  1. 174
      api/app/lib/controllers/patrolManage/patrolTemplate.js
  2. 43
      api/app/lib/models/patrol_template.js
  3. 51
      api/app/lib/models/patrol_template_check_items.js
  4. 17
      api/app/lib/routes/patrolManage/patrolTemplate.js
  5. 4
      api/sequelize-automate.config.js
  6. 30
      script/1.0.3/schema/1.create_template.sql
  7. 2
      web/client/src/sections/patrolManage/actions/index.js
  8. 46
      web/client/src/sections/patrolManage/actions/template.js
  9. 120
      web/client/src/sections/patrolManage/components/planTemplateModal.js
  10. 14
      web/client/src/sections/patrolManage/containers/patrolTemplate.js

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

@ -0,0 +1,174 @@
'use strict';
async function getPatrolTemplate (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { limit, page, userId } = ctx.query;
let userWhere = {};
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);
}
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;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "获取巡检模板失败"
}
}
}
async function createPatrolTemplate (ctx, next) {
const transaction = await ctx.fs.dc.orm.transaction();
try {
const models = ctx.fs.dc.models;
const data = ctx.request.body;
const { name, describe, checkItems = [] } = data;
let Template = {
name, describe
};
const templateRes = await models.PatrolTemplate.create(
Template,
{
transaction
}
);
await models.PatrolTemplateCheckItems.bulkCreate(
checkItems.map(cid => {
return {
templateId: templateRes.id,
checkItemsId: cid
}
})
, {
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": '新增巡检模板失败'
}
}
}
async function updatePatrolTemplate (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, describe, checkItems } = data;
let Template = { name, describe, checkItems };
if (data && data.id) {
await models.PatrolTemplate.update(Template, {
where: { id: data.id },
transaction
})
await models.PatrolTemplateCheckItems.destroy({
where: {
templateId: data.id
},
transaction
})
await models.PatrolTemplateCheckItems.bulkCreate(
checkItems.map(cid => {
return {
templateId: data.id,
checkItemsId: cid
}
})
, {
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 = {
"message": errMsg
}
}
}
async function delPatrolTemplate (ctx, next) {
const transaction = await ctx.fs.dc.orm.transaction();
try {
let errMsg = '删除巡检模板失败';
const models = ctx.fs.dc.models;
const { id } = ctx.params;
const record = await models.PatrolPlan.findOne({
where: { patrolTemplateId: id }
});
if (record) {
errMsg = '不能删除有巡检记录的模板';
throw errMsg;
}
await models.PatrolTemplate.destroy({
where: { id },
transaction
})
await models.PatrolTemplateCheckItems.destroy({
where: { templateId: 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 }
}
}
module.exports = {
getPatrolTemplate,
createPatrolTemplate,
updatePatrolTemplate,
delPatrolTemplate,
}

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

@ -0,0 +1,43 @@
/* eslint-disable*/
'use strict';
module.exports = dc => {
const DataTypes = dc.ORM;
const sequelize = dc.orm;
const PatrolTemplate = sequelize.define("patrolTemplate", {
id: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: true,
field: "id",
autoIncrement: true,
unique: "patrol_template_id_uindex"
},
name: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "name",
autoIncrement: false
},
describe: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
comment: null,
primaryKey: false,
field: "describe",
autoIncrement: false
}
}, {
tableName: "patrol_template",
comment: "",
indexes: []
});
dc.models.PatrolTemplate = PatrolTemplate;
return PatrolTemplate;
};

51
api/app/lib/models/patrol_template_check_items.js

@ -0,0 +1,51 @@
/* eslint-disable*/
'use strict';
module.exports = dc => {
const DataTypes = dc.ORM;
const sequelize = dc.orm;
const PatrolTemplateCheckItems = sequelize.define("patrolTemplateCheckItems", {
id: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: true,
field: "id",
autoIncrement: true,
unique: "patrol_template_check_items_id_uindex"
},
templateId: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "template_id",
autoIncrement: false,
references: {
key: "id",
model: "patrolTemplate"
}
},
checkItemsId: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "check_items_id",
autoIncrement: false,
references: {
key: "id",
model: "checkItems"
}
}
}, {
tableName: "patrol_template_check_items",
comment: "",
indexes: []
});
dc.models.PatrolTemplateCheckItems = PatrolTemplateCheckItems;
return PatrolTemplateCheckItems;
};

17
api/app/lib/routes/patrolManage/patrolTemplate.js

@ -0,0 +1,17 @@
'use strict';
const patrolTemplate = require('../../controllers/patrolManage/patrolTemplate');
module.exports = function (app, router, opts) {
app.fs.api.logAttr['GET/patrolTemplate'] = { content: '获取巡检模板', visible: false };
router.get('/patrolTemplate', patrolTemplate.getPatrolTemplate);
app.fs.api.logAttr['POST/patrolTemplate'] = { content: '新增巡检模板', visible: true };
router.post('/patrolTemplate', patrolTemplate.createPatrolTemplate);
app.fs.api.logAttr['PUT/patrolTemplate'] = { content: '修改巡检模板', visible: true };
router.put('/patrolTemplate', patrolTemplate.updatePatrolTemplate);
app.fs.api.logAttr['DELETE/patrolTemplate/:id'] = { content: '删除巡检模板', visible: true };
router.del('/patrolTemplate/:id', patrolTemplate.delPatrolTemplate);
};

4
api/sequelize-automate.config.js

@ -1,7 +1,7 @@
module.exports = {
// 数据库配置 与 sequelize 相同
dbOptions: {
database: 'ZhongDing',
database: 'Inspection',
username: 'FashionAdmin',
password: '123456',
dialect: 'postgres',
@ -26,7 +26,7 @@ module.exports = {
dir: './app/lib/models', // 指定输出 models 文件的目录
typesDir: 'models', // 指定输出 TypeScript 类型定义的文件目录,只有 TypeScript / Midway 等会有类型定义
emptyDir: false, // !!! 谨慎操作 生成 models 之前是否清空 `dir` 以及 `typesDir`
tables: ['hide_danger_report'], // 指定生成哪些表的 models,如 ['user', 'user_post'];如果为 null,则忽略改属性
tables: ['patrol_template','patrol_template_check_items'], // 指定生成哪些表的 models,如 ['user', 'user_post'];如果为 null,则忽略改属性
skipTables: [], // 指定跳过哪些表的 models,如 ['user'];如果为 null,则忽略改属性
tsNoCheck: false, // 是否添加 `@ts-nocheck` 注释到 models 文件中
ignorePrefix: ['t_',], // 生成的模型名称忽略的前缀,因为 项目中有以下表名是以 t_ 开头的,在实际模型中不需要, 可以添加多个 [ 't_data_', 't_',] ,长度较长的 前缀放前面

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

@ -0,0 +1,30 @@
create table patrol_template
(
id serial not null,
name varchar(128) not null,
describe varchar(1024)
);
create unique index 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
(
id serial not null
constraint patrol_template_check_items_pk
primary key,
template_id integer not null
constraint patrol_template_check_items_patrol_template_id_fk
references patrol_template,
check_items_id integer not null
constraint patrol_template_check_items_check_items_id_fk
references check_items
);
create unique index if not exists patrol_template_check_items_id_uindex
on patrol_template_check_items (id);

2
web/client/src/sections/patrolManage/actions/index.js

@ -2,8 +2,10 @@
import * as plan from './plan'
import * as record from './record'
import * as template from './template'
export default {
...plan,
...record,
...template,
}

46
web/client/src/sections/patrolManage/actions/template.js

@ -0,0 +1,46 @@
'use strict';
import { basicAction } from '@peace/utils'
import { ApiTable } from '$utils'
export function getPatrolTemplate() {
return dispatch => basicAction({
type: 'get',
dispatch: dispatch,
actionType: 'GET_PATROL_TEMPLATE',
url: ApiTable.patrolTemplate,
msg: { error: '获取巡检模板失败' },
});
}
export function createPatrolTemplate(data) {
return dispatch => basicAction({
type: 'post',
data,
dispatch: dispatch,
actionType: 'CREATE_PATROL_TEMPLATE',
url: ApiTable.patrolTemplate,
msg: { error: '新增巡检模板失败' },
});
}
export function delPatrolTemplate(id) {
return dispatch => basicAction({
type: 'del',
dispatch: dispatch,
actionType: 'DEL_PATROL_TEMPLATE',
url: ApiTable.delPatrolTemplate.replace('{id}', id),
msg: { error: '删除巡检模板失败' },
});
}
export function updatePatrolTemplate(data) {
return dispatch => basicAction({
type: 'put',
data,
dispatch: dispatch,
actionType: 'UPDATE_PATROL_TEMPLATE',
url: ApiTable.patrolTemplate,
msg: { error: '修改巡检模板失败' },
});
}

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

@ -0,0 +1,120 @@
import { Button, Form, Input, Modal, Select, DatePicker } from 'antd';
import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import moment from 'moment';
const { RangePicker } = DatePicker;
const { TextArea } = Input;
const PlanModal = ({ visible, onCreate, onCancel, dispatch, type, curRecord }) => {
const [form] = Form.useForm();
return (
<Modal
visible={visible}
title="新增巡检模板"
okText="确定"
cancelText="取消"
onCancel={() => {
form.resetFields();
onCancel();
}}
onOk={() => {
if (type === 'view') {
form.resetFields();
onCancel();
return;
}
form
.validateFields()
.then((values) => {
form.resetFields();
const params = {
...values,
}
// onCreate(params);
})
.catch((info) => {
console.log('Validate Failed:', info);
});
}}
>
<Form
form={form}
layout="vertical"
name="form_in_modal"
initialValues={{
...curRecord,
}}
disabled={type === 'view'}
>
<Form.Item
name="name"
label="模板名称"
rules={[
{ required: true, message: '请输入巡检模板名称' },
]}
>
<Input disabled={type === 'view'} />
</Form.Item>
<Form.Item
name="describe"
label="描述"
rules={[]}
>
<TextArea rows={4} disabled={type === 'view'} />
</Form.Item>
<Form.Item
name="checkItems"
label="检查项"
rules={[
{ required: true, message: '请选择检查项' },
]}
>
<Select
style={{
// width: 200,
}}
mode="multiple"
options={[
{
label: 'Manager',
options: [
{
label: 'Jack',
value: 'jack',
},
{
label: 'Lucy',
value: 'lucy',
},
],
},
{
label: 'Engineer',
options: [
{
label: 'yiminghe',
value: 'Yiminghe',
},
],
},
]}
/>
</Form.Item>
</Form>
</Modal >
);
};
function mapStateToProps (state) {
const { auth, userList, structureList } = state
return {
user: auth.user,
userList: userList.data || [],
structureList: structureList.data || [],
userLoading: userList.isRequesting,
struLoading: structureList.isRequesting
}
}
export default connect(mapStateToProps)(PlanModal);

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

@ -2,8 +2,8 @@ import React, { useState, useRef } from 'react';
import { connect } from 'react-redux';
import { Button, Popconfirm } from 'antd';
import ProTable from '@ant-design/pro-table';
import PlanModal from '../components/planModal';
import { createPatrolPlan, delPatrolPlan, updatePatrolPlan, getPatrolPlan } from '../actions/plan';
import PlanTemplateModal from '../components/planTemplateModal';
import { createPatrolTemplate, delPatrolTemplate, updatePatrolTemplate, getPatrolTemplate } from '../actions/plan';
import moment from 'moment';
function PatrolTemplate (props) {
@ -16,13 +16,13 @@ function PatrolTemplate (props) {
const onCreate = (values) => {
if (type === 'create') {
dispatch(createPatrolPlan(values)).then(res => {
dispatch(createPatrolTemplate(values)).then(res => {
if (res.success) {
tableRef.current.reload();
}
})
} else {
dispatch(updatePatrolPlan({ ...values, id: curRecord.id })).then(res => {
dispatch(updatePatrolTemplate({ ...values, id: curRecord.id })).then(res => {
if (res.success) {
tableRef.current.reload();
}
@ -76,7 +76,7 @@ function PatrolTemplate (props) {
<Popconfirm
title="确认删除?"
onConfirm={() => {
dispatch(delPatrolPlan(record.id)).then(res => {
dispatch(delPatrolTemplate(record.id)).then(res => {
if (res.success) {
tableRef.current.reload();
}
@ -99,7 +99,7 @@ function PatrolTemplate (props) {
pagination={{ pageSize: 10 }}
search={false}
request={async (params = {}) => {
const res = await dispatch(getPatrolPlan(params));
const res = await dispatch(getPatrolTemplate(params));
setDataSource(res?.payload.data?.rows);
return { ...res };
}}
@ -117,7 +117,7 @@ function PatrolTemplate (props) {
/>
{
visible ?
<PlanModal
<PlanTemplateModal
visible={visible}
onCreate={onCreate}
type={type}

Loading…
Cancel
Save