'use strict'; 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 } } } module.exports = { busCarLevelList, vehicleStatistic, };