'use strict'; const moment = require('moment') async function add (ctx) { try { const { models } = ctx.fs.dc; const { pepUserId, idNumber, idPhoto, gender, birthday, nativePlace, marital, politicsStatus, phoneNumber, workPlace, graduatedFrom, educationBackground, specialty, graduationDate, hiredate, turnProbationPeriod, regularDate, dimissionDate, experienceYear, occupationalHistory, vitae } = ctx.request.body const existMemberRes = await models.Member.findOne({ where: { pepUserId } }) if (existMemberRes && !existMemberRes.del) { throw '当前人员信息已存在' } let storageData = { pepUserId, idNumber, idPhoto, gender, birthday, nativePlace, marital, politicsStatus, phoneNumber, workPlace, graduatedFrom, educationBackground, specialty, graduationDate, hiredate, turnProbationPeriod, regularDate, dimissionDate, experienceYear, occupationalHistory, vitae, del: false } if (existMemberRes && existMemberRes.del) { await models.Member.update(storageData, { where: { pepUserId, } }) } else { await models.create(storageData) } ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function edit (ctx) { try { const { models } = ctx.fs.dc; const { pepUserId, idNumber, idPhoto, gender, birthday, nativePlace, marital, politicsStatus, phoneNumber, workPlace, graduatedFrom, educationBackground, specialty, graduationDate, hiredate, turnProbationPeriod, regularDate, dimissionDate, experienceYear, occupationalHistory, vitae } = ctx.request.body const existMemberRes = await models.Member.findOne({ where: { pepUserId } }) if (!existMemberRes) { throw '当前人员信息不存在' } let storageData = { pepUserId, idNumber, idPhoto, gender, birthday, nativePlace, marital, politicsStatus, phoneNumber, workPlace, graduatedFrom, educationBackground, specialty, graduationDate, hiredate, turnProbationPeriod, regularDate, dimissionDate, experienceYear, occupationalHistory, vitae, del: false } await models.Member.update(storageData, { where: { pepUserId: pepUserId } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function searchPepMember (ctx) { try { const { models } = ctx.fs.dc; const { clickHouse } = ctx.app.fs const { keyword } = ctx.query let whereOption = [] if (keyword) { whereOption.push(`user.id = ${keyword}`) whereOption.push(`user.name LIKE '${keyword}'`) } const userRes = await clickHouse.pepEmis.query(` SELECT user.id AS pepUserId, user.name AS userName, role.name AS roleName, role.id AS roleId, department.name AS depName, department.id AS depId FROM user LEFT JOIN user_role ON user_role.user = user.id LEFT JOIN role ON role.id = user_role.role LEFT JOIN department_user ON department_user.user = user.id LEFT JOIN department ON department.id = department_user.department ${whereOption.length ? `WHERE ${whereOption.join(' OR ')}` : ''} `).toPromise() let returnD = [] userRes.forEach(u => { let existUser = returnD.find(r => r.pepUserId == u.pepUserId) if (existUser) { if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) { existUser.departmrnt.push({ id: u.depId, name: u.depName }) } if (u.roleId && !existUser.role.some(r => r.id == u.roleId)) { existUser.role.push({ id: u.roleId, name: u.roleName }) } } else { returnD.push({ pepUserId: u.pepUserId, name: u.userName, departmrnt: u.depId ? [{ id: u.depId, name: u.depName }] : [], role: u.roleId ? [{ id: u.roleId, name: u.roleName }] : [], }) } }) ctx.status = 200; ctx.body = returnD } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function del (ctx) { try { const { models } = ctx.fs.dc; const { pepUserId } = ctx.query await models.Member.update({ del: true, }, { where: { pepUserId, } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } 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 { 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() let returnD = [] userRes.forEach(u => { let existUser = returnD.find(r => r.pepUserId == u.pepUserId) if (existUser) { if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) { existUser.departmrnt.push({ id: u.depId, name: u.depName }) } if (u.roleId && !existUser.role.some(r => r.id == u.roleId)) { existUser.role.push({ id: u.roleId, name: u.roleName }) } } else { let obj = {} for (let k in u) { obj[ k.replace('hrMember.', '') .replace('member.', '') // 变为小驼峰 .toLowerCase() .replace( /(_)[a-z]/g, (L) => L.toUpperCase() ) .replace(/_/g, '') ] = u[k] } returnD.push({ ...obj, departmrnt: u.depId ? [{ id: u.depId, name: u.depName }] : [], role: u.roleId ? [{ id: u.roleId, name: u.roleName }] : [], del: undefined, pepuserid: undefined }) } }) ctx.body = returnD } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } module.exports = { add, edit, del, searchPepMember, list, };