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: 'highwaytest', // password: '123', // port: 5432, // }) // 测试环境 const pool = new Pool({ user: 'highwayadmin', host: '10.8.40.223', database: 'highwaytest', password: 'highway123', port: 5432, }) // 商用环境 // const pool = new Pool({ // user: 'highwayadmin', // host: '10.8.40.223', // database: 'highway4good', // password: 'highway123', // port: 5432, // }) // const userId = '' const fun = async () => { const client = await pool.connect() try { await client.query('BEGIN') console.log(`开始`); let allVillageRes = (await client.query(`SELECT * FROM village`)).rows let allRoads = (await client.query(`SELECT * FROM road`)).rows let towns = (await client.query(`SELECT * FROM town`)).rows // 读取数据文件 let workbook = XLSX.readFile(path.join(__dirname, './data/sihao.xlsx')); let firstSheetName = workbook.SheetNames[0]; let worksheet = workbook.Sheets[firstSheetName]; let res = XLSX.utils.sheet_to_json(worksheet, { defval: '' }); let villageSql = '' let noExistVillage = '' let noExistVillageObj = {} let delSql = '' allRoads.forEach(r => { let isExit = res.find(s => s.route_code == r.route_code && s.section_no == r.section_no && s.route_name == r.route_name) if (!isExit) { delSql += `UPDATE road SET del = true WHERE id = ${r.id};\n` } }) if (delSql) fs.writeFileSync('delete_road.sql', delSql, 'utf-8'); for (let r of [...res]) { 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 = false let isDel = r['isdelete'] if (isDel) { isDel = isDel.toString() isDel = isDel.trim() if (isDel) { if (isDel == 1) { del = true } } } let mark = r['mark'] if (mark) { spot = false } else { spot = true } let rId = r.id if (rId) { rId = rId.toString() rId = rId.trim() if (rId) { r.id = rId } } let town = towns.find(x => x.name == r['乡镇定稿']) let township_code = town ? town.code : '' const arr = [] Object.keys(r).forEach(key => { arr.push(key) }) if (r.route_name == '中心公路一龚乐路') { console.log(1) } let isExit = allRoads.find(s => s.route_code == r.route_code && (s.section_no == r.section_no || !s.section_no) && s.route_name == r.route_name) if (isExit) { villageSql += `UPDATE road SET village_id = ${villageId}, "del" = ${del}, spot = ${spot}, "township_code" = ${township_code ? township_code : `null`} WHERE id = ${isExit.id};\n` } else { const { route_name = `null`, route_code = `null`, section_no = `null`, starting_place_name = `null`, start_station = `null`, category_of_starting_point_and_dividing_point = `null`, stop_place_name = `null`, category_of_dead_center_and_dividing_point = `null`, stop_station = `null`, section_type = `null`, route_code_before_road_network_adjustment = `null`, serial_number_of_original_section = `null`, starting_stake_number_of_the_original_road_section = `null`, ending_point_stake_no_of_the_original_road_section = `null`, route_level = `null`, nature_of_road_section = `null`, completion_time = `null`, reconstruction_time = `null`, nature_of_construction = `null`, gbm_and_civilized_model_road = `null`, landforms = `null`, nature_of_charges = `null`, toll_station = `null`, number_of_culverts = `null`, technical_level = `null`, pavement_type = `null`, pavement_width = `null`, subgrade_width = `null`, lane_characteristics = `null`, whether_it_is_open_to_traffic_in_sunny_or_rainy_days = `null`, design_speed_per_hour = `null`, urban_management_section_or_not = `null`, management_and_maintenance_unit = `null`, road_administration_unit = `null`, alimentation = `null`, source_of_listed_maintenance_funds = `null`, curing_time = `null`, greening_mileage = `null`, greening_mileaged = `null`, type_of_repeated_road_section = `null`, serial_number_of_repeated_section = `null`, repeated_section_route_code = `null`, planned_fund_category = `null`, planned_year = `null`, plan_document_no = `null`, plan_item_unique_code = `null`, planned_project_route_code = `null`, plan_project_name = `null`, planned_project_type = `null`, completion_status = `null`, year_of_completion = `null`, planned_fund_category__one = `null`, planned_year__one = `null`, plan_document_no__one = `null`, plan_item_unique_code__one = `null`, plan_project_name__one = `null`, completion_status__one = `null`, year_of_completion__one = `null`, station_range = `null`, reporting_unit = `null`, reason_for_change = `null`, change_time = `null`, last_repair_and_maintenance_year = `null`, whether_maintenance_managed_highway = `null`, remarks = `null`, route_code_of_last_year = `null`, route_name_of_last_year = `null`, starting_station_of_last_year = `null`, last_years_ending_point_stake_number = `null`, graphic_mileage = `null`, chainage_mileage = `null`, districtcounty = `null`, location_city = `null`, level = `null`, surface_thickness = `null`, } = r; villageSql += ` INSERT INTO road ( village_id, "del", spot, route_name, route_code, section_no, township_code, starting_place_name, start_station, category_of_starting_point_and_dividing_point, stop_place_name, category_of_dead_center_and_dividing_point, stop_station, section_type, route_code_before_road_network_adjustment, serial_number_of_original_section, starting_stake_number_of_the_original_road_section, ending_point_stake_no_of_the_original_road_section, route_level, nature_of_road_section, completion_time, reconstruction_time, nature_of_construction, gbm_and_civilized_model_road, landforms, nature_of_charges, toll_station, number_of_culverts, technical_level, pavement_type, pavement_width, subgrade_width, lane_characteristics, whether_it_is_open_to_traffic_in_sunny_or_rainy_days, design_speed_per_hour, urban_management_section_or_not, management_and_maintenance_unit, road_administration_unit, alimentation, source_of_listed_maintenance_funds, curing_time, greening_mileage, greening_mileaged, type_of_repeated_road_section, serial_number_of_repeated_section, repeated_section_route_code, planned_fund_category, planned_year, plan_document_no, plan_item_unique_code, planned_project_route_code, plan_project_name, planned_project_type, completion_status, year_of_completion, planned_fund_category__one, planned_year__one, plan_document_no__one, plan_item_unique_code__one, plan_project_name__one, completion_status__one, year_of_completion__one, station_range, reporting_unit, reason_for_change, change_time, last_repair_and_maintenance_year, whether_maintenance_managed_highway, remarks, route_code_of_last_year, route_name_of_last_year, starting_station_of_last_year, last_years_ending_point_stake_number, graphic_mileage, chainage_mileage, districtcounty, location_city, level, surface_thickness ) VALUES ( ${villageId || `null`}, ${del}, ${spot}, ${route_name ? `'${r.route_name}'` : `null`}, ${route_code ? `'${r.route_code}'` : `null`}, ${section_no ? `'${r.section_no}'` : `null`}, ${township_code ? `'${township_code}'` : `null`}, ${starting_place_name ? `'${r.starting_place_name}'` : `null`}, ${start_station ? `'${r.start_station}'` : `null`}, ${category_of_starting_point_and_dividing_point ? `'${r.category_of_starting_point_and_dividing_point}'` : `null`}, ${stop_place_name ? `'${r.stop_place_name}'` : `null`}, ${category_of_dead_center_and_dividing_point ? `'${r.category_of_dead_center_and_dividing_point}'` : `null`}, ${stop_station ? `'${r.stop_station}'` : `null`}, ${section_type ? `'${r.section_type}'` : `null`}, ${route_code_before_road_network_adjustment ? `'${r.route_code_before_road_network_adjustment}'` : `null`}, ${serial_number_of_original_section ? `'${r.serial_number_of_original_section}'` : `null`}, ${starting_stake_number_of_the_original_road_section ? `'${r.starting_stake_number_of_the_original_road_section}'` : `null`}, ${ending_point_stake_no_of_the_original_road_section ? `'${r.ending_point_stake_no_of_the_original_road_section}'` : `null`}, ${route_level ? `'${r.route_level}'` : `null`}, ${nature_of_road_section ? `'${r.nature_of_road_section}'` : `null`}, ${completion_time ? `'${r.completion_time}'` : `null`}, ${reconstruction_time ? `'${r.reconstruction_time}'` : `null`}, ${nature_of_construction ? `'${r.nature_of_construction}'` : `null`}, ${gbm_and_civilized_model_road ? `'${r.gbm_and_civilized_model_road}'` : `null`}, ${landforms ? `'${r.landforms}'` : `null`}, ${nature_of_charges ? `'${r.nature_of_charges}'` : `null`}, ${toll_station ? `'${r.toll_station}'` : `null`}, ${number_of_culverts ? `'${r.number_of_culverts}'` : `null`}, ${technical_level ? `'${r.technical_level}'` : `null`}, ${pavement_type ? `'${r.pavement_type}'` : `null`}, ${pavement_width ? `'${r.pavement_width}'` : `null`}, ${subgrade_width ? `'${r.subgrade_width}'` : `null`}, ${lane_characteristics ? `'${r.lane_characteristics}'` : `null`}, ${whether_it_is_open_to_traffic_in_sunny_or_rainy_days ? `'${r.whether_it_is_open_to_traffic_in_sunny_or_rainy_days}'` : `null`}, ${design_speed_per_hour ? `'${r.design_speed_per_hour}'` : `null`}, ${urban_management_section_or_not ? `'${r.urban_management_section_or_not}'` : `null`}, ${management_and_maintenance_unit ? `'${r.management_and_maintenance_unit}'` : `null`}, ${road_administration_unit ? `'${r.road_administration_unit}'` : `null`}, ${alimentation ? `'${r.alimentation}'` : `null`}, ${source_of_listed_maintenance_funds ? `'${r.source_of_listed_maintenance_funds}'` : `null`}, ${curing_time ? `'${r.curing_time}'` : `null`}, ${greening_mileage ? `'${r.greening_mileage}'` : `null`}, ${greening_mileaged ? `'${r.greening_mileaged}'` : `null`}, ${type_of_repeated_road_section ? `'${r.type_of_repeated_road_section}'` : `null`}, ${serial_number_of_repeated_section ? `'${r.serial_number_of_repeated_section}'` : `null`}, ${repeated_section_route_code ? `'${r.repeated_section_route_code}'` : `null`}, ${planned_fund_category ? `'${r.planned_fund_category}'` : `null`}, ${planned_year ? `'${r.planned_year}'` : `null`}, ${plan_document_no ? `'${r.plan_document_no}'` : `null`}, ${plan_item_unique_code ? `'${r.plan_item_unique_code}'` : `null`}, ${planned_project_route_code ? `'${r.planned_project_route_code}'` : `null`}, ${plan_project_name ? `'${r.plan_project_name}'` : `null`}, ${planned_project_type ? `'${r.planned_project_type}'` : `null`}, ${completion_status ? `'${r.completion_status}'` : `null`}, ${year_of_completion ? `'${r.year_of_completion}'` : `null`}, ${planned_fund_category__one ? `'${r.planned_fund_category__one}'` : `null`}, ${planned_year__one ? `'${r.planned_year__one}'` : `null`}, ${plan_document_no__one ? `'${r.plan_document_no__one}'` : `null`}, ${plan_item_unique_code__one ? `'${r.plan_item_unique_code__one}'` : `null`}, ${plan_project_name__one ? `'${r.plan_project_name__one}'` : `null`}, ${completion_status__one ? `'${r.completion_status__one}'` : `null`}, ${year_of_completion__one ? `'${r.year_of_completion__one}'` : `null`}, ${station_range ? `'${r.station_range}'` : `null`}, ${reporting_unit ? `'${r.reporting_unit}'` : `null`}, ${reason_for_change ? `'${r.reason_for_change}'` : `null`}, ${change_time ? `'${r.change_time}'` : `null`}, ${last_repair_and_maintenance_year ? `'${r.last_repair_and_maintenance_year}'` : `null`}, ${whether_maintenance_managed_highway ? `'${r.whether_maintenance_managed_highway}'` : `null`}, ${remarks ? `'${r.remarks}'` : `null`}, ${route_code_of_last_year ? `'${r.route_code_of_last_year}'` : `null`}, ${route_name_of_last_year ? `'${r.route_name_of_last_year}'` : `null`}, ${starting_station_of_last_year ? `'${r.starting_station_of_last_year}'` : `null`}, ${last_years_ending_point_stake_number ? `'${r.last_years_ending_point_stake_number}'` : `null`}, ${graphic_mileage ? `'${r.graphic_mileage}'` : `null`}, ${chainage_mileage ? `'${r.chainage_mileage}'` : `null`}, ${districtcounty ? `'${r.districtcounty}'` : `null`}, ${location_city ? `'${r.location_city}'` : `null`}, ${level ? `'${r.level}'` : `null`}, ${surface_thickness ? `'${r.surface_thickness}'` : `null`} );\n` } } // fs.writeFileSync('road_update.sql', villageSql, 'utf-8'); // fs.writeFileSync('no_exist_village.txt', noExistVillage, 'utf-8'); // await client.query('ROLLBACK') await client.query(delSql); await client.query(villageSql); await client.query('COMMIT') console.log('执行完毕~') } catch (e) { await client.query('ROLLBACK') console.log('执行错误~' + JSON.stringify(e)) throw e } finally { client.release(); } } fun() } catch (error) { console.error(error) }