Browse Source

(*)人员信息相关模块

master
zmh 1 year 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) {
async function memberList ({
keywordTarget, keyword, limit, page, state,
hiredateStart, hiredateEnd, marital, native, workPlace,
orderBy, orderDirection,
nowAttendanceTime,
overtimeDayStatisticStartDate, overtimeDayStatisticendDate,
overtimeCountStatistic, overtimeCountStatisticStartDate, overtimeCountStatisticendDate,
vacateDayStatisticStartDate, vacateDayStatisticendDate,
vacateDurationStatistic, vacateCountStatistic, vacateCountStatisticStartDate, vacateCountStatisticendDate
}) {
const { judgeHoliday } = app.fs.utils
const { clickHouse } = app.fs
const { database: pepEmis } = clickHouse.pepEmis.opts.config
async function memberList({
keywordTarget, keyword, limit, page, state,
hiredateStart, hiredateEnd, marital, native, workPlace,
orderBy, orderDirection,
nowAttendanceTime,
overtimeDayStatisticStartDate, overtimeDayStatisticendDate,
overtimeCountStatistic, overtimeCountStatisticStartDate, overtimeCountStatisticendDate,
vacateDayStatisticStartDate, vacateDayStatisticendDate,
vacateDurationStatistic, vacateCountStatistic, vacateCountStatisticStartDate, vacateCountStatisticendDate,
userIds
}) {
const { judgeHoliday } = app.fs.utils
const { clickHouse } = app.fs
const { database: pepEmis } = clickHouse.pepEmis.opts.config
const curDay = moment().format('YYYY-MM-DD')
const nowTime = moment()
let whereOption = []
let whereFromSelectOption = []
let returnEmpty = false
if (state == 'inOffice') {
// 在岗
const holidayJudge = await judgeHoliday(curDay)
if (holidayJudge) {
if (
holidayJudge.workday
&& nowTime.isAfter(moment(curDay + ' 08:30'))
&& nowTime.isBefore(moment(curDay + ' 17:30'))
) {
// 在工作日的工作时间范围 无请假记录
whereFromSelectOption.push(`vacateStartTime = '1970-01-01 00:00:00.000000'`)
const curDay = moment().format('YYYY-MM-DD')
const nowTime = moment()
let whereOption = []
let whereFromSelectOption = []
let returnEmpty = false
if (state == 'inOffice') {
// 在岗
const holidayJudge = await judgeHoliday(curDay)
if (holidayJudge) {
if (
holidayJudge.workday
&& nowTime.isAfter(moment(curDay + ' 08:30'))
&& nowTime.isBefore(moment(curDay + ' 17:30'))
) {
// 在工作日的工作时间范围 无请假记录
whereFromSelectOption.push(`vacateStartTime = '1970-01-01 00:00:00.000000'`)
} else {
returnEmpty = true
}
} else {
returnEmpty = true
returnEmpty = true
}
} else {
returnEmpty = true
}
}
if (state == 'dayoff') {
// 放假
const holidayJudge = await judgeHoliday(curDay)
if (holidayJudge) {
if (
holidayJudge.dayoff || holidayJudge.festivals
) {
// 在休息日范围内且无加班申请
whereFromSelectOption.push(`overtimeStartTime = '1970-01-01 00:00:00.000000'`)
}
if (state == 'dayoff') {
// 放假
const holidayJudge = await judgeHoliday(curDay)
if (holidayJudge) {
if (
holidayJudge.dayoff || holidayJudge.festivals
) {
// 在休息日范围内且无加班申请
whereFromSelectOption.push(`overtimeStartTime = '1970-01-01 00:00:00.000000'`)
} else {
returnEmpty = true
}
} else {
returnEmpty = true
returnEmpty = true
}
} else {
returnEmpty = true
}
}
if (returnEmpty) {
return {
count: 0,
rows: []
}
}
}
if (returnEmpty) {
return {
count: 0,
rows: []
}
}
let overtimeDayStatisticWhere = []
if (overtimeDayStatisticStartDate) {
overtimeDayStatisticWhere.push(`overtime_day.day >= '${moment(overtimeDayStatisticStartDate).format('YYYY-MM-DD')}'`)
}
if (overtimeDayStatisticendDate) {
overtimeDayStatisticWhere.push(`overtime_day.day <= '${moment(overtimeDayStatisticendDate).format('YYYY-MM-DD')}'`)
}
let overtimeDayStatisticWhere = []
if (overtimeDayStatisticStartDate) {
overtimeDayStatisticWhere.push(`overtime_day.day >= '${moment(overtimeDayStatisticStartDate).format('YYYY-MM-DD')}'`)
}
if (overtimeDayStatisticendDate) {
overtimeDayStatisticWhere.push(`overtime_day.day <= '${moment(overtimeDayStatisticendDate).format('YYYY-MM-DD')}'`)
}
let overtimeCountStatisticWhere = []
if (overtimeCountStatisticStartDate) {
overtimeCountStatisticWhere.push(`overtime.start_time >= '${moment(overtimeCountStatisticStartDate).startOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
}
if (overtimeCountStatisticendDate) {
overtimeCountStatisticWhere.push(`overtime.end_time <= '${moment(overtimeCountStatisticendDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
}
let overtimeCountStatisticWhere = []
if (overtimeCountStatisticStartDate) {
overtimeCountStatisticWhere.push(`overtime.start_time >= '${moment(overtimeCountStatisticStartDate).startOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
}
if (overtimeCountStatisticendDate) {
overtimeCountStatisticWhere.push(`overtime.end_time <= '${moment(overtimeCountStatisticendDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
}
let vacateDayStatisticWhere = []
if (vacateDayStatisticStartDate) {
vacateDayStatisticWhere.push(`vacate_day.day >= '${moment(vacateDayStatisticStartDate).format('YYYY-MM-DD')}'`)
}
if (vacateDayStatisticendDate) {
vacateDayStatisticWhere.push(`vacate_day.day <= '${moment(vacateDayStatisticendDate).format('YYYY-MM-DD')}'`)
}
let vacateDayStatisticWhere = []
if (vacateDayStatisticStartDate) {
vacateDayStatisticWhere.push(`vacate_day.day >= '${moment(vacateDayStatisticStartDate).format('YYYY-MM-DD')}'`)
}
if (vacateDayStatisticendDate) {
vacateDayStatisticWhere.push(`vacate_day.day <= '${moment(vacateDayStatisticendDate).format('YYYY-MM-DD')}'`)
}
let vacateCountStatisticWhere = []
if (vacateCountStatisticStartDate) {
vacateCountStatisticWhere.push(`vacate.start_time >= '${moment(vacateCountStatisticStartDate).startOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
}
if (vacateCountStatisticendDate) {
vacateCountStatisticWhere.push(`vacate.end_time <= '${moment(vacateCountStatisticendDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
}
// CRAZY
const innerSelectQuery = `
let vacateCountStatisticWhere = []
if (vacateCountStatisticStartDate) {
vacateCountStatisticWhere.push(`vacate.start_time >= '${moment(vacateCountStatisticStartDate).startOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
}
if (vacateCountStatisticendDate) {
vacateCountStatisticWhere.push(`vacate.end_time <= '${moment(vacateCountStatisticendDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')}'`)
}
// CRAZY
const innerSelectQuery = `
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}%'
`: ''}
@ -137,9 +140,9 @@ module.exports = function (app, opts) {
`: ''}
${orderBy == 'overtimeTakeRestSum' ||
orderBy == 'overtimePaySum' ||
orderBy == 'overtimeSum' ?
`LEFT JOIN (
orderBy == 'overtimePaySum' ||
orderBy == 'overtimeSum' ?
`LEFT JOIN (
SELECT
overtime.pep_user_id AS pepUserId,
sum(overtime_day.duration) AS duration
@ -170,8 +173,8 @@ module.exports = function (app, opts) {
`: ''}
${vacateDurationStatistic ||
orderBy == 'vacateSum' ?
`LEFT JOIN (
orderBy == 'vacateSum' ?
`LEFT JOIN (
SELECT
vacate.pep_user_id AS pepUserId,
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
hrMember."member.pep_user_id" AS pepUserId,
hrMember.*,
@ -253,8 +256,8 @@ module.exports = function (app, opts) {
FROM (
SELECT
${orderBy == 'overtimeTakeRestSum'
|| orderBy == 'overtimePaySum'
|| orderBy == 'overtimeSum' ? `
|| orderBy == 'overtimePaySum'
|| orderBy == 'overtimeSum' ? `
overtimeDayStatistic.duration AS overtimeDayStatisticDuration,
`: ''}
@ -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,120 +319,103 @@ 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(`
const countRes = await clickHouse.hr.query(`
SELECT
count(member.pep_user_id) AS count
${innerSelectQuery}
`).toPromise()
return {
count: countRes[0].count,
rows: userRes
}
}
return {
count: countRes[0].count,
rows: userRes
}
}
async function packageUserData (userRes, option = {}) {
async function packageUserData(userRes, option = {}) {
const { judgeHoliday, } = app.fs.utils
let workTime = false
let dayoffTime = false
if (option.state) {
const curDay = moment().format('YYYY-MM-DD')
const nowTime = moment()
const holidayJudge = await judgeHoliday(curDay)
if (holidayJudge) {
if (
holidayJudge.workday
&& nowTime.isAfter(moment(curDay + ' 08:30'))
&& nowTime.isBefore(moment(curDay + ' 17:30'))
) {
workTime = true
} else if (holidayJudge.dayoff || holidayJudge.festivals) {
dayoffTime = true
const { judgeHoliday, } = app.fs.utils
let workTime = false
let dayoffTime = false
if (option.state) {
const curDay = moment().format('YYYY-MM-DD')
const nowTime = moment()
const holidayJudge = await judgeHoliday(curDay)
if (holidayJudge) {
if (
holidayJudge.workday
&& nowTime.isAfter(moment(curDay + ' 08:30'))
&& nowTime.isBefore(moment(curDay + ' 17:30'))
) {
workTime = true
} else if (holidayJudge.dayoff || holidayJudge.festivals) {
dayoffTime = true
}
}
}
}
}
let returnD = []
let pepUserIds = [-1]
userRes.rows.forEach(u => {
let existUser = returnD.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] == '1970-01-01 00:00:00.000000' || u[k] == '1970-01-01 08:00:00.000000' ? null : u[k]
let returnD = []
let pepUserIds = [-1]
userRes.rows.forEach(u => {
let existUser = returnD.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] == '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);
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 { packageUser: returnD, pepUserIds }
}
return {
memberList,
packageUserData
}
return {
memberList,
packageUserData
}
}
Loading…
Cancel
Save