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

182 lines
7.2 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,
})
let appid = '20200917000567738';
let key = 'xXm4jsuuD38JIkkhEcK6';
const getAnswer = async (query) => {
let start = (new Date()).getTime();
let salt = start;
let str1 = appid + query + salt + key;
let sign = Hex.stringify(MD5(str1));
console.log(`翻译:${query}`);
let answer = await request.get('http://api.fanyi.baidu.com/api/trans/vip/translate').timeout(1000 * 30).query({
q: query,
appid: appid,
salt: salt,
from: 'zh',
to: 'en',
sign: sign
});
if (answer.body.error_code) {
console.warn(answer.body);
throw '百度不给力,快快debug'
}
let rslt = answer.body.trans_result;
// let upperCaseRslt = rslt[0].dst.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase()).replace(/ /g, '');
// let upperCaseRslt = rslt[0].dst.toUpperCase().replace(/ /g, '_');
// let upperCaseRslt = rslt[0].dst.toLowerCase().replace(/ /g, '_');
let upperCaseRslt = rslt[0].dst.replace(/\//g, ' ').replace(/'/g, '').replace(/\s{2,}/g, '');
console.log(`翻译结果:${upperCaseRslt}`);
while (((new Date()).getTime() - start) < (1000 / 8)) {//每s只能调用10次
continue;
}
return upperCaseRslt
}
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',
// },
]
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]);
insertValues.push(v);
if (f.n == '工程一览') {
if (k == '项目进展情况' && v == '已完工') {
insertValues[0] = true
}
}
}
}
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)
}