Browse Source

人员查询真分页

master
巴林闲侠 2 years ago
parent
commit
f0743ccc8e
  1. 9
      api/app/lib/controllers/member/index.js
  2. 132
      api/app/lib/utils/member.js

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

@ -209,7 +209,7 @@ async function list (ctx) {
const userRes = await memberList({ keywordTarget, keyword, limit, page, state })
let returnD = []
userRes.forEach(u => {
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)) {
@ -260,7 +260,10 @@ async function list (ctx) {
})
ctx.status = 200;
ctx.body = returnD
ctx.body = {
count: userRes.count,
rows: returnD
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
ctx.status = 400;
@ -413,7 +416,7 @@ async function exportData (ctx) {
let exportD = []
let pepUserIds = []
userRes.forEach(u => {
userRes.rows.forEach(u => {
let existUser = exportD.find(r => r.pepUserId == u.pepUserId)
if (existUser) {
if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) {

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

@ -47,6 +47,68 @@ module.exports = function (app, opts) {
return []
}
}
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 != '1970-01-01 00:00:00.000000'` : ''}
${state == 'onJob' ? `AND member.dimission_date = '1970-01-01 00:00:00.000000'` : ''}
${whereFromSelectOption.length ? `AND ${whereFromSelectOption.join('AND')}` : ''}
`
const userRes = await clickHouse.hr.query(`
SELECT
hrMember."member.pep_user_id" AS pepUserId,
@ -63,64 +125,7 @@ module.exports = function (app, opts) {
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 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(toString(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')}` : ''}
${innerSelectQuery}
${limit ? `LIMIT ${limit}` : ''}
${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''}
) AS hrMember
@ -138,9 +143,16 @@ module.exports = function (app, opts) {
${whereOption.length ? `WHERE ${whereOption.join(' AND ')}` : ''}
`).toPromise()
const countRes = await clickHouse.hr.query(`
SELECT
count(member.pep_user_id) AS count
${innerSelectQuery}
`).toPromise()
return userRes
return {
count: countRes[0].count,
rows: userRes
}
}
return {

Loading…
Cancel
Save