Browse Source

(*)人员信息相关模块

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

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

File diff suppressed because it is too large

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

@ -4,102 +4,105 @@ const request = require('superagent');
module.exports = function (app, opts) { module.exports = function (app, opts) {
async function memberList ({ async function memberList({
keywordTarget, keyword, limit, page, state, keywordTarget, keyword, limit, page, state,
hiredateStart, hiredateEnd, marital, native, workPlace, hiredateStart, hiredateEnd, marital, native, workPlace,
orderBy, orderDirection, orderBy, orderDirection,
nowAttendanceTime, nowAttendanceTime,
overtimeDayStatisticStartDate, overtimeDayStatisticendDate, overtimeDayStatisticStartDate, overtimeDayStatisticendDate,
overtimeCountStatistic, overtimeCountStatisticStartDate, overtimeCountStatisticendDate, overtimeCountStatistic, overtimeCountStatisticStartDate, overtimeCountStatisticendDate,
vacateDayStatisticStartDate, vacateDayStatisticendDate, vacateDayStatisticStartDate, vacateDayStatisticendDate,
vacateDurationStatistic, vacateCountStatistic, vacateCountStatisticStartDate, vacateCountStatisticendDate vacateDurationStatistic, vacateCountStatistic, vacateCountStatisticStartDate, vacateCountStatisticendDate,
}) { userIds
const { judgeHoliday } = app.fs.utils }) {
const { clickHouse } = app.fs const { judgeHoliday } = app.fs.utils
const { database: pepEmis } = clickHouse.pepEmis.opts.config const { clickHouse } = app.fs
const { database: pepEmis } = clickHouse.pepEmis.opts.config
const curDay = moment().format('YYYY-MM-DD') const curDay = moment().format('YYYY-MM-DD')
const nowTime = moment() const nowTime = moment()
let whereOption = [] let whereOption = []
let whereFromSelectOption = [] let whereFromSelectOption = []
let returnEmpty = false let returnEmpty = false
if (state == 'inOffice') { if (state == 'inOffice') {
// 在岗 // 在岗
const holidayJudge = await judgeHoliday(curDay) const holidayJudge = await judgeHoliday(curDay)
if (holidayJudge) { if (holidayJudge) {
if ( if (
holidayJudge.workday holidayJudge.workday
&& nowTime.isAfter(moment(curDay + ' 08:30')) && nowTime.isAfter(moment(curDay + ' 08:30'))
&& nowTime.isBefore(moment(curDay + ' 17:30')) && nowTime.isBefore(moment(curDay + ' 17:30'))
) { ) {
// 在工作日的工作时间范围 无请假记录 // 在工作日的工作时间范围 无请假记录
whereFromSelectOption.push(`vacateStartTime = '1970-01-01 00:00:00.000000'`) whereFromSelectOption.push(`vacateStartTime = '1970-01-01 00:00:00.000000'`)
} else {
returnEmpty = true
}
} else { } else {
returnEmpty = true returnEmpty = true
} }
} else { }
returnEmpty = true if (state == 'dayoff') {
} // 放假
} const holidayJudge = await judgeHoliday(curDay)
if (state == 'dayoff') { if (holidayJudge) {
// 放假 if (
const holidayJudge = await judgeHoliday(curDay) holidayJudge.dayoff || holidayJudge.festivals
if (holidayJudge) { ) {
if ( // 在休息日范围内且无加班申请
holidayJudge.dayoff || holidayJudge.festivals whereFromSelectOption.push(`overtimeStartTime = '1970-01-01 00:00:00.000000'`)
) { } else {
// 在休息日范围内且无加班申请 returnEmpty = true
whereFromSelectOption.push(`overtimeStartTime = '1970-01-01 00:00:00.000000'`) }
} else { } else {
returnEmpty = true returnEmpty = true
} }
} else { }
returnEmpty = true if (returnEmpty) {
} return {
} count: 0,
if (returnEmpty) { rows: []
return { }
count: 0, }
rows: []
}
}
let overtimeDayStatisticWhere = [] let overtimeDayStatisticWhere = []
if (overtimeDayStatisticStartDate) { if (overtimeDayStatisticStartDate) {
overtimeDayStatisticWhere.push(`overtime_day.day >= '${moment(overtimeDayStatisticStartDate).format('YYYY-MM-DD')}'`) overtimeDayStatisticWhere.push(`overtime_day.day >= '${moment(overtimeDayStatisticStartDate).format('YYYY-MM-DD')}'`)
} }
if (overtimeDayStatisticendDate) { if (overtimeDayStatisticendDate) {
overtimeDayStatisticWhere.push(`overtime_day.day <= '${moment(overtimeDayStatisticendDate).format('YYYY-MM-DD')}'`) overtimeDayStatisticWhere.push(`overtime_day.day <= '${moment(overtimeDayStatisticendDate).format('YYYY-MM-DD')}'`)
} }
let overtimeCountStatisticWhere = [] let overtimeCountStatisticWhere = []
if (overtimeCountStatisticStartDate) { if (overtimeCountStatisticStartDate) {
overtimeCountStatisticWhere.push(`overtime.start_time >= '${moment(overtimeCountStatisticStartDate).startOf('day').format('YYYY-MM-DD HH:mm:ss')}'`) overtimeCountStatisticWhere.push(`overtime.start_time >= '${moment(overtimeCountStatisticStartDate).startOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
} }
if (overtimeCountStatisticendDate) { if (overtimeCountStatisticendDate) {
overtimeCountStatisticWhere.push(`overtime.end_time <= '${moment(overtimeCountStatisticendDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')}'`) overtimeCountStatisticWhere.push(`overtime.end_time <= '${moment(overtimeCountStatisticendDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
} }
let vacateDayStatisticWhere = [] let vacateDayStatisticWhere = []
if (vacateDayStatisticStartDate) { if (vacateDayStatisticStartDate) {
vacateDayStatisticWhere.push(`vacate_day.day >= '${moment(vacateDayStatisticStartDate).format('YYYY-MM-DD')}'`) vacateDayStatisticWhere.push(`vacate_day.day >= '${moment(vacateDayStatisticStartDate).format('YYYY-MM-DD')}'`)
} }
if (vacateDayStatisticendDate) { if (vacateDayStatisticendDate) {
vacateDayStatisticWhere.push(`vacate_day.day <= '${moment(vacateDayStatisticendDate).format('YYYY-MM-DD')}'`) vacateDayStatisticWhere.push(`vacate_day.day <= '${moment(vacateDayStatisticendDate).format('YYYY-MM-DD')}'`)
} }
let vacateCountStatisticWhere = [] let vacateCountStatisticWhere = []
if (vacateCountStatisticStartDate) { if (vacateCountStatisticStartDate) {
vacateCountStatisticWhere.push(`vacate.start_time >= '${moment(vacateCountStatisticStartDate).startOf('day').format('YYYY-MM-DD HH:mm:ss')}'`) vacateCountStatisticWhere.push(`vacate.start_time >= '${moment(vacateCountStatisticStartDate).startOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
} }
if (vacateCountStatisticendDate) { if (vacateCountStatisticendDate) {
vacateCountStatisticWhere.push(`vacate.end_time <= '${moment(vacateCountStatisticendDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')}'`) vacateCountStatisticWhere.push(`vacate.end_time <= '${moment(vacateCountStatisticendDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
} }
// CRAZY // CRAZY
const innerSelectQuery = ` const innerSelectQuery = `
FROM member FROM member
INNER JOIN ${pepEmis}.user AS user INNER JOIN ${pepEmis}.user AS user
ON member.pep_user_id = user.id ON member.pep_user_id = user.id
${userIds && userIds.length ? `AND user.id in (${userIds.join(',')})` : ''}
${keywordTarget == 'number' && keyword ? ` ${keywordTarget == 'number' && keyword ? `
AND user.people_code LIKE '%${keyword}%' AND user.people_code LIKE '%${keyword}%'
`: ''} `: ''}
@ -137,9 +140,9 @@ module.exports = function (app, opts) {
`: ''} `: ''}
${orderBy == 'overtimeTakeRestSum' || ${orderBy == 'overtimeTakeRestSum' ||
orderBy == 'overtimePaySum' || orderBy == 'overtimePaySum' ||
orderBy == 'overtimeSum' ? orderBy == 'overtimeSum' ?
`LEFT JOIN ( `LEFT JOIN (
SELECT SELECT
overtime.pep_user_id AS pepUserId, overtime.pep_user_id AS pepUserId,
sum(overtime_day.duration) AS duration sum(overtime_day.duration) AS duration
@ -170,8 +173,8 @@ module.exports = function (app, opts) {
`: ''} `: ''}
${vacateDurationStatistic || ${vacateDurationStatistic ||
orderBy == 'vacateSum' ? orderBy == 'vacateSum' ?
`LEFT JOIN ( `LEFT JOIN (
SELECT SELECT
vacate.pep_user_id AS pepUserId, vacate.pep_user_id AS pepUserId,
sum(vacate_day.duration) AS duration sum(vacate_day.duration) AS duration
@ -236,7 +239,7 @@ module.exports = function (app, opts) {
`: ''} `: ''}
` `
const userRes = await clickHouse.hr.query(` const userRes = await clickHouse.hr.query(`
SELECT SELECT
hrMember."member.pep_user_id" AS pepUserId, hrMember."member.pep_user_id" AS pepUserId,
hrMember.*, hrMember.*,
@ -253,8 +256,8 @@ module.exports = function (app, opts) {
FROM ( FROM (
SELECT SELECT
${orderBy == 'overtimeTakeRestSum' ${orderBy == 'overtimeTakeRestSum'
|| orderBy == 'overtimePaySum' || orderBy == 'overtimePaySum'
|| orderBy == 'overtimeSum' ? ` || orderBy == 'overtimeSum' ? `
overtimeDayStatistic.duration AS overtimeDayStatisticDuration, overtimeDayStatistic.duration AS overtimeDayStatisticDuration,
`: ''} `: ''}
@ -279,6 +282,26 @@ module.exports = function (app, opts) {
member.* member.*
${innerSelectQuery} ${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 ? `LIMIT ${limit}` : ''}
${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''} ${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''}
) AS hrMember ) AS hrMember
@ -296,120 +319,103 @@ module.exports = function (app, opts) {
LEFT JOIN ${pepEmis}.department AS department LEFT JOIN ${pepEmis}.department AS department
ON department.id = department_user.department ON department.id = department_user.department
${whereOption.length ? `WHERE ${whereOption.join(' AND ')}` : ''} ${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() `).toPromise()
const countRes = await clickHouse.hr.query(` const countRes = await clickHouse.hr.query(`
SELECT SELECT
count(member.pep_user_id) AS count count(member.pep_user_id) AS count
${innerSelectQuery} ${innerSelectQuery}
`).toPromise() `).toPromise()
return { return {
count: countRes[0].count, count: countRes[0].count,
rows: userRes rows: userRes
} }
} }
async function packageUserData (userRes, option = {}) { async function packageUserData(userRes, option = {}) {
const { judgeHoliday, } = app.fs.utils const { judgeHoliday, } = app.fs.utils
let workTime = false let workTime = false
let dayoffTime = false let dayoffTime = false
if (option.state) { if (option.state) {
const curDay = moment().format('YYYY-MM-DD') const curDay = moment().format('YYYY-MM-DD')
const nowTime = moment() const nowTime = moment()
const holidayJudge = await judgeHoliday(curDay) const holidayJudge = await judgeHoliday(curDay)
if (holidayJudge) { if (holidayJudge) {
if ( if (
holidayJudge.workday holidayJudge.workday
&& nowTime.isAfter(moment(curDay + ' 08:30')) && nowTime.isAfter(moment(curDay + ' 08:30'))
&& nowTime.isBefore(moment(curDay + ' 17:30')) && nowTime.isBefore(moment(curDay + ' 17:30'))
) { ) {
workTime = true workTime = true
} else if (holidayJudge.dayoff || holidayJudge.festivals) { } else if (holidayJudge.dayoff || holidayJudge.festivals) {
dayoffTime = true dayoffTime = true
}
} }
} }
}
let returnD = [] let returnD = []
let pepUserIds = [-1] let pepUserIds = [-1]
userRes.rows.forEach(u => { userRes.rows.forEach(u => {
let existUser = returnD.find(r => r.pepUserId == u.pepUserId) let existUser = returnD.find(r => r.pepUserId == u.pepUserId)
if (existUser) { if (existUser) {
if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) { if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) {
existUser.departmrnt.push({ existUser.departmrnt.push({
id: u.depId, id: u.depId,
name: u.depName name: u.depName
}) })
} }
if (u.roleId && !existUser.role.some(r => r.id == u.roleId)) { if (u.roleId && !existUser.role.some(r => r.id == u.roleId)) {
existUser.role.push({ existUser.role.push({
id: u.roleId, id: u.roleId,
name: u.roleName name: u.roleName
}) })
} }
} else { } else {
let obj = {} let obj = {}
for (let k in u) { for (let k in u) {
let nextKey = k.replace('hrMember.', '') let nextKey = k.replace('hrMember.', '')
.replace('member.', '') .replace('member.', '')
if (nextKey.includes('_')) { if (nextKey.includes('_')) {
nextKey = nextKey.toLowerCase() nextKey = nextKey.toLowerCase()
.replace( .replace(
/(_)[a-z]/g, /(_)[a-z]/g,
(L) => L.toUpperCase() (L) => L.toUpperCase()
) )
.replace(/_/g, '') .replace(/_/g, '')
} }
obj[nextKey] = u[k] == '1970-01-01 00:00:00.000000' || u[k] == '1970-01-01 08:00:00.000000' ? null : u[k] 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);
returnD.push({
...obj,
departmrnt: u.depId ? [{
id: u.depId,
name: u.depName
}] : [],
role: u.roleId ? [{
id: u.roleId,
name: u.roleName
}] : [],
state: option.state ?
obj['dimissionDate'] ? 'dimission' :
obj['vacateStartTime'] ? 'vacate' :
workTime ? 'inOffice' :
dayoffTime ? 'dayoff' : 'rest'
: undefined,
del: undefined,
pepuserid: undefined
})
} }
pepUserIds.push(u.pepUserId) })
console.log("查询到的用户信息:", obj); return { packageUser: returnD, pepUserIds }
returnD.push({ }
...obj,
departmrnt: u.depId ? [{
id: u.depId,
name: u.depName
}] : [],
role: u.roleId ? [{
id: u.roleId,
name: u.roleName
}] : [],
state: option.state ?
obj['dimissionDate'] ? 'dimission' :
obj['vacateStartTime'] ? 'vacate' :
workTime ? 'inOffice' :
dayoffTime ? 'dayoff' : 'rest'
: undefined,
del: undefined,
pepuserid: undefined
})
}
})
return { packageUser: returnD, pepUserIds }
}
return { return {
memberList, memberList,
packageUserData packageUserData
} }
} }
Loading…
Cancel
Save