'use strict'; const zhidiaoSystem = require('../../../../utils/zhidiaoSystem.js'); const moment = require('moment') async function busCarLevelList (ctx) { try { const models = ctx.fs.dc.models; const { userId } = ctx.fs.api; let data = [] const busCarRes = await models.BusCar.findAll({ attributes: ['id', 'company', 'fleet', 'vehicleLicensePlateNumber', 'line'], }) for (let c of busCarRes) { const { company, fleet, vehicleLicensePlateNumber, line } = c const corCompany = data.find(d => d.name === company) if (!corCompany) { data.push({ name: company || '--', child: [{ name: fleet || '--', child: [{ name: line || '--', child: [{ id: c.id, name: vehicleLicensePlateNumber, }] }] }] }) } else { const corFleet = corCompany.child.find(d => d.name === fleet) if (!corFleet) { corCompany.child.push({ name: fleet || '--', child: [{ name: line || '--', child: [{ id: c.id, name: vehicleLicensePlateNumber || '--', }] }] }) } else { const corLine = corFleet.child.find(d => d.name === line) if (!corLine) { corFleet.child.push({ name: line || '--', child: [{ id: c.id, name: vehicleLicensePlateNumber || '--', }] }) } else { corLine.child.push({ id: c.id, name: vehicleLicensePlateNumber || '--', }) } } } } ctx.status = 200 ctx.body = data } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function vehicleStatistic (ctx) { try { const models = ctx.fs.dc.models; const { userId } = ctx.fs.api; const taxiBusiness = await models.MunicipalBusiness.findAll({ where: { type: '出租车' }, attributes: ['id', 'nameOfBusinessOwner'] }) const hazardousGoodsBusiness = await models.MunicipalBusiness.findAll({ where: { type: '危货' }, attributes: ['id', 'nameOfBusinessOwner'] }) const passengerTransport = await models.Statistic.findOne({ where: { type: 'vehicle' } }) const freightTransport = await models.Statistic.findOne({ where: { type: 'freight' } }) const vehicleState = { passengerTransport: passengerTransport ? passengerTransport.count : 0, freightTransport: freightTransport ? freightTransport.count : 0, taxi: await models.MunicipalVehicle.count({ where: { type: '出租车' }, }), hazardousGoods: await models.MunicipalVehicle.count({ where: { type: '危货' } }), bus: await models.BusCar.count(), taxiBusiness: taxiBusiness, hazardousGoodsBusiness: hazardousGoodsBusiness, } ctx.status = 200 ctx.body = vehicleState } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function busRunRealTime (ctx) { try { const { models } = ctx.fs.dc; const { app } = ctx const busCarRes = await models.BusCar.findAll({ attributes: ['id', 'vehicleLicensePlateNumber',], }) const Authorization = zhidiaoSystem.createAuthorization() const busRunRes = await app.fs.zhidiaoRequest.get(`getBusRunList`, { header: { Authorization: Authorization } }) app.fs.zhidiaoData ? app.fs.zhidiaoData.busRun = busRunRes : app.fs.zhidiaoData = { busRun: busRunRes } let now = moment() let hourBefour = now.subtract(1, 'h') ctx.status = 200; ctx.body = (busRunRes || []).filter(b => b.runState == 1 && moment(b.siteTime).isAfter(hourBefour) && busCarRes.some(bc => bc.vehicleLicensePlateNumber == b.busNoChar)) } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function busLine (ctx) { try { const { models } = ctx.fs.dc; const { app } = ctx const { busLicence } = ctx.query const zhidiaoData = app.fs.zhidiaoData let rslt = {} if (zhidiaoData) { let corBus = (zhidiaoData.busRun || []).find(b => b.busNoChar == busLicence) if (corBus) { rslt = (zhidiaoData.busLine || {})[corBus.lineNo] || {} } } ctx.status = 200; ctx.body = rslt } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } module.exports = { busCarLevelList, vehicleStatistic, busRunRealTime, busLine, };