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

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 pool = new Pool({
user: 'postgres',
host: '10.8.30.32',
database: 'highways4good',
password: '123',
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 = [
// {
// 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: ['乡'],
// },
// {
// 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/工程一览/道路.xls'],
// n: '工程一览',
// tableName: 'project',
// defaultKey: ['done', 'type'],
// defaultValue: [false, 'road'],
// },
// {
// path: ['./data/工程一览/桥梁.xls'],
// n: '工程一览',
// tableName: 'project',
// defaultKey: ['done', 'type'],
// defaultValue: [false, 'bridge'],
// },
// {
// 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',
// },
]
for (let f of fileList) {
console.log(f.path);
// 读取数据文件
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]);
if (f.n == '工程一览') {
if (k == '项目进展情况' && v == '已完工') {
insertValues[0] = true
}
}
if (f.n == '公交车辆') {
if (k == '所属公司') {
insertValues.push(v.split(':')[1]);
continue
}
if (k == '所属车队') {
insertValues.push(v.split(':')[1]);
continue
}
}
insertValues.push(v);
}
}
insertStr += insertKeys.join(',') + ') VALUES (';
insertStr += insertKeys.map((k, i) => `$${i + 1}`).join(',') + ')';
// console.log(insertStr, insertValues);
console.log(`插入 ${insertValues}`);
await client.query(insertStr, insertValues);
// break;
}
// break;
}
}
// 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)
}