人力资源
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

187 lines
6.5 KiB

'use strict';
const moment = require('moment')
const request = require('superagent');
module.exports = function (app, opts) {
async function memberList ({
keywordTarget, keyword, limit, page, state,
hiredateStart, hiredateEnd, marital, native, workPlace
}) {
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'`)
} else {
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'`)
} else {
returnEmpty = true
}
} else {
returnEmpty = true
}
}
if (returnEmpty) {
return {
count: 0,
rows: []
}
}
const innerSelectQuery = `
FROM member
INNER JOIN ${pepEmis}.user AS user
ON member.pep_user_id = user.id
${keywordTarget == 'number' && keyword ? `
AND toString(user.id) LIKE '%${keyword}%'
`: ''}
${keywordTarget == 'name' && keyword ? `
AND user.name LIKE '%${keyword}%'
`: ''}
${state == 'vacate' ? 'INNER' : 'LEFT'} JOIN (
SELECT
pep_user_id,
any(start_time) AS vacateStartTime,
any(end_time) AS vacateEndTime
FROM vacate
WHERE
start_time <= '${nowTime.format('YYYY-MM-DD HH:mm:ss')}'
AND end_time > '${nowTime.format('YYYY-MM-DD HH:mm:ss')}'
GROUP BY pep_user_id
) AS hrVacate
ON hrVacate.pep_user_id = member.pep_user_id
LEFT JOIN (
SELECT
pep_user_id,
any(start_time) AS overtimeStartTime,
any(end_time) AS overtimeEndTime
FROM overtime
WHERE
start_time <= '${nowTime.format('YYYY-MM-DD HH:mm:ss')}'
AND end_time > '${nowTime.format('YYYY-MM-DD HH:mm:ss')}'
GROUP BY pep_user_id
) AS hrOvertime
ON hrOvertime.pep_user_id = member.pep_user_id
WHERE
member.del = 0
${keywordTarget == 'role' && keyword ? `
AND user.id IN (
SELECT user_role.user
FROM ${pepEmis}.user_role AS user_role
INNER JOIN ${pepEmis}.role AS role
ON role.id = user_role.role
AND role.name LIKE '%${keyword}%'
)
` : ''}
${keywordTarget == 'dep' && keyword ? `
AND user.id IN (
SELECT department_user.user
FROM ${pepEmis}.department_user AS department_user
INNER JOIN ${pepEmis}.department AS department
ON department.id = department_user.department
AND department.name LIKE '%${keyword}%'
)
` : ''}
${state == 'dimission' ? `AND member.dimission_date IS NOT null` : ''}
${state == 'onJob' ? `AND member.dimission_date IS null` : ''}
${whereFromSelectOption.length ? `AND ${whereFromSelectOption.join('AND')}` : ''}
${hiredateStart ? `
AND member.hiredate >= '${moment(hiredateStart).format('YYYY-MM-DD')}'
`: ''}
${hiredateEnd ? `
AND member.hiredate <= '${moment(hiredateEnd).format('YYYY-MM-DD')}'
` : ''}
${marital ? `
AND member.marital = '${marital}'
`: ''}
${native ? `
AND member.native_place = '${native}'
`: ''}
${workPlace ? `
AND member.work_place = '${workPlace}'
`: ''}
`
const userRes = await clickHouse.hr.query(`
SELECT
hrMember."member.pep_user_id" AS pepUserId,
hrMember.*,
user.name AS userName,
user.people_code AS userCode,
role.name AS roleName,
role.id AS roleId,
department.name AS depName,
department.id AS depId
FROM (
SELECT
member.*,
hrVacate.vacateStartTime AS vacateStartTime,
hrVacate.vacateEndTime AS vacateEndTime,
hrOvertime.overtimeStartTime AS overtimeStartTime,
hrOvertime.overtimeEndTime AS overtimeEndTime
${innerSelectQuery}
${limit ? `LIMIT ${limit}` : ''}
${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''}
) AS hrMember
LEFT JOIN ${pepEmis}.user AS user
ON hrMember."member.pep_user_id" = user.id
LEFT JOIN ${pepEmis}.user_role AS user_role
ON ${pepEmis}.user_role.user = user.id
LEFT JOIN ${pepEmis}.role AS role
ON ${pepEmis}.role.id = user_role.role
LEFT JOIN ${pepEmis}.department_user AS department_user
ON department_user.user = user.id
LEFT JOIN ${pepEmis}.department AS department
ON department.id = department_user.department
${whereOption.length ? `WHERE ${whereOption.join(' AND ')}` : ''}
`).toPromise()
const countRes = await clickHouse.hr.query(`
SELECT
count(member.pep_user_id) AS count
${innerSelectQuery}
`).toPromise()
return {
count: countRes[0].count,
rows: userRes
}
}
return {
memberList
}
}