四好公路
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.

216 lines
9.0 KiB

3 years ago
try {
const { Pool, Client } = require('pg')
const request = require('superagent');
const Hex = require('crypto-js/enc-hex');
const MD5 = require('crypto-js/md5');
const XLSX = require('xlsx')
const path = require('path')
const fs = require("fs");
const moment = require('moment');
3 years ago
// 连接数据库
const pool = new Pool({
user: 'postgres',
host: '10.8.30.32',
// database: 'highways4good',
database: 'highways4good_initd',
password: '123',
3 years ago
port: 5432,
})
const fun = async () => {
// note: we don't try/catch this because if connecting throws an exception
// we don't need to dispose of the client (it will be undefined)
const client = await pool.connect()
try {
await client.query('BEGIN')
const fileList = [
3 years ago
// {
// path: ['./data/道路/村道第三方.xls'],
// n: '道路',
// tableName: 'road',
// defaultKey: ['level'],
// defaultValue: ['村'],
// },
// {
// path: ['./data/道路/县道第三方.xls'],
// n: '道路',
// tableName: 'road',
// defaultKey: ['level'],
// defaultValue: ['县'],
// },
// {
// path: ['./data/道路/乡道第三方.xls'],
// n: '道路',
// tableName: 'road',
// defaultKey: ['level'],
// defaultValue: ['乡'],
// },
3 years ago
// {
// path: ['./data/桥梁/桥第三方.xls'],
// n: '桥梁',
// tableName: 'bridge'
// },
// {
3 years ago
// path: (() => {
// let p = [];
// fs.readdirSync(path.join(__dirname, '/data/运政/车辆/出租车')).forEach((filename) => {
// p.push(`./data/运政/车辆/出租车/${filename}`)
// });
// return p;
// })(),
// n: '运政车辆',
// tableName: 'municipal_vehicle',
// defaultKey: ['type'],
// defaultValue: ['出租车'],
// },
// {
// path: (() => {
// let p = [];
// fs.readdirSync(path.join(__dirname, '/data/运政/车辆/危货')).forEach((filename) => {
// p.push(`./data/运政/车辆/危货/${filename}`)
// });
// return p;
// })(),
// n: '运政车辆',
// tableName: 'municipal_vehicle',
// defaultKey: ['type'],
// defaultValue: ['危货'],
// },
// {
// path: ['./data/运政/业户/出租车/事业发展中心巡游出租业户信息表.xlsx'],
// n: '运政业户',
// tableName: 'municipal_business',
// defaultKey: ['type'],
// defaultValue: ['出租车'],
// },
// {
// path: (() => {
// let p = [];
// fs.readdirSync(path.join(__dirname, '/data/运政/业户/危货')).forEach((filename) => {
// p.push(`./data/运政/业户/危货/${filename}`)
// });
// return p;
// })(),
// n: '运政业户',
// tableName: 'municipal_business',
// defaultKey: ['type'],
// defaultValue: ['危货'],
// },
3 years ago
// {
// path: ['./data/工程一览/道路.xls'],
// n: '工程一览',
// tableName: 'project',
// defaultKey: ['done', 'type'],
// defaultValue: [false, 'road'],
// },
// {
// path: ['./data/工程一览/桥梁.xls'],
// n: '工程一览',
// tableName: 'project',
// defaultKey: ['done', 'type'],
// defaultValue: [false, 'bridge'],
// },
3 years ago
// {
// path: ['./data/治超/非现场处罚总台账更新至2022.7.5(最新).xlsx'],
// n: '治超',
// tableName: 'overspeed',
// },
// {
// path: ['./data/公交/运营线路/(四公司)南昌公交运营线路基础信息表2022年6月(总表).xlsx'],
// n: '公交线路',
// tableName: 'bus_line',
// },
// {
// path: ['./data/公交/车辆信息/四公司车辆信息(1).xls'],
// n: '公交车辆',
// tableName: 'bus_car',
// },
3 years ago
]
for (let f of fileList) {
3 years ago
console.log(f.path);
3 years ago
// 读取数据文件
3 years ago
for (let p of f.path) {
console.log(`读取 ${p}`);
let workbook = XLSX.readFile(path.join(__dirname, p))
let firstSheetName = workbook.SheetNames[0];
let worksheet = workbook.Sheets[firstSheetName];
let res = XLSX.utils.sheet_to_json(worksheet);
const keyMap = require(`./${f.n}_数据库表对应.json`);
console.log(keyMap);
for (let d of res) {
let insertStr = `INSERT INTO "${f.tableName}" (`;
let insertKeys = (f.defaultKey || []).concat([]);
let insertValues = (f.defaultValue || []).concat([]);
for (let k in keyMap) {
// 没做判重
let v = d[k];
if (v) {
insertKeys.push(keyMap[k]);
3 years ago
if (f.n == '工程一览') {
if (k == '项目进展情况' && v == '已完工') {
insertValues[0] = true
}
}
if (f.n == '公交车辆') {
if (k == '所属公司') {
insertValues.push(v.split(':')[1]);
continue
}
if (k == '所属车队') {
3 years ago
insertValues.push(v.split(':')[1].replace(/.公司/, ''));
continue
}
}
if (f.n == '治超') {
if (k == '检测时间') {
if (v) {
v = v.toString().replace(/\//g, '-').replace(/"/g, '');
if (moment(v).isValid()) {
insertValues.push(moment(v).format())
} else {
insertValues.push(null)
}
} else {
insertValues.push(null)
}
continue
}
}
insertValues.push(v);
3 years ago
}
3 years ago
}
3 years ago
insertStr += insertKeys.join(',') + ') VALUES (';
insertStr += insertKeys.map((k, i) => `$${i + 1}`).join(',') + ')';
// console.log(insertStr, insertValues);
3 years ago
console.log(`插入 ${insertValues}`);
3 years ago
await client.query(insertStr, insertValues);
// break;
3 years ago
}
// break;
3 years ago
}
}
// await client.query('ROLLBACK')
await client.query('COMMIT')
console.log('执行完毕~')
} catch (e) {
await client.query('ROLLBACK')
console.log('执行错误~')
throw e
} finally {
client.release();
}
}
fun()
} catch (error) {
console.error(error)
}