四好公路
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.3 KiB

'use strict';
const fs = require('fs');
const xlsxDownload = require('../../../../utils/xlsxDownload.js');
const moment = require('moment');
const request = require('superagent');
async function dataExport(ctx) {
try {
let tableAttributesCopy = {}
const models = ctx.fs.dc.models;
const { userId } = ctx.fs.api
const { exp, ids, roadLevel, municipalType,
patrolType } = ctx.query;
if (!exp) {
throw '参数错误';
}
const modalList = [
3 years ago
{
n: '道路',
k: 'road',
tableName: 'Road',
3 years ago
},
{
n: '桥梁',
k: 'bridge',
tableName: 'Bridge'
3 years ago
},
{
n: '运政车辆',
k: 'vehicle',
tableName: 'MunicipalVehicle',
3 years ago
},
{
n: '运政业户',
k: 'business',
tableName: 'MunicipalBusiness',
3 years ago
},
{
n: '在建项目',
k: 'project',
tableName: 'Project',
3 years ago
},
{
n: '治超',
k: 'overspeed',
tableName: 'Overspeed',
3 years ago
},
{
n: '公交线路',
k: 'busLine',
tableName: 'BusLine',
3 years ago
},
{
n: '公交车辆',
k: 'busCar',
tableName: 'BusCar',
3 years ago
},
{
n: '巡更记录',
k: 'patrol',
tableName: 'Report',
},
{
n: '养护记录',
k: 'maintenance',
tableName: 'Report',
},
3 years ago
]
const modalOption = modalList.find(item => item.k == exp);
if (!modalOption) {
throw '参数错误';
}
3 years ago
let findOption = {
where: {}
}
if (ids) {
findOption.where.id = { $in: ids.split(',') }
}
if (roadLevel) {
findOption.where.level = roadLevel
}
if (municipalType) {
findOption.where.type = municipalType
}
const exportData = await models[modalOption.tableName].findAll(findOption)
const tableAttributes = models[modalOption.tableName].tableAttributes
//过滤project部门字段
if (modalOption.tableName === 'Project') {
const { entryName, projectMileage, investment, buildUnit,
constructionControlUnit, designUnit, constructionUnit, qutityUnit, startTime, remark, done } = tableAttributes
tableAttributesCopy = {
entryName, projectMileage, investment, buildUnit,
constructionControlUnit, designUnit, constructionUnit, qutityUnit, startTime, remark, done
}
} else {
tableAttributesCopy = tableAttributes
}
3 years ago
let header = []
console.log('tableAttributesCopy', tableAttributesCopy)
for (let k in tableAttributesCopy) {
let comment = tableAttributes[k].comment
if (k != 'id' && comment) {
if (comment == '品名' && municipalType == '出租车') {
continue
}
if (patrolType) {
if (patrolType == 'road') {
} else if (patrolType == 'anomaly') {
} else {
// 正常的之前的巡查内容
if (comment == '工程名称') {
continue
}
}
}
3 years ago
header.push({
title: comment || '-',
3 years ago
key: k,
index: tableAttributes[k].index,
})
}
}
header.sort((a, b) => { return a.index - b.index })
const fileName = `${modalOption.n}_${moment().format('YYYYMMDDHHmmss')}` + '.csv'
2 years ago
const filePath = await xlsxDownload.simpleExcelDown({ data: exportData, header, fileName: fileName, exp })
3 years ago
const fileData = fs.readFileSync(filePath);
3 years ago
ctx.status = 200;
ctx.set('Content-Type', 'application/x-xls');
ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName));
ctx.body = fileData;
} catch (error) {
3 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function godTrans(ctx) {
2 years ago
try {
const models = ctx.fs.dc.models;
const { userId } = ctx.fs.api
const res = await request.get('https://report.amap.com/ajax/districtRank.do?linksType=1&cityCode=360100')
const data = JSON.parse(res.text)
const nanchang = data.find(item => item.name == '南昌县') || {}
ctx.status = 200
ctx.body = nanchang
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
module.exports = {
dataExport,
godTrans
};