'use strict'; const roadKeyMap = require('./road.json') async function importIn (ctx) { // 数据导入 try { const models = ctx.fs.dc.models; const { level } = ctx.query; const data = ctx.request.body; const roadRes = await models.Road.findAll({ where: { level } }) let preCreateArr = [] for (let d of data) { if (roadRes.some(r => r.routeCode + r.sectionNo == d['路线代码'] + d['路段序号'])) { //repeat } else { // await models.Road.create(d); } } 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 get (ctx) { try { const models = ctx.fs.dc.models; const { level } = ctx.query; const roadRes = await models.Road.findAll({ where: { level }, order: [['id', 'DESC']] }) ctx.status = 200; ctx.body = roadRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function edit (ctx) { try { const models = ctx.fs.dc.models; const data = ctx.request.body; if (!data.roadId) { await models.Road.create(data) } else { await models.Road.update( data, { where: { id: data.roadId } }) } 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 del (ctx) { try { const models = ctx.fs.dc.models; const { roadId } = ctx.params; await models.Road.destroy({ where: { id: roadId } }) 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 } } } module.exports = { importIn, get, edit, del, };