Browse Source

养护更新查询接口

dev
LUCAS 2 years ago
parent
commit
28212810c3
  1. 99
      api/app/lib/controllers/roadMaintenance/index.js
  2. 10
      api/app/lib/routes/roadMaintenance/index.js

99
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,
}

10
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);
}
Loading…
Cancel
Save