|
|
@ -8,7 +8,11 @@ module.exports = function (app, opts) { |
|
|
|
keywordTarget, keyword, limit, page, state, |
|
|
|
hiredateStart, hiredateEnd, marital, native, workPlace, |
|
|
|
orderBy, orderDirection, |
|
|
|
overtimeDayStatisticStartDate, overtimeDayStatisticendDate |
|
|
|
nowAttendanceTime, |
|
|
|
overtimeDayStatisticStartDate, overtimeDayStatisticendDate, |
|
|
|
overtimeCountStatistic, overtimeCountStatisticStartDate, overtimeCountStatisticendDate, |
|
|
|
vacateDayStatisticStartDate, vacateDayStatisticendDate, |
|
|
|
vacateDurationStatistic, vacateCountStatistic, vacateCountStatisticStartDate, vacateCountStatisticendDate |
|
|
|
}) { |
|
|
|
const { judgeHoliday } = app.fs.utils |
|
|
|
const { clickHouse } = app.fs |
|
|
@ -62,10 +66,34 @@ module.exports = function (app, opts) { |
|
|
|
|
|
|
|
let overtimeDayStatisticWhere = [] |
|
|
|
if (overtimeDayStatisticStartDate) { |
|
|
|
overtimeDayStatisticWhere.push(`overtime_day.day >= '${moment(startDate).format('YYYY-MM-DD')}'`) |
|
|
|
overtimeDayStatisticWhere.push(`overtime_day.day >= '${moment(overtimeDayStatisticStartDate).format('YYYY-MM-DD')}'`) |
|
|
|
} |
|
|
|
if (overtimeDayStatisticendDate) { |
|
|
|
overtimeDayStatisticWhere.push(`overtime_day.day <= '${moment(endDate).format('YYYY-MM-DD')}'`) |
|
|
|
overtimeDayStatisticWhere.push(`overtime_day.day <= '${moment(overtimeDayStatisticendDate).format('YYYY-MM-DD')}'`) |
|
|
|
} |
|
|
|
|
|
|
|
let overtimeCountStatisticWhere = [] |
|
|
|
if (overtimeCountStatisticStartDate) { |
|
|
|
overtimeCountStatisticWhere.push(`overtime.start_time >= '${moment(overtimeCountStatisticStartDate).format('YYYY-MM-DD')}'`) |
|
|
|
} |
|
|
|
if (overtimeCountStatisticendDate) { |
|
|
|
overtimeCountStatisticWhere.push(`overtime.end_time <= '${moment(overtimeCountStatisticendDate).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).format('YYYY-MM-DD')}'`) |
|
|
|
} |
|
|
|
if (vacateCountStatisticendDate) { |
|
|
|
vacateCountStatisticWhere.push(`vacate.end_time <= '${moment(vacateCountStatisticendDate).format('YYYY-MM-DD')}'`) |
|
|
|
} |
|
|
|
// CRAZY
|
|
|
|
const innerSelectQuery = ` |
|
|
@ -78,33 +106,39 @@ module.exports = function (app, opts) { |
|
|
|
${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 |
|
|
|
|
|
|
|
${orderBy == 'overtimeTakeRestSum' || orderBy == 'overtimePaySum' || orderBy == 'overtimeSum' ? |
|
|
|
|
|
|
|
${nowAttendanceTime ? ` |
|
|
|
${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 |
|
|
|
`: ''}
|
|
|
|
${nowAttendanceTime ? ` |
|
|
|
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 |
|
|
|
`: ''}
|
|
|
|
|
|
|
|
${orderBy == 'overtimeTakeRestSum' || |
|
|
|
orderBy == 'overtimePaySum' || |
|
|
|
orderBy == 'overtimeSum' ? |
|
|
|
`LEFT JOIN (
|
|
|
|
SELECT |
|
|
|
overtime.pep_user_id AS pepUserId, |
|
|
@ -121,6 +155,49 @@ module.exports = function (app, opts) { |
|
|
|
) AS overtimeDayStatistic |
|
|
|
ON overtimeDayStatistic.pepUserId = member.pep_user_id`: ''}
|
|
|
|
|
|
|
|
${overtimeCountStatistic ? ` |
|
|
|
LEFT JOIN ( |
|
|
|
SELECT |
|
|
|
overtime.pep_user_id AS pepUserId, |
|
|
|
count(pep_process_story_id) AS count |
|
|
|
FROM overtime |
|
|
|
${overtimeCountStatisticWhere.length ? ` |
|
|
|
WHERE ${overtimeCountStatisticWhere.join(' AND ')} |
|
|
|
`: ''}
|
|
|
|
GROUP BY overtime.pep_user_id |
|
|
|
) AS overtimeCountStatistic |
|
|
|
ON overtimeCountStatistic.pepUserId = member.pep_user_id |
|
|
|
`: ''}
|
|
|
|
|
|
|
|
${vacateDurationStatistic || |
|
|
|
orderBy == 'vacateSum' ? |
|
|
|
`LEFT JOIN (
|
|
|
|
SELECT |
|
|
|
vacate.pep_user_id AS pepUserId, |
|
|
|
sum(vacate_day.duration) AS duration |
|
|
|
FROM vacate_day |
|
|
|
INNER JOIN vacate |
|
|
|
ON vacate.id = vacate_day.vacate_id |
|
|
|
${vacateDayStatisticWhere.length ? ` |
|
|
|
WHERE ${vacateDayStatisticWhere.join(' AND ')} |
|
|
|
`: ''}
|
|
|
|
GROUP BY vacate.pep_user_id |
|
|
|
) AS vacateDayStatistic |
|
|
|
ON vacateDayStatistic.pepUserId = member.pep_user_id`: ''}
|
|
|
|
|
|
|
|
${vacateCountStatistic ? ` |
|
|
|
LEFT JOIN ( |
|
|
|
SELECT |
|
|
|
vacate.pep_user_id AS pepUserId, |
|
|
|
count(pep_process_story_id) AS count |
|
|
|
FROM vacate |
|
|
|
${vacateCountStatisticWhere.length ? ` |
|
|
|
WHERE ${vacateCountStatisticWhere.join(' AND ')} |
|
|
|
`: ''}
|
|
|
|
GROUP BY vacate.pep_user_id |
|
|
|
) AS vacateCountStatistic |
|
|
|
ON vacateCountStatistic.pepUserId = member.pep_user_id |
|
|
|
`: ''}
|
|
|
|
WHERE |
|
|
|
member.del = 0 |
|
|
|
${keywordTarget == 'role' && keyword ? ` |
|
|
@ -143,7 +220,7 @@ module.exports = function (app, opts) { |
|
|
|
` : ''}
|
|
|
|
${state == 'dimission' ? `AND member.dimission_date IS NOT null` : ''} |
|
|
|
${state == 'onJob' ? `AND member.dimission_date IS null` : ''} |
|
|
|
${whereFromSelectOption.length ? `AND ${whereFromSelectOption.join('AND')}` : ''} |
|
|
|
${whereFromSelectOption.length && nowAttendanceTime ? `AND ${whereFromSelectOption.join('AND')}` : ''} |
|
|
|
${hiredateStart ? ` |
|
|
|
AND member.hiredate >= '${moment(hiredateStart).format('YYYY-MM-DD')}' |
|
|
|
`: ''}
|
|
|
@ -173,23 +250,39 @@ module.exports = function (app, opts) { |
|
|
|
department.id AS depId |
|
|
|
FROM ( |
|
|
|
SELECT |
|
|
|
member.*, |
|
|
|
|
|
|
|
${orderBy == 'overtimeTakeRestSum' || orderBy == 'overtimePaySum' || orderBy == 'overtimeSum' ? ` |
|
|
|
${orderBy == 'overtimeTakeRestSum' |
|
|
|
|| orderBy == 'overtimePaySum' |
|
|
|
|| orderBy == 'overtimeSum' ? ` |
|
|
|
overtimeDayStatistic.duration AS overtimeDayStatisticDuration |
|
|
|
`: ''}
|
|
|
|
|
|
|
|
hrVacate.vacateStartTime AS vacateStartTime, |
|
|
|
hrVacate.vacateEndTime AS vacateEndTime, |
|
|
|
hrOvertime.overtimeStartTime AS overtimeStartTime, |
|
|
|
hrOvertime.overtimeEndTime AS overtimeEndTime |
|
|
|
${overtimeCountStatistic ? ` |
|
|
|
overtimeCountStatistic.count AS overtimeCount, |
|
|
|
`: ''}
|
|
|
|
|
|
|
|
${orderBy == 'vacateSum' || vacateDurationStatistic ? ` |
|
|
|
vacateDayStatistic.duration AS vacateDayStatisticDuration, |
|
|
|
`: ''}
|
|
|
|
|
|
|
|
${vacateCountStatistic ? ` |
|
|
|
vacateCountStatistic.count AS vacateCount, |
|
|
|
`: ''}
|
|
|
|
|
|
|
|
${nowAttendanceTime ? ` |
|
|
|
hrVacate.vacateStartTime AS vacateStartTime, |
|
|
|
hrVacate.vacateEndTime AS vacateEndTime, |
|
|
|
hrOvertime.overtimeStartTime AS overtimeStartTime, |
|
|
|
hrOvertime.overtimeEndTime AS overtimeEndTime, |
|
|
|
`: ''}
|
|
|
|
|
|
|
|
member.* |
|
|
|
${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 |
|
|
|
ON pepUserId = user.id |
|
|
|
LEFT JOIN ${pepEmis}.user_role AS user_role |
|
|
|
ON ${pepEmis}.user_role.user = user.id |
|
|
|
LEFT JOIN ${pepEmis}.role AS role |
|
|
@ -199,11 +292,23 @@ 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."member.overtimeDayStatisticDuration"' |
|
|
|
: 'user.people_code'} |
|
|
|
ORDER BY ${orderBy == 'code' ? |
|
|
|
'user.people_code' |
|
|
|
: orderBy == 'hiredate' |
|
|
|
? 'hrMember."member.hiredate"' |
|
|
|
: orderBy == 'age' |
|
|
|
? 'hrMember."member.birthday"' |
|
|
|
: orderBy == 'overtimeTakeRestSum' |
|
|
|
|| orderBy == 'overtimePaySum' |
|
|
|
|| orderBy == 'overtimeSum' ? |
|
|
|
'hrMember."member.overtimeDayStatisticDuration"' |
|
|
|
: orderBy == 'overtimeCount' ? |
|
|
|
'hrMember.overtimeCount' |
|
|
|
: orderBy == 'vacateSum' ? |
|
|
|
'hrMember.vacateDayStatisticDuration' |
|
|
|
: orderBy == 'vacateCount' ? |
|
|
|
'hrMember.vacateCount' |
|
|
|
: 'user.people_code'} |
|
|
|
${orderDirection || 'ASC'} |
|
|
|
`).toPromise()
|
|
|
|
|
|
|
|