|
|
@ -1,8 +1,9 @@ |
|
|
|
'use strict'; |
|
|
|
const { QueryTypes } = require('sequelize'); |
|
|
|
const moment = require('moment'); |
|
|
|
|
|
|
|
async function reportList(ctx) { |
|
|
|
const xlsxDownload = require('../../../../utils/xlsxDownload.js'); |
|
|
|
const fs = require('fs'); |
|
|
|
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 |
|
|
@ -148,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 |
|
|
@ -198,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 |
|
|
@ -220,7 +221,7 @@ async function reportDetail(ctx) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function reportHandle(ctx) { |
|
|
|
async function reportHandle (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
|
|
|
@ -250,7 +251,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; |
|
|
@ -272,7 +273,7 @@ async function createReport(ctx) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function deleteReport(ctx) { |
|
|
|
async function deleteReport (ctx) { |
|
|
|
try { |
|
|
|
const models = ctx.fs.dc.models; |
|
|
|
const { reportId } = ctx.params; |
|
|
@ -295,7 +296,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; |
|
|
@ -367,7 +368,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; |
|
|
@ -456,7 +457,7 @@ async function spotCheck(ctx) { |
|
|
|
} |
|
|
|
], |
|
|
|
}) : [] |
|
|
|
console.log('rslt111', rslt) |
|
|
|
|
|
|
|
await transaction.commit(); |
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = rslt |
|
|
@ -469,7 +470,7 @@ async function spotCheck(ctx) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
async function spotCheckDetail(ctx) { |
|
|
|
async function spotCheckDetail (ctx) { |
|
|
|
const { models } = ctx.fs.dc |
|
|
|
const { startTime, endTime } = ctx.query |
|
|
|
try { |
|
|
@ -508,10 +509,633 @@ 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 = [] // 上次查得的村道id
|
|
|
|
let lastVillageIds = [] |
|
|
|
|
|
|
|
if (lastSpotRes) { |
|
|
|
lastCountyPercentage = lastSpotRes.countyPercentage |
|
|
|
// 遍历 RoadSpotCheck,保存上次查询的路的id
|
|
|
|
lastCountyRoadIds = lastSpotRes.countyRoadId || [] |
|
|
|
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 = []) { |
|
|
|
let res = await sequelize.query(` |
|
|
|
SELECT |
|
|
|
SUM(COALESCE(CAST(chainage_mileage AS DOUBLE PRECISION), 0)) AS total_mileage |
|
|
|
FROM road |
|
|
|
WHERE ${keyMap[key]} ${otherWhere.length ? `AND ${otherWhere.join(' AND ')}` : ''} |
|
|
|
`)
|
|
|
|
return res[0][0].total_mileage |
|
|
|
} |
|
|
|
|
|
|
|
async function getRoadSpot (key, lastRoadIds = [], inOrNot, otherWhere = []) { |
|
|
|
if (!lastRoadIds.length && !inOrNot) { |
|
|
|
return [] |
|
|
|
} |
|
|
|
return await sequelize.query(` |
|
|
|
SELECT id, chainage_mileage FROM road |
|
|
|
WHERE ${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 |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
if (accumulationMileage >= targetMileage) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
if (accumulationMileage < targetMileage) { |
|
|
|
// 还小于 说明没取够
|
|
|
|
await filterRoad(otherWhere, true) |
|
|
|
} |
|
|
|
} else { |
|
|
|
await filterRoad(otherWhere, true) |
|
|
|
} |
|
|
|
|
|
|
|
return spotRoadIds |
|
|
|
} |
|
|
|
|
|
|
|
// 抽县
|
|
|
|
const countryRoadTotalMileage = await getRoadTotalMileage('县') |
|
|
|
const countryRoadNeedMileage = countryRoadTotalMileage * countyPercentage / 100 |
|
|
|
let spotCountyRoadIds = await spotRoadId('县', lastCountyRoadIds, countryRoadNeedMileage, []) |
|
|
|
|
|
|
|
// 抽乡
|
|
|
|
const allTownCodeRes = await sequelize.query(` |
|
|
|
SELECT |
|
|
|
DISTINCT township_code |
|
|
|
FROM road |
|
|
|
WHERE township_code IS NOT NULL |
|
|
|
`, { type: QueryTypes.SELECT });
|
|
|
|
|
|
|
|
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) |
|
|
|
spotTownRoadIds = spotTownRoadIds.concat(spotTownRoadIds_) |
|
|
|
|
|
|
|
// 抽村
|
|
|
|
const villageRoadTotalMileage = await getRoadTotalMileage('村', otherWhere) |
|
|
|
const villageRoadNeedMileage = villageRoadTotalMileage * 10 / 100 |
|
|
|
|
|
|
|
let spotFirstVillageId = -1 |
|
|
|
// 随机选取一个不在上次查过的村
|
|
|
|
let 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 (villageRes.length) { |
|
|
|
spotVillageIds.push(villageRes[0].id) |
|
|
|
spotFirstVillageId = villageRes[0].id |
|
|
|
} else { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
const villageNearRes = await sequelize.query(` |
|
|
|
SELECT id,calc_village,diatance FROM village_distance |
|
|
|
WHERE origin_village = ${spotFirstVillageId} |
|
|
|
ORDER BY diatance ASC |
|
|
|
`, { type: QueryTypes.SELECT })
|
|
|
|
|
|
|
|
let villageCheckIdList = villageNearRes.map(item => item.calc_village) |
|
|
|
villageCheckIdList.unshift(spotFirstVillageId) |
|
|
|
villageCheckIdList = [...(new Set(villageCheckIdList))] |
|
|
|
|
|
|
|
let spotVillageRoadIds_ = await spotRoadId('村', lastVillageRoadRoadIds, villageRoadNeedMileage, otherWhere, villageCheckIdList) |
|
|
|
spotVillageRoadIds = spotVillageRoadIds.concat(spotVillageRoadIds_) |
|
|
|
} |
|
|
|
|
|
|
|
const previewRes = await models.RoadSpotCheckPreview.create({ |
|
|
|
countyPercentage: countyPercentage, |
|
|
|
date: moment().format(), |
|
|
|
countyRoadId: spotCountyRoadIds, |
|
|
|
townshipRoadId: spotTownRoadIds, |
|
|
|
villageRoadId: spotVillageRoadIds, |
|
|
|
villageId: spotVillageIds, |
|
|
|
checked: false |
|
|
|
}) |
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = { |
|
|
|
previewId: previewRes.id, |
|
|
|
spotCountyRoadCount: spotCountyRoadIds.length, |
|
|
|
spotTownRoadCount: spotTownRoadIds.length, |
|
|
|
spotVillageRoadCount: spotVillageRoadIds.length, |
|
|
|
} |
|
|
|
} 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(); |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const sequelize = ctx.fs.dc.orm; |
|
|
|
const { previewId } = ctx.request.body |
|
|
|
|
|
|
|
const prepareRes = await models.RoadSpotCheckPreview.findOne({ |
|
|
|
where: { |
|
|
|
id: previewId |
|
|
|
} |
|
|
|
}) |
|
|
|
let spotedRoadIds = [] |
|
|
|
spotedRoadIds = spotedRoadIds.concat(prepareRes.dataValues.countyRoadId) |
|
|
|
spotedRoadIds = spotedRoadIds.concat(prepareRes.dataValues.townshipRoadId) |
|
|
|
spotedRoadIds = spotedRoadIds.concat(prepareRes.dataValues.villageRoadId) |
|
|
|
|
|
|
|
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(item => item.road_id == item) |
|
|
|
spotRslt.push({ |
|
|
|
roadId: item, |
|
|
|
maintenanceCount: corConserveCount ? corConserveCount.count : 0, |
|
|
|
prepareId: previewId |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
await models.RoadSpotCheck.bulkCreate(spotRslt, { |
|
|
|
transaction |
|
|
|
}) |
|
|
|
await models.RoadSpotCheckPreview.update({ |
|
|
|
checked: true |
|
|
|
}, { |
|
|
|
where: { |
|
|
|
id: previewId |
|
|
|
}, |
|
|
|
transaction |
|
|
|
}) |
|
|
|
|
|
|
|
await transaction.commit(); |
|
|
|
ctx.status = 204; |
|
|
|
} catch (error) { |
|
|
|
await transaction.rollback(); |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function roadSpotList (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { startTime, endTime, page, limit } = ctx.query |
|
|
|
|
|
|
|
let findOpt = { |
|
|
|
order: [['id', 'DESC']], |
|
|
|
where: { |
|
|
|
checked: false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (startTime && endTime) { |
|
|
|
findOpt.where = { |
|
|
|
date: { |
|
|
|
$between: [moment(startTime).startOf('day').format(), moment(endTime).endOf('day').format()] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const listRes = await models.RoadSpotCheckPreview.findAll({ |
|
|
|
order: [['id', 'DESC']], |
|
|
|
where: { |
|
|
|
checked: true, |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
if (page && limit) { |
|
|
|
findOpt.offset = (page - 1) * limit |
|
|
|
findOpt.limit = limit |
|
|
|
} |
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = listRes.map(item => { |
|
|
|
return { |
|
|
|
id: item.id, |
|
|
|
date: item.date, |
|
|
|
countyPercentage: item.countyPercentage, |
|
|
|
spotCountyRoadCount: item.countyRoadId ? item.countyRoadId.length : 0, |
|
|
|
spotTownRoadCount: item.townshipRoadId ? item.townshipRoadId.length : 0, |
|
|
|
spotVillageRoadCount: item.villageId ? item.villageId.length : 0, |
|
|
|
} |
|
|
|
}) |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function roadSpotDetail (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { previewId } = ctx.query |
|
|
|
|
|
|
|
const listRes = await models.RoadSpotCheck.findAll({ |
|
|
|
where: { |
|
|
|
prepareId: previewId |
|
|
|
}, |
|
|
|
include: [{ |
|
|
|
model: models.Road, |
|
|
|
// required: false,
|
|
|
|
}] |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = listRes |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function roadSpotChange (ctx) { |
|
|
|
const transaction = await ctx.fs.dc.orm.transaction(); |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const sequelize = ctx.fs.dc.orm; |
|
|
|
const { originRoadId, changeRoadId, previewId } = ctx.request.body |
|
|
|
|
|
|
|
if (originRoadId == changeRoadId) { |
|
|
|
throw '原路段与目标路段不能相同' |
|
|
|
} |
|
|
|
|
|
|
|
const previewRes = await models.RoadSpotCheckPreview.findOne({ |
|
|
|
where: { |
|
|
|
id: previewId |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
if (!previewRes) { |
|
|
|
throw '预览数据不存在' |
|
|
|
} |
|
|
|
|
|
|
|
let previewUpdated = false |
|
|
|
async function updatePreview (key) { |
|
|
|
if (previewUpdated) return |
|
|
|
|
|
|
|
if (previewRes[key] && previewRes[key].includes(originRoadId)) { |
|
|
|
let originRoadIds = previewRes.dataValues[key] |
|
|
|
let originRoadIdIndex = originRoadIds.indexOf(originRoadId) |
|
|
|
|
|
|
|
originRoadIds.splice(originRoadIdIndex, 1, changeRoadId) |
|
|
|
|
|
|
|
await models.RoadSpotCheckPreview.update({ |
|
|
|
[key]: originRoadIds |
|
|
|
}, { |
|
|
|
where: { |
|
|
|
id: previewId |
|
|
|
}, |
|
|
|
transaction |
|
|
|
}) |
|
|
|
previewUpdated = true |
|
|
|
} |
|
|
|
} |
|
|
|
await updatePreview('countyRoadId') |
|
|
|
await updatePreview('townshipRoadId') |
|
|
|
await updatePreview('villageRoadId') |
|
|
|
|
|
|
|
if (!previewUpdated) { |
|
|
|
throw '更改路段不在抽查范围内' |
|
|
|
} |
|
|
|
|
|
|
|
const roadRes = await models.Road.findAll({ |
|
|
|
where: { |
|
|
|
id: { $in: [originRoadId, changeRoadId] } |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
if (roadRes.length != 2) { |
|
|
|
throw '路段不存在' |
|
|
|
} |
|
|
|
|
|
|
|
let content = '' |
|
|
|
let curOriginRoad = roadRes.find(item => item.id == originRoadId) |
|
|
|
let curChangeRoad = roadRes.find(item => item.id == changeRoadId) |
|
|
|
|
|
|
|
content = `将${curOriginRoad.routeName}从${curOriginRoad.startingPlaceName}到${curOriginRoad.stopPlaceName}改为${curChangeRoad.routeName}从${curChangeRoad.startingPlaceName}到${curChangeRoad.stopPlaceName}` |
|
|
|
|
|
|
|
const maintenanceCount = await sequelize.query(` |
|
|
|
SELECT COUNT(id) as count |
|
|
|
FROM report |
|
|
|
WHERE report_type = 'conserve' AND road_id=${changeRoadId} |
|
|
|
`, { type: QueryTypes.SELECT })
|
|
|
|
|
|
|
|
await models.RoadSpotCheck.update({ |
|
|
|
roadId: changeRoadId, |
|
|
|
maintenanceCount: maintenanceCount[0].count |
|
|
|
}, { |
|
|
|
where: { |
|
|
|
roadId: originRoadId, |
|
|
|
prepareId: previewId |
|
|
|
}, |
|
|
|
transaction |
|
|
|
}) |
|
|
|
await models.RoadSpotCheckChangeLog.create({ |
|
|
|
userId: ctx.fs.api.userId, |
|
|
|
time: moment().format(), |
|
|
|
originRoadId: originRoadId, |
|
|
|
changeRoadId: changeRoadId, |
|
|
|
content: content, |
|
|
|
prepareId: previewId |
|
|
|
}, { |
|
|
|
transaction |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
await transaction.commit(); |
|
|
|
ctx.status = 204; |
|
|
|
} catch (error) { |
|
|
|
await transaction.rollback(); |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function roadSpotChangList (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { startTime, endTime, page, limit } = ctx.query |
|
|
|
|
|
|
|
let findOptPreview = { |
|
|
|
where: { |
|
|
|
checked: true, |
|
|
|
}, |
|
|
|
attributes: ['id'] |
|
|
|
} |
|
|
|
|
|
|
|
if (startTime && endTime) { |
|
|
|
findOptPreview.where = { |
|
|
|
date: { |
|
|
|
$between: [moment(startTime).startOf('day').format(), moment(endTime).endOf('day').format()] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
let findOpt = { |
|
|
|
order: [['id', 'DESC']], |
|
|
|
where: { |
|
|
|
|
|
|
|
}, |
|
|
|
include: [{ |
|
|
|
model: models.RoadSpotCheckPreview, |
|
|
|
...findOptPreview, |
|
|
|
required: true, |
|
|
|
}, { |
|
|
|
model: models.User, |
|
|
|
attributes: ['name'] |
|
|
|
}] |
|
|
|
} |
|
|
|
|
|
|
|
if (page && limit) { |
|
|
|
findOpt.offset = (page - 1) * limit |
|
|
|
findOpt.limit = limit |
|
|
|
} |
|
|
|
|
|
|
|
const listRes = await models.RoadSpotCheckChangeLog.findAll(findOpt) |
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = listRes |
|
|
|
} 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 previewRes = await models.RoadSpotCheckPreview.findOne({ |
|
|
|
where: { |
|
|
|
id: previewId |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
const listRes = await models.RoadSpotCheck.findAll({ |
|
|
|
where: { |
|
|
|
prepareId: previewId |
|
|
|
}, |
|
|
|
include: [{ |
|
|
|
model: models.Road, |
|
|
|
// required: false,
|
|
|
|
}] |
|
|
|
}) |
|
|
|
|
|
|
|
const header = [{ |
|
|
|
key: 'level', |
|
|
|
title: '道路类型', |
|
|
|
}, { |
|
|
|
key: 'routeName', |
|
|
|
title: '路线名称', |
|
|
|
}, { |
|
|
|
key: 'routeCode', |
|
|
|
title: '路线代码', |
|
|
|
}, { |
|
|
|
key: 'sectionNo', |
|
|
|
title: '路段序号', |
|
|
|
}, { |
|
|
|
key: 'startingPlaceName', |
|
|
|
title: '起点名称', |
|
|
|
}, { |
|
|
|
key: 'stopPlaceName', |
|
|
|
title: '止点名称', |
|
|
|
}, { |
|
|
|
key: '', |
|
|
|
title: '里程', |
|
|
|
}, { |
|
|
|
key: '', |
|
|
|
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.dataValues |
|
|
|
return { |
|
|
|
level: |
|
|
|
judgeLevel(road.routeCode) |
|
|
|
, |
|
|
|
routeName: road.routeName, |
|
|
|
routeCode: road.routeCode, |
|
|
|
sectionNo: road.sectionNo, |
|
|
|
startingPlaceName: road.startingPlaceName, |
|
|
|
stopPlaceName: road.stopPlaceName, |
|
|
|
mileage: road.chainageMileage, |
|
|
|
maintenanceCount: item.maintenanceCount, |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
const fileName = `${moment(previewRes.date).format('YYYY年MM月DD日HH时mm分')}道路抽查记录` + '.csv' |
|
|
|
const filePath = await xlsxDownload.simpleExcelDown({ |
|
|
|
data: exportData, header, fileName: fileName |
|
|
|
}) |
|
|
|
const fileData = fs.readFileSync(filePath); |
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.set('Content-Type', 'application/x-xls'); |
|
|
|
ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName)); |
|
|
|
ctx.body = fileData; |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
reportList, |
|
|
|
reportPosition, |
|
|
|
reportDetail, createReport, deleteReport, reportHandle, |
|
|
|
spotPrepare, spotCheck, spotCheckDetail |
|
|
|
spotPrepare, spotCheck, spotCheckDetail, |
|
|
|
roadSpotPrepare, confirmRoadSpot, roadSpotList, roadSpotDetail, roadSpotChange, roadSpotChangList, exportSpotRode |
|
|
|
}; |