diff --git a/api/app/lib/controllers/report/index.js b/api/app/lib/controllers/report/index.js index 1df99b5e..34ab55d8 100644 --- a/api/app/lib/controllers/report/index.js +++ b/api/app/lib/controllers/report/index.js @@ -698,9 +698,9 @@ async function roadSpotPrepare (ctx) { } const villageNearRes = await sequelize.query(` - SELECT id,calc_village,diatance FROM village_distance + SELECT id,calc_village,distance FROM village_distance WHERE origin_village = ${spotFirstVillageId} - ORDER BY diatance ASC + ORDER BY distance ASC `, { type: QueryTypes.SELECT }) let villageCheckIdList = villageNearRes.map(item => item.calc_village) diff --git a/api/app/lib/models/village_distance.js b/api/app/lib/models/village_distance.js index b9695804..5d706998 100644 --- a/api/app/lib/models/village_distance.js +++ b/api/app/lib/models/village_distance.js @@ -2,59 +2,59 @@ 'use strict'; module.exports = dc => { - const DataTypes = dc.ORM; - const sequelize = dc.orm; - const VillageDistance = sequelize.define("villageDistance", { - id: { - type: DataTypes.INTEGER, - allowNull: false, - defaultValue: null, - comment: null, - primaryKey: true, - field: "id", - autoIncrement: true, - unique: "village_distance_id_uindex" - }, - originVillage: { - type: DataTypes.INTEGER, - allowNull: false, - defaultValue: null, - comment: "原点村庄", - primaryKey: false, - field: "origin_village", - autoIncrement: false, - references: { - key: "id", - model: "village" - } - }, - calcVillage: { - type: DataTypes.INTEGER, - allowNull: true, - defaultValue: null, - comment: "相对计算村庄", - primaryKey: false, - field: "calc_village", - autoIncrement: false, - references: { - key: "id", - model: "village" - } - }, - diatance: { - type: DataTypes.INTEGER, - allowNull: true, - defaultValue: null, - comment: "距离 m", - primaryKey: false, - field: "diatance", - autoIncrement: false - } - }, { - tableName: "village_distance", - comment: "", - indexes: [] - }); - dc.models.VillageDistance = VillageDistance; - return VillageDistance; + const DataTypes = dc.ORM; + const sequelize = dc.orm; + const VillageDistance = sequelize.define("villageDistance", { + id: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: true, + field: "id", + autoIncrement: true, + unique: "village_distance_id_uindex" + }, + originVillage: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: "原点村庄", + primaryKey: false, + field: "origin_village", + autoIncrement: false, + references: { + key: "id", + model: "village" + } + }, + calcVillage: { + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: null, + comment: "相对计算村庄", + primaryKey: false, + field: "calc_village", + autoIncrement: false, + references: { + key: "id", + model: "village" + } + }, + distance: { + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: null, + comment: "距离 m", + primaryKey: false, + field: "distance", + autoIncrement: false + } + }, { + tableName: "village_distance", + comment: "", + indexes: [] + }); + dc.models.VillageDistance = VillageDistance; + return VillageDistance; }; \ No newline at end of file diff --git a/scripts/1.4.0/data/1_calc_village_distance/Dockerfilenew b/scripts/1.4.0/data/1_calc_village_distance/Dockerfilenew index dba16607..a3d52b8d 100644 --- a/scripts/1.4.0/data/1_calc_village_distance/Dockerfilenew +++ b/scripts/1.4.0/data/1_calc_village_distance/Dockerfilenew @@ -1,5 +1,5 @@ FROM repository.anxinyun.cn/base-images/nodejs12:20.10.12.2 -COPY ./scripts/1.3.1/data/1_insert_report_data /var/app +COPY ./scripts/1.4.0/data/1_calc_village_distance /var/app WORKDIR /var/app RUN npm cache clean -f RUN rm -rf package-lock.json diff --git a/scripts/1.4.0/data/1_calc_village_distance/index.js b/scripts/1.4.0/data/1_calc_village_distance/index.js index a2623fbf..95fa6e5a 100644 --- a/scripts/1.4.0/data/1_calc_village_distance/index.js +++ b/scripts/1.4.0/data/1_calc_village_distance/index.js @@ -11,6 +11,7 @@ try { password: '123', database: 'highways4good', }) + const godKey = '21c2d970e1646bb9a795900dd00093ce' function getDistance (lat1, lon1, lat2, lon2) { var R = 6371000; // 半径 of the earth in meters @@ -44,60 +45,81 @@ try { await client.query('BEGIN') console.log(`开始`); - // 遍历南昌县所有村 - let pageNum = 1 - let continueSearch = true - for (; continueSearch;) { - console.log(`查询第${pageNum}页`); - const res = await request.get(`https://restapi.amap.com/v5/place/text?key=21c2d970e1646bb9a795900dd00093ce&keywords=%E6%9D%91%E5%A7%94%E4%BC%9A%7C%E6%9D%91%E6%B0%91%E5%A7%94%E5%91%98%E4%BC%9A&types=130106®ion=360121&citylimit=true&page_size=25&page_num=${pageNum}&extensions=all`) - - // console.log(res.body); - const data = res.body - console.log(`查得 ${data.pois.length} 条`); - if (data.count == 0) { - continueSearch = false - break; - } else { - pageNum++ - } - for await (let p of data.pois) { - if (p.name.includes('村委会') || p.name.includes('村民委员会')) { - let vName = p.name.replace('村委会', '').replace('村民委员会', '').replace(/\(.*?\)/g, '') - - if (vName.includes('乡')) { - let townKeyIndex = vName.indexOf('乡') - if (townKeyIndex >= 0) { - vName = vName.substring(townKeyIndex + 1) + const nanchangRes = await request.get(`https://restapi.amap.com/v3/config/district?key=${godKey}&keywords=360100&subdistrict=1`) + console.log(nanchangRes.body); + + let townshipList = nanchangRes.body.districts[0].districts + console.log(townshipList); + + for (let t of townshipList) { + console.log(`${t.name}`); + + let pageNum = 1 + let continueSearch = true + for (; continueSearch;) { + console.log(`查询第${pageNum}页`); + // const res = await request.get(`https://restapi.amap.com/v5/place/text?key=${godKey}&types=130106®ion=360000&citylimit=true&page_size=25&page_num=${pageNum}&extensions=all`) + + const res = await request.get(`https://restapi.amap.com/v3/place/text?key=${godKey}&keywords=%E6%9D%91%E5%A7%94%E4%BC%9A%7C%E6%9D%91%E6%B0%91%E5%A7%94%E5%91%98%E4%BC%9A&page_size=25&types=130106&citylimit=true&city=${t.adcode}&page=${pageNum}`) + + await new Promise(resolve => setTimeout(() => resolve(), 2000)); + + // console.log(res.body); + const data = res.body + console.log(`查得 ${data.pois.length} 条`); + if (data.count == 0) { + continueSearch = false + break; + } else { + pageNum++ + } + for await (let p of data.pois) { + console.log(p.name); + if (p.name.includes('村委会') || p.name.includes('村民委员会')) { + let vName = p.name.replace('村委会', '').replace('村民委员会', '').replace(/\(.*?\)/g, '') + + if (vName.includes('乡')) { + let townKeyIndex = vName.indexOf('乡') + if (townKeyIndex >= 0) { + vName = vName.substring(townKeyIndex + 1) + } } - } - if (vName.includes('镇')) { - let townKeyIndex = vName.indexOf('镇') - if (townKeyIndex >= 0) { - vName = vName.substring(townKeyIndex + 1) + if (vName.includes('镇')) { + let townKeyIndex = vName.indexOf('镇') + if (townKeyIndex >= 0) { + vName = vName.substring(townKeyIndex + 1) + } } - } - if (!vName.endsWith('村')) { - vName = vName + '村' - } + if (!vName.endsWith('村')) { + vName = vName + '村' + } - console.log(`${vName}`); - let locationArr = p.location.split(',') + // console.log(`${vName}`); + let locationArr = p.location.split(',') - // 查询镇名是否存在 - const existRes = await client.query( - `SELECT * FROM village WHERE name=$1`, - [vName] - ) + // 查询镇名是否存在 + let existRes = await client.query( + `SELECT * FROM village WHERE name=$1`, + [vName] + ) + if (existRes.rowCount == 0) { + existRes = await client.query( + `SELECT * FROM village WHERE name=$1`, + [vName + '委会'] + ) + } - if (existRes.rowCount == 0) { + if (existRes.rowCount == 0) { - } else { - let existV = existRes[0] - await client.query( - `UPDATE village SET longitude=$1, latitude=$2 WHERE id=$3`, - [locationArr[0], locationArr[1], existV.id] - ) + } else { + let existV = existRes.rows[0] + // console.log(existRes); + await client.query( + `UPDATE village SET longitude=$1, latitude=$2 WHERE id=$3`, + [locationArr[0], locationArr[1], existV.id] + ) + } } } } @@ -121,7 +143,8 @@ try { continue } let distance = getDistance(v.latitude, v.longitude, vv.latitude, vv.longitude) - console.log(`${v.name} 与 ${vv.name} 的距离为 ${distance} 米`); + + // console.log(`${v.name} 与 ${vv.name} 的距离为 ${distance} 米`); distance = Math.round(distance) const existRes = await client.query( @@ -134,8 +157,9 @@ try { [v.id, vv.id, distance] ) } else { + let upSql = `UPDATE village_distance SET distance=$1 WHERE origin_village=$2 AND calc_village=$3` await client.query( - `UPDATE village_distance SET distance=$1 WHERE origin_village=$2 AND calc_village=$` + upSql , [distance, v.id, vv.id] ) } diff --git a/scripts/1.4.0/schema/2.create_village_table.sql b/scripts/1.4.0/schema/2.create_village_table.sql index a0c148b6..b128c9cc 100644 --- a/scripts/1.4.0/schema/2.create_village_table.sql +++ b/scripts/1.4.0/schema/2.create_village_table.sql @@ -34,7 +34,7 @@ create table if not exists village_distance calc_village integer constraint village_distance_village_id_fk_2 references village, - diatance integer + distance integer ); comment on table village_distance is '村之间的距离'; @@ -43,7 +43,7 @@ comment on column village_distance.origin_village is '原点村庄'; comment on column village_distance.calc_village is '相对计算村庄'; -comment on column village_distance.diatance is '距离 m'; +comment on column village_distance.distance is '距离 m'; create unique index if not exists village_distance_id_uindex on village_distance (id);