From 8b7f1d5aa81b0536aa8399e82eb3d394e12aff46 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Sat, 15 Oct 2022 17:29:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=AD=97=E6=AE=B5=E5=B0=8F?= =?UTF-8?q?=E9=A9=BC=E5=B3=B0=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/member/index.js | 162 +++--------------------- api/app/lib/utils/member.js | 147 +++++++++++++++++++++ 2 files changed, 164 insertions(+), 145 deletions(-) create mode 100644 api/app/lib/utils/member.js diff --git a/api/app/lib/controllers/member/index.js b/api/app/lib/controllers/member/index.js index a760e39..59bf603 100644 --- a/api/app/lib/controllers/member/index.js +++ b/api/app/lib/controllers/member/index.js @@ -186,145 +186,10 @@ async function del (ctx) { async function list (ctx) { try { const { models } = ctx.fs.dc; - const { judgeHoliday } = ctx.app.fs.utils - const { clickHouse } = ctx.app.fs - const { database: pepEmis } = clickHouse.pepEmis.opts.config + const { judgeHoliday, memberList } = ctx.app.fs.utils const { keywordTarget, keyword, limit, page, state } = ctx.query - ctx.status = 200; - - 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 { - 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 = [] - 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() + const userRes = await memberList({ keywordTarget, keyword, limit, page, state }) let returnD = [] userRes.forEach(u => { @@ -345,17 +210,17 @@ async function list (ctx) { } else { let obj = {} for (let k in u) { - obj[ - k.replace('hrMember.', '') - .replace('member.', '') - // 变为小驼峰 - .toLowerCase() + let nextKey = k.replace('hrMember.', '') + .replace('member.', '') + if (nextKey.includes('_')) { + nextKey = nextKey.toLowerCase() .replace( /(_)[a-z]/g, (L) => L.toUpperCase() ) .replace(/_/g, '') - ] = u[k] + } + obj[nextKey] = u[k] } returnD.push({ ...obj, @@ -373,7 +238,7 @@ async function list (ctx) { } }) - + ctx.status = 200; ctx.body = returnD } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); @@ -516,8 +381,15 @@ async function vacateStatistics (ctx) { async function exportData (ctx) { try { const { models } = ctx.fs.dc; + const { judgeHoliday, memberList } = ctx.app.fs.utils + const { keywordTarget, keyword, limit, page, state } = ctx.query + + const userRes = await memberList({ keywordTarget, keyword, limit, page, state }) + + const tableAttributes = models['Member'].tableAttributes + + ctx.status = 200; - ctx.status = 20; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; diff --git a/api/app/lib/utils/member.js b/api/app/lib/utils/member.js new file mode 100644 index 0000000..2559d9e --- /dev/null +++ b/api/app/lib/utils/member.js @@ -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 + } +} \ No newline at end of file