From 85e0fd11bc4f5677c7de213943dd7d1d5da3eb48 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Sat, 15 Oct 2022 13:57:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=BA=E5=91=98=E6=9F=A5=E8=AF=A2=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/member/index.js | 153 ++++++++++++++++++------ 1 file changed, 115 insertions(+), 38 deletions(-) diff --git a/api/app/lib/controllers/member/index.js b/api/app/lib/controllers/member/index.js index b6848dd..b04c495 100644 --- a/api/app/lib/controllers/member/index.js +++ b/api/app/lib/controllers/member/index.js @@ -191,13 +191,14 @@ async function list (ctx) { const { database: pepEmis } = clickHouse.pepEmis.opts.config const { keywordTarget, keyword, limit, page, state } = ctx.query - let nowTime = moment() + ctx.status = 200; + + const curDay = moment().format('YYYY-MM-DD') + const nowTime = moment() let whereOption = [] - if (state == 'dimission') { - whereOption.push(`member.dimission_date IS NOT null`) - } + let whereFromSelectOption = [] if (state == 'inOffice') { - const curDay = moment().format('YYYY-MM-DD') + // 在岗 const holidayJudge = await judgeHoliday(curDay) if (holidayJudge) { if ( @@ -205,8 +206,29 @@ async function list (ctx) { && nowTime.isAfter(moment(curDay + ' 08:30')) && nowTime.isBefore(moment(curDay + ' 17:30')) ) { - // 在工作日的工作时间范围 - whereOption.push(``) + // 在工作日的工作时间范围 无请假记录 + whereFromSelectOption.push(`vacateStartTime IS NULL`) + } else { + ctx.body = [] + return + } + } else { + ctx.body = [] + return + } + } + if (state == 'dayoff') { + // 放假 + const holidayJudge = await judgeHoliday(curDay) + if (holidayJudge) { + if ( + holidayJudge.dayoff || holidayJudge.festivals + ) { + // 在休息日范围内且无加班申请 + whereFromSelectOption.push(`overtimeStartTime IS NULL`) + } else { + ctx.body = [] + return } } else { ctx.body = [] @@ -215,17 +237,84 @@ async function list (ctx) { } const userRes = await clickHouse.hr.query(` SELECT - member.pep_user_id AS pepUserId, - member.*, + 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, - vacate.id AS vacateId - FROM member - LEFT JOIN ${pepEmis}.user AS user + 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 @@ -234,21 +323,11 @@ async function list (ctx) { ON department_user.user = user.id LEFT JOIN ${pepEmis}.department AS department ON department.id = department_user.department - ${state == 'vacate' ? 'INNER' : 'LEFT'} JOIN vacate - ON vacate.pep_user_id = member.pep_user_id - AND vacate.start_time <= '${moment().format('YYYY-MM-DD HH:mm:ss')}' - AND vacate.end_time > '${moment().format('YYYY-MM-DD HH:mm:ss')}' ${whereOption.length ? `WHERE ${whereOption.join(' AND ')}` : ''} - ${/** limit 错啦*/''} - ${limit ? `LIMIT ${limit}` : ''} - ${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''} `).toPromise() let returnD = [] userRes.forEach(u => { - if (u.overtimeId) { - let a = 1 - } let existUser = returnD.find(r => r.pepUserId == u.pepUserId) if (existUser) { if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) { @@ -266,20 +345,17 @@ async function list (ctx) { } else { let obj = {} for (let k in u) { - if (k.indexOf('member.') >= 0) { - obj[ - k.replace('member.', '') - // 变为小驼峰 - .toLowerCase() - .replace( - /(_)[a-z]/g, - (L) => L.toUpperCase() - ) - .replace(/_/g, '') - ] = u[k] - } else { - obj[k] = u[k] - } + obj[ + k.replace('hrMember.', '') + .replace('member.', '') + // 变为小驼峰 + .toLowerCase() + .replace( + /(_)[a-z]/g, + (L) => L.toUpperCase() + ) + .replace(/_/g, '') + ] = u[k] } returnD.push({ ...obj, @@ -292,11 +368,12 @@ async function list (ctx) { name: u.roleName }] : [], del: undefined, + pepuserid: undefined }) } }) - ctx.status = 200; + ctx.body = returnD } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`);