From 28212810c39318eea2d24989a589138feb168526 Mon Sep 17 00:00:00 2001 From: LUCAS Date: Tue, 30 Aug 2022 17:28:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BB=E6=8A=A4=E6=9B=B4=E6=96=B0=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/controllers/roadMaintenance/index.js | 99 +++++++++++++++++-- api/app/lib/routes/roadMaintenance/index.js | 10 +- 2 files changed, 101 insertions(+), 8 deletions(-) diff --git a/api/app/lib/controllers/roadMaintenance/index.js b/api/app/lib/controllers/roadMaintenance/index.js index fcb1720e..91d99cd4 100644 --- a/api/app/lib/controllers/roadMaintenance/index.js +++ b/api/app/lib/controllers/roadMaintenance/index.js @@ -5,22 +5,38 @@ const moment = require('moment'); const { QueryTypes } = require('sequelize'); async function getLastMonthCost(ctx) { + const { fourceUpdate } = ctx.query; let rslt = []; try { const startTime = new Date(moment().startOf('year')), endTime = new Date(moment().endOf('year').valueOf()); const models = ctx.fs.dc.models; const costList = await models.RoadMaintenanceCost.findAll({ where: { - maintenanceDate: { '$between': [startTime, endTime] } + maintenanceDate: { '$between': [startTime, endTime] }, + countyName: { $not: '南昌县*' } } }) - if (!costList || !costList.length) { + if (!costList || !costList.length || fourceUpdate) { const dataList = await models.RoadMaintenance.findAll(); + if (fourceUpdate) { + await models.RoadMaintenanceCost.destroy({ where: { id: { $not: null } } }); + } for (var i = 0; i < 12; i++) { + const dateTime = new Date(moment().month(i)); + + let nanChang = { + countyName: '南昌县*', + roadLengthCounty: 0, + roadLengthTownship: 0, + roadLengthVillage: 0, + roadCostCounty: 0, + roadCostTownship: 0, + roadCostVillage: 0, + maintenanceDate: dateTime + }; dataList.map(item => { const { countyHighway, townshipRoad, villageRoad, responsibleUnitForManagementAndMaintenance } = item; const randomNum = 0.1 * (0.8 + Math.random() / 2); - const dateTime = new Date(moment().month(i)); let data = { countyName: responsibleUnitForManagementAndMaintenance, @@ -29,14 +45,21 @@ async function getLastMonthCost(ctx) { roadLengthVillage: villageRoad ? villageRoad * randomNum : 0, maintenanceDate: dateTime } - data.roadCostCounty = data.roadLengthCounty * 15000 / 12, - data.roadCostTownship = data.roadLengthTownship * 7500 / 12, - data.roadCostVillage = data.roadLengthVillage * 3900 / 12; + data.roadCostCounty = typeof data.roadLengthCounty == 'number' && !isNaN(data.roadLengthCounty) ? data.roadLengthCounty * 15000 / 12 : 0, + data.roadCostTownship = typeof data.roadLengthTownship == 'number' && !isNaN(data.roadLengthTownship) ? data.roadLengthTownship * 7500 / 12 : 0, + data.roadCostVillage = typeof data.roadLengthVillage == 'number' && !isNaN(data.roadLengthVillage) ? data.roadLengthVillage * 3900 / 12 : 0; rslt.push(data) + nanChang.roadLengthCounty += data.roadLengthCounty; + nanChang.roadLengthTownship += data.roadLengthTownship; + nanChang.roadLengthVillage += data.roadLengthVillage; + nanChang.roadCostCounty += data.roadCostCounty; + nanChang.roadCostTownship += data.roadCostTownship; + nanChang.roadCostVillage += data.roadCostVillage; models.RoadMaintenanceCost.create(data) }) + models.RoadMaintenanceCost.create(nanChang) } } else { @@ -55,6 +78,70 @@ async function getLastMonthCost(ctx) { } } +async function getConcatData(ctx) { + try { + let startTime = new Date(moment().startOf('year')), + endTime = new Date(moment().endOf('month')); + + if (moment().year() == 2022) { + startTime = new Date(moment().startOf('year').month(3)) + } + const models = ctx.fs.dc.models; + const costList = await models.RoadMaintenanceCost.findAll({ + where: { + maintenanceDate: { '$between': [startTime, endTime] }, + countyName: '南昌县*' + }, + order: [['maintenanceDate', 'DESC']] + }) + ctx.status = 200; + ctx.body = costList; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +async function getRoadMaintenanceList(ctx) { + try { + const models = ctx.fs.dc.models; + const list = await models.RoadMaintenance.findAll(); + ctx.status = 200; + ctx.body = list; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +async function updateRoadMaintenanceList(ctx) { + try { + const data = ctx.request.body; + const models = ctx.fs.dc.models; + // const list = await models.RoadMaintenance.findAll(); + await models.RoadMaintenance.update(data, { where: { id: data.id } }) + + ctx.status = 200; + ctx.body = {message}; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + + module.exports = { getLastMonthCost, + getConcatData, + getRoadMaintenanceList, + updateRoadMaintenanceList, } \ No newline at end of file diff --git a/api/app/lib/routes/roadMaintenance/index.js b/api/app/lib/routes/roadMaintenance/index.js index dfd9e7e7..5df95bc0 100644 --- a/api/app/lib/routes/roadMaintenance/index.js +++ b/api/app/lib/routes/roadMaintenance/index.js @@ -7,7 +7,13 @@ module.exports = function (app, router, opts) { app.fs.api.logAttr['GET/road/maintenance/cost/query'] = { content: '获取上月道路养护费用', visible: false }; router.get('/road/maintenance/cost/query', roadMaintenance.getLastMonthCost); - // app.fs.api.logAttr['GET/road/maintenance/cost/query'] = { content: '获取上月道路养护费用', visible: false }; - // router.get('/road/maintenance/cost/query', roadMaintenance.getLastMonthCost); + app.fs.api.logAttr['GET/road/maintenance/cost/nanchang/query'] = { content: '获取南昌县道路养护费用', visible: false }; + router.get('/road/maintenance/cost/nanchang/query', roadMaintenance.getConcatData); + + app.fs.api.logAttr['GET/road/maintenance'] = { content: '获取南昌县道路养护数据', visible: false }; + router.get('/road/maintenance', roadMaintenance.getRoadMaintenanceList); + + app.fs.api.logAttr['POST/road/maintenance'] = { content: '更新南昌县道路养护数据', visible: false }; + router.post('/road/maintenance', roadMaintenance.updateRoadMaintenanceList); } \ No newline at end of file