@ -1297,6 +1297,13 @@ async function roadSpotPrepare (ctx) {
const sequelize = ctx . fs . dc . orm ;
const { countyPercentage , drawPeople , level , code } = ctx . request . body ;
//获取最新的一次
const lastSpotRes = await models . RoadSpotCheckPreview . findOne ( {
where : {
checked : true
} ,
order : [ [ 'date' , 'DESC' ] ] ,
} )
// 先查上次没查的范围内的 然后比较百分比 如果重叠 再查上次查过的
@ -1318,9 +1325,9 @@ async function roadSpotPrepare (ctx) {
}
async function getRoadSpot ( key , lastRoadIds = [ ] , inOrNot , otherWhere = [ ] ) {
if ( ! lastRoadIds . length && ! inOrNot ) {
return [ ]
}
// if (!lastRoadIds.length && !inOrNot) {
// return []
// }
return await sequelize . query ( `
SELECT id , chainage_mileage , route_code FROM road
WHERE del = false
@ -1342,131 +1349,92 @@ async function roadSpotPrepare (ctx) {
let last = null
let spotVillageIds = [ ]
async function filterRoad ( otherWhere , getRepeat = true ) {
//县道乡道随机抽取
async function extractRandomElement ( arr ) {
if ( ! arr . length || accumulationMileage >= targetMileage ) {
return
}
const index = Math . floor ( Math . random ( ) * arr . length ) ;
const extracted = arr . splice ( index , 1 ) [ 0 ] ;
if ( key == '村' && getRepeat == true ) {
spotRoadIds . push ( extracted . id )
accumulationMileage += parseFloat ( extracted . chainage_mileage )
if ( accumulationMileage >= targetMileage ) {
return
} else {
let roadUnSpotedRes = await getRoadSpot ( key , lastRoadIds , false , otherWhere )
if ( key == '乡' ) {
if ( code == '360121201000' ) {
let townY002 = roadUnSpotedRes . filter ( d => d . route_code . indexOf ( 'Y002' ) != - 1 )
if ( townY002 [ 0 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY002 [ 0 ] . id )
accumulationMileage += parseFloat ( townY002 [ 0 ] . chainage_mileage )
}
if ( countyPercentage == 75 ) {
if ( townY002 [ 1 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY002 [ 1 ] . id )
accumulationMileage += parseFloat ( townY002 [ 1 ] . chainage_mileage )
}
}
roadUnSpotedRes = roadUnSpotedRes . filter ( d => ! ( d . route_code . indexOf ( 'Y002' ) != - 1 ) )
}
if ( code == '360121105000' ) {
let townY031 = roadUnSpotedRes . filter ( d => d . route_code . indexOf ( 'Y031' ) != - 1 )
let townY032 = roadUnSpotedRes . filter ( d => d . route_code . indexOf ( 'Y032' ) != - 1 )
if ( townY031 [ 0 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY031 [ 0 ] . id )
accumulationMileage += parseFloat ( townY031 [ 0 ] . chainage_mileage )
}
if ( townY032 [ 0 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY032 [ 0 ] . id )
accumulationMileage += parseFloat ( townY032 [ 0 ] . chainage_mileage )
}
if ( countyPercentage == 75 ) {
if ( townY031 [ 1 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY031 [ 1 ] . id )
accumulationMileage += parseFloat ( townY031 [ 1 ] . chainage_mileage )
}
if ( townY032 [ 1 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY032 [ 1 ] . id )
accumulationMileage += parseFloat ( townY032 [ 1 ] . chainage_mileage )
}
}
roadUnSpotedRes = roadUnSpotedRes . filter ( d => ! ( d . route_code . indexOf ( 'Y031' ) != - 1 ) || ! ( d . route_code . indexOf ( 'Y032' ) != - 1 ) )
return await extractRandomElement ( arr )
}
}
if ( key == '县' ) {
let roadUnSpotedRes = await getRoadSpot ( key , lastRoadIds , false , otherWhere ) || [ ]
await extractRandomElement ( roadUnSpotedRes )
if ( accumulationMileage < targetMileage ) {
// 还小于 说明没取够
let roadUnSpotedRepeatRes = await getRoadSpot ( key , lastRoadIds , true , otherWhere )
await extractRandomElement ( roadUnSpotedRepeatRes )
}
} else if ( key == '乡' ) {
let roadUnSpotedRes = await getRoadSpot ( key , lastRoadIds , false , otherWhere )
if ( code == '360121201000' ) {
let townY002 = roadUnSpotedRes . filter ( d => d . route_code . indexOf ( 'Y002' ) != - 1 )
if ( townY002 [ 0 ] ) {
spotRoadIds . push ( townY002 [ 0 ] . id )
accumulationMileage += parseFloat ( townY002 [ 0 ] . chainage_mileage )
}
if ( countyPercentage == 75 ) {
if ( townY002 [ 1 ] ) {
spotRoadIds . push ( townY002 [ 1 ] . id )
accumulationMileage += parseFloat ( townY002 [ 1 ] . chainage_mileage )
}
}
for ( let r of roadUnSpotedRes ) {
if ( accumulationMileage >= targetMileage ) {
break ;
roadUnSpotedRes = roadUnSpotedRes . filter ( d => ! ( d . route_code . indexOf ( 'Y002' ) != - 1 ) )
}
if ( code == '360121105000' ) {
let townY031 = roadUnSpotedRes . filter ( d => d . route_code . indexOf ( 'Y031' ) != - 1 )
let townY032 = roadUnSpotedRes . filter ( d => d . route_code . indexOf ( 'Y032' ) != - 1 )
if ( townY031 [ 0 ] ) {
spotRoadIds . push ( townY031 [ 0 ] . id )
accumulationMileage += parseFloat ( townY031 [ 0 ] . chainage_mileage )
}
if ( townY032 [ 0 ] ) {
spotRoadIds . push ( townY032 [ 0 ] . id )
accumulationMileage += parseFloat ( townY032 [ 0 ] . chainage_mileage )
}
if ( countyPercentage == 75 ) {
if ( townY031 [ 1 ] ) {
spotRoadIds . push ( townY031 [ 1 ] . id )
accumulationMileage += parseFloat ( townY031 [ 1 ] . chainage_mileage )
}
spotRoadIds . push ( r . id )
accumulationMileage += parseFloat ( r . chainage_mileage )
if ( accumulationMileage >= targetMileage ) {
break ;
if ( townY032 [ 1 ] ) {
spotRoadIds . push ( townY032 [ 1 ] . id )
accumulationMileage += parseFloat ( townY032 [ 1 ] . chainage_mileage )
}
}
roadUnSpotedRes = roadUnSpotedRes . filter ( d => ! ( d . route_code . indexOf ( 'Y031' ) != - 1 ) && ! ( d . route_code . indexOf ( 'Y032' ) != - 1 ) )
}
if ( accumulationMileage < targetMileage && getRepeat ) {
await extractRandomElement ( roadUnSpotedRes )
if ( accumulationMileage < targetMileage ) {
// 还小于 说明没取够
let roadUnSpotedRepeatRes = await getRoadSpot ( key , lastRoadIds , true , otherWhere )
if ( key == '乡' ) {
if ( code == '360121201000' ) {
let townY002 = roadUnSpotedRepeatRes . filter ( d => d . route_code . indexOf ( 'Y002' ) != - 1 )
if ( townY002 [ 0 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY002 [ 0 ] . id )
accumulationMileage += parseFloat ( townY002 [ 0 ] . chainage_mileage )
}
if ( countyPercentage == 75 ) {
if ( townY002 [ 1 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY002 [ 1 ] . id )
accumulationMileage += parseFloat ( townY002 [ 1 ] . chainage_mileage )
}
}
roadUnSpotedRepeatRes = roadUnSpotedRepeatRes . filter ( d => ! ( d . route_code . indexOf ( 'Y002' ) != - 1 ) )
}
if ( code == '360121105000' ) {
let townY031 = roadUnSpotedRepeatRes . filter ( d => d . route_code . indexOf ( 'Y031' ) != - 1 )
let townY032 = roadUnSpotedRepeatRes . filter ( d => d . route_code . indexOf ( 'Y032' ) != - 1 )
if ( townY031 [ 0 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY031 [ 0 ] . id )
accumulationMileage += parseFloat ( townY031 [ 0 ] . chainage_mileage )
}
if ( townY032 [ 0 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY032 [ 0 ] . id )
accumulationMileage += parseFloat ( townY032 [ 0 ] . chainage_mileage )
}
if ( countyPercentage == 75 ) {
if ( townY031 [ 1 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY031 [ 1 ] . id )
accumulationMileage += parseFloat ( townY031 [ 1 ] . chainage_mileage )
}
if ( townY032 [ 1 ] && ! spotRoadIds . length ) {
spotRoadIds . push ( townY032 [ 1 ] . id )
accumulationMileage += parseFloat ( townY032 [ 1 ] . chainage_mileage )
}
}
roadUnSpotedRepeatRes = roadUnSpotedRepeatRes . filter ( d => ! ( d . route_code . indexOf ( 'Y031' ) != - 1 ) || ! ( d . route_code . indexOf ( 'Y032' ) != - 1 ) )
let roadUnSpotedRepeatRes = await getRoadSpot ( key , lastRoadIds , true , [ ... otherWhere , ` route_code not Like 'Y032%' ` , ` route_code not Like 'Y002%' ` , ` route_code not Like 'Y031%' ` ] )
await extractRandomElement ( roadUnSpotedRepeatRes )
}
}
}
} else if ( key == '村' ) {
for ( let r of roadUnSpotedRepeatRes ) {
if ( accumulationMileage >= targetMileage ) {
break ;
}
for await ( let villageId of villageIdList ) {
let roadUnSpotedRes = await getRoadSpot ( key , lastRoadIds , false , [ ... otherWhere , ` village_id= ${ villageId } ` ] )
for ( let r of roadUnSpotedRes ) {
spotRoadIds . push ( r . id )
accumulationMileage += parseFloat ( r . chainage_mileage )
if ( accumulationMileage >= targetMileage ) {
break ;
}
}
}
}
if ( key == '村' ) {
for await ( let villageId of villageIdList ) {
await filterRoad ( [
... otherWhere ,
` village_id= ${ villageId } `
] , false )
spotVillageIds . push ( villageId )
last = villageId
@ -1479,10 +1447,14 @@ async function roadSpotPrepare (ctx) {
// await filterRoad(otherWhere, true)
for await ( let villageId of villageIdList . reverse ( ) ) {
await filterRoad ( [
... otherWhere ,
` village_id= ${ villageId } `
] , true )
let roadUnSpotedRes = await getRoadSpot ( key , lastRoadIds , true , [ ... otherWhere , ` village_id= ${ villageId } ` ] )
for ( let r of roadUnSpotedRes ) {
spotRoadIds . push ( r . id )
accumulationMileage += parseFloat ( r . chainage_mileage )
if ( accumulationMileage >= targetMileage ) {
break ;
}
}
spotVillageIds . push ( villageId )
last = villageId
@ -1492,10 +1464,10 @@ async function roadSpotPrepare (ctx) {
}
}
}
} else {
await filterRoad ( otherWhere , true )
}
return [ spotRoadIds , accumulationMileage , last , spotVillageIds ]
}
@ -1528,20 +1500,9 @@ async function roadSpotPrepare (ctx) {
//获取最新的一次
const lastSpotRes = await models . RoadSpotCheckPreview . findOne ( {
where : {
checked : true
} ,
order : [ [ 'date' , 'DESC' ] ] ,
} )
let gather = { } //汇总
if ( level == '县' ) {
let lastCountyRoadIds = [ ] // 上次查得的县道id
@ -1564,6 +1525,7 @@ async function roadSpotPrepare (ctx) {
gather . countyRoad = {
id : 'countyRoad' ,
name : '南昌县交通运输局' ,
//总里程
county : countryRoadTotalMileage + countryRoadHide ,
@ -1667,7 +1629,7 @@ async function roadSpotPrepare (ctx) {
let spotVillageIds = lastSpotRes . villageId || [ ]
let lastEndVillage = lastSpotRes . lastAbstractVillage || { } //获取每个乡镇最后一个被抽取的村
let villageMil = lastSpotRes . villageMil || 0 //已抽取的村道公里数
let allTownCodeResTown = [ ]
@ -1715,19 +1677,6 @@ async function roadSpotPrepare (ctx) {
// spotVillageIds.push(villageRes[0].id)
spotFirstVillageId = villageRes [ 0 ] . id
} else {
allTownCodeResTown . push ( {
code : code ,
township : townRoadTotalMileage + townRoadHide ,
townshipParticipate : townRoadTotalMileage ,
townshipPresent : townMil_ ,
townshipDifferenceValue : townMil_ - townRoadNeedMileage ,
village : villageRoadTotalMileage + villageRoadTotalHide ,
villageParticipate : villageRoadTotalMileage ,
villagePresent : 0 ,
villageDifferenceValue : 0
} )
}
//按照与当前村距离排序获取村id
@ -1755,6 +1704,7 @@ async function roadSpotPrepare (ctx) {
let fidOne = town . find ( d => d . code == code )
gather [ code ] = {
id : code ,
name : fidOne . name ,
//总里程 乡
township : townRoadTotalMileage + townRoadHide ,
@ -1767,7 +1717,7 @@ async function roadSpotPrepare (ctx) {
//本次抽取的乡道id
townRoadId : spotTownRoadIds_ ,
//总里程 村
village : townRoadTotalMileage + townRoad Hide,
village : villageRoadTotalMileage + villageRoadTotal Hide,
//纳入考核里程 村
villageParticipate : villageRoadNeedMileage ,
//本次考核里程 村
@ -1838,7 +1788,7 @@ async function roadSpotPrepare (ctx) {
async function exportSpotRode ( ctx ) {
try {
const { models } = ctx . fs . dc ;
const { previewId , roadId , isgather } = ctx . query
const { previewId , roadId , isgather , code } = ctx . query
const previewRes = await models . RoadSpotCheckPreview . findOne ( {
where : {
@ -1904,7 +1854,11 @@ async function exportSpotRode (ctx) {
} , {
key : 'maintenanceCount' ,
title : '养护次数(次)' ,
} , ]
} ,
{
key : 'remark' ,
title : '备注' ,
} ]
let exportData = listRes . map ( ( { dataValues : item } ) => {
@ -1922,6 +1876,7 @@ async function exportSpotRode (ctx) {
subgradeWidth : road . subgradeWidth || 0 ,
chainageMileage : road . chainageMileage || 0 ,
maintenanceCount : item . maintenanceCount ,
remark : true
}
} )
@ -1961,11 +1916,55 @@ async function exportSpotRode (ctx) {
}
async function postRoadSpotNext ( ctx ) {
try {
const { models } = ctx . fs . dc ;
const { previewId , level , roadId } = ctx . request . body
const previewRes = await models . RoadSpotCheckPreview . findOne ( {
where : {
id : previewId
}
} )
let nextAbstract = previewRes && previewRes . nextAbstract || { }
if ( level == '县' ) {
nextAbstract . county = [ ... ( nextAbstract . county || [ ] ) , roadId ]
}
if ( level == '乡' ) {
nextAbstract . town = [ ... ( nextAbstract . town || [ ] ) , roadId ]
}
if ( level == '村' ) {
nextAbstract . village = [ ... ( nextAbstract . village || [ ] ) , roadId ]
}
await models . RoadSpotCheckPreview . update ( {
nextAbstract
} , {
where : {
id : previewId
} ,
} )
ctx . status = 204 ;
} catch ( error ) {
ctx . fs . logger . error ( ` path: ${ ctx . path } , error: ${ error } ` ) ;
ctx . status = 400 ;
ctx . body = {
message : typeof error == 'string' ? error : undefined
}
}
}
module . exports = {
reportList ,
reportPosition ,
reportDetail , createReport , deleteReport , reportHandle ,
spotPrepare , spotCheck , spotCheckDetail ,
roadSpotPrepare , confirmRoadSpot , roadSpotList , roadSpotDetail , roadSpotChange , roadSpotChangList , exportSpotRode ,
town
town , postRoadSpotNext
} ;