From f0743ccc8e80d0f3a4e78395344c4f8d52be581b Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Mon, 17 Oct 2022 14:58:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=BA=E5=91=98=E6=9F=A5=E8=AF=A2=E7=9C=9F?= =?UTF-8?q?=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/member/index.js | 9 +- api/app/lib/utils/member.js | 132 +++++++++++++----------- 2 files changed, 78 insertions(+), 63 deletions(-) diff --git a/api/app/lib/controllers/member/index.js b/api/app/lib/controllers/member/index.js index 25eed4a..66750f8 100644 --- a/api/app/lib/controllers/member/index.js +++ b/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)) { diff --git a/api/app/lib/utils/member.js b/api/app/lib/utils/member.js index b3844d0..b0b4530 100644 --- a/api/app/lib/utils/member.js +++ b/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 {