Browse Source

(*)人员信息相关模块

master
zmh 2 years ago
parent
commit
679654e933
  1. 116
      api/app/lib/controllers/member/index.js
  2. 50
      api/app/lib/utils/member.js

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

@ -1,6 +1,7 @@
'use strict';
const moment = require('moment')
const fs = require('fs');
const { getDataRange } = require('../auth/index')
async function add(ctx) {
try {
@ -283,12 +284,19 @@ async function list(ctx) {
hiredateStart, hiredateEnd, marital, native, workPlace,
orderBy, orderDirection,
} = ctx.query
let dataRange = await getDataRange(ctx);
if (dataRange.userIds && dataRange.userIds.length === 0) {
ctx.status = 200;
ctx.body = {
count: 0,
rows: []
}
} else {
const userRes = await memberList({
keywordTarget, keyword, limit, page, state,
hiredateStart, hiredateEnd, marital, native, workPlace,
orderBy, orderDirection,
nowAttendanceTime: true
nowAttendanceTime: true, userIds: dataRange.userIds
})
let { packageUser: returnD, pepUserIds } = await packageUserData(userRes, {
@ -300,6 +308,7 @@ async function list(ctx) {
count: userRes.count,
rows: returnD
}
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
@ -498,19 +507,65 @@ async function exportData(ctx) {
hiredateStart, hiredateEnd, marital, native, workPlace,
orderBy, orderDirection,
} = ctx.query
const tableAttributes = models['Member'].tableAttributes
const optionKeys = keys.split(',')
let exportD = null;
let dataRange = await getDataRange(ctx);
if (dataRange.userIds && dataRange.userIds.length === 0) {
exportD = [];
} else {
const userRes = await memberList({
keywordTarget, keyword, limit, page, state,
hiredateStart, hiredateEnd, marital, native, workPlace,
orderBy, orderDirection,
nowAttendanceTime: true
nowAttendanceTime: true, userIds: dataRange.userIds
})
const tableAttributes = models['Member'].tableAttributes
const optionKeys = keys.split(',')
let { packageUser, pepUserIds } = await packageUserData(userRes)
exportD = packageUser;
let { packageUser: exportD, pepUserIds } = await packageUserData(userRes)
// 查询累计加班次数及总时长
const statisticOvertimeRes = await clickHouse.hr.query(`
SELECT
pep_user_id AS pepUserId,
count(id) AS count,
sum(duration) AS duration
FROM
overtime
WHERE pep_user_id IN (${pepUserIds.join(',')})
GROUP BY pep_user_id
`).toPromise()
const statisticVacateRes = await clickHouse.hr.query(`
SELECT
pep_user_id AS pepUserId,
count(id) AS count,
sum(duration) AS duration
FROM
vacate
WHERE pep_user_id IN (${pepUserIds.join(',')})
GROUP BY pep_user_id
`).toPromise()
exportD.forEach(d => {
d.departmrnt = d.departmrnt.map(dep => dep.name).join('、')
d.role = d.role.map(r => r.name).join('、')
d.userJob = d.userJob ? UserAttribute.jobDataSource[d.userJob - 1] : '';
d.userActiveStatus = d.userActiveStatus ? UserAttribute.activeStatusDataSource[d.userActiveStatus - 1] : '';
d.userOrganization = d.userOrganization ? UserAttribute.organizationDataSource[d.userOrganization - 1] : '';
d.idPhoto ? d.idPhoto = qiniu.domain + '/' + d.idPhoto : ''
d.vitae ? d.vitae = qiniu.domain + '/' + d.vitae : ''
const corOverTime = statisticOvertimeRes.find(so => so.pepUserId == d.pepUserId)
d.overTimeCount = corOverTime ? corOverTime.count : 0
d.overTimeDuration = corOverTime ? (corOverTime.duration / 3600).toFixed(1) : 0
const corVacate = statisticVacateRes.find(so => so.pepUserId == d.pepUserId)
d.vacateCount = corVacate ? corVacate.count : 0
d.vacateDuration = corVacate ? (corVacate.duration / 3600).toFixed(1) : 0
})
}
let preHeader = [{
title: '员工编号',
key: 'userCode',
@ -573,49 +628,6 @@ async function exportData(ctx) {
key: 'vacateDuration',
},])
}
// 查询累计加班次数及总时长
const statisticOvertimeRes = await clickHouse.hr.query(`
SELECT
pep_user_id AS pepUserId,
count(id) AS count,
sum(duration) AS duration
FROM
overtime
WHERE pep_user_id IN (${pepUserIds.join(',')})
GROUP BY pep_user_id
`).toPromise()
const statisticVacateRes = await clickHouse.hr.query(`
SELECT
pep_user_id AS pepUserId,
count(id) AS count,
sum(duration) AS duration
FROM
vacate
WHERE pep_user_id IN (${pepUserIds.join(',')})
GROUP BY pep_user_id
`).toPromise()
exportD.forEach(d => {
d.departmrnt = d.departmrnt.map(dep => dep.name).join('、')
d.role = d.role.map(r => r.name).join('、')
d.userJob = d.userJob ? UserAttribute.jobDataSource[d.userJob - 1] : '';
d.userActiveStatus = d.userActiveStatus ? UserAttribute.activeStatusDataSource[d.userActiveStatus - 1] : '';
d.userOrganization = d.userOrganization ? UserAttribute.organizationDataSource[d.userOrganization - 1] : '';
d.idPhoto ? d.idPhoto = qiniu.domain + '/' + d.idPhoto : ''
d.vitae ? d.vitae = qiniu.domain + '/' + d.vitae : ''
const corOverTime = statisticOvertimeRes.find(so => so.pepUserId == d.pepUserId)
d.overTimeCount = corOverTime ? corOverTime.count : 0
d.overTimeDuration = corOverTime ? (corOverTime.duration / 3600).toFixed(1) : 0
const corVacate = statisticVacateRes.find(so => so.pepUserId == d.pepUserId)
d.vacateCount = corVacate ? corVacate.count : 0
d.vacateDuration = corVacate ? (corVacate.duration / 3600).toFixed(1) : 0
})
const fileName = `人员信息_${moment().format('YYYYMMDDHHmmss')}` + '.csv'
const filePath = await simpleExcelDown({ data: exportD, header, fileName: fileName })
const fileData = fs.readFileSync(filePath);
@ -714,7 +726,9 @@ async function getPositionRating(ctx) {
findObj.limit = Number(limit);
findObj.offset = Number(page) * Number(limit);
}
let dataRange = await getDataRange(ctx);
if (dataRange.userIds)
findObj.where = { pepUserId: { $in: dataRange.userIds || [] } }
const list = await models.PositionRating.findAndCountAll(findObj);
if (list.rows.length) {
const userIds = list.rows.map(u => u.pepUserId);

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

@ -4,7 +4,7 @@ const request = require('superagent');
module.exports = function (app, opts) {
async function memberList ({
async function memberList({
keywordTarget, keyword, limit, page, state,
hiredateStart, hiredateEnd, marital, native, workPlace,
orderBy, orderDirection,
@ -12,7 +12,8 @@ module.exports = function (app, opts) {
overtimeDayStatisticStartDate, overtimeDayStatisticendDate,
overtimeCountStatistic, overtimeCountStatisticStartDate, overtimeCountStatisticendDate,
vacateDayStatisticStartDate, vacateDayStatisticendDate,
vacateDurationStatistic, vacateCountStatistic, vacateCountStatisticStartDate, vacateCountStatisticendDate
vacateDurationStatistic, vacateCountStatistic, vacateCountStatisticStartDate, vacateCountStatisticendDate,
userIds
}) {
const { judgeHoliday } = app.fs.utils
const { clickHouse } = app.fs
@ -100,6 +101,8 @@ module.exports = function (app, opts) {
FROM member
INNER JOIN ${pepEmis}.user AS user
ON member.pep_user_id = user.id
${userIds && userIds.length ? `AND user.id in (${userIds.join(',')})` : ''}
${keywordTarget == 'number' && keyword ? `
AND user.people_code LIKE '%${keyword}%'
`: ''}
@ -279,6 +282,26 @@ module.exports = function (app, opts) {
member.*
${innerSelectQuery}
ORDER BY ${orderBy == 'code' ?
'user.people_code'
: orderBy == 'hiredate'
? 'member.hiredate'
: orderBy == 'age'
? 'member.birthday'
: orderBy == 'overtimeTakeRestSum'
|| orderBy == 'overtimePaySum'
|| orderBy == 'overtimeSum' ?
'overtimeDayStatisticDuration'
: orderBy == 'overtimeCount' ?
'overtimeCount'
: orderBy == 'vacateSum' ?
'vacateDayStatisticDuration'
: orderBy == 'vacateCount' ?
'vacateCount'
: 'user.people_code'}
${orderDirection || 'ASC'}
${limit ? `LIMIT ${limit}` : ''}
${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''}
) AS hrMember
@ -296,24 +319,7 @@ module.exports = function (app, opts) {
LEFT JOIN ${pepEmis}.department AS department
ON department.id = department_user.department
${whereOption.length ? `WHERE ${whereOption.join(' AND ')}` : ''}
ORDER BY ${orderBy == 'code' ?
'user.people_code'
: orderBy == 'hiredate'
? 'hrMember."member.hiredate"'
: orderBy == 'age'
? 'hrMember."member.birthday"'
: orderBy == 'overtimeTakeRestSum'
|| orderBy == 'overtimePaySum'
|| orderBy == 'overtimeSum' ?
'hrMember.overtimeDayStatisticDuration'
: orderBy == 'overtimeCount' ?
'hrMember.overtimeCount'
: orderBy == 'vacateSum' ?
'hrMember.vacateDayStatisticDuration'
: orderBy == 'vacateCount' ?
'hrMember.vacateCount'
: 'user.people_code'}
${orderDirection || 'ASC'}
`).toPromise()
const countRes = await clickHouse.hr.query(`
@ -328,7 +334,7 @@ module.exports = function (app, opts) {
}
}
async function packageUserData (userRes, option = {}) {
async function packageUserData(userRes, option = {}) {
const { judgeHoliday, } = app.fs.utils
let workTime = false
@ -383,7 +389,7 @@ module.exports = function (app, opts) {
obj[nextKey] = u[k] == '1970-01-01 00:00:00.000000' || u[k] == '1970-01-01 08:00:00.000000' ? null : u[k]
}
pepUserIds.push(u.pepUserId)
console.log("查询到的用户信息:", obj);
// console.log("查询到的用户信息:", obj);
returnD.push({
...obj,
departmrnt: u.depId ? [{

Loading…
Cancel
Save