'use strict'; async function getTask(ctx) { try { const models = ctx.fs.dc.models const query = ctx.query const whereopt = { isdanger: query.isdanger } const whereRoadOpt = { id: query.id } const taskRes = await models.TaskManage.findAndCountAll({ order: [['id', 'DESC']], attributes: ['id', 'dangerDescription', 'isdanger', 'issuanceTime', 'reportTime'], include: [ { attributes: ['id', 'routeName', 'routeCode'], model: models.Road, where: query.id == undefined ? {} : whereRoadOpt }, { attributes: ['id', 'name'], model: models.User } ], where: query.isdanger == undefined ? {} : whereopt }) ctx.body = 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 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 const road = await models.Road.findOne({ where: { id: params.routeId } }) const user = await models.User.findOne({ where: { id: params.userId } }) if (!params.id) { await models.TaskManage.create({ roadid: road.id, userid: user.id, dangerDescription: params.dangerDescription.trim() }) } else { await models.TaskManage.update({ roadid: road.id, userid: user.id, dangerDescription: params.dangerDescription.trim() }, { where: { id: params.id } }) } ctx.status = 204 } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '新增失败' } } } module.exports = { getTask, delTask, editTask };