diff --git a/api/app/lib/controllers/report/index.js b/api/app/lib/controllers/report/index.js new file mode 100644 index 00000000..df0d1199 --- /dev/null +++ b/api/app/lib/controllers/report/index.js @@ -0,0 +1,117 @@ +'use strict'; + +async function reportList (ctx) { + try { + const models = ctx.fs.dc.models; + const { limit, page, startTime, endTime, keyword } = ctx.query + let findOption = { + where: { + + }, + attributes: ['id', 'road', 'time'], + include: [{ + model: models.User, + attributes: ['name'] + }], + } + if (limit) { + findOption.limit = limit + } + if (page && limit) { + findOption.offset = page * limit + } + if (startTime && endTime) { + findOption.where = { + time: { + '$between': [startTime, endTime] + } + } + } + if (keyword) { + findOption.where.road = { + '$like': `%${keyword}%` + } + } + const reportRes = await models.Report.findAll(findOption) + + ctx.status = 200; + ctx.body = reportRes + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +async function reportDetail (ctx) { + try { + const models = ctx.fs.dc.models; + const { reportId } = ctx.params + + const reportRes = await models.Report.findOne({ + where: { + id: reportId + } + }) + + ctx.status = 200; + ctx.body = reportRes + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +async function createReport (ctx) { + try { + const { userId } = ctx.fs.api + const models = ctx.fs.dc.models; + const data = ctx.request.body; + + await models.Report.create({ + ...data, + userId, + time: new Date(), + }) + + ctx.status = 204 + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +async function deleteReport (ctx) { + try { + const models = ctx.fs.dc.models; + const { reportId } = ctx.params; + + await models.Report.destroy({ + where: { + id: reportId + } + }) + + ctx.status = 204 + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +// TODO 小程序填写道路名称的时候的道路筛选 是一起都返回 还是不断传关键字搜索返回 + +module.exports = { + reportList, reportDetail, createReport, deleteReport, +}; \ No newline at end of file diff --git a/api/app/lib/index.js b/api/app/lib/index.js index 41389303..72b6097a 100644 --- a/api/app/lib/index.js +++ b/api/app/lib/index.js @@ -27,8 +27,12 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq require(`./models/${filename}`)(dc) }); - const { User, Department } = dc.models; + const { User, Department, Report } = dc.models; // 定义外键 User.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' }); Department.hasMany(User, { foreignKey: 'departmentId', sourceKey: 'id' }); + + // 定义外键 + Report.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' }); + User.hasMany(Report, { foreignKey: 'userId', sourceKey: 'id' }); }; diff --git a/api/app/lib/models/report.js b/api/app/lib/models/report.js new file mode 100644 index 00000000..6b8cafd3 --- /dev/null +++ b/api/app/lib/models/report.js @@ -0,0 +1,160 @@ +/* eslint-disable*/ +'use strict'; + +module.exports = dc => { + const DataTypes = dc.ORM; + const sequelize = dc.orm; + const Report = sequelize.define("report", { + id: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: true, + field: "id", + autoIncrement: true, + unique: "report_id_uindex" + }, + reportType: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: null, + comment: "上报类型 巡查:patrol / 养护:conserve", + primaryKey: false, + field: "report_type", + autoIncrement: false + }, + projectType: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: null, + comment: "工程类型 道路:road / 桥梁:birdge / 涵洞:culvert", + primaryKey: false, + field: "project_type", + autoIncrement: false + }, + road: { + type: DataTypes.STRING, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "road", + autoIncrement: false + }, + roadSectionStart: { + type: DataTypes.STRING, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "road_section_start", + autoIncrement: false + }, + roadSectionEnd: { + type: DataTypes.STRING, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "road_section_end", + autoIncrement: false + }, + longitude: { + type: DataTypes.DOUBLE, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "longitude", + autoIncrement: false + }, + latitude: { + type: DataTypes.DOUBLE, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "latitude", + autoIncrement: false + }, + content: { + type: DataTypes.STRING, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "content", + autoIncrement: false + }, + scenePic: { + type: DataTypes.ARRAY(DataTypes.STRING), + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "scene_pic", + autoIncrement: false + }, + conserveBeforePic: { + type: DataTypes.ARRAY(DataTypes.STRING), + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "conserve_before_pic", + autoIncrement: false + }, + conserveUnderwayPic: { + type: DataTypes.ARRAY(DataTypes.STRING), + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "conserve_underway_pic", + autoIncrement: false + }, + conserveAfterPic: { + type: DataTypes.ARRAY(DataTypes.STRING), + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "conserve_after_pic", + autoIncrement: false + }, + userId: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: false, + field: "user_id", + autoIncrement: false + }, + time: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: false, + field: "time", + autoIncrement: false + }, + address: { + type: DataTypes.STRING, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "address", + autoIncrement: false + }, + }, { + tableName: "report", + comment: "", + indexes: [] + }); + dc.models.Report = Report; + return Report; +}; \ No newline at end of file diff --git a/api/app/lib/routes/report/index.js b/api/app/lib/routes/report/index.js new file mode 100644 index 00000000..a3581102 --- /dev/null +++ b/api/app/lib/routes/report/index.js @@ -0,0 +1,17 @@ +'use strict'; + +const report = require('../../controllers/report'); + +module.exports = function (app, router, opts) { + app.fs.api.logAttr['GET/report/list'] = { content: '获取上报列表', visible: false }; + router.get('/report/list', report.reportList); + + app.fs.api.logAttr['GET/report/:reportId/detail'] = { content: '获取上报详情', visible: false }; + router.get('/report/:reportId//detail', report.reportDetail); + + app.fs.api.logAttr['POST/report'] = { content: '创建上报', visible: false }; + router.post('/report', report.createReport); + + app.fs.api.logAttr['DEL/report/:reportId'] = { content: '删除上报', visible: false }; + router.del('/report/:reportId', report.deleteReport); +} \ No newline at end of file diff --git a/api/log/development.log b/api/log/development.log index 3344fc40..ddab91d4 100644 --- a/api/log/development.log +++ b/api/log/development.log @@ -7127,3 +7127,23 @@ 2022-07-21 16:08:58.916 - debug: [FS-LOGGER] Init. 2022-07-21 16:08:59.026 - info: [FS-ATTACHMENT] Inject attachment mw into router. 2022-07-21 16:08:59.027 - info: [FS-AUTH] Inject auth and api mv into router. +2022-07-21 18:04:15.192 - debug: [FS-LOGGER] Init. +2022-07-21 18:04:15.278 - info: [FS-ATTACHMENT] Inject attachment mw into router. +2022-07-21 18:04:15.278 - info: [FS-AUTH] Inject auth and api mv into router. +2022-07-21 19:05:35.649 - error: path: /department, error: SequelizeValidationError: notNull Violation: department.name cannot be null +2022-07-21 19:43:58.076 - debug: [FS-LOGGER] Init. +2022-07-21 19:43:58.144 - info: [FS-ATTACHMENT] Inject attachment mw into router. +2022-07-21 19:43:58.144 - info: [FS-AUTH] Inject auth and api mv into router. +2022-07-21 19:49:22.814 - debug: [FS-LOGGER] Init. +2022-07-21 19:49:22.917 - info: [FS-ATTACHMENT] Inject attachment mw into router. +2022-07-21 19:49:22.918 - info: [FS-AUTH] Inject auth and api mv into router. +2022-07-21 19:49:54.235 - error: path: /report/1/detail, error: ReferenceError: findOption is not defined +2022-07-21 19:50:06.642 - error: path: /report/1/detail, error: ReferenceError: findOption is not defined +2022-07-21 19:50:23.541 - debug: [FS-LOGGER] Init. +2022-07-21 19:50:23.619 - info: [FS-ATTACHMENT] Inject attachment mw into router. +2022-07-21 19:50:23.619 - info: [FS-AUTH] Inject auth and api mv into router. +2022-07-21 20:10:08.604 - debug: [FS-LOGGER] Init. +2022-07-21 20:10:08.705 - info: [FS-ATTACHMENT] Inject attachment mw into router. +2022-07-21 20:10:08.705 - info: [FS-AUTH] Inject auth and api mv into router. +2022-07-21 20:10:18.909 - error: path: /report, error: SequelizeValidationError: notNull Violation: report.userId cannot be null +2022-07-21 20:10:26.541 - error: path: /report, error: SequelizeValidationError: notNull Violation: report.userId cannot be null diff --git a/api/sequelize-automate.config.js b/api/sequelize-automate.config.js index cd6e5e4c..f1d34d04 100644 --- a/api/sequelize-automate.config.js +++ b/api/sequelize-automate.config.js @@ -27,7 +27,7 @@ module.exports = { typesDir: 'models', // 指定输出 TypeScript 类型定义的文件目录,只有 TypeScript / Midway 等会有类型定义 emptyDir: false, // !!! 谨慎操作 生成 models 之前是否清空 `dir` 以及 `typesDir` tables: null, // 指定生成哪些表的 models,如 ['user', 'user_post'];如果为 null,则忽略改属性 - skipTables: null, // 指定跳过哪些表的 models,如 ['user'];如果为 null,则忽略改属性 + skipTables: ['report'], // 指定跳过哪些表的 models,如 ['user'];如果为 null,则忽略改属性 tsNoCheck: false, // 是否添加 `@ts-nocheck` 注释到 models 文件中 ignorePrefix: [], // 生成的模型名称忽略的前缀,因为 项目中有以下表名是以 t_ 开头的,在实际模型中不需要, 可以添加多个 [ 't_data_', 't_',] ,长度较长的 前缀放前面 attrLength: false, // 在生成模型的字段中 是否生成 如 var(128)这种格式,公司一般使用 String ,则配置为 false