diff --git a/api/app/lib/controllers/patrolManage/patrolTemplate.js b/api/app/lib/controllers/patrolManage/patrolTemplate.js index 07a1938..ed33026 100644 --- a/api/app/lib/controllers/patrolManage/patrolTemplate.js +++ b/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( diff --git a/api/app/lib/index.js b/api/app/lib/index.js index 1411eb9..dab112e 100644 --- a/api/app/lib/index.js +++ b/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' }); }; diff --git a/api/app/lib/models/patrol_template.js b/api/app/lib/models/patrol_template.js index be11dd2..0d715a4 100644 --- a/api/app/lib/models/patrol_template.js +++ b/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", diff --git a/api/log/development.log b/api/log/development.log index 9aee79a..5394c5f 100644 --- a/api/log/development.log +++ b/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 diff --git a/script/1.0.3/schema/1.create_template.sql b/script/1.0.3/schema/1.create_template.sql index 77e0aaf..b08ef64 100644 --- a/script/1.0.3/schema/1.create_template.sql +++ b/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 diff --git a/web/client/src/sections/patrolManage/components/planTemplateModal.js b/web/client/src/sections/patrolManage/components/planTemplateModal.js index 362eb7e..d41ad71 100644 --- a/web/client/src/sections/patrolManage/components/planTemplateModal.js +++ b/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'} > - + -