|
|
@ -1,5 +1,6 @@ |
|
|
|
'use strict'; |
|
|
|
const moment = require('moment') |
|
|
|
const fs = require('fs'); |
|
|
|
|
|
|
|
async function add (ctx) { |
|
|
|
try { |
|
|
@ -381,15 +382,89 @@ async function vacateStatistics (ctx) { |
|
|
|
async function exportData (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { judgeHoliday, memberList } = ctx.app.fs.utils |
|
|
|
const { keywordTarget, keyword, limit, page, state } = ctx.query |
|
|
|
const { simpleExcelDown, memberList } = ctx.app.fs.utils |
|
|
|
const { keywordTarget, keyword, limit, page, state, keys = '' } = ctx.query |
|
|
|
|
|
|
|
const userRes = await memberList({ keywordTarget, keyword, limit, page, state }) |
|
|
|
|
|
|
|
const tableAttributes = models['Member'].tableAttributes |
|
|
|
const optionKeys = keys.split(',') |
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
let exportD = [] |
|
|
|
userRes.forEach(u => { |
|
|
|
let existUser = exportD.find(r => r.pepUserId == u.pepUserId) |
|
|
|
if (existUser) { |
|
|
|
if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) { |
|
|
|
existUser.departmrnt.push({ |
|
|
|
id: u.depId, |
|
|
|
name: u.depName |
|
|
|
}) |
|
|
|
} |
|
|
|
if (u.roleId && !existUser.role.some(r => r.id == u.roleId)) { |
|
|
|
existUser.role.push({ |
|
|
|
id: u.roleId, |
|
|
|
name: u.roleName |
|
|
|
}) |
|
|
|
} |
|
|
|
} else { |
|
|
|
let obj = {} |
|
|
|
for (let k in u) { |
|
|
|
let nextKey = k.replace('hrMember.', '') |
|
|
|
.replace('member.', '') |
|
|
|
if (nextKey.includes('_')) { |
|
|
|
nextKey = nextKey.toLowerCase() |
|
|
|
.replace( |
|
|
|
/(_)[a-z]/g, |
|
|
|
(L) => L.toUpperCase() |
|
|
|
) |
|
|
|
.replace(/_/g, '') |
|
|
|
} |
|
|
|
obj[nextKey] = u[k] |
|
|
|
} |
|
|
|
exportD.push({ |
|
|
|
...obj, |
|
|
|
departmrnt: u.depId ? [{ |
|
|
|
id: u.depId, |
|
|
|
name: u.depName |
|
|
|
}] : [], |
|
|
|
role: u.roleId ? [{ |
|
|
|
id: u.roleId, |
|
|
|
name: u.roleName |
|
|
|
}] : [], |
|
|
|
del: undefined, |
|
|
|
pepuserid: undefined |
|
|
|
}) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
let preHeader = [{ |
|
|
|
title: '姓名', |
|
|
|
key: 'userName', |
|
|
|
}] |
|
|
|
let header = [].concat(preHeader) |
|
|
|
for (let k in tableAttributes) { |
|
|
|
const comment = tableAttributes[k].comment |
|
|
|
if (k != 'id' && comment) { |
|
|
|
if ([].includes(k)) { |
|
|
|
// 截住不想导出的字段
|
|
|
|
continue |
|
|
|
} |
|
|
|
header.push({ |
|
|
|
title: comment || '-', |
|
|
|
key: k, |
|
|
|
// index: tableAttributes[k].index,
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const fileName = `人员信息_${moment().format('YYYYMMDDHHmmss')}` + '.csv' |
|
|
|
const filePath = await simpleExcelDown({ data: exportD, 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; |
|
|
|