peng.peng 1 year ago
parent
commit
37275b8e65
  1. 122
      api/app/lib/controllers/report/index.js
  2. 11
      api/app/lib/models/road_spot_check_preview.js
  3. 42
      api/app/lib/models/town.js
  4. 42
      api/utils/xlsxDownload.js
  5. 5
      scripts/1.4.1/schema/3.alter_road_spot_check_preview.sql

122
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
@ -210,9 +210,9 @@ async function reportDetail(ctx) {
}, },
include: [{ include: [{
model: models.Road, model: models.Road,
where: { // where: {
del: false // del: false
}, // },
as: 'road_', as: 'road_',
}], }],
}) })
@ -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;
@ -302,7 +302,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;
@ -374,7 +374,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;
@ -476,7 +476,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 {
@ -515,7 +515,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;
@ -541,6 +541,7 @@ async function roadSpotPrepare(ctx) {
let lastTownRoadIds = [] // 上次查得的乡镇道id let lastTownRoadIds = [] // 上次查得的乡镇道id
let lastVillageRoadRoadIds = [] // 上次查得的村道id let lastVillageRoadRoadIds = [] // 上次查得的村道id
let lastVillageIds = [] let lastVillageIds = []
let gather = [] //汇总
if (lastSpotRes) { if (lastSpotRes) {
lastCountyPercentage = lastSpotRes.countyPercentage lastCountyPercentage = lastSpotRes.countyPercentage
@ -591,19 +592,19 @@ 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 = [], spot) {
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
FROM road FROM road
WHERE del = false WHERE del = false
AND spot = true AND spot = ${spot}
AND ${keyMap[key]} ${otherWhere.length ? `AND ${otherWhere.join(' AND ')}` : ''} AND ${keyMap[key]} ${otherWhere.length ? `AND ${otherWhere.join(' AND ')}` : ''}
`) `)
return res[0][0].total_mileage return res[0][0].total_mileage || 0
} }
async function getRoadSpot(key, lastRoadIds = [], inOrNot, otherWhere = []) { async function getRoadSpot (key, lastRoadIds = [], inOrNot, otherWhere = []) {
if (!lastRoadIds.length && !inOrNot) { if (!lastRoadIds.length && !inOrNot) {
return [] return []
} }
@ -622,11 +623,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) {
@ -687,13 +688,25 @@ async function roadSpotPrepare(ctx) {
let villageMil = 0, townMil = 0, countryMil = 0; let villageMil = 0, townMil = 0, countryMil = 0;
// 抽县 // 抽县
const countryRoadTotalMileage = await getRoadTotalMileage('县') const countryRoadTotalMileage = await getRoadTotalMileage('县', '', true)
const countryRoadHide = await getRoadTotalMileage('县', '', false)
const countryRoadNeedMileage = countryRoadTotalMileage * countyPercentage / 100 const countryRoadNeedMileage = countryRoadTotalMileage * countyPercentage / 100
let spotCountyRoadIdsArr = await spotRoadId('县', lastCountyRoadIds, countryRoadNeedMileage, []) let spotCountyRoadIdsArr = await spotRoadId('县', lastCountyRoadIds, countryRoadNeedMileage, [])
let spotCountyRoadIds = spotCountyRoadIdsArr[0] let spotCountyRoadIds = spotCountyRoadIdsArr[0]
let countryMil_ = spotCountyRoadIdsArr[1] let countryMil_ = spotCountyRoadIdsArr[1] || 0
if (countryMil_) countryMil += countryMil_; 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(` const allTownCodeRes = await sequelize.query(`
SELECT SELECT
@ -708,10 +721,12 @@ async function roadSpotPrepare(ctx) {
let spotVillageRoadIds = [] let spotVillageRoadIds = []
let spotVillageIds = [] let spotVillageIds = []
let allTownCodeResTown = []
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, true)
const townRoadHide = await getRoadTotalMileage('乡', otherWhere, false)
const townRoadNeedMileage = townRoadTotalMileage * 25 / 100 const townRoadNeedMileage = townRoadTotalMileage * 25 / 100
let spotTownRoadIdsArr = await spotRoadId('乡', lastTownRoadIds, townRoadNeedMileage, otherWhere) let spotTownRoadIdsArr = await spotRoadId('乡', lastTownRoadIds, townRoadNeedMileage, otherWhere)
@ -719,8 +734,11 @@ async function roadSpotPrepare(ctx) {
let townMil_ = spotTownRoadIdsArr[1] let townMil_ = spotTownRoadIdsArr[1]
spotTownRoadIds = spotTownRoadIds.concat(spotTownRoadIds_) spotTownRoadIds = spotTownRoadIds.concat(spotTownRoadIds_)
if (townMil_) townMil += townMil_ if (townMil_) townMil += townMil_
// 抽村 // 抽村
const villageRoadTotalMileage = await getRoadTotalMileage('村', otherWhere) const villageRoadTotalMileage = await getRoadTotalMileage('村', otherWhere, true)
const villageRoadTotalHide = await getRoadTotalMileage('村', otherWhere, false)
const villageRoadNeedMileage = villageRoadTotalMileage * 10 / 100 const villageRoadNeedMileage = villageRoadTotalMileage * 10 / 100
let spotFirstVillageId = -1 let spotFirstVillageId = -1
@ -747,6 +765,17 @@ async function roadSpotPrepare(ctx) {
spotVillageIds.push(villageRes[0].id) spotVillageIds.push(villageRes[0].id)
spotFirstVillageId = villageRes[0].id spotFirstVillageId = villageRes[0].id
} else { } 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; continue;
} }
@ -765,7 +794,34 @@ async function roadSpotPrepare(ctx) {
let villageMil_ = spotVillageRoadIdsArr[1] let villageMil_ = spotVillageRoadIdsArr[1]
spotVillageRoadIds = spotVillageRoadIds.concat(spotVillageRoadIds_) spotVillageRoadIds = spotVillageRoadIds.concat(spotVillageRoadIds_)
if (villageMil_) villageMil += villageMil_ 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
})
}
town.forEach(async d => {
if (allTownCodeResTown.find(c => c.code == d.code)) {
gather.push({ ...(allTownCodeResTown.find(c => c.code == d.code) || {}), name: d.name })
} else {
gather.push({
name: d.name,
township: await getRoadTotalMileage('乡', [`township_code='${d.code}'`], false),
village: await getRoadTotalMileage('村', [`township_code='${d.code}'`], false),
})
} }
})
const previewRes = await models.RoadSpotCheckPreview.create({ const previewRes = await models.RoadSpotCheckPreview.create({
countyPercentage: countyPercentage, countyPercentage: countyPercentage,
@ -777,9 +833,12 @@ async function roadSpotPrepare(ctx) {
checked: false, checked: false,
villageMil, villageMil,
townMil, townMil,
countryMil countryMil,
gather
}) })
ctx.status = 200; ctx.status = 200;
ctx.body = { ctx.body = {
previewId: previewRes.id, previewId: previewRes.id,
@ -800,7 +859,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;
@ -860,7 +919,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
@ -914,7 +973,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
@ -951,7 +1010,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;
@ -973,7 +1032,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)) {
@ -1058,7 +1117,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
@ -1194,7 +1253,8 @@ async function exportSpotRode (ctx) {
const fileName = `${moment(previewRes.date).format('YYYY年MM月DD日HH时mm分')}道路抽查记录` + '.csv' const fileName = `${moment(previewRes.date).format('YYYY年MM月DD日HH时mm分')}道路抽查记录` + '.csv'
const filePath = await xlsxDownload.simpleExcelDown({ const filePath = await xlsxDownload.simpleExcelDown({
data: exportData, header, fileName: fileName, gather:{data: exportData, header} // data: exportData, header, fileName: fileName, gather: { data: previewRes.gather || [] }
data: exportData, header, fileName: fileName,
}) })
const fileData = fs.readFileSync(filePath); const fileData = fs.readFileSync(filePath);

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

@ -104,7 +104,16 @@ module.exports = dc => {
primaryKey: false, primaryKey: false,
field: "country_mil", field: "country_mil",
autoIncrement: false autoIncrement: false
} },
gather: {
type: DataTypes.JSONB,
allowNull: true,
defaultValue: null,
comment: null,
primaryKey: false,
field: "gather",
autoIncrement: false
},
}, { }, {
tableName: "road_spot_check_preview", tableName: "road_spot_check_preview",
comment: "", comment: "",

42
api/app/lib/models/town.js

@ -0,0 +1,42 @@
/* eslint-disable*/
'use strict';
module.exports = dc => {
const DataTypes = dc.ORM;
const sequelize = dc.orm;
const Town = sequelize.define("town", {
id: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: true,
field: "id",
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
comment: null,
primaryKey: false,
field: "name",
autoIncrement: false
},
code: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
comment: null,
primaryKey: false,
field: "code",
autoIncrement: false
},
}, {
tableName: "town",
comment: "",
indexes: []
});
dc.models.Town = Town;
return Town;
};

42
api/utils/xlsxDownload.js

@ -52,7 +52,7 @@ async function gatherSheet ({ sheet_2, data = [] }) {
} }
const header = [{ const header = [{
key: '', key: 'name',
title: '', title: '',
},{ },{
key: 'county', key: 'county',
@ -64,7 +64,7 @@ async function gatherSheet ({ sheet_2, data = [] }) {
key: 'countyPresent', key: 'countyPresent',
title: '本次考核里程', title: '本次考核里程',
}, { }, {
key: 'county', key: 'countyDifferenceValue',
title: '实际抽取比原计划多', title: '实际抽取比原计划多',
}, { }, {
key: 'township', key: 'township',
@ -76,7 +76,7 @@ async function gatherSheet ({ sheet_2, data = [] }) {
key: 'townshipPresent', key: 'townshipPresent',
title: '本次考核里程', title: '本次考核里程',
}, { }, {
key: 'township', key: 'townshipDifferenceValue',
title: '实际抽取比原计划多', title: '实际抽取比原计划多',
}, { }, {
key: 'village', key: 'village',
@ -88,7 +88,7 @@ async function gatherSheet ({ sheet_2, data = [] }) {
key: 'villagePresent', key: 'villagePresent',
title: '本次考核里程', title: '本次考核里程',
}, { }, {
key: 'village', key: 'villageDifferenceValue',
title: '实际抽取比原计划多', title: '实际抽取比原计划多',
},] },]
@ -100,24 +100,22 @@ async function gatherSheet ({ sheet_2, data = [] }) {
} }
// data // data
// const style = new xlsx.Style(); const style = new xlsx.Style();
// style.align.h = 'left'; style.align.h = 'left';
// style.align.v = 'center'; style.align.v = 'center';
// style.border.right = 'thin'; style.border.right = 'thin';
// style.border.rightColor = '#000000'; style.border.rightColor = '#000000';
// style.border.bottom = 'thin'; style.border.bottom = 'thin';
// style.border.bottomColor = '#000000'; style.border.bottomColor = '#000000';
// for (let i = 0; i < data.length; i++) {
// const row = sheet_2.addRow(); for (let i = 0; i < data.length; i++) {
// const indexCell = row.addCell(); const row = sheet_2.addRow();
// indexCell.value = i + 1 for (let h of header) {
// indexCell.style = headerStyle const cell = row.addCell();
// for (let h of header) { cell.value = data[i][h.key];
// const cell = row.addCell(); cell.style = style
// cell.value = data[i][h.key]; }
// cell.style = style }
// }
// }
} }
async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss'), exp, gather } = {}) { async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss'), exp, gather } = {}) {

5
scripts/1.4.1/schema/3.alter_road_spot_check_preview.sql

@ -0,0 +1,5 @@
alter table road_spot_check_preview
add gather jsonb;
comment on column road_spot_check_preview.gather is '抽查汇总';
Loading…
Cancel
Save