巴林闲侠
2 years ago
2 changed files with 164 additions and 145 deletions
@ -0,0 +1,147 @@ |
|||
'use strict'; |
|||
const moment = require('moment') |
|||
const request = require('superagent'); |
|||
|
|||
module.exports = function (app, opts) { |
|||
|
|||
async function memberList ({ keywordTarget, keyword, limit, page, state }) { |
|||
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 = [] |
|||
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 IS NULL`) |
|||
} else { |
|||
return [] |
|||
} |
|||
} else { |
|||
return [] |
|||
} |
|||
} |
|||
if (state == 'dayoff') { |
|||
// 放假
|
|||
const holidayJudge = await judgeHoliday(curDay) |
|||
if (holidayJudge) { |
|||
if ( |
|||
holidayJudge.dayoff || holidayJudge.festivals |
|||
) { |
|||
// 在休息日范围内且无加班申请
|
|||
whereFromSelectOption.push(`overtimeStartTime IS NULL`) |
|||
} else { |
|||
return [] |
|||
} |
|||
} else { |
|||
return [] |
|||
} |
|||
} |
|||
const userRes = await clickHouse.hr.query(` |
|||
SELECT |
|||
hrMember."member.pep_user_id" AS pepUserId, |
|||
hrMember.*, |
|||
user.name AS userName, |
|||
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 |
|||
FROM member |
|||
INNER JOIN ${pepEmis}.user AS user |
|||
ON member.pep_user_id = user.id |
|||
${keywordTarget == 'number' && keyword ? ` |
|||
AND 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.pep_user_id > 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')}` : ''} |
|||
${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()
|
|||
|
|||
return userRes |
|||
} |
|||
|
|||
return { |
|||
memberList |
|||
} |
|||
} |
Loading…
Reference in new issue