Browse Source

人员信息导出 40%

master
巴林闲侠 2 years ago
parent
commit
f56e5ddb2f
  1. 81
      api/app/lib/controllers/member/index.js
  2. 3
      api/app/lib/routes/member/index.js
  3. 4
      api/app/lib/utils/member.js

81
api/app/lib/controllers/member/index.js

@ -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;

3
api/app/lib/routes/member/index.js

@ -26,4 +26,7 @@ module.exports = function (app, router, opts) {
app.fs.api.logAttr['POST/add/members/bulk'] = { content: '导入员工信息', visible: true };
router.post('/add/members/bulk', member.addMembersBulk);
app.fs.api.logAttr['GET/members/export'] = { content: '导出员工信息', visible: true };
router.get('/members/export', member.exportData);
};

4
api/app/lib/utils/member.js

@ -99,7 +99,7 @@ module.exports = function (app, opts) {
ON hrOvertime.pep_user_id = member.pep_user_id
WHERE
member.pep_user_id > 0
member.del = 0
${keywordTarget == 'role' && keyword ? `
AND user.id IN (
SELECT user_role.user
@ -138,6 +138,8 @@ module.exports = function (app, opts) {
${whereOption.length ? `WHERE ${whereOption.join(' AND ')}` : ''}
`).toPromise()
return userRes
}

Loading…
Cancel
Save