'use strict'; const fs = require('fs'); const moment = require('moment'); /** * 查询员工沟通统计数据 * @param {*} ctx ctx ctx.query:{keywordTarget-关键字项、keyword-关键字内容、timeRange-沟通时间、limit-页宽, page-页码} */ async function get(ctx) { try { const { models } = ctx.fs.dc; const { keywordTarget, keyword, timeRange, limit, page } = ctx.query; const where = {}; if (keywordTarget && keyword) { where[keywordTarget] = { $iLike: `%${keyword}%` }; } if (timeRange) { where.communicateDate = { $between: timeRange.split(',') }; } let employeeCommunicate = await models.EmployeeCommunicate.findAndCountAll({ where: where, offset: Number(page) * Number(limit), limit: Number(limit), order: [['id', 'ASC']] }); ctx.status = 200 ctx.body = employeeCommunicate; } catch (error) { ctx.fs.logger.error(`path:${ctx.path},error:${error}`) ctx.status = 400; ctx.body = { name: 'FindError', message: '查询员工沟通统计数据失败' } } } /** * 导出员工沟通统计数据 */ async function exportData(ctx) { try { const { models } = ctx.fs.dc; const { simpleExcelDown } = ctx.app.fs.utils; const columnKeys = { personalName: '被沟通人', job: '岗位', departmentName: '部门', communicateDate: '沟通时间', communicateContent: '沟通内容', communicateResult: '沟通成果', valuation: '对被沟通人近期表现的评价', communicateCondition: '沟通情况反馈', nextPlan: '下一步工作计划或提升方向' } let header = []; let attributes = ['id']; Object.keys(columnKeys).map(key => { header.push({ title: columnKeys[key], key: key }); attributes.push(key); }) let exportData = await models.EmployeeCommunicate.findAll({ attributes: attributes, order: [['id', 'ASC']] }); 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, exportData }