From e78e608fe633e48f4cc27d1fa6d8661f19eb5dee Mon Sep 17 00:00:00 2001 From: "peng.peng" Date: Wed, 20 Dec 2023 17:21:16 +0800 Subject: [PATCH] =?UTF-8?q?(*)=E6=8A=BD=E5=8F=96=E5=85=AC=E9=87=8C?= =?UTF-8?q?=E6=95=B0=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/report/index.js | 74 ++++--- api/app/lib/models/road_spot_check_preview.js | 193 ++++++++++-------- 2 files changed, 156 insertions(+), 111 deletions(-) diff --git a/api/app/lib/controllers/report/index.js b/api/app/lib/controllers/report/index.js index 3ad56cf2..238c8f29 100644 --- a/api/app/lib/controllers/report/index.js +++ b/api/app/lib/controllers/report/index.js @@ -3,7 +3,7 @@ const { QueryTypes } = require('sequelize'); const moment = require('moment'); const xlsxDownload = require('../../../../utils/xlsxDownload.js'); const fs = require('fs'); -async function reportList (ctx) { +async function reportList(ctx) { try { const models = ctx.fs.dc.models; const { limit, page, startTime, endTime, keyword, userId, reportType, isTop, asc, projectType, handleState = '', performerId = '', codeRoad } = ctx.query @@ -149,7 +149,7 @@ async function reportList (ctx) { } } -async function reportPosition (ctx) { +async function reportPosition(ctx) { try { const models = ctx.fs.dc.models; const { startTime, endTime, userId, reportType } = ctx.query @@ -199,7 +199,7 @@ async function reportPosition (ctx) { } } -async function reportDetail (ctx) { +async function reportDetail(ctx) { try { const models = ctx.fs.dc.models; const { reportId } = ctx.params @@ -228,7 +228,7 @@ async function reportDetail (ctx) { } } -async function reportHandle (ctx) { +async function reportHandle(ctx) { try { const { models } = ctx.fs.dc; @@ -258,7 +258,7 @@ async function reportHandle (ctx) { } } -async function createReport (ctx) { +async function createReport(ctx) { try { const { userId } = ctx.fs.api const models = ctx.fs.dc.models; @@ -280,7 +280,7 @@ async function createReport (ctx) { } } -async function deleteReport (ctx) { +async function deleteReport(ctx) { try { const models = ctx.fs.dc.models; const { reportId } = ctx.params; @@ -303,7 +303,7 @@ async function deleteReport (ctx) { // TODO 小程序填写道路名称的时候的道路筛选 是一起都返回 还是不断传关键字搜索返回 -async function spotPrepare (ctx) { +async function spotPrepare(ctx) { try { const { models } = ctx.fs.dc; const sequelize = ctx.fs.dc.orm; @@ -375,7 +375,7 @@ async function spotPrepare (ctx) { } } -async function spotCheck (ctx) { +async function spotCheck(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const { models } = ctx.fs.dc; @@ -477,7 +477,7 @@ async function spotCheck (ctx) { } } } -async function spotCheckDetail (ctx) { +async function spotCheckDetail(ctx) { const { models } = ctx.fs.dc const { startTime, endTime } = ctx.query try { @@ -516,7 +516,7 @@ async function spotCheckDetail (ctx) { } -async function roadSpotPrepare (ctx) { +async function roadSpotPrepare(ctx) { try { const { models } = ctx.fs.dc; const sequelize = ctx.fs.dc.orm; @@ -594,7 +594,7 @@ async function roadSpotPrepare (ctx) { '乡': `route_code LIKE 'Y%'`, '村': `route_code LIKE 'C%'` } - async function getRoadTotalMileage (key, otherWhere = []) { + async function getRoadTotalMileage(key, otherWhere = []) { let res = await sequelize.query(` SELECT SUM(COALESCE(CAST(chainage_mileage AS DOUBLE PRECISION), 0)) AS total_mileage @@ -606,7 +606,7 @@ async function roadSpotPrepare (ctx) { return res[0][0].total_mileage } - async function getRoadSpot (key, lastRoadIds = [], inOrNot, otherWhere = []) { + async function getRoadSpot(key, lastRoadIds = [], inOrNot, otherWhere = []) { if (!lastRoadIds.length && !inOrNot) { return [] } @@ -625,11 +625,11 @@ async function roadSpotPrepare (ctx) { `, { type: QueryTypes.SELECT }); } - async function spotRoadId (key, lastRoadIds, targetMileage, otherWhere = [], villageIdList = []) { + async function spotRoadId(key, lastRoadIds, targetMileage, otherWhere = [], villageIdList = []) { let spotRoadIds = [] let accumulationMileage = 0 - async function filterRoad (otherWhere, getRepeat = true) { + async function filterRoad(otherWhere, getRepeat = true) { if (key == '村' && getRepeat == true) { @@ -657,6 +657,7 @@ async function roadSpotPrepare (ctx) { spotRoadIds.push(r.id) accumulationMileage += parseFloat(r.chainage_mileage) if (accumulationMileage >= targetMileage) { + break; } } @@ -684,13 +685,17 @@ async function roadSpotPrepare (ctx) { await filterRoad(otherWhere, true) } - return spotRoadIds + return [spotRoadIds, accumulationMileage] } + let villageMil = 0, townMil = 0, countryMil = 0; // 抽县 const countryRoadTotalMileage = await getRoadTotalMileage('县') const countryRoadNeedMileage = countryRoadTotalMileage * countyPercentage / 100 - let spotCountyRoadIds = await spotRoadId('县', lastCountyRoadIds, countryRoadNeedMileage, []) + let spotCountyRoadIdsArr = await spotRoadId('县', lastCountyRoadIds, countryRoadNeedMileage, []) + let spotCountyRoadIds = spotCountyRoadIdsArr[0] + let countryMil_ = spotCountyRoadIdsArr[1] + if (countryMil_) countryMil += countryMil_; // 抽乡 const allTownCodeRes = await sequelize.query(` @@ -705,15 +710,18 @@ async function roadSpotPrepare (ctx) { let spotTownRoadIds = [] let spotVillageRoadIds = [] let spotVillageIds = [] + for await (let t of allTownCodeRes) { // 遍历每个乡镇并抽取 let otherWhere = [`township_code='${t.township_code}'`] const townRoadTotalMileage = await getRoadTotalMileage('乡', otherWhere) const townRoadNeedMileage = townRoadTotalMileage * 25 / 100 - let spotTownRoadIds_ = await spotRoadId('乡', lastTownRoadIds, townRoadNeedMileage, otherWhere) + 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) const villageRoadNeedMileage = villageRoadTotalMileage * 10 / 100 @@ -755,8 +763,11 @@ async function roadSpotPrepare (ctx) { villageCheckIdList.unshift(spotFirstVillageId) villageCheckIdList = [...(new Set(villageCheckIdList))] - let spotVillageRoadIds_ = await spotRoadId('村', lastVillageRoadRoadIds, villageRoadNeedMileage, otherWhere, villageCheckIdList) + let spotVillageRoadIdsArr = await spotRoadId('村', lastVillageRoadRoadIds, villageRoadNeedMileage, otherWhere, villageCheckIdList) + let spotVillageRoadIds_ = spotVillageRoadIdsArr[0] + let villageMil_ = spotVillageRoadIdsArr[1] spotVillageRoadIds = spotVillageRoadIds.concat(spotVillageRoadIds_) + if (villageMil_) villageMil += villageMil_ } const previewRes = await models.RoadSpotCheckPreview.create({ @@ -766,7 +777,10 @@ async function roadSpotPrepare (ctx) { townshipRoadId: spotTownRoadIds, villageRoadId: spotVillageRoadIds, villageId: spotVillageIds, - checked: false + checked: false, + villageMil, + townMil, + countryMil }) ctx.status = 200; @@ -775,6 +789,10 @@ async function roadSpotPrepare (ctx) { spotCountyRoadCount: spotCountyRoadIds.length, spotTownRoadCount: spotTownRoadIds.length, spotVillageRoadCount: spotVillageRoadIds.length, + villageMil, + townMil, + countryMil + } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); @@ -785,7 +803,7 @@ async function roadSpotPrepare (ctx) { } } -async function confirmRoadSpot (ctx) { +async function confirmRoadSpot(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const { models } = ctx.fs.dc; @@ -845,7 +863,7 @@ async function confirmRoadSpot (ctx) { } } -async function roadSpotList (ctx) { +async function roadSpotList(ctx) { try { const { models } = ctx.fs.dc; const { startTime, endTime, page, limit } = ctx.query @@ -896,7 +914,7 @@ async function roadSpotList (ctx) { } } -async function roadSpotDetail (ctx) { +async function roadSpotDetail(ctx) { try { const { models } = ctx.fs.dc; const { previewId, keyword } = ctx.query @@ -933,7 +951,7 @@ async function roadSpotDetail (ctx) { } } -async function roadSpotChange (ctx) { +async function roadSpotChange(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const { models } = ctx.fs.dc; @@ -955,7 +973,7 @@ async function roadSpotChange (ctx) { } let previewUpdated = false - async function updatePreview (key) { + async function updatePreview(key) { if (previewUpdated) return if (previewRes[key] && previewRes[key].includes(originRoadId)) { @@ -1040,7 +1058,7 @@ async function roadSpotChange (ctx) { } } -async function roadSpotChangList (ctx) { +async function roadSpotChangList(ctx) { try { const { models } = ctx.fs.dc; const { startTime, endTime, page, limit } = ctx.query @@ -1093,7 +1111,7 @@ async function roadSpotChangList (ctx) { } } -async function exportSpotRode (ctx) { +async function exportSpotRode(ctx) { try { const { models } = ctx.fs.dc; const { previewId } = ctx.query @@ -1143,7 +1161,7 @@ async function exportSpotRode (ctx) { title: '养护次数(次)', },] - function judgeLevel (routeCode) { + function judgeLevel(routeCode) { if (routeCode) { if (routeCode.startsWith('X')) { return '县道' diff --git a/api/app/lib/models/road_spot_check_preview.js b/api/app/lib/models/road_spot_check_preview.js index c3519155..0dfb16ba 100644 --- a/api/app/lib/models/road_spot_check_preview.js +++ b/api/app/lib/models/road_spot_check_preview.js @@ -2,87 +2,114 @@ 'use strict'; module.exports = dc => { - const DataTypes = dc.ORM; - const sequelize = dc.orm; - const RoadSpotCheckPreview = sequelize.define("roadSpotCheckPreview", { - id: { - type: DataTypes.INTEGER, - allowNull: false, - defaultValue: null, - comment: null, - primaryKey: true, - field: "id", - autoIncrement: true, - unique: "report_spot_check_preview_v2_id_uindex" - }, - countyPercentage: { - type: DataTypes.INTEGER, - allowNull: false, - defaultValue: null, - comment: "县道抽查比例", - primaryKey: false, - field: "county_percentage", - autoIncrement: false - }, - date: { - type: DataTypes.DATE, - allowNull: true, - defaultValue: null, - comment: "抽取时间", - primaryKey: false, - field: "date", - autoIncrement: false - }, - countyRoadId: { - type: DataTypes.ARRAY(DataTypes.INTEGER), - allowNull: true, - defaultValue: null, - comment: "抽到的县道id", - primaryKey: false, - field: "county_road_id", - autoIncrement: false - }, - townshipRoadId: { - type: DataTypes.ARRAY(DataTypes.INTEGER), - allowNull: true, - defaultValue: null, - comment: "乡道id", - primaryKey: false, - field: "township_road_id", - autoIncrement: false - }, - checked: { - type: DataTypes.BOOLEAN, - allowNull: false, - defaultValue: null, - comment: "是否应用", - primaryKey: false, - field: "checked", - autoIncrement: false - }, - villageRoadId: { - type: DataTypes.ARRAY(DataTypes.INTEGER), - allowNull: true, - defaultValue: null, - comment: "村道id", - primaryKey: false, - field: "village_road_id", - autoIncrement: false - }, - villageId: { - type: DataTypes.ARRAY(DataTypes.INTEGER), - allowNull: true, - defaultValue: null, - comment: "抽到的村庄的id", - primaryKey: false, - field: "village_id", - autoIncrement: false - } - }, { - tableName: "road_spot_check_preview", - comment: "", - indexes: [] - }); - dc.models.RoadSpotCheckPreview = RoadSpotCheckPreview; - return RoadSpotCheckPreview; + const DataTypes = dc.ORM; + const sequelize = dc.orm; + const RoadSpotCheckPreview = sequelize.define("roadSpotCheckPreview", { + id: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: true, + field: "id", + autoIncrement: true, + unique: "report_spot_check_preview_v2_id_uindex" + }, + countyPercentage: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: "县道抽查比例", + primaryKey: false, + field: "county_percentage", + autoIncrement: false + }, + date: { + type: DataTypes.DATE, + allowNull: true, + defaultValue: null, + comment: "抽取时间", + primaryKey: false, + field: "date", + autoIncrement: false + }, + countyRoadId: { + type: DataTypes.ARRAY(DataTypes.INTEGER), + allowNull: true, + defaultValue: null, + comment: "抽到的县道id", + primaryKey: false, + field: "county_road_id", + autoIncrement: false + }, + townshipRoadId: { + type: DataTypes.ARRAY(DataTypes.INTEGER), + allowNull: true, + defaultValue: null, + comment: "乡道id", + primaryKey: false, + field: "township_road_id", + autoIncrement: false + }, + checked: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: null, + comment: "是否应用", + primaryKey: false, + field: "checked", + autoIncrement: false + }, + villageRoadId: { + type: DataTypes.ARRAY(DataTypes.INTEGER), + allowNull: true, + defaultValue: null, + comment: "村道id", + primaryKey: false, + field: "village_road_id", + autoIncrement: false + }, + villageId: { + type: DataTypes.ARRAY(DataTypes.INTEGER), + allowNull: true, + defaultValue: null, + comment: "抽到的村庄的id", + primaryKey: false, + field: "village_id", + autoIncrement: false + }, + villageMil: { + type: DataTypes.DOUBLE, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "village_mil", + autoIncrement: false + }, + townMil: { + type: DataTypes.DOUBLE, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "town_mil", + autoIncrement: false + }, + countryMil: { + type: DataTypes.DOUBLE, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "country_mil", + autoIncrement: false + } + }, { + tableName: "road_spot_check_preview", + comment: "", + indexes: [] + }); + dc.models.RoadSpotCheckPreview = RoadSpotCheckPreview; + return RoadSpotCheckPreview; }; \ No newline at end of file