人力资源
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
8.6 KiB

'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,
department.name AS depName
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) {
existUser.departmrnt.push({
name: u.depName
})
existUser.role.push({
name: u.roleName
})
} else {
returnD.push({
pepUserId: u.pepUserId,
name: u.userName,
departmrnt: [{
name: u.depName
}],
role: [{
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 { clickHouse } = ctx.app.fs
const { database: pepEmis } = clickHouse.pepEmis.opts.config
const { keywordTarget, keyword, limit, page, state } = ctx.query
let whereOption = []
if (state == 'dimission') {
whereOption.push(`member.dimission_date IS NOT null`)
}
const userRes = await clickHouse.hr.query(`
SELECT
member.pep_user_id AS pepUserId,
member.*,
user.name AS userName,
role.name AS roleName,
role.id AS roleId,
department.name AS depName,
department.id AS depId
FROM member
LEFT JOIN ${pepEmis}.user AS user
ON 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
${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 && 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)) {
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) {
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]
}
}
returnD.push({
...obj,
departmrnt: u.depId ? [{
id: u.depId,
name: u.depName
}] : [],
role: u.roleId ? [{
id: u.roleId,
name: u.roleName
}] : [],
del: undefined,
})
}
})
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
}
}
}
module.exports = {
add,
edit,
del,
searchPepMember,
list,
};