Browse Source

(*)抽取公里数提交

dev
peng.peng 1 year ago
parent
commit
e78e608fe6
  1. 74
      api/app/lib/controllers/report/index.js
  2. 193
      api/app/lib/models/road_spot_check_preview.js

74
api/app/lib/controllers/report/index.js

@ -3,7 +3,7 @@ const { QueryTypes } = require('sequelize');
const moment = require('moment'); const moment = require('moment');
const xlsxDownload = require('../../../../utils/xlsxDownload.js'); const xlsxDownload = require('../../../../utils/xlsxDownload.js');
const fs = require('fs'); const fs = require('fs');
async function reportList (ctx) { async function reportList(ctx) {
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { limit, page, startTime, endTime, keyword, userId, reportType, isTop, asc, projectType, handleState = '', performerId = '', codeRoad } = ctx.query 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 { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { startTime, endTime, userId, reportType } = ctx.query const { startTime, endTime, userId, reportType } = ctx.query
@ -199,7 +199,7 @@ async function reportPosition (ctx) {
} }
} }
async function reportDetail (ctx) { async function reportDetail(ctx) {
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { reportId } = ctx.params const { reportId } = ctx.params
@ -228,7 +228,7 @@ async function reportDetail (ctx) {
} }
} }
async function reportHandle (ctx) { async function reportHandle(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
@ -258,7 +258,7 @@ async function reportHandle (ctx) {
} }
} }
async function createReport (ctx) { async function createReport(ctx) {
try { try {
const { userId } = ctx.fs.api const { userId } = ctx.fs.api
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
@ -280,7 +280,7 @@ async function createReport (ctx) {
} }
} }
async function deleteReport (ctx) { async function deleteReport(ctx) {
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { reportId } = ctx.params; const { reportId } = ctx.params;
@ -303,7 +303,7 @@ async function deleteReport (ctx) {
// TODO 小程序填写道路名称的时候的道路筛选 是一起都返回 还是不断传关键字搜索返回 // TODO 小程序填写道路名称的时候的道路筛选 是一起都返回 还是不断传关键字搜索返回
async function spotPrepare (ctx) { async function spotPrepare(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const sequelize = ctx.fs.dc.orm; 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(); const transaction = await ctx.fs.dc.orm.transaction();
try { try {
const { models } = ctx.fs.dc; 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 { models } = ctx.fs.dc
const { startTime, endTime } = ctx.query const { startTime, endTime } = ctx.query
try { try {
@ -516,7 +516,7 @@ async function spotCheckDetail (ctx) {
} }
async function roadSpotPrepare (ctx) { async function roadSpotPrepare(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const sequelize = ctx.fs.dc.orm; const sequelize = ctx.fs.dc.orm;
@ -594,7 +594,7 @@ async function roadSpotPrepare (ctx) {
'乡': `route_code LIKE 'Y%'`, '乡': `route_code LIKE 'Y%'`,
'村': `route_code LIKE 'C%'` '村': `route_code LIKE 'C%'`
} }
async function getRoadTotalMileage (key, otherWhere = []) { async function getRoadTotalMileage(key, otherWhere = []) {
let res = await sequelize.query(` let res = await sequelize.query(`
SELECT SELECT
SUM(COALESCE(CAST(chainage_mileage AS DOUBLE PRECISION), 0)) AS total_mileage 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 return res[0][0].total_mileage
} }
async function getRoadSpot (key, lastRoadIds = [], inOrNot, otherWhere = []) { async function getRoadSpot(key, lastRoadIds = [], inOrNot, otherWhere = []) {
if (!lastRoadIds.length && !inOrNot) { if (!lastRoadIds.length && !inOrNot) {
return [] return []
} }
@ -625,11 +625,11 @@ async function roadSpotPrepare (ctx) {
`, { type: QueryTypes.SELECT }); `, { type: QueryTypes.SELECT });
} }
async function spotRoadId (key, lastRoadIds, targetMileage, otherWhere = [], villageIdList = []) { async function spotRoadId(key, lastRoadIds, targetMileage, otherWhere = [], villageIdList = []) {
let spotRoadIds = [] let spotRoadIds = []
let accumulationMileage = 0 let accumulationMileage = 0
async function filterRoad (otherWhere, getRepeat = true) { async function filterRoad(otherWhere, getRepeat = true) {
if (key == '村' && getRepeat == true) { if (key == '村' && getRepeat == true) {
@ -657,6 +657,7 @@ async function roadSpotPrepare (ctx) {
spotRoadIds.push(r.id) spotRoadIds.push(r.id)
accumulationMileage += parseFloat(r.chainage_mileage) accumulationMileage += parseFloat(r.chainage_mileage)
if (accumulationMileage >= targetMileage) { if (accumulationMileage >= targetMileage) {
break; break;
} }
} }
@ -684,13 +685,17 @@ async function roadSpotPrepare (ctx) {
await filterRoad(otherWhere, true) await filterRoad(otherWhere, true)
} }
return spotRoadIds return [spotRoadIds, accumulationMileage]
} }
let villageMil = 0, townMil = 0, countryMil = 0;
// 抽县 // 抽县
const countryRoadTotalMileage = await getRoadTotalMileage('县') const countryRoadTotalMileage = await getRoadTotalMileage('县')
const countryRoadNeedMileage = countryRoadTotalMileage * countyPercentage / 100 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(` const allTownCodeRes = await sequelize.query(`
@ -705,15 +710,18 @@ async function roadSpotPrepare (ctx) {
let spotTownRoadIds = [] let spotTownRoadIds = []
let spotVillageRoadIds = [] let spotVillageRoadIds = []
let spotVillageIds = [] let spotVillageIds = []
for await (let t of allTownCodeRes) { for await (let t of allTownCodeRes) {
// 遍历每个乡镇并抽取 // 遍历每个乡镇并抽取
let otherWhere = [`township_code='${t.township_code}'`] let otherWhere = [`township_code='${t.township_code}'`]
const townRoadTotalMileage = await getRoadTotalMileage('乡', otherWhere) const townRoadTotalMileage = await getRoadTotalMileage('乡', otherWhere)
const townRoadNeedMileage = townRoadTotalMileage * 25 / 100 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_) spotTownRoadIds = spotTownRoadIds.concat(spotTownRoadIds_)
if (townMil_) townMil += townMil_
// 抽村 // 抽村
const villageRoadTotalMileage = await getRoadTotalMileage('村', otherWhere) const villageRoadTotalMileage = await getRoadTotalMileage('村', otherWhere)
const villageRoadNeedMileage = villageRoadTotalMileage * 10 / 100 const villageRoadNeedMileage = villageRoadTotalMileage * 10 / 100
@ -755,8 +763,11 @@ async function roadSpotPrepare (ctx) {
villageCheckIdList.unshift(spotFirstVillageId) villageCheckIdList.unshift(spotFirstVillageId)
villageCheckIdList = [...(new Set(villageCheckIdList))] 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_) spotVillageRoadIds = spotVillageRoadIds.concat(spotVillageRoadIds_)
if (villageMil_) villageMil += villageMil_
} }
const previewRes = await models.RoadSpotCheckPreview.create({ const previewRes = await models.RoadSpotCheckPreview.create({
@ -766,7 +777,10 @@ async function roadSpotPrepare (ctx) {
townshipRoadId: spotTownRoadIds, townshipRoadId: spotTownRoadIds,
villageRoadId: spotVillageRoadIds, villageRoadId: spotVillageRoadIds,
villageId: spotVillageIds, villageId: spotVillageIds,
checked: false checked: false,
villageMil,
townMil,
countryMil
}) })
ctx.status = 200; ctx.status = 200;
@ -775,6 +789,10 @@ async function roadSpotPrepare (ctx) {
spotCountyRoadCount: spotCountyRoadIds.length, spotCountyRoadCount: spotCountyRoadIds.length,
spotTownRoadCount: spotTownRoadIds.length, spotTownRoadCount: spotTownRoadIds.length,
spotVillageRoadCount: spotVillageRoadIds.length, spotVillageRoadCount: spotVillageRoadIds.length,
villageMil,
townMil,
countryMil
} }
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${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(); const transaction = await ctx.fs.dc.orm.transaction();
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
@ -845,7 +863,7 @@ async function confirmRoadSpot (ctx) {
} }
} }
async function roadSpotList (ctx) { async function roadSpotList(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { startTime, endTime, page, limit } = ctx.query const { startTime, endTime, page, limit } = ctx.query
@ -896,7 +914,7 @@ async function roadSpotList (ctx) {
} }
} }
async function roadSpotDetail (ctx) { async function roadSpotDetail(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { previewId, keyword } = ctx.query 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(); const transaction = await ctx.fs.dc.orm.transaction();
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
@ -955,7 +973,7 @@ async function roadSpotChange (ctx) {
} }
let previewUpdated = false let previewUpdated = false
async function updatePreview (key) { async function updatePreview(key) {
if (previewUpdated) return if (previewUpdated) return
if (previewRes[key] && previewRes[key].includes(originRoadId)) { if (previewRes[key] && previewRes[key].includes(originRoadId)) {
@ -1040,7 +1058,7 @@ async function roadSpotChange (ctx) {
} }
} }
async function roadSpotChangList (ctx) { async function roadSpotChangList(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { startTime, endTime, page, limit } = ctx.query const { startTime, endTime, page, limit } = ctx.query
@ -1093,7 +1111,7 @@ async function roadSpotChangList (ctx) {
} }
} }
async function exportSpotRode (ctx) { async function exportSpotRode(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { previewId } = ctx.query const { previewId } = ctx.query
@ -1143,7 +1161,7 @@ async function exportSpotRode (ctx) {
title: '养护次数(次)', title: '养护次数(次)',
},] },]
function judgeLevel (routeCode) { function judgeLevel(routeCode) {
if (routeCode) { if (routeCode) {
if (routeCode.startsWith('X')) { if (routeCode.startsWith('X')) {
return '县道' return '县道'

193
api/app/lib/models/road_spot_check_preview.js

@ -2,87 +2,114 @@
'use strict'; 'use strict';
module.exports = dc => { module.exports = dc => {
const DataTypes = dc.ORM; const DataTypes = dc.ORM;
const sequelize = dc.orm; const sequelize = dc.orm;
const RoadSpotCheckPreview = sequelize.define("roadSpotCheckPreview", { const RoadSpotCheckPreview = sequelize.define("roadSpotCheckPreview", {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: null,
primaryKey: true, primaryKey: true,
field: "id", field: "id",
autoIncrement: true, autoIncrement: true,
unique: "report_spot_check_preview_v2_id_uindex" unique: "report_spot_check_preview_v2_id_uindex"
}, },
countyPercentage: { countyPercentage: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: "县道抽查比例", comment: "县道抽查比例",
primaryKey: false, primaryKey: false,
field: "county_percentage", field: "county_percentage",
autoIncrement: false autoIncrement: false
}, },
date: { date: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: "抽取时间", comment: "抽取时间",
primaryKey: false, primaryKey: false,
field: "date", field: "date",
autoIncrement: false autoIncrement: false
}, },
countyRoadId: { countyRoadId: {
type: DataTypes.ARRAY(DataTypes.INTEGER), type: DataTypes.ARRAY(DataTypes.INTEGER),
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: "抽到的县道id", comment: "抽到的县道id",
primaryKey: false, primaryKey: false,
field: "county_road_id", field: "county_road_id",
autoIncrement: false autoIncrement: false
}, },
townshipRoadId: { townshipRoadId: {
type: DataTypes.ARRAY(DataTypes.INTEGER), type: DataTypes.ARRAY(DataTypes.INTEGER),
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: "乡道id", comment: "乡道id",
primaryKey: false, primaryKey: false,
field: "township_road_id", field: "township_road_id",
autoIncrement: false autoIncrement: false
}, },
checked: { checked: {
type: DataTypes.BOOLEAN, type: DataTypes.BOOLEAN,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: "是否应用", comment: "是否应用",
primaryKey: false, primaryKey: false,
field: "checked", field: "checked",
autoIncrement: false autoIncrement: false
}, },
villageRoadId: { villageRoadId: {
type: DataTypes.ARRAY(DataTypes.INTEGER), type: DataTypes.ARRAY(DataTypes.INTEGER),
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: "村道id", comment: "村道id",
primaryKey: false, primaryKey: false,
field: "village_road_id", field: "village_road_id",
autoIncrement: false autoIncrement: false
}, },
villageId: { villageId: {
type: DataTypes.ARRAY(DataTypes.INTEGER), type: DataTypes.ARRAY(DataTypes.INTEGER),
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: "抽到的村庄的id", comment: "抽到的村庄的id",
primaryKey: false, primaryKey: false,
field: "village_id", field: "village_id",
autoIncrement: false autoIncrement: false
} },
}, { villageMil: {
tableName: "road_spot_check_preview", type: DataTypes.DOUBLE,
comment: "", allowNull: true,
indexes: [] defaultValue: null,
}); comment: null,
dc.models.RoadSpotCheckPreview = RoadSpotCheckPreview; primaryKey: false,
return RoadSpotCheckPreview; 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;
}; };
Loading…
Cancel
Save