diff --git a/api/app/lib/controllers/report/index.js b/api/app/lib/controllers/report/index.js index bc35f341..36693e1f 100644 --- a/api/app/lib/controllers/report/index.js +++ b/api/app/lib/controllers/report/index.js @@ -516,404 +516,404 @@ async function spotCheckDetail (ctx) { } -async function roadSpotPrepare (ctx) { - try { - const { models } = ctx.fs.dc; - const sequelize = ctx.fs.dc.orm; - const { countyPercentage } = ctx.request.body; - - const lastSpotRes = await models.RoadSpotCheckPreview.findOne({ - where: { - checked: true - }, - // attributes: ['id', 'countyPercentage', 'date',], - order: [['date', 'DESC']], - // include: [{ - // model: models.RoadSpotCheck, - // include: [{ - // model: models.Road, - // // attributes: ['id', 'name'] - // }] - // }] - }) - - let lastCountyPercentage = 0 // 最后一次的县道百分比 - let lastCountyRoadIds = [] // 上次查得的县道id - let lastTownRoadIds = [] // 上次查得的乡镇道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: { - checked: true, - id: { $lte: lastSpotRes.id } - }, - limit: 3, - order: [['date', 'DESC']], - }) - - if (lastCounty) { - let countyNnmter = 0 - lastCounty.forEach(d => { - if (countyNnmter < (100 - (countyPercentage == 75 ? 50 : 25))) { - lastTownRoadIds = lastTownRoadIds.concat(d.townshipRoadId) || [] - countyNnmter += (d.countyPercentage == 75 ? 50 : 25) - } - }) - } - - let lastVillage = await models.RoadSpotCheckPreview.findAll({ - where: { - checked: true, - id: { $lte: lastSpotRes.id } - }, - limit: 9, - order: [['date', 'DESC']], - }) - - if (lastVillage) { - let villageNnmter = 0 - lastVillage.forEach(d => { - if (villageNnmter < (100 - (countyPercentage == 75 ? 20 : 10))) { - lastVillageRoadRoadIds = lastVillageRoadRoadIds.concat(d.villageRoadId) || [] - lastVillageIds = lastVillageIds.concat(d.villageId) || [] - - villageNnmter += (d.countyPercentage == 75 ? 20 : 10) - } - }) - } - - - // lastTownRoadIds = lastSpotRes.townshipRoadId || [] - // lastVillageRoadRoadIds = lastSpotRes.villageRoadId || [] - // lastVillageIds = lastSpotRes.villageId || [] - } - - // 先查上次没查的范围内的 然后比较百分比 如果重叠 再查上次查过的 - let keyMap = { - '县': `route_code LIKE 'X%'`, - '乡': `route_code LIKE 'Y%'`, - '村': `route_code LIKE 'C%'` - } - async function getRoadTotalMileage (key, otherWhere = [], spot) { - let res = await sequelize.query(` - SELECT - SUM(COALESCE(CAST(chainage_mileage AS DOUBLE PRECISION), 0)) AS total_mileage - FROM road - WHERE del = false - AND spot = ${spot} - AND ${keyMap[key]} ${otherWhere.length ? `AND ${otherWhere.join(' AND ')}` : ''} - `) - return res[0][0].total_mileage || 0 - } - - async function getRoadSpot (key, lastRoadIds = [], inOrNot, otherWhere = []) { - if (!lastRoadIds.length && !inOrNot) { - return [] - } - return await sequelize.query(` - SELECT id, chainage_mileage FROM road - WHERE del = false - AND spot = true - AND ${keyMap[key]} - ${lastRoadIds.length ? - `AND id ${inOrNot ? 'IN' : 'NOT IN'} ( - ${lastRoadIds.map(item => `'${item}'`).join(',')},-1 - )` : '' - } - AND chainage_mileage IS NOT NULL ${otherWhere.length ? `AND ${otherWhere.join(' AND ')}` : ''} - ORDER BY RANDOM() - `, { type: QueryTypes.SELECT }); - } - - async function spotRoadId (key, lastRoadIds, targetMileage, otherWhere = [], villageIdList = []) { - let spotRoadIds = [] - let accumulationMileage = 0 - let last = null - - async function filterRoad (otherWhere, getRepeat = true) { - - if (key == '村' && getRepeat == true) { - - } else { - let roadUnSpotedRes = await getRoadSpot(key, lastRoadIds, false, otherWhere) - - for (let r of roadUnSpotedRes) { - spotRoadIds.push(r.id) - accumulationMileage += parseFloat(r.chainage_mileage) - if (accumulationMileage >= targetMileage) { - break; - } - } - } - - if (accumulationMileage < targetMileage && getRepeat) { - // 还小于 说明没取够 - let roadUnSpotedRepeatRes = await getRoadSpot( - key, - lastRoadIds, - true, - otherWhere - ) - for (let r of roadUnSpotedRepeatRes) { - 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 - - if (accumulationMileage >= targetMileage) { - break; - } - } - if (accumulationMileage < targetMileage) { - // 还小于 说明没取够 - // 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, last] - } - let villageMil = 0, townMil = 0, countryMil = 0; - - // 抽县 - const countryRoadTotalMileage = await getRoadTotalMileage('县', '', true) - const countryRoadHide = await getRoadTotalMileage('县', '', false) - const countryRoadNeedMileage = countryRoadTotalMileage * countyPercentage / 100 - let spotCountyRoadIdsArr = await spotRoadId('县', lastCountyRoadIds, countryRoadNeedMileage, []) - let spotCountyRoadIds = spotCountyRoadIdsArr[0] - let countryMil_ = spotCountyRoadIdsArr[1] || 0 - if (countryMil_) countryMil += countryMil_; - - gather.push({ - name: '南昌县交通运输局', - county: countryRoadTotalMileage + countryRoadHide, - countyParticipate: countryRoadTotalMileage, - countyPresent: countryMil_, - countyDifferenceValue: countryMil_ - countryRoadNeedMileage - }) - - let town = await models.Town.findAll() || [] - - - // 抽乡 - const allTownCodeRes = await sequelize.query(` - SELECT - DISTINCT township_code - FROM road - WHERE del = false - AND spot = true - AND township_code IS NOT NULL - `, { type: QueryTypes.SELECT }); - - let spotTownRoadIds = [] - let spotVillageRoadIds = [] - let spotVillageIds = [] - - let allTownCodeResTown = [] - let lastEndVillage = {} //获取每个乡镇最后一个被抽取的村 - - for await (let t of allTownCodeRes) { - // 遍历每个乡镇并抽取 - let otherWhere = [`township_code='${t.township_code}'`] - const townRoadTotalMileage = await getRoadTotalMileage('乡', otherWhere, true) - const townRoadHide = await getRoadTotalMileage('乡', otherWhere, false) - const townRoadNeedMileage = townRoadTotalMileage * (countyPercentage == 75 ? 50 : 25) / 100 - - let spotTownRoadIdsArr = await spotRoadId('乡', lastTownRoadIds, townRoadNeedMileage, otherWhere) - let spotTownRoadIds_ = spotTownRoadIdsArr[0] - let townMil_ = spotTownRoadIdsArr[1] - spotTownRoadIds = spotTownRoadIds.concat(spotTownRoadIds_) - if (townMil_) townMil += townMil_ - - - // 抽村 - const villageRoadTotalMileage = await getRoadTotalMileage('村', otherWhere, true) - const villageRoadTotalHide = await getRoadTotalMileage('村', otherWhere, false) - const villageRoadNeedMileage = villageRoadTotalMileage * (countyPercentage == 75 ? 20 : 10) / 100 - - let spotFirstVillageId = -1 - - let villageRes = [] - if (lastAbstractVillage[t.township_code]) { - - } else { - // 随机选取一个不在上次查过的村 - 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) { - // 没有村了,随机选一个 - villageRes = await sequelize.query(` - SELECT id FROM village - WHERE township_code = '${t.township_code}' - ORDER BY RANDOM() - LIMIT 1 - `, { type: QueryTypes.SELECT }); - } - } - - 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 { - allTownCodeResTown.push({ - code: t.township_code, - township: townRoadTotalMileage + townRoadHide, - townshipParticipate: townRoadTotalMileage, - townshipPresent: townMil_, - townshipDifferenceValue: townMil_ - townRoadNeedMileage, - village: villageRoadTotalMileage + villageRoadTotalHide, - villageParticipate: villageRoadTotalMileage, - villagePresent: 0, - villageDifferenceValue: 0 - }) - continue; - } - - const villageNearRes = await sequelize.query(` - SELECT id,calc_village,distance FROM village_distance - WHERE origin_village = ${spotFirstVillageId} - ORDER BY distance ASC - `, { type: QueryTypes.SELECT }) - - let villageCheckIdList = villageNearRes.map(item => item.calc_village) - villageCheckIdList.unshift(spotFirstVillageId) - villageCheckIdList = [...(new Set(villageCheckIdList))] - - 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_ - - allTownCodeResTown.push({ - code: t.township_code, - township: townRoadTotalMileage + townRoadHide, - townshipParticipate: townRoadTotalMileage, - townshipPresent: townMil_, - townshipDifferenceValue: townMil_ - townRoadNeedMileage, - village: villageRoadTotalMileage + villageRoadTotalHide, - villageParticipate: villageRoadTotalMileage, - villagePresent: villageMil_, - villageDifferenceValue: villageMil_ - villageRoadNeedMileage - }) - - - - } - - for (let index = 0; index < town.length; index++) { - let d = town[index] - if (allTownCodeResTown.find(c => c.code == d.code)) { - gather.push({ ...(allTownCodeResTown.find(c => c.code == d.code) || {}), name: d.name }) - } else { - let where = [`township_code='${d.code}'`] - let township = await getRoadTotalMileage('乡', where, false) - let village = await getRoadTotalMileage('村', where, false) - gather.push({ - name: d.name, - code: d.code, - township: township, - townshipParticipate: 0, - townshipPresent: 0, - townshipDifferenceValue: 0, - village: village, - villageParticipate: 0, - villagePresent: 0, - villageDifferenceValue: 0 - }) - } - - } - - - const previewRes = await models.RoadSpotCheckPreview.create({ - countyPercentage: countyPercentage, - date: moment().format(), - countyRoadId: spotCountyRoadIds, - townshipRoadId: spotTownRoadIds, - villageRoadId: spotVillageRoadIds, - villageId: spotVillageIds, - checked: false, - villageMil, - townMil, - countryMil, - gather, - lastAbstractVillage: lastEndVillage - }) - - - - ctx.status = 200; - ctx.body = { - previewId: previewRes.id, - spotCountyRoadCount: spotCountyRoadIds.length, - spotTownRoadCount: spotTownRoadIds.length, - spotVillageRoadCount: spotVillageRoadIds.length, - villageMil, - townMil, - countryMil - - } - } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); - ctx.status = 400; - ctx.body = { - message: typeof error == 'string' ? error : undefined - } - } -} +// async function roadSpotPrepare (ctx) { +// try { +// const { models } = ctx.fs.dc; +// const sequelize = ctx.fs.dc.orm; +// const { countyPercentage } = ctx.request.body; + +// const lastSpotRes = await models.RoadSpotCheckPreview.findOne({ +// where: { +// checked: true +// }, +// // attributes: ['id', 'countyPercentage', 'date',], +// order: [['date', 'DESC']], +// // include: [{ +// // model: models.RoadSpotCheck, +// // include: [{ +// // model: models.Road, +// // // attributes: ['id', 'name'] +// // }] +// // }] +// }) + +// let lastCountyPercentage = 0 // 最后一次的县道百分比 +// let lastCountyRoadIds = [] // 上次查得的县道id +// let lastTownRoadIds = [] // 上次查得的乡镇道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: { +// checked: true, +// id: { $lte: lastSpotRes.id } +// }, +// limit: 3, +// order: [['date', 'DESC']], +// }) + +// if (lastCounty) { +// let countyNnmter = 0 +// lastCounty.forEach(d => { +// if (countyNnmter < (100 - (countyPercentage == 75 ? 50 : 25))) { +// lastTownRoadIds = lastTownRoadIds.concat(d.townshipRoadId) || [] +// countyNnmter += (d.countyPercentage == 75 ? 50 : 25) +// } +// }) +// } + +// let lastVillage = await models.RoadSpotCheckPreview.findAll({ +// where: { +// checked: true, +// id: { $lte: lastSpotRes.id } +// }, +// limit: 9, +// order: [['date', 'DESC']], +// }) + +// if (lastVillage) { +// let villageNnmter = 0 +// lastVillage.forEach(d => { +// if (villageNnmter < (100 - (countyPercentage == 75 ? 20 : 10))) { +// lastVillageRoadRoadIds = lastVillageRoadRoadIds.concat(d.villageRoadId) || [] +// lastVillageIds = lastVillageIds.concat(d.villageId) || [] + +// villageNnmter += (d.countyPercentage == 75 ? 20 : 10) +// } +// }) +// } + + +// // lastTownRoadIds = lastSpotRes.townshipRoadId || [] +// // lastVillageRoadRoadIds = lastSpotRes.villageRoadId || [] +// // lastVillageIds = lastSpotRes.villageId || [] +// } + +// // 先查上次没查的范围内的 然后比较百分比 如果重叠 再查上次查过的 +// let keyMap = { +// '县': `route_code LIKE 'X%'`, +// '乡': `route_code LIKE 'Y%'`, +// '村': `route_code LIKE 'C%'` +// } +// async function getRoadTotalMileage (key, otherWhere = [], spot) { +// let res = await sequelize.query(` +// SELECT +// SUM(COALESCE(CAST(chainage_mileage AS DOUBLE PRECISION), 0)) AS total_mileage +// FROM road +// WHERE del = false +// AND spot = ${spot} +// AND ${keyMap[key]} ${otherWhere.length ? `AND ${otherWhere.join(' AND ')}` : ''} +// `) +// return res[0][0].total_mileage || 0 +// } + +// async function getRoadSpot (key, lastRoadIds = [], inOrNot, otherWhere = []) { +// if (!lastRoadIds.length && !inOrNot) { +// return [] +// } +// return await sequelize.query(` +// SELECT id, chainage_mileage FROM road +// WHERE del = false +// AND spot = true +// AND ${keyMap[key]} +// ${lastRoadIds.length ? +// `AND id ${inOrNot ? 'IN' : 'NOT IN'} ( +// ${lastRoadIds.map(item => `'${item}'`).join(',')},-1 +// )` : '' +// } +// AND chainage_mileage IS NOT NULL ${otherWhere.length ? `AND ${otherWhere.join(' AND ')}` : ''} +// ORDER BY RANDOM() +// `, { type: QueryTypes.SELECT }); +// } + +// async function spotRoadId (key, lastRoadIds, targetMileage, otherWhere = [], villageIdList = []) { +// let spotRoadIds = [] +// let accumulationMileage = 0 +// let last = null + +// async function filterRoad (otherWhere, getRepeat = true) { + +// if (key == '村' && getRepeat == true) { + +// } else { +// let roadUnSpotedRes = await getRoadSpot(key, lastRoadIds, false, otherWhere) + +// for (let r of roadUnSpotedRes) { +// spotRoadIds.push(r.id) +// accumulationMileage += parseFloat(r.chainage_mileage) +// if (accumulationMileage >= targetMileage) { +// break; +// } +// } +// } + +// if (accumulationMileage < targetMileage && getRepeat) { +// // 还小于 说明没取够 +// let roadUnSpotedRepeatRes = await getRoadSpot( +// key, +// lastRoadIds, +// true, +// otherWhere +// ) +// for (let r of roadUnSpotedRepeatRes) { +// 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 + +// if (accumulationMileage >= targetMileage) { +// break; +// } +// } +// if (accumulationMileage < targetMileage) { +// // 还小于 说明没取够 +// // 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, last] +// } +// let villageMil = 0, townMil = 0, countryMil = 0; + +// // 抽县 +// const countryRoadTotalMileage = await getRoadTotalMileage('县', '', true) +// const countryRoadHide = await getRoadTotalMileage('县', '', false) +// const countryRoadNeedMileage = countryRoadTotalMileage * countyPercentage / 100 +// let spotCountyRoadIdsArr = await spotRoadId('县', lastCountyRoadIds, countryRoadNeedMileage, []) +// let spotCountyRoadIds = spotCountyRoadIdsArr[0] +// let countryMil_ = spotCountyRoadIdsArr[1] || 0 +// if (countryMil_) countryMil += countryMil_; + +// gather.push({ +// name: '南昌县交通运输局', +// county: countryRoadTotalMileage + countryRoadHide, +// countyParticipate: countryRoadTotalMileage, +// countyPresent: countryMil_, +// countyDifferenceValue: countryMil_ - countryRoadNeedMileage +// }) + +// let town = await models.Town.findAll() || [] + + +// // 抽乡 +// const allTownCodeRes = await sequelize.query(` +// SELECT +// DISTINCT township_code +// FROM road +// WHERE del = false +// AND spot = true +// AND township_code IS NOT NULL +// `, { type: QueryTypes.SELECT }); + +// let spotTownRoadIds = [] +// let spotVillageRoadIds = [] +// let spotVillageIds = [] + +// let allTownCodeResTown = [] +// let lastEndVillage = {} //获取每个乡镇最后一个被抽取的村 + +// for await (let t of allTownCodeRes) { +// // 遍历每个乡镇并抽取 +// let otherWhere = [`township_code='${t.township_code}'`] +// const townRoadTotalMileage = await getRoadTotalMileage('乡', otherWhere, true) +// const townRoadHide = await getRoadTotalMileage('乡', otherWhere, false) +// const townRoadNeedMileage = townRoadTotalMileage * (countyPercentage == 75 ? 50 : 25) / 100 + +// let spotTownRoadIdsArr = await spotRoadId('乡', lastTownRoadIds, townRoadNeedMileage, otherWhere) +// let spotTownRoadIds_ = spotTownRoadIdsArr[0] +// let townMil_ = spotTownRoadIdsArr[1] +// spotTownRoadIds = spotTownRoadIds.concat(spotTownRoadIds_) +// if (townMil_) townMil += townMil_ + + +// // 抽村 +// const villageRoadTotalMileage = await getRoadTotalMileage('村', otherWhere, true) +// const villageRoadTotalHide = await getRoadTotalMileage('村', otherWhere, false) +// const villageRoadNeedMileage = villageRoadTotalMileage * (countyPercentage == 75 ? 20 : 10) / 100 + +// let spotFirstVillageId = -1 + +// let villageRes = [] +// if (lastAbstractVillage[t.township_code]) { + +// } else { +// // 随机选取一个不在上次查过的村 +// 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) { +// // 没有村了,随机选一个 +// villageRes = await sequelize.query(` +// SELECT id FROM village +// WHERE township_code = '${t.township_code}' +// ORDER BY RANDOM() +// LIMIT 1 +// `, { type: QueryTypes.SELECT }); +// } +// } + +// 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 { +// allTownCodeResTown.push({ +// code: t.township_code, +// township: townRoadTotalMileage + townRoadHide, +// townshipParticipate: townRoadTotalMileage, +// townshipPresent: townMil_, +// townshipDifferenceValue: townMil_ - townRoadNeedMileage, +// village: villageRoadTotalMileage + villageRoadTotalHide, +// villageParticipate: villageRoadTotalMileage, +// villagePresent: 0, +// villageDifferenceValue: 0 +// }) +// continue; +// } + +// const villageNearRes = await sequelize.query(` +// SELECT id,calc_village,distance FROM village_distance +// WHERE origin_village = ${spotFirstVillageId} +// ORDER BY distance ASC +// `, { type: QueryTypes.SELECT }) + +// let villageCheckIdList = villageNearRes.map(item => item.calc_village) +// villageCheckIdList.unshift(spotFirstVillageId) +// villageCheckIdList = [...(new Set(villageCheckIdList))] + +// 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_ + +// allTownCodeResTown.push({ +// code: t.township_code, +// township: townRoadTotalMileage + townRoadHide, +// townshipParticipate: townRoadTotalMileage, +// townshipPresent: townMil_, +// townshipDifferenceValue: townMil_ - townRoadNeedMileage, +// village: villageRoadTotalMileage + villageRoadTotalHide, +// villageParticipate: villageRoadTotalMileage, +// villagePresent: villageMil_, +// villageDifferenceValue: villageMil_ - villageRoadNeedMileage +// }) + + + +// } + +// for (let index = 0; index < town.length; index++) { +// let d = town[index] +// if (allTownCodeResTown.find(c => c.code == d.code)) { +// gather.push({ ...(allTownCodeResTown.find(c => c.code == d.code) || {}), name: d.name }) +// } else { +// let where = [`township_code='${d.code}'`] +// let township = await getRoadTotalMileage('乡', where, false) +// let village = await getRoadTotalMileage('村', where, false) +// gather.push({ +// name: d.name, +// code: d.code, +// township: township, +// townshipParticipate: 0, +// townshipPresent: 0, +// townshipDifferenceValue: 0, +// village: village, +// villageParticipate: 0, +// villagePresent: 0, +// villageDifferenceValue: 0 +// }) +// } + +// } + + +// const previewRes = await models.RoadSpotCheckPreview.create({ +// countyPercentage: countyPercentage, +// date: moment().format(), +// countyRoadId: spotCountyRoadIds, +// townshipRoadId: spotTownRoadIds, +// villageRoadId: spotVillageRoadIds, +// villageId: spotVillageIds, +// checked: false, +// villageMil, +// townMil, +// countryMil, +// gather, +// lastAbstractVillage: lastEndVillage +// }) + + + +// ctx.status = 200; +// ctx.body = { +// previewId: previewRes.id, +// spotCountyRoadCount: spotCountyRoadIds.length, +// spotTownRoadCount: spotTownRoadIds.length, +// spotVillageRoadCount: spotVillageRoadIds.length, +// villageMil, +// townMil, +// countryMil + +// } +// } catch (error) { +// ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); +// ctx.status = 400; +// ctx.body = { +// message: typeof error == 'string' ? error : undefined +// } +// } +// } async function confirmRoadSpot (ctx) { const transaction = await ctx.fs.dc.orm.transaction(); @@ -1006,18 +1006,10 @@ async function roadSpotList (ctx) { ctx.status = 200; ctx.body = listRes.map(item => { return { - id: item.id, - date: item.date, - countyPercentage: item.countyPercentage, + ...item.dataValues, spotCountyRoadCount: item.countyRoadId ? item.countyRoadId.length : 0, spotTownRoadCount: item.townshipRoadId ? item.townshipRoadId.length : 0, spotVillageRoadCount: item.villageRoadId ? item.villageRoadId.length : 0, - countyRoadId: item.countyRoadId, - townshipRoadId: item.townshipRoadId, - villageRoadId: item.villageRoadId, - villageMil: item.villageMil, - townMil: item.townMil, - countryMil: item.countryMil } }) } catch (error) { @@ -1274,10 +1266,579 @@ async function roadSpotChangList (ctx) { } } + +async function town (ctx) { + try { + const { models } = ctx.fs.dc; + const { } = ctx.query + + let options = { + where: { + + }, + } + + const res = await models.Town.findAll(options) + + ctx.status = 200; + ctx.body = res + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + +async function roadSpotPrepare (ctx) { + try { + const { models } = ctx.fs.dc; + const sequelize = ctx.fs.dc.orm; + const { countyPercentage, drawPeople, level, code } = ctx.request.body; + + + + // 先查上次没查的范围内的 然后比较百分比 如果重叠 再查上次查过的 + let keyMap = { + '县': `route_code LIKE 'X%'`, + '乡': `route_code LIKE 'Y%'`, + '村': `route_code LIKE 'C%'` + } + async function getRoadTotalMileage (key, otherWhere = [], spot) { + let res = await sequelize.query(` + SELECT + SUM(COALESCE(CAST(chainage_mileage AS DOUBLE PRECISION), 0)) AS total_mileage + FROM road + WHERE del = false + AND spot = ${spot} + AND ${keyMap[key]} ${otherWhere.length ? `AND ${otherWhere.join(' AND ')}` : ''} + `) + return res[0][0].total_mileage || 0 + } + + async function getRoadSpot (key, lastRoadIds = [], inOrNot, otherWhere = []) { + if (!lastRoadIds.length && !inOrNot) { + return [] + } + return await sequelize.query(` + SELECT id, chainage_mileage,route_code FROM road + WHERE del = false + AND spot = true + AND ${keyMap[key]} + ${lastRoadIds.length ? + `AND id ${inOrNot ? 'IN' : 'NOT IN'} ( + ${lastRoadIds.map(item => `'${item}'`).join(',')},-1 + )` : '' + } + AND chainage_mileage IS NOT NULL ${otherWhere.length ? `AND ${otherWhere.join(' AND ')}` : ''} + ORDER BY RANDOM() + `, { type: QueryTypes.SELECT }); + } + + async function spotRoadId (key, lastRoadIds, targetMileage, otherWhere = [], villageIdList = []) { + let spotRoadIds = [] + let accumulationMileage = 0 + let last = null + let spotVillageIds = [] + + async function filterRoad (otherWhere, getRepeat = true) { + + if (key == '村' && getRepeat == true) { + + } 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.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) + } + } + 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) + } + 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)) + + } + } + for (let r of roadUnSpotedRes) { + if (accumulationMileage >= targetMileage) { + break; + } + spotRoadIds.push(r.id) + accumulationMileage += parseFloat(r.chainage_mileage) + if (accumulationMileage >= targetMileage) { + break; + } + } + } + + if (accumulationMileage < targetMileage && getRepeat) { + // 还小于 说明没取够 + 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.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) + } + } + 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.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) + } + if (townY032[1]) { + 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)) + + } + } + + for (let r of roadUnSpotedRepeatRes) { + if (accumulationMileage >= targetMileage) { + break; + } + 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 + + if (accumulationMileage >= targetMileage) { + break; + } + } + if (accumulationMileage < targetMileage) { + // 还小于 说明没取够 + // 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, last, spotVillageIds] + } + + + async function relevance (previewId, spotedRoadIds) { + const conserveCountRes = await sequelize.query(` + SELECT road_id, COUNT(road_id) as count + FROM report + WHERE road_id IN ( + ${spotedRoadIds.map(item => item).join(',')} + ) AND report_type = 'conserve' + GROUP BY road_id + `, { type: QueryTypes.SELECT }) + + let spotRslt = [] + for await (let item of spotedRoadIds) { + let corConserveCount = conserveCountRes.find(cc => cc.road_id == item) + spotRslt.push({ + roadId: item, + maintenanceCount: corConserveCount ? corConserveCount.count : 0, + prepareId: previewId + }) + } + + await models.RoadSpotCheck.bulkCreate(spotRslt) + } + + + + + + + + //获取最新的一次 + const lastSpotRes = await models.RoadSpotCheckPreview.findOne({ + where: { + checked: true + }, + order: [['date', 'DESC']], + }) + + + let gather = {} //汇总 + + + + if (level == '县') { + + let lastCountyRoadIds = [] // 上次查得的县道id + if (lastSpotRes) { + lastCountyRoadIds = lastSpotRes.countyRoadId || [] + } + + + + //抽取县道长度 + let countryMil = 0; + // 抽县 + const countryRoadTotalMileage = await getRoadTotalMileage('县', '', true) + const countryRoadHide = await getRoadTotalMileage('县', '', false) + const countryRoadNeedMileage = countryRoadTotalMileage * countyPercentage / 100 + let spotCountyRoadIdsArr = await spotRoadId('县', lastCountyRoadIds, countryRoadNeedMileage, []) + let spotCountyRoadIds = spotCountyRoadIdsArr[0] + let countryMil_ = spotCountyRoadIdsArr[1] || 0 + if (countryMil_) countryMil += countryMil_; + + + gather.countyRoad = { + name: '南昌县交通运输局', + //总里程 + county: countryRoadTotalMileage + countryRoadHide, + //纳入考核里程 + countyParticipate: countryRoadTotalMileage, + //本次考核里程 + countyPresent: countryMil_, + //实际抽取比原计划多 + countyDifferenceValue: countryMil_ - countryRoadNeedMileage, + //本次抽取的县道id + countyRoadId: spotCountyRoadIds, + //抽取时间 + abstractTime: moment().format(), + //抽取人 + drawPeople + } + + + + let spotCheckCreate = await models.RoadSpotCheckPreview.create({ + countyPercentage: countyPercentage, + date: moment().format(), + countyRoadId: spotCountyRoadIds, + checked: true, + countryMil, + gather, + abstractFinish: false + }) + if (spotCheckCreate) { + await relevance(spotCheckCreate.id, spotCountyRoadIds) + } + + } else { + + if (lastSpotRes) { + + let lastTownRoadIds = [] // 上次查得的乡镇道id + let lastVillageRoadRoadIds = [-1] // 上次查得的村道id + let lastVillageIds = [] //抽到的村道id + let lastAbstractVillage = {} //每个乡最后抽查的村 + + + + let gather = lastSpotRes.gather || {} //汇总 + + + let lastCounty = await models.RoadSpotCheckPreview.findAll({ + where: { + checked: true, + id: { $lte: lastSpotRes.id } + }, + limit: 9, + order: [['date', 'DESC']], + }) + + + if (lastCounty) { + + let countyNnmter = 0 + let villageNnmter = 0 + lastAbstractVillage = lastCounty[0] && lastCounty[0].lastAbstractVillage || {} + lastCounty.forEach(d => { + if (countyNnmter < (100 - (countyPercentage == 75 ? 50 : 25))) { + lastTownRoadIds = lastTownRoadIds.concat(d.townshipRoadId || []) || [] + countyNnmter += (d.countyPercentage == 75 ? 50 : 25) + } + if (villageNnmter < (100 - (countyPercentage == 75 ? 20 : 10))) { + lastVillageRoadRoadIds = lastVillageRoadRoadIds.concat(d.villageRoadId || []) || [] + lastVillageIds = lastVillageIds.concat(d.villageId || []) || [] + villageNnmter += (d.countyPercentage == 75 ? 20 : 10) + } + }) + } + + + + let town = await models.Town.findAll() || [] + + + let spotTownRoadIds = [] + let townMil = lastSpotRes.townMil || 0 //已抽取的乡道公里数 + + + // 抽乡 + let otherWhere = [`township_code='${code}'`] + const townRoadTotalMileage = await getRoadTotalMileage('乡', otherWhere, true) + const townRoadHide = await getRoadTotalMileage('乡', otherWhere, false) + const townRoadNeedMileage = townRoadTotalMileage * (countyPercentage == 75 ? 50 : 25) / 100 + + let spotTownRoadIdsArr = await spotRoadId('乡', lastTownRoadIds, townRoadNeedMileage, otherWhere) + let spotTownRoadIds_ = spotTownRoadIdsArr[0] + let townMil_ = spotTownRoadIdsArr[1] + spotTownRoadIds = spotTownRoadIds.concat(spotTownRoadIds_ || []) + if (townMil_) townMil += townMil_ || 0 + + + + + + let spotVillageRoadIds = [] + let spotVillageIds = [] + let lastEndVillage = {} //获取每个乡镇最后一个被抽取的村 + let villageMil = lastSpotRes.villageMil || 0 //已抽取的村道公里数 + let allTownCodeResTown = [] + + + + + // 抽村 + const villageRoadTotalMileage = await getRoadTotalMileage('村', otherWhere, true) + const villageRoadTotalHide = await getRoadTotalMileage('村', otherWhere, false) + const villageRoadNeedMileage = villageRoadTotalMileage * (countyPercentage == 75 ? 20 : 10) / 100 + + let spotFirstVillageId = -1 + + let villageRes = [] + + + //获取该抽取的村的id + if (lastAbstractVillage[code]) { + + } else { + // 随机选取一个不在上次查过的村 + villageRes = await sequelize.query(` + SELECT id FROM village + WHERE township_code = '${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 = '${code}' + ORDER BY RANDOM() + LIMIT 1 + `, { type: QueryTypes.SELECT }); + } + } + + if (lastAbstractVillage[code]) { + // spotVillageIds.push(lastAbstractVillage[code]) + spotFirstVillageId = lastAbstractVillage[code] + + } else if (villageRes.length) { + // 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 + const villageNearRes = await sequelize.query(` + SELECT id,calc_village,distance FROM village_distance + WHERE origin_village = ${spotFirstVillageId} + ORDER BY distance ASC + `, { type: QueryTypes.SELECT }) + + let villageCheckIdList = villageNearRes.map(item => item.calc_village) + + villageCheckIdList.unshift(spotFirstVillageId) + villageCheckIdList = [...(new Set(villageCheckIdList))] + + let spotVillageRoadIdsArr = await spotRoadId('村', lastVillageRoadRoadIds, villageRoadNeedMileage, otherWhere, villageCheckIdList) + let spotVillageRoadIds_ = spotVillageRoadIdsArr[0] + let villageMil_ = spotVillageRoadIdsArr[1] + lastEndVillage[code] = spotVillageRoadIdsArr[2] + spotVillageIds = spotVillageIds.concat(spotVillageRoadIdsArr[3] || []) + + spotVillageRoadIds = spotVillageRoadIds.concat(spotVillageRoadIds_ || []) + if (villageMil_) villageMil += villageMil_ + + + let fidOne = town.find(d => d.code == code) + + gather[code] = { + name: fidOne.name, + //总里程 乡 + township: townRoadTotalMileage + townRoadHide, + //纳入考核里程 乡 + townshipParticipate: townRoadNeedMileage, + //本次考核里程 乡 + townshipPresent: townMil_, + //实际抽取比原计划多 乡 + townshipDifferenceValue: townMil_ - townRoadNeedMileage, + //本次抽取的乡道id + townRoadId: spotTownRoadIds_, + //总里程 村 + village: townRoadTotalMileage + townRoadHide, + //纳入考核里程 村 + villageParticipate: villageRoadNeedMileage, + //本次考核里程 村 + villagePresent: villageMil_, + //实际抽取比原计划多 村 + villageDifferenceValue: villageMil_ - villageRoadNeedMileage, + //本次抽取的村道id + villageRoadId: spotVillageRoadIds_, + //抽取时间 + abstractTime: moment().format(), + //抽取人 + drawPeople: drawPeople + } + + let abstractFinish = false + + for (let item of town) { + if (item.code != '360121205000') { + abstractFinish = true + if (!gather[item.code]) { + abstractFinish = false + break; + } + } + } + + + + let spotCheckCreate = await models.RoadSpotCheckPreview.update({ + spotTownRoadIds: spotTownRoadIds, + spotVillageIds: spotVillageIds, + villageId: spotVillageIds, + townMil, + villageMil, + gather, + abstractFinish: abstractFinish + }, { where: { id: lastSpotRes.id } }) + + + if (spotCheckCreate) { + await relevance(lastSpotRes.id, [...spotTownRoadIds_, ...spotVillageRoadIds_]) + } + + + } + + + } + + const res = await models.RoadSpotCheckPreview.findOne({ + where: { + checked: true + }, + order: [['date', 'DESC']], + }) + + ctx.status = 200; + ctx.body = res + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + async function exportSpotRode (ctx) { try { const { models } = ctx.fs.dc; - const { previewId } = ctx.query + const { previewId, roadId, isgather } = ctx.query const previewRes = await models.RoadSpotCheckPreview.findOne({ where: { @@ -1285,7 +1846,7 @@ async function exportSpotRode (ctx) { } }) - const listRes = await models.RoadSpotCheck.findAll({ + let options = { where: { prepareId: previewId }, @@ -1296,7 +1857,13 @@ async function exportSpotRode (ctx) { del: false, } }] - }) + } + + if (roadId) { + options.where.roadId = { $in: roadId.split(',') } + } + + const listRes = await models.RoadSpotCheck.findAll(options) let town = await models.Town.findAll({}) || [] let village = await models.Village.findAll({ attributes: ['id', 'name'] }) || [] @@ -1339,20 +1906,6 @@ async function exportSpotRode (ctx) { title: '养护次数(次)', },] - // function judgeLevel (routeCode) { - // if (routeCode) { - // if (routeCode.startsWith('X')) { - // return '县道' - // } else if (routeCode.startsWith('Y')) { - // return '乡道' - // } else if (routeCode.startsWith('C')) { - // return '村道' - // } - // return '' - // } else { - // return '' - // } - // } let exportData = listRes.map(({ dataValues: item }) => { let road = item.road && item.road.dataValues || {} @@ -1372,11 +1925,25 @@ async function exportSpotRode (ctx) { } }) + const fileName = `${moment(previewRes.date).format('YYYY年MM月DD日HH时mm分')}道路抽查记录` + '.csv' - const filePath = await xlsxDownload.simpleExcelDown({ - // data: exportData, header, fileName: fileName - data: exportData, header, fileName: fileName, gather: { data: previewRes.gather || [] } - }) + let data = { + data: exportData, header, fileName: fileName + } + + if (isgather) { + let gather = [previewRes.dataValues.gather['countyRoad']] + town.forEach(d => { + if (d.code != '360121205000') { + gather.push(previewRes.dataValues.gather[d.code] || {}) + } + + }) + + data.gather = { data: gather } + } + + const filePath = await xlsxDownload.simpleExcelDown(data) const fileData = fs.readFileSync(filePath); @@ -1393,10 +1960,12 @@ async function exportSpotRode (ctx) { } } + module.exports = { reportList, reportPosition, reportDetail, createReport, deleteReport, reportHandle, spotPrepare, spotCheck, spotCheckDetail, - roadSpotPrepare, confirmRoadSpot, roadSpotList, roadSpotDetail, roadSpotChange, roadSpotChangList, exportSpotRode + roadSpotPrepare, confirmRoadSpot, roadSpotList, roadSpotDetail, roadSpotChange, roadSpotChangList, exportSpotRode, + town }; \ No newline at end of file diff --git a/api/app/lib/models/road_spot_check_preview.js b/api/app/lib/models/road_spot_check_preview.js index 553f305c..22e21ca5 100644 --- a/api/app/lib/models/road_spot_check_preview.js +++ b/api/app/lib/models/road_spot_check_preview.js @@ -122,7 +122,25 @@ module.exports = dc => { primaryKey: false, field: "last_abstract_village", autoIncrement: false - } + }, + abstractFinish: { + type: DataTypes.BOOLEAN, + allowNull: true, + defaultValue: null, + comment: "是否全部抽取完", + primaryKey: false, + field: "abstract_finish", + autoIncrement: false + }, + nextAbstract:{ + type: DataTypes.JSONB, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "next_abstract", + autoIncrement: false + }, }, { tableName: "road_spot_check_preview", comment: "", diff --git a/api/app/lib/routes/report/index.js b/api/app/lib/routes/report/index.js index 933b7262..6c6449b4 100644 --- a/api/app/lib/routes/report/index.js +++ b/api/app/lib/routes/report/index.js @@ -55,4 +55,7 @@ module.exports = function (app, router, opts) { app.fs.api.logAttr['GET/road/spot/export'] = { content: '导出路线抽查', visible: false }; router.get('/road/spot/export', report.exportSpotRode) + + app.fs.api.logAttr['GET/town'] = { content: '获取乡镇', visible: false }; + router.get('/town', report.town) } \ No newline at end of file diff --git a/api/utils/xlsxDownload.js b/api/utils/xlsxDownload.js index 7818222b..6323e73c 100644 --- a/api/utils/xlsxDownload.js +++ b/api/utils/xlsxDownload.js @@ -31,7 +31,7 @@ async function gatherSheet ({ sheet_2, data = [] }) { const indexCell1 = headerRow1.addCell(); indexCell1.value = '南昌县农村公路养护管理暨用地范围内环境整治提升工程 考核汇总表' indexCell1.style = headerStyle - indexCell1.hMerge = 12 + indexCell1.hMerge = 14 const headerRow2 = sheet_2.addRow(); const indexCell2 = headerRow2.addCell(); @@ -90,6 +90,14 @@ async function gatherSheet ({ sheet_2, data = [] }) { }, { key: 'villageDifferenceValue', title: '实际抽取比原计划多', + }, + { + key: 'drawPeople', + title: '抽查人', + }, + { + key: 'abstractTime', + title: '抽查时间', },] const headerRow3 = sheet_2.addRow(); @@ -110,16 +118,24 @@ async function gatherSheet ({ sheet_2, data = [] }) { let rowGather = sheet_2.addRow(); for (let h of header) { - const cell = rowGather.addCell(); - cell.value = h.key == 'name' ? '汇总' : data.reduce((a, b) => a + (b[h.key] || 0), 0) - cell.style = style + if (h.key != 'drawPeople' && h.key != 'abstractTime') { + const cell = rowGather.addCell(); + cell.value = h.key == 'name' ? '汇总' : data.reduce((a, b) => a + (b[h.key] || 0), 0) + cell.style = style + } + } for (let i = 0; i < data.length; i++) { const row = sheet_2.addRow(); for (let h of header) { const cell = row.addCell(); - cell.value = data[i][h.key] + if (h.key == 'abstractTime') { + cell.value = moment(data[i][h.key]).format('YYYY-MM-DD HH:mm:ss') + } else { + cell.value = data[i][h.key] + } + cell.style = style } } diff --git a/scripts/1.5/road_spot_check_preview.sql b/scripts/1.5/road_spot_check_preview.sql new file mode 100644 index 00000000..dbd5270d --- /dev/null +++ b/scripts/1.5/road_spot_check_preview.sql @@ -0,0 +1,11 @@ +alter table road_spot_check_preview + add abstract_finish boolean; + +comment on column road_spot_check_preview.abstract_finish is '是否全部抽取完'; + + +alter table road_spot_check_preview + add next_abstract jsonb; + +comment on column road_spot_check_preview.next_abstract is '下次必抽道路'; + diff --git a/web/client/src/sections/fillion/actions/spotCheck.js b/web/client/src/sections/fillion/actions/spotCheck.js index a07add7d..e617ab06 100644 --- a/web/client/src/sections/fillion/actions/spotCheck.js +++ b/web/client/src/sections/fillion/actions/spotCheck.js @@ -89,23 +89,14 @@ export function roadSpotChange (data = {}) { }); } -// export function delAssess (query) { -// return dispatch => basicAction({ -// type: 'del', -// dispatch: dispatch, -// actionType: 'DEL_ASSESS', -// url: ApiTable.delAssess.replace("{assessId}", query?.id), -// msg: { option: '删除考核评分信息' }, -// }); -// } - -// export function editAssess (query) { -// return dispatch => basicAction({ -// type: 'put', -// dispatch: dispatch, -// data: query, -// actionType: 'PUT_ASSESS', -// url: ApiTable.editAssess, -// msg: { option: '编辑/新增考核评分信息' }, -// }); -// } \ No newline at end of file +export function getTown (query = {}) { + return dispatch => basicAction({ + type: 'get', + dispatch: dispatch, + query: query, + actionType: 'GET_TOWN', + url: 'town', + msg: { error: '获取乡镇code失败' }, + reducer: { name: 'town' } + }); +} \ No newline at end of file diff --git a/web/client/src/sections/fillion/components/spotCheck.js b/web/client/src/sections/fillion/components/spotCheck.js new file mode 100644 index 00000000..8563a5f4 --- /dev/null +++ b/web/client/src/sections/fillion/components/spotCheck.js @@ -0,0 +1,315 @@ +import { connect } from 'react-redux' +import React, { useEffect, useState } from 'react' +import { Button, Modal, Form, Input, Divider, Spin, Select, DatePicker, Descriptions, Table } from 'antd' +import { roadSpotPrepare, getTown } from '../actions/spotCheck' + +import moment from 'moment' +import '../components/maintenanceTable.less' + + + +const SpotCheck = ({ dispatch, user, loading, reportDetail, editData, onCancel, road, onOk, town = [], viewDetails }) => { + + + const [countyRoadTime, setCountyRoadTime] = useState(true) + const [currentValue, setCurrentValue] = useState({}) + const [townDisabled, setTownDisabled] = useState({}) + const [dataList, setDataList] = useState([]) + + + + const [form] = Form.useForm() + + + + + useEffect(() => { + if (!viewDetails) { + + if (editData?.id) { + exhibition(editData) + setCurrentValue(editData) + + } + } else { + + let gather = [editData.gather['countyRoad']] + town.forEach(d => { + if (d.code != '360121205000') { + gather.push(editData.gather[d.code] || {}) + } + + }) + setDataList(gather) + } + + }, []) + + + const exhibition = (show) => { + form.setFieldsValue({ + 'countyPercentage': show?.countyPercentage, + 'townPercentage': show?.countyPercentage == 50 ? 25 : 50, + 'villagePercentage': show?.countyPercentage == 50 ? 10 : 20, + }) + + let disabled = {} + + for (let code in show.gather) { + if (code == 'countyRoad') { + setCountyRoadTime(false) + form.setFieldsValue({ + 'countyPresent': show.gather[code]?.countyPresent?.toFixed(4) || 0, + 'countyDifferenceValue': show.gather[code]?.countyDifferenceValue?.toFixed(4) || 0, + 'drawPeople': show.gather[code]?.drawPeople, + 'abstractTime': moment(show.gather[code]?.abstractTime), + }) + } else { + form.setFieldsValue({ + [code + 'townshipPresent']: show.gather[code]?.townshipPresent?.toFixed(4) || 0, + [code + 'townshipDifferenceValue']: show.gather[code]?.townshipDifferenceValue?.toFixed(4) || 0, + [code + 'villagePresent']: show.gather[code]?.villagePresent?.toFixed(4) || 0, + [code + 'villageDifferenceValue']: show.gather[code]?.villageDifferenceValue?.toFixed(4) || 0, + [code + 'drawPeople']: show.gather[code]?.drawPeople, + [code + 'abstractTime']: moment(show.gather[code]?.abstractTime), + }) + disabled[code] = true + } + } + + setTownDisabled(disabled) + + } + + + + return ( + { + onCancel() + }} + onOk={() => { + + + }}> + {viewDetails ?
+
南昌县农村公路养护管理暨用地范围内环境整治提升工程 考核汇总表
+ t?.toFixed(4) }, + { title: '纳入考核里程', dataIndex: 'countyParticipate', key: 'countyParticipate', render: (t, r) => t?.toFixed(4) }, + { title: '本次考核里程', dataIndex: 'countyPresent', key: 'countyPresent', render: (t, r) => t?.toFixed(4) }, + { title: '实际抽取比原计划多', dataIndex: 'countyDifferenceValue', key: 'countyDifferenceValue', render: (t, r) => t?.toFixed(4) }, + ] + }, + { + title: '乡道', + children: [ + { title: '总里程', dataIndex: 'township', key: 'township', render: (t, r) => t?.toFixed(4) }, + { title: '纳入考核里程', dataIndex: 'townshipParticipate', key: 'townshipParticipate', render: (t, r) => t?.toFixed(4) }, + { title: '本次考核里程', dataIndex: 'townshipPresent', key: 'townshipPresent', render: (t, r) => t?.toFixed(4) }, + { title: '实际抽取比原计划多', dataIndex: 'townshipDifferenceValue', key: 'townshipDifferenceValue', render: (t, r) => t?.toFixed(4) }, + ] + }, + { + title: '村道', + children: [ + { title: '总里程', dataIndex: 'village', key: 'village', render: (t, r) => t?.toFixed(4) }, + { title: '纳入考核里程', dataIndex: 'villageParticipate', key: 'villageParticipate', render: (t, r) => t?.toFixed(4) }, + { title: '本次考核里程', dataIndex: 'villagePresent', key: 'villagePresent', render: (t, r) => t?.toFixed(4) }, + { title: '实际抽取比原计划多', dataIndex: 'villageDifferenceValue', key: 'villageDifferenceValue', render: (t, r) => t?.toFixed(4) }, + ] + }, + { + title: '', + children: [ + { title: '抽查人', dataIndex: 'drawPeople', key: 'drawPeople', }, + ] + }, + { + title: '', + children: [ + { title: '抽查时间', dataIndex: 'abstractTime', key: 'abstractTime', width: 160, render: (t, r) => moment(t).format('YYYY-MM-DD HH:mm:ss') }, + ] + }, + { + title: '', + children: [ + { + title: '操作', dataIndex: 'action', key: 'action', fixed: 'right', + render: (t, r) => { + let roadId = [] + if (r.name == '南昌县交通运输局') { + roadId = r.countyRoadId || [] + } else { + roadId = [...r.townRoadId, ...r.villageRoadId] + } + return 导出 + } + }, + ] + },]} + dataSource={dataList} + /> + + : { + + + + }}> +
+ + + + + + + + + +
+
+ + + + + + + + + +
+
+ + + +
+
+
责任单位
+
乡道实际抽取(公里)
+
乡道比计划多(公里)
+
村道实际抽取(公里)
+
村道比计划多(公里)
+
抽查人
+
抽查时间
+
操作
+
+ {town?.map(d => { + return
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + +
+ }) + } + + + } + + + + + ) +} +function mapStateToProps (state) { + const { auth, road, } = state + + return { + user: auth.user, + road: road?.data || [], + // town: town?.data || [], + } +} +export default connect(mapStateToProps)(SpotCheck) \ No newline at end of file diff --git a/web/client/src/sections/fillion/containers/maintenanceSpotCheck-new.js b/web/client/src/sections/fillion/containers/maintenanceSpotCheck-new.js index 49420d6a..a1b86655 100644 --- a/web/client/src/sections/fillion/containers/maintenanceSpotCheck-new.js +++ b/web/client/src/sections/fillion/containers/maintenanceSpotCheck-new.js @@ -1,11 +1,12 @@ import { connect } from 'react-redux' import React, { useEffect, useState } from 'react' -import { Button, Modal, Form, Input, Divider, Spin, Select, DatePicker, Tooltip, Table } from 'antd' +import { Button, Modal, Form, Input, Divider, Spin, Select, DatePicker, Tooltip, Table, } from 'antd' import { ExclamationCircleOutlined } from '@ant-design/icons' import { getVillageList } from "../actions/infor" -import { roadSpotList, roadSpotDetail, roadSpotPrepare, confirmRoadSpot, exportSpotRode } from '../actions/spotCheck' +import { roadSpotList, roadSpotDetail, roadSpotPrepare, confirmRoadSpot, exportSpotRode, getTown } from '../actions/spotCheck' import moment from 'moment' import Adjustment from '../components/adjustment' +import SpotCheck from '../components/spotCheck' import '../components/maintenanceTable.less' @@ -30,28 +31,10 @@ const MaintenanceSpotCheck = (props) => { const [editData, setEditData] = useState({}) const [keyword, setKeyword] = useState("") const [show, setShow] = useState() + const [town, setTown] = useState([]) + const [viewDetails, setViewDetails] = useState(false) - const roadCode = [ - { title: "八一乡", value: "360121206000" }, - { title: "东新乡", value: "360121205000" }, - { title: "富山乡", value: "360121204000" }, - { title: "冈上镇", value: "360121107000" }, - { title: "广福镇", value: "360121108000" }, - { title: "黄马乡", value: "360121203000" }, - { title: "蒋巷镇", value: "360121105000" }, - { title: "金湖管理处", value: "330052" }, - { title: "泾口乡", value: "360121200000" }, - { title: "莲塘镇", value: "360121100000" }, - { title: "南新乡", value: "360121201000" }, - { title: "三江镇", value: "360121102000" }, - { title: "塔城乡", value: "360121202000" }, - { title: "塘南镇", value: "360121103000" }, - { title: "武阳镇", value: "360121106000" }, - { title: "向塘镇", value: "360121101000" }, - { title: "银三角管委会", value: "360121471000" }, - { title: "幽兰镇", value: "360121104000" }, - ] const columns = [ { @@ -87,8 +70,39 @@ const MaintenanceSpotCheck = (props) => { title: '操作', key: 'operation', dataIndex: 'operation', - render: (_, record) => - 导出 + width: 180, + render: (_, record, index) => { + + return <> + {index != 0 ? + + : record.abstractFinish ? + + : + } + {index != 0 ? + + : !reportData[0]?.abstractFinish ? + + + + : + + } + + + } }, ] @@ -114,6 +128,11 @@ const MaintenanceSpotCheck = (props) => { useEffect(() => { queryData() + dispatch(getTown({})).then(res => { + if (res.success) { + setTown(res?.payload.data?.filter(d => d.code != '360121205000')) + } + }) }, []) @@ -144,9 +163,15 @@ const MaintenanceSpotCheck = (props) => { return (
- + + {reportData?.length > 0 && !reportData[0]?.abstractFinish ? + + + + : + }
{ @@ -212,9 +237,9 @@ const MaintenanceSpotCheck = (props) => { { title: '所属乡镇', key: 'townshipCode', dataIndex: 'townshipCode', render: (_, r) => { const targetValue = r?.road?.townshipCode ?? '' - const foundItem = roadCode.find(item => item.value === targetValue) + const foundItem = town.find(item => item.code === targetValue) if (foundItem) { - return foundItem.title + return foundItem.name } else { return "--" } @@ -268,82 +293,29 @@ const MaintenanceSpotCheck = (props) => { // search={false} /> - { - vis && { - form.validateFields().then((values) => { - dispatch(confirmRoadSpot({ previewId })).then(res => { - if (res.success) { - setPage(1) - setExpandedRowKeys([]) - queryData({ startTime: dateRange[0], endTime: dateRange[1] }) - form.resetFields() - setVis(false) - setShow("") - } - }) - }) - }}> -
- - - - - - - - - - - - - - -
+ }} + /> } - { isAdjustment &&