Browse Source

人员查询列表

master
巴林闲侠 2 years ago
parent
commit
85e0fd11bc
  1. 153
      api/app/lib/controllers/member/index.js

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

@ -191,13 +191,14 @@ async function list (ctx) {
const { database: pepEmis } = clickHouse.pepEmis.opts.config const { database: pepEmis } = clickHouse.pepEmis.opts.config
const { keywordTarget, keyword, limit, page, state } = ctx.query 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 = [] let whereOption = []
if (state == 'dimission') { let whereFromSelectOption = []
whereOption.push(`member.dimission_date IS NOT null`)
}
if (state == 'inOffice') { if (state == 'inOffice') {
const curDay = moment().format('YYYY-MM-DD') // 在岗
const holidayJudge = await judgeHoliday(curDay) const holidayJudge = await judgeHoliday(curDay)
if (holidayJudge) { if (holidayJudge) {
if ( if (
@ -205,8 +206,29 @@ async function list (ctx) {
&& nowTime.isAfter(moment(curDay + ' 08:30')) && nowTime.isAfter(moment(curDay + ' 08:30'))
&& nowTime.isBefore(moment(curDay + ' 17: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 { } else {
ctx.body = [] ctx.body = []
@ -215,17 +237,84 @@ async function list (ctx) {
} }
const userRes = await clickHouse.hr.query(` const userRes = await clickHouse.hr.query(`
SELECT SELECT
member.pep_user_id AS pepUserId, hrMember."member.pep_user_id" AS pepUserId,
member.*, hrMember.*,
user.name AS userName, user.name AS userName,
role.name AS roleName, role.name AS roleName,
role.id AS roleId, role.id AS roleId,
department.name AS depName, department.name AS depName,
department.id AS depId, department.id AS depId
vacate.id AS vacateId FROM (
FROM member SELECT
LEFT JOIN ${pepEmis}.user AS user 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 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 LEFT JOIN ${pepEmis}.user_role AS user_role
ON ${pepEmis}.user_role.user = user.id ON ${pepEmis}.user_role.user = user.id
LEFT JOIN ${pepEmis}.role AS role LEFT JOIN ${pepEmis}.role AS role
@ -234,21 +323,11 @@ async function list (ctx) {
ON department_user.user = user.id ON department_user.user = user.id
LEFT JOIN ${pepEmis}.department AS department LEFT JOIN ${pepEmis}.department AS department
ON department.id = department_user.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 ')}` : ''} ${whereOption.length ? `WHERE ${whereOption.join(' AND ')}` : ''}
${/** limit 错啦*/''}
${limit ? `LIMIT ${limit}` : ''}
${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''}
`).toPromise() `).toPromise()
let returnD = [] let returnD = []
userRes.forEach(u => { userRes.forEach(u => {
if (u.overtimeId) {
let a = 1
}
let existUser = returnD.find(r => r.pepUserId == u.pepUserId) let existUser = returnD.find(r => r.pepUserId == u.pepUserId)
if (existUser) { if (existUser) {
if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) { if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) {
@ -266,20 +345,17 @@ async function list (ctx) {
} else { } else {
let obj = {} let obj = {}
for (let k in u) { for (let k in u) {
if (k.indexOf('member.') >= 0) { obj[
obj[ k.replace('hrMember.', '')
k.replace('member.', '') .replace('member.', '')
// 变为小驼峰 // 变为小驼峰
.toLowerCase() .toLowerCase()
.replace( .replace(
/(_)[a-z]/g, /(_)[a-z]/g,
(L) => L.toUpperCase() (L) => L.toUpperCase()
) )
.replace(/_/g, '') .replace(/_/g, '')
] = u[k] ] = u[k]
} else {
obj[k] = u[k]
}
} }
returnD.push({ returnD.push({
...obj, ...obj,
@ -292,11 +368,12 @@ async function list (ctx) {
name: u.roleName name: u.roleName
}] : [], }] : [],
del: undefined, del: undefined,
pepuserid: undefined
}) })
} }
}) })
ctx.status = 200;
ctx.body = returnD ctx.body = returnD
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.fs.logger.error(`path: ${ctx.path}, error: error`);

Loading…
Cancel
Save