From 7df13db12e29fdd0e9e88bfda776a10c61543887 Mon Sep 17 00:00:00 2001 From: zhangminghua Date: Wed, 28 Dec 2022 15:52:36 +0800 Subject: [PATCH] =?UTF-8?q?(*)=E5=91=98=E5=B7=A5=E6=B2=9F=E9=80=9A?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/employeeCommunicate/index.js | 49 +++++++++++++++++-- .../lib/routes/employeeCommunicate/index.js | 3 ++ api/app/lib/utils/xlsxDownload.js | 7 ++- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/api/app/lib/controllers/employeeCommunicate/index.js b/api/app/lib/controllers/employeeCommunicate/index.js index f0073fd..7ebff07 100644 --- a/api/app/lib/controllers/employeeCommunicate/index.js +++ b/api/app/lib/controllers/employeeCommunicate/index.js @@ -5,7 +5,7 @@ const moment = require('moment'); * 查询员工沟通统计数据 * @param {*} ctx ctx ctx.query:{keywordTarget-关键字项、keyword-关键字内容、timeRange-沟通时间、limit-页宽, page-页码} */ - async function get(ctx) { +async function get(ctx) { try { const { models } = ctx.fs.dc; const { keywordTarget, keyword, timeRange, limit, page } = ctx.query; @@ -13,8 +13,8 @@ const moment = require('moment'); if (keywordTarget && keyword) { where[keywordTarget] = { $iLike: `%${keyword}%` }; } - if(timeRange){ - where.communicateDate= { $between: timeRange.split(',') }; + if (timeRange) { + where.communicateDate = { $between: timeRange.split(',') }; } let employeeCommunicate = await models.EmployeeCommunicate.findAndCountAll({ where: where, @@ -31,6 +31,47 @@ const moment = require('moment'); } } +/** + * 导出员工沟通统计数据 + */ +async function exportData(ctx) { + try { + const { models } = ctx.fs.dc; + const { simpleExcelDown } = ctx.app.fs.utils; + let exportData = await models.EmployeeCommunicate.findAll({ + attributes: ['id', 'personalName', 'job', 'departmentName', 'communicateDate', 'communicateContent', + 'communicateResult', 'valuation', 'communicateCondition', 'nextPlan'], + order: [['id', 'ASC']] + }); + const columnKeys = { + personalName: '被沟通人', + job: '岗位', + departmentName: '部门', + communicateDate: '沟通时间', + communicateContent: '沟通内容', + communicateResult: '沟通成果', + valuation: '对被沟通人近期表现的评价', + communicateCondition: '沟通情况反馈', + nextPlan: '下一步工作计划或提升方向' + } + let header = []; + Object.keys(columnKeys).map(key => { + header.push({ title: columnKeys[key], key: key }); + }) + const fileName = `员工沟通统计_${moment().format('YYYYMMDDHHmmss')}` + '.xlsx' + const filePath = await simpleExcelDown({ data: exportData, header, fileName: fileName, format: 'YYYY-MM-DD', formatKey: 'communicateDate' }) + 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 = { name: 'ExportAllError', message: `导出员工沟通统计数据失败` } + } +} module.exports = { - get + get, + exportData } \ No newline at end of file diff --git a/api/app/lib/routes/employeeCommunicate/index.js b/api/app/lib/routes/employeeCommunicate/index.js index 6e22f08..7b856f3 100644 --- a/api/app/lib/routes/employeeCommunicate/index.js +++ b/api/app/lib/routes/employeeCommunicate/index.js @@ -6,4 +6,7 @@ module.exports = function (app, router, opts) { app.fs.api.logAttr['GET/employee/communicate/list'] = { content: '查询员工沟通统计数据', visible: true }; router.get('/employee/communicate/list', employeeCommunicate.get); + + app.fs.api.logAttr['GET/export/employee/communicate'] = { content: '导出员工沟通统计数据', visible: false }; + router.get('/export/employee/communicate', employeeCommunicate.exportData); }; \ No newline at end of file diff --git a/api/app/lib/utils/xlsxDownload.js b/api/app/lib/utils/xlsxDownload.js index 21f8fea..155a78e 100644 --- a/api/app/lib/utils/xlsxDownload.js +++ b/api/app/lib/utils/xlsxDownload.js @@ -19,7 +19,7 @@ module.exports = function (app, opts) { } } - async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss') } = {}) { + async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss'), format = '', formatKey = ''} = {}) { const fileDirPath = path.join(__dirname, `../../downloadFiles`) makeDir(fileDirPath) const file = new xlsx.File(); @@ -59,7 +59,10 @@ module.exports = function (app, opts) { indexCell.style = headerStyle for (let h of header) { const cell = row.addCell(); - cell.value = data[i][h.key] || h.defaultValue || '-'; + if (format && formatKey && formatKey === h.key && data[i][h.key]) { + cell.value = moment(data[i][h.key]).format(format) + } else + cell.value = data[i][h.key] || h.defaultValue || '-'; cell.style = style } }