巴林闲侠
2 years ago
10 changed files with 492 additions and 9 deletions
@ -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, |
|||
} |
@ -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; |
|||
}; |
@ -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; |
|||
}; |
@ -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); |
|||
}; |
@ -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); |
@ -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: '修改巡检模板失败' }, |
|||
}); |
|||
} |
@ -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); |
Loading…
Reference in new issue