zhaobing
2 years ago
9 changed files with 242 additions and 22 deletions
@ -0,0 +1,101 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
async function getTask(ctx) { |
||||
|
try { |
||||
|
const models = ctx.fs.dc.models |
||||
|
const query = ctx.query |
||||
|
const whereopt = { |
||||
|
isdanger: query.isdanger |
||||
|
} |
||||
|
console.log('whereopt', whereopt) |
||||
|
const whereRoadOpt = { |
||||
|
id: query.id |
||||
|
} |
||||
|
console.log('wherwhereRoadOpteopt', whereRoadOpt) |
||||
|
|
||||
|
const taskRes = await models.TaskManage.findAndCountAll({ |
||||
|
order: [['id', 'DESC']], |
||||
|
attributes: ['id', 'dangerDescription', 'isdanger', 'issuanceTime', 'reportTime'], |
||||
|
include: [ |
||||
|
{ |
||||
|
attributes: ['routeName', 'routeCode'], |
||||
|
model: models.Road, |
||||
|
where: query.id == undefined ? {} : whereRoadOpt |
||||
|
|
||||
|
}, |
||||
|
{ |
||||
|
attributes: ['name'], |
||||
|
model: models.User |
||||
|
} |
||||
|
], |
||||
|
where: query.isdanger == undefined ? {} : whereopt |
||||
|
}) |
||||
|
ctx.body = taskRes |
||||
|
console.log('tas', taskRes) |
||||
|
ctx.status = 200 |
||||
|
} |
||||
|
catch (error) { |
||||
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
||||
|
ctx.status = 400; |
||||
|
ctx.body = { |
||||
|
message: '获取失败' |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//删除任务
|
||||
|
async function delTask(ctx) { |
||||
|
try { |
||||
|
const models = ctx.fs.dc.models |
||||
|
const { id } = ctx.params |
||||
|
console.log('params', id) |
||||
|
await models.TaskManage.destroy({ where: { id: id } }) |
||||
|
ctx.status = 204 |
||||
|
} catch (error) { |
||||
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
||||
|
ctx.status = 400; |
||||
|
ctx.body = { |
||||
|
message: '删除失败' |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
//编辑任务
|
||||
|
async function editTask(ctx) { |
||||
|
//const transaction = await ctx.fs.dc.orm.transaction();
|
||||
|
try { |
||||
|
const models = ctx.fs.dc.models |
||||
|
const params = ctx.request.body |
||||
|
if (!params.id) { |
||||
|
|
||||
|
const road = await models.Road.findOne({ where: { routeCode: params.routeCode } }) |
||||
|
const user = await models.User.findOne({ where: { name: params.name } }) |
||||
|
console.log('params', road.id, user.id) |
||||
|
await models.TaskManage.create({ |
||||
|
roadid: road.id, userid: user.id, dangerDescription: params.dangerDescription.trim() |
||||
|
}) |
||||
|
} else { |
||||
|
const road = await models.Road.findOne({ where: { routeCode: params.routeCode } }) |
||||
|
const user = await models.User.findOne({ where: { name: params.name } }) |
||||
|
await models.TaskManage.update({ |
||||
|
roadid: road.id, userid: user.id, dangerDescription: params.dangerDescription.trim() |
||||
|
}, { where: { id: params.id } }) |
||||
|
} |
||||
|
ctx.status = 204 |
||||
|
//await transaction.commit();
|
||||
|
} catch (error) { |
||||
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
||||
|
ctx.status = 400; |
||||
|
ctx.body = { |
||||
|
message: '新增失败' |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
module.exports = { |
||||
|
getTask, delTask, editTask |
||||
|
}; |
@ -0,0 +1,87 @@ |
|||||
|
/* eslint-disable*/ |
||||
|
|
||||
|
'use strict'; |
||||
|
|
||||
|
module.exports = dc => { |
||||
|
const DataTypes = dc.ORM; |
||||
|
const sequelize = dc.orm; |
||||
|
const TaskManage = sequelize.define("taskManage", { |
||||
|
id: { |
||||
|
type: DataTypes.INTEGER, |
||||
|
allowNull: false, |
||||
|
defaultValue: null, |
||||
|
comment: "id", |
||||
|
primaryKey: true, |
||||
|
field: "id", |
||||
|
autoIncrement: true |
||||
|
}, |
||||
|
roadid: { |
||||
|
type: DataTypes.INTEGER, |
||||
|
allowNull: false, |
||||
|
defaultValue: null, |
||||
|
comment: "道路id", |
||||
|
primaryKey: false, |
||||
|
field: "roadid", |
||||
|
autoIncrement: false, |
||||
|
references: { |
||||
|
key: "id", |
||||
|
model: "road" |
||||
|
} |
||||
|
}, |
||||
|
dangerDescription: { |
||||
|
type: DataTypes.CHAR, |
||||
|
allowNull: true, |
||||
|
defaultValue: null, |
||||
|
comment: "隐患说明", |
||||
|
primaryKey: false, |
||||
|
field: "danger_description", |
||||
|
autoIncrement: false |
||||
|
}, |
||||
|
issuanceTime: { |
||||
|
type: DataTypes.DATE, |
||||
|
allowNull: true, |
||||
|
defaultValue: null, |
||||
|
comment: "下发时间", |
||||
|
primaryKey: false, |
||||
|
field: "issuance_time", |
||||
|
autoIncrement: false |
||||
|
}, |
||||
|
userid: { |
||||
|
type: DataTypes.INTEGER, |
||||
|
allowNull: false, |
||||
|
defaultValue: null, |
||||
|
comment: "用户id", |
||||
|
primaryKey: false, |
||||
|
field: "userid", |
||||
|
autoIncrement: false, |
||||
|
references: { |
||||
|
key: "id", |
||||
|
model: "user" |
||||
|
} |
||||
|
}, |
||||
|
isdanger: { |
||||
|
type: DataTypes.BOOLEAN, |
||||
|
allowNull: true, |
||||
|
defaultValue: null, |
||||
|
comment: "是否存在安全隐患,flase(不存在)", |
||||
|
primaryKey: false, |
||||
|
field: "isdanger", |
||||
|
autoIncrement: false |
||||
|
}, |
||||
|
reportTime: { |
||||
|
type: DataTypes.DATE, |
||||
|
allowNull: true, |
||||
|
defaultValue: null, |
||||
|
comment: "上报时间", |
||||
|
primaryKey: false, |
||||
|
field: "report_time", |
||||
|
autoIncrement: false |
||||
|
} |
||||
|
}, { |
||||
|
tableName: "task_manage", |
||||
|
comment: "", |
||||
|
indexes: [] |
||||
|
}); |
||||
|
dc.models.TaskManage = TaskManage; |
||||
|
return TaskManage; |
||||
|
}; |
Loading…
Reference in new issue