|
|
@ -540,13 +540,17 @@ async function roadSpotPrepare (ctx) { |
|
|
|
let lastCountyPercentage = 0 // 最后一次的县道百分比
|
|
|
|
let lastCountyRoadIds = [] // 上次查得的县道id
|
|
|
|
let lastTownRoadIds = [] // 上次查得的乡镇道id
|
|
|
|
let lastVillageRoadRoadIds = [] // 上次查得的村道id
|
|
|
|
let lastVillageRoadRoadIds = [-1] // 上次查得的村道id
|
|
|
|
let lastVillageIds = [] |
|
|
|
let lastAbstractVillage = {} |
|
|
|
let gather = [] //汇总
|
|
|
|
|
|
|
|
if (lastSpotRes) { |
|
|
|
lastCountyPercentage = lastSpotRes.countyPercentage |
|
|
|
lastCountyRoadIds = lastSpotRes.countyRoadId || [] |
|
|
|
lastAbstractVillage = lastSpotRes.lastAbstractVillage || {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let lastCounty = await models.RoadSpotCheckPreview.findAll({ |
|
|
|
where: { |
|
|
@ -558,9 +562,12 @@ async function roadSpotPrepare (ctx) { |
|
|
|
}) |
|
|
|
|
|
|
|
if (lastCounty) { |
|
|
|
let countyNnmter = 0 |
|
|
|
lastCounty.forEach(d => { |
|
|
|
lastTownRoadIds = lastTownRoadIds.concat(d.townshipRoadId) || [] |
|
|
|
|
|
|
|
if (countyNnmter < (100 - (countyPercentage == 75 ? 50 : 25))) { |
|
|
|
lastTownRoadIds = lastTownRoadIds.concat(d.townshipRoadId) || [] |
|
|
|
countyNnmter += (d.countyPercentage == 75 ? 50 : 25) |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
@ -574,10 +581,14 @@ async function roadSpotPrepare (ctx) { |
|
|
|
}) |
|
|
|
|
|
|
|
if (lastVillage) { |
|
|
|
let villageNnmter = 0 |
|
|
|
lastVillage.forEach(d => { |
|
|
|
lastVillageRoadRoadIds = lastVillageRoadRoadIds.concat(d.villageRoadId) || [] |
|
|
|
lastVillageIds = lastVillageIds.concat(d.villageId) || [] |
|
|
|
if (villageNnmter < (100 - (countyPercentage == 75 ? 20 : 10))) { |
|
|
|
lastVillageRoadRoadIds = lastVillageRoadRoadIds.concat(d.villageRoadId) || [] |
|
|
|
lastVillageIds = lastVillageIds.concat(d.villageId) || [] |
|
|
|
|
|
|
|
villageNnmter += (d.countyPercentage == 75 ? 20 : 10) |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
@ -627,6 +638,7 @@ async function roadSpotPrepare (ctx) { |
|
|
|
async function spotRoadId (key, lastRoadIds, targetMileage, otherWhere = [], villageIdList = []) { |
|
|
|
let spotRoadIds = [] |
|
|
|
let accumulationMileage = 0 |
|
|
|
let last = null |
|
|
|
|
|
|
|
async function filterRoad (otherWhere, getRepeat = true) { |
|
|
|
|
|
|
@ -671,6 +683,7 @@ async function roadSpotPrepare (ctx) { |
|
|
|
], false) |
|
|
|
|
|
|
|
spotVillageIds.push(villageId) |
|
|
|
last = villageId |
|
|
|
|
|
|
|
if (accumulationMileage >= targetMileage) { |
|
|
|
break; |
|
|
@ -678,13 +691,27 @@ async function roadSpotPrepare (ctx) { |
|
|
|
} |
|
|
|
if (accumulationMileage < targetMileage) { |
|
|
|
// 还小于 说明没取够
|
|
|
|
await filterRoad(otherWhere, true) |
|
|
|
// await filterRoad(otherWhere, true)
|
|
|
|
|
|
|
|
for await (let villageId of villageIdList.reverse()) { |
|
|
|
await filterRoad([ |
|
|
|
...otherWhere, |
|
|
|
`village_id=${villageId}` |
|
|
|
], true) |
|
|
|
|
|
|
|
spotVillageIds.push(villageId) |
|
|
|
last = villageId |
|
|
|
|
|
|
|
if (accumulationMileage >= targetMileage) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
await filterRoad(otherWhere, true) |
|
|
|
} |
|
|
|
|
|
|
|
return [spotRoadIds, accumulationMileage] |
|
|
|
return [spotRoadIds, accumulationMileage, last] |
|
|
|
} |
|
|
|
let villageMil = 0, townMil = 0, countryMil = 0; |
|
|
|
|
|
|
@ -723,6 +750,8 @@ async function roadSpotPrepare (ctx) { |
|
|
|
let spotVillageIds = [] |
|
|
|
|
|
|
|
let allTownCodeResTown = [] |
|
|
|
let lastEndVillage = {} //获取每个乡镇最后一个被抽取的村
|
|
|
|
|
|
|
|
for await (let t of allTownCodeRes) { |
|
|
|
// 遍历每个乡镇并抽取
|
|
|
|
let otherWhere = [`township_code='${t.township_code}'`] |
|
|
@ -743,26 +772,36 @@ async function roadSpotPrepare (ctx) { |
|
|
|
const villageRoadNeedMileage = villageRoadTotalMileage * (countyPercentage == 75 ? 20 : 10) / 100 |
|
|
|
|
|
|
|
let spotFirstVillageId = -1 |
|
|
|
// 随机选取一个不在上次查过的村
|
|
|
|
let villageRes = await sequelize.query(` |
|
|
|
SELECT id FROM village |
|
|
|
WHERE township_code = '${t.township_code}' |
|
|
|
${lastVillageIds.length ? `AND id NOT IN (
|
|
|
|
${lastVillageIds.map(item => `'${item}'`).join(',')},-1 |
|
|
|
)`: ''}
|
|
|
|
ORDER BY RANDOM() |
|
|
|
LIMIT 1 |
|
|
|
`, { type: QueryTypes.SELECT });
|
|
|
|
if (!villageRes.length) { |
|
|
|
// 没有村了,随机选一个
|
|
|
|
|
|
|
|
let villageRes = [] |
|
|
|
if (lastAbstractVillage[t.township_code]) { |
|
|
|
|
|
|
|
} else { |
|
|
|
// 随机选取一个不在上次查过的村
|
|
|
|
villageRes = await sequelize.query(` |
|
|
|
SELECT id FROM village |
|
|
|
WHERE township_code = '${t.township_code}' |
|
|
|
ORDER BY RANDOM() |
|
|
|
LIMIT 1 |
|
|
|
`, { type: QueryTypes.SELECT });
|
|
|
|
SELECT id FROM village |
|
|
|
WHERE township_code = '${t.township_code}' |
|
|
|
${lastVillageIds.length ? `AND id NOT IN (
|
|
|
|
${lastVillageIds.map(item => `'${item}'`).join(',')},-1 |
|
|
|
)`: ''}
|
|
|
|
ORDER BY RANDOM() |
|
|
|
LIMIT 1 |
|
|
|
`, { type: QueryTypes.SELECT });
|
|
|
|
if (!villageRes.length) { |
|
|
|
// 没有村了,随机选一个
|
|
|
|
villageRes = await sequelize.query(` |
|
|
|
SELECT id FROM village |
|
|
|
WHERE township_code = '${t.township_code}' |
|
|
|
ORDER BY RANDOM() |
|
|
|
LIMIT 1 |
|
|
|
`, { type: QueryTypes.SELECT });
|
|
|
|
} |
|
|
|
} |
|
|
|
if (villageRes.length) { |
|
|
|
|
|
|
|
if (lastAbstractVillage[t.township_code]) { |
|
|
|
spotVillageIds.push(lastAbstractVillage[t.township_code]) |
|
|
|
spotFirstVillageId = lastAbstractVillage[t.township_code] |
|
|
|
} else if (villageRes.length) { |
|
|
|
spotVillageIds.push(villageRes[0].id) |
|
|
|
spotFirstVillageId = villageRes[0].id |
|
|
|
} else { |
|
|
@ -793,6 +832,8 @@ async function roadSpotPrepare (ctx) { |
|
|
|
let spotVillageRoadIdsArr = await spotRoadId('村', lastVillageRoadRoadIds, villageRoadNeedMileage, otherWhere, villageCheckIdList) |
|
|
|
let spotVillageRoadIds_ = spotVillageRoadIdsArr[0] |
|
|
|
let villageMil_ = spotVillageRoadIdsArr[1] |
|
|
|
lastEndVillage[t.township_code] = spotVillageRoadIdsArr[2] |
|
|
|
|
|
|
|
spotVillageRoadIds = spotVillageRoadIds.concat(spotVillageRoadIds_) |
|
|
|
if (villageMil_) villageMil += villageMil_ |
|
|
|
|
|
|
@ -808,6 +849,8 @@ async function roadSpotPrepare (ctx) { |
|
|
|
villageDifferenceValue: villageMil_ - villageRoadNeedMileage |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (let index = 0; index < town.length; index++) { |
|
|
@ -846,7 +889,8 @@ async function roadSpotPrepare (ctx) { |
|
|
|
villageMil, |
|
|
|
townMil, |
|
|
|
countryMil, |
|
|
|
gather |
|
|
|
gather, |
|
|
|
lastAbstractVillage: lastEndVillage |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|