|
|
|
try {
|
|
|
|
const { Pool, Client } = require('pg')
|
|
|
|
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')
|
|
|
|
console.log(`开始`);
|
|
|
|
|
|
|
|
let allVillageRes = (await client.query(`SELECT * FROM village`)).rows
|
|
|
|
// console.log(allVillageRes);
|
|
|
|
|
|
|
|
// 读取数据文件
|
|
|
|
let workbook = XLSX.readFile(path.join(__dirname, './data/村道(新)(2).xlsx'));
|
|
|
|
let firstSheetName = workbook.SheetNames[0];
|
|
|
|
let worksheet = workbook.Sheets[firstSheetName];
|
|
|
|
let res = XLSX.utils.sheet_to_json(worksheet, {
|
|
|
|
defval: ''
|
|
|
|
});
|
|
|
|
|
|
|
|
let workbook2 = XLSX.readFile(path.join(__dirname, './data/县道(新).xlsx'));
|
|
|
|
let firstSheetName2 = workbook2.SheetNames[0];
|
|
|
|
let worksheet2 = workbook2.Sheets[firstSheetName2];
|
|
|
|
let res2 = XLSX.utils.sheet_to_json(worksheet2, {
|
|
|
|
defval: ''
|
|
|
|
});
|
|
|
|
|
|
|
|
let workbook3 = XLSX.readFile(path.join(__dirname, './data/乡道(新).xlsx'));
|
|
|
|
let firstSheetName3 = workbook3.SheetNames[0];
|
|
|
|
let worksheet3 = workbook3.Sheets[firstSheetName3];
|
|
|
|
let res3 = XLSX.utils.sheet_to_json(worksheet3, {
|
|
|
|
defval: ''
|
|
|
|
});
|
|
|
|
|
|
|
|
let villageSql = ''
|
|
|
|
let noExistVillage = ''
|
|
|
|
let noExistVillageObj = {}
|
|
|
|
|
|
|
|
for (let r of [...res, ...res2, ...res3]) {
|
|
|
|
console.log(r);
|
|
|
|
// if (r.id == 3259) {
|
|
|
|
// console.log(r);
|
|
|
|
// }
|
|
|
|
let villageId = null
|
|
|
|
if (r['所属行政村']) {
|
|
|
|
let v = r['所属行政村']
|
|
|
|
v = v.split(' ')[0]
|
|
|
|
v = v.trim()
|
|
|
|
if (v) {
|
|
|
|
let noExist = false
|
|
|
|
let corV = allVillageRes.find(village => village.name === v)
|
|
|
|
if (corV) {
|
|
|
|
villageId = corV.id
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (!v.endsWith('村')) {
|
|
|
|
v += '村'
|
|
|
|
let corV = allVillageRes.find(village => village.name === v)
|
|
|
|
if (corV) {
|
|
|
|
villageId = corV.id
|
|
|
|
} else {
|
|
|
|
noExist = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
noExist = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noExist) {
|
|
|
|
if (!noExistVillageObj[v]) {
|
|
|
|
noExistVillage += `${v}\n`
|
|
|
|
noExistVillageObj[v] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let del = false
|
|
|
|
let spot = true
|
|
|
|
|
|
|
|
let isDel = r['isdelete']
|
|
|
|
|
|
|
|
if (isDel) {
|
|
|
|
isDel = isDel.toString()
|
|
|
|
isDel = isDel.trim()
|
|
|
|
if (isDel) {
|
|
|
|
if (isDel == 1) {
|
|
|
|
del = true
|
|
|
|
} else if (isDel == 2) {
|
|
|
|
spot = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let rId = r.id
|
|
|
|
if (rId) {
|
|
|
|
rId = rId.toString()
|
|
|
|
rId = rId.trim()
|
|
|
|
if (rId) {
|
|
|
|
r.id = rId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r.id) {
|
|
|
|
villageSql += `UPDATE road SET village_id = ${villageId}, "del" = ${del}, spot = ${spot} WHERE id = ${r.id};\n`
|
|
|
|
} else {
|
|
|
|
villageSql += `
|
|
|
|
INSERT INTO road (
|
|
|
|
route_name,
|
|
|
|
village_id,
|
|
|
|
"del",
|
|
|
|
spot,
|
|
|
|
route_code,
|
|
|
|
section_no,
|
|
|
|
township_code,
|
|
|
|
stop_station,
|
|
|
|
section_type
|
|
|
|
) VALUES (
|
|
|
|
'${r.route_name}',
|
|
|
|
${villageId || `null`},
|
|
|
|
${del},
|
|
|
|
${spot},
|
|
|
|
${r.route_code ? `'${r.route_code}'` : `null`},
|
|
|
|
${r.section_no ? `'${r.section_no}'` : `null`},
|
|
|
|
${r.township_code ? `'${r.township_code}'` : `null`},
|
|
|
|
${r.stop_station ? `'${r.stop_station}'` : `null`},
|
|
|
|
${r.section_type ? `'${r.section_type}'` : `null`}
|
|
|
|
);\n`
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
fs.writeFileSync('village_update.sql', villageSql, 'utf-8');
|
|
|
|
fs.writeFileSync('no_exist_village.txt', noExistVillage, 'utf-8');
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|