diff --git a/api/app/lib/controllers/report/index.js b/api/app/lib/controllers/report/index.js index 4eac2257..a81030f1 100644 --- a/api/app/lib/controllers/report/index.js +++ b/api/app/lib/controllers/report/index.js @@ -302,7 +302,6 @@ async function deleteReport(ctx) { } // TODO 小程序填写道路名称的时候的道路筛选 是一起都返回 还是不断传关键字搜索返回 - async function spotPrepare(ctx) { try { const { models } = ctx.fs.dc; @@ -538,7 +537,6 @@ async function roadSpotPrepare(ctx) { }) let lastCountyPercentage = 0 // 最后一次的县道百分比 - let lastCountyRoadIds = [] // 上次查得的县道id let lastTownRoadIds = [] // 上次查得的乡镇道id let lastVillageRoadRoadIds = [] // 上次查得的村道id @@ -588,7 +586,6 @@ async function roadSpotPrepare(ctx) { } // 先查上次没查的范围内的 然后比较百分比 如果重叠 再查上次查过的 - let keyMap = { '县': `route_code LIKE 'X%'`, '乡': `route_code LIKE 'Y%'`, @@ -1114,104 +1111,105 @@ async function roadSpotChangList(ctx) { } } -async function exportSpotRode(ctx) { - try { - const { models } = ctx.fs.dc; - const { previewId } = ctx.query +async function exportSpotRode (ctx) { + try { + const { models } = ctx.fs.dc; + const { previewId } = ctx.query - const previewRes = await models.RoadSpotCheckPreview.findOne({ - where: { - id: previewId - } - }) + const previewRes = await models.RoadSpotCheckPreview.findOne({ + where: { + id: previewId + } + }) - const listRes = await models.RoadSpotCheck.findAll({ + const listRes = await models.RoadSpotCheck.findAll({ + where: { + prepareId: previewId + }, + include: [{ + model: models.Road, + // required: false, where: { - prepareId: previewId - }, - include: [{ - model: models.Road, - // required: false, - where: { - del: false, - } - }] - }) - - const header = [{ - key: 'level', - title: '道路类型', - }, { - key: 'routeName', - title: '路线名称', - }, { - key: 'routeCode', - title: '路线代码', - }, { - key: 'sectionNo', - title: '路段序号', - }, { - key: 'startingPlaceName', - title: '起点名称', - }, { - key: 'stopPlaceName', - title: '止点名称', - }, { - key: 'chainageMileage', - title: '里程', - }, { - key: 'maintenanceCount', - 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 '' + del: false, } - } - - 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, - chainageMileage: road.chainageMileage, - maintenanceCount: item.maintenanceCount, + }] + }) + + const header = [{ + key: 'level', + title: '道路类型', + }, { + key: 'routeName', + title: '路线名称', + }, { + key: 'routeCode', + title: '路线代码', + }, { + key: 'sectionNo', + title: '路段序号', + }, { + key: 'startingPlaceName', + title: '起点名称', + }, { + key: 'stopPlaceName', + title: '止点名称', + }, { + key: 'chainageMileage', + title: '里程', + }, { + key: 'maintenanceCount', + title: '养护次数(次)', + },] + + function judgeLevel (routeCode) { + if (routeCode) { + if (routeCode.startsWith('X')) { + return '县道' + } else if (routeCode.startsWith('Y')) { + return '乡道' + } else if (routeCode.startsWith('C')) { + return '村道' } - }) - - 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 - } - } + return '' + } else { + return '' + } + } + + let exportData = listRes.map(({ dataValues: item }) => { + let road = item.road && item.road.dataValues || {} + return { + level: + judgeLevel(road.routeCode) + , + routeName: road.routeName, + routeCode: road.routeCode, + sectionNo: road.sectionNo, + startingPlaceName: road.startingPlaceName, + stopPlaceName: road.stopPlaceName, + chainageMileage: 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, gather:{data: exportData, header} + }) + 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 = { diff --git a/api/utils/xlsxDownload.js b/api/utils/xlsxDownload.js index a8dc0c2f..4db50d5c 100644 --- a/api/utils/xlsxDownload.js +++ b/api/utils/xlsxDownload.js @@ -6,99 +6,209 @@ const moment = require('moment') //递归创建目录 同步方法 async function makeDir (dir) { - if (!fs.existsSync(dir)) { - makeDir(path.dirname(dir)) - fs.mkdirSync(dir, function (err) { - if (err) { - throw err - } - }); - } + if (!fs.existsSync(dir)) { + makeDir(path.dirname(dir)) + fs.mkdirSync(dir, function (err) { + if (err) { + throw err + } + }); + } } -async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss'), exp } = {}) { - const fileDirPath = path.join(__dirname, `../../downloadFiles`) - makeDir(fileDirPath) - const file = new xlsx.File(); - const sheet_1 = file.addSheet('sheet_1'); - - // header - const headerStyle = new xlsx.Style(); - headerStyle.align.h = 'center'; - headerStyle.align.v = 'center'; - headerStyle.border.right = 'thin'; - headerStyle.border.rightColor = '#000000'; - headerStyle.border.bottom = 'thin'; - headerStyle.border.bottomColor = '#000000'; - - const headerRow = sheet_1.addRow(); - const indexCell = headerRow.addCell(); - indexCell.value = '序号' - indexCell.style = headerStyle - for (let h of header) { - const cell = headerRow.addCell(); - cell.value = h.title; - cell.style = headerStyle - } - - // data - const style = new xlsx.Style(); - style.align.h = 'left'; - style.align.v = 'center'; - style.border.right = 'thin'; - style.border.rightColor = '#000000'; - style.border.bottom = 'thin'; - style.border.bottomColor = '#000000'; - for (let i = 0; i < data.length; i++) { - const row = sheet_1.addRow(); - const indexCell = row.addCell(); - indexCell.value = i + 1 - indexCell.style = headerStyle - for (let h of header) { - const cell = row.addCell(); - cell.value = data[i][h.key]; - - if (exp == 'patrol' || exp == 'maintenance') { - if (h.key == 'projectType') { - let type = data[i][h.key] - if (type == 'road') { - cell.value = '道路' - } else if (type == 'bridge') { - cell.value = '桥梁' - } else if (type == 'culvert') { - cell.value = '涵洞' - } else if (type == 'other') { - cell.value = '其他' - } - continue - } - - if (h.key == 'reportType') { - let type = data[i][h.key] - if (type == 'patrol') { - cell.value = '巡查' - } else if (type == 'conserve') { - cell.value = '养护' - } - continue - } +async function gatherSheet ({ sheet_2, data = [] }) { + + // header + const headerStyle = new xlsx.Style(); + headerStyle.align.h = 'center'; + headerStyle.align.v = 'center'; + headerStyle.border.right = 'thin'; + headerStyle.border.rightColor = '#000000'; + headerStyle.border.bottom = 'thin'; + headerStyle.border.bottomColor = '#000000'; + + const headerRow1 = sheet_2.addRow(); + const indexCell1 = headerRow1.addCell(); + indexCell1.value = '南昌县农村公路养护管理暨用地范围内环境整治提升工程 考核汇总表' + indexCell1.style = headerStyle + indexCell1.hMerge = 12 + + const headerRow2 = sheet_2.addRow(); + const indexCell2 = headerRow2.addCell(); + indexCell2.value = '责任单位' + indexCell2.style = headerStyle + indexCell2.vMerge = 1 + + + for (let h of ['县道', '', '', '', '乡道', '', '', '', '村道', '', '', '',]) { + const cell = headerRow2.addCell(); + + cell.value = h; + cell.style = headerStyle + if(h){ + cell.hMerge = 3 + + } + } + + const header = [{ + key: '', + title: '', + },{ + key: 'county', + title: '总里程', + }, { + key: 'countyParticipate', + title: '纳入考核里程', + }, { + key: 'countyPresent', + title: '本次考核里程', + }, { + key: 'county', + title: '实际抽取比原计划多', + }, { + key: 'township', + title: '总里程', + }, { + key: 'townshipParticipate', + title: '纳入考核里程', + }, { + key: 'townshipPresent', + title: '本次考核里程', + }, { + key: 'township', + title: '实际抽取比原计划多', + }, { + key: 'village', + title: '总里程', + }, { + key: 'villageParticipate', + title: '纳入考核里程', + }, { + key: 'villagePresent', + title: '本次考核里程', + }, { + key: 'village', + title: '实际抽取比原计划多', + },] + + const headerRow3 = sheet_2.addRow(); + for (let h of header) { + const cell = headerRow3.addCell(); + cell.value = h.title; + cell.style = headerStyle + } + + // data + // const style = new xlsx.Style(); + // style.align.h = 'left'; + // style.align.v = 'center'; + // style.border.right = 'thin'; + // style.border.rightColor = '#000000'; + // style.border.bottom = 'thin'; + // style.border.bottomColor = '#000000'; + // for (let i = 0; i < data.length; i++) { + // const row = sheet_2.addRow(); + // const indexCell = row.addCell(); + // indexCell.value = i + 1 + // indexCell.style = headerStyle + // for (let h of header) { + // const cell = row.addCell(); + // 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 } = {}) { + const fileDirPath = path.join(__dirname, `../../downloadFiles`) + makeDir(fileDirPath) + const file = new xlsx.File(); + const sheet_1 = file.addSheet('sheet_1'); + if (gather) { + const sheet_2 = file.addSheet('sheet_2'); + await gatherSheet({ sheet_2, ...gather }) + } + + // header + const headerStyle = new xlsx.Style(); + headerStyle.align.h = 'center'; + headerStyle.align.v = 'center'; + headerStyle.border.right = 'thin'; + headerStyle.border.rightColor = '#000000'; + headerStyle.border.bottom = 'thin'; + headerStyle.border.bottomColor = '#000000'; + + const headerRow = sheet_1.addRow(); + const indexCell = headerRow.addCell(); + indexCell.value = '序号' + indexCell.style = headerStyle + for (let h of header) { + const cell = headerRow.addCell(); + cell.value = h.title; + cell.style = headerStyle + } + + // data + const style = new xlsx.Style(); + style.align.h = 'left'; + style.align.v = 'center'; + style.border.right = 'thin'; + style.border.rightColor = '#000000'; + style.border.bottom = 'thin'; + style.border.bottomColor = '#000000'; + for (let i = 0; i < data.length; i++) { + const row = sheet_1.addRow(); + const indexCell = row.addCell(); + indexCell.value = i + 1 + indexCell.style = headerStyle + for (let h of header) { + const cell = row.addCell(); + cell.value = data[i][h.key]; + + if (exp == 'patrol' || exp == 'maintenance') { + if (h.key == 'projectType') { + let type = data[i][h.key] + if (type == 'road') { + cell.value = '道路' + } else if (type == 'bridge') { + cell.value = '桥梁' + } else if (type == 'culvert') { + cell.value = '涵洞' + } else if (type == 'other') { + cell.value = '其他' + } + continue } - cell.style = style - } - } - - const savePath = path.join(fileDirPath, fileName) - await new Promise(function (resolve, reject) { - file.saveAs() - .pipe(fs.createWriteStream(savePath)) - .on('finish', () => { - resolve() - }); - }) - return savePath + if (h.key == 'reportType') { + let type = data[i][h.key] + if (type == 'patrol') { + cell.value = '巡查' + } else if (type == 'conserve') { + cell.value = '养护' + } + continue + } + } + + cell.style = style + } + } + + const savePath = path.join(fileDirPath, fileName) + await new Promise(function (resolve, reject) { + file.saveAs() + .pipe(fs.createWriteStream(savePath)) + .on('finish', () => { + resolve() + }); + }) + return savePath } + + module.exports = { - simpleExcelDown + simpleExcelDown } \ No newline at end of file diff --git a/jenkinsifile_script_1.4.1/data/1_insert_road_data b/jenkinsifile_script_1.4.1/data/1_insert_road_data new file mode 100644 index 00000000..35c7de25 --- /dev/null +++ b/jenkinsifile_script_1.4.1/data/1_insert_road_data @@ -0,0 +1,21 @@ +podTemplate { + node('pod-templ-jenkins-slave-common') { + + env.IMAGE_NAME = "${IOT_IMAGES_REGISTRY}/${SMARTCITY}/${JOB_NAME}" + env.IMAGE_NAME_SHORT = "${SMARTCITY}/${JOB_NAME}" + env.CODE_ADDR = "${GIT_ADDRESS}/free-sun/Highways4Good.git" + + stage('Run shell') { + git branch: 'dev', credentialsId: 'gitea-builder', url: "${CODE_ADDR}" + + container('image-builder') { + sh''' + /kaniko/executor --context=${BUILD_WORKSPACE} --dockerfile=./scripts/1.1.4.1/data/1_insert_road_data/Dockerfilenew --destination=${IMAGE_NAME}:${IMAGE_VERSION} --cache=false --cleanup + ''' + } + + buildName "${IMAGE_NAME_SHORT}:${IMAGE_VERSION}" + buildDescription "${IMAGE_NAME}:${IMAGE_VERSION}" + } + } +} \ No newline at end of file diff --git a/scripts/1.4.1/data/1_insert_road_data/Dockerfilenew b/scripts/1.4.1/data/1_insert_road_data/Dockerfilenew index dba16607..503776db 100644 --- a/scripts/1.4.1/data/1_insert_road_data/Dockerfilenew +++ b/scripts/1.4.1/data/1_insert_road_data/Dockerfilenew @@ -1,5 +1,5 @@ FROM repository.anxinyun.cn/base-images/nodejs12:20.10.12.2 -COPY ./scripts/1.3.1/data/1_insert_report_data /var/app +COPY ./scripts/1.4.1/data/1_insert_road_data /var/app WORKDIR /var/app RUN npm cache clean -f RUN rm -rf package-lock.json