ww664853070 1 year ago
parent
commit
8678a0e5c5
  1. 708
      api/app/lib/controllers/attendance/index.js
  2. 18
      api/app/lib/controllers/departmentTrain/index.js
  3. 32
      api/app/lib/controllers/employeeCommunicate/index.js
  4. 17
      api/app/lib/controllers/personalTrain/index.js
  5. 201
      api/app/lib/controllers/userRole/index.js
  6. 8
      api/app/lib/models/role_resource.js
  7. 11
      api/app/lib/routes/userRole/index.js

708
api/app/lib/controllers/attendance/index.js

@ -1,371 +1,403 @@
'use strict'; 'use strict';
const moment = require('moment') const moment = require('moment')
const fs = require('fs'); const fs = require('fs');
const { getDataRange } = require('../auth/index')
async function overtimeStatistic(ctx) { async function overtimeStatistic(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { clickHouse } = ctx.app.fs const { clickHouse } = ctx.app.fs
const { memberList, packageUserData, overtimeStatisticListDayType } = ctx.app.fs.utils const { memberList, packageUserData, overtimeStatisticListDayType } = ctx.app.fs.utils
const { const {
keywordTarget, keyword, limit, page, orderBy, orderDirection, keywordTarget, keyword, limit, page, orderBy, orderDirection,
startDate, endDate, startDate, endDate,
} = ctx.query } = ctx.query
const userRes = await memberList({ let dataRange = await getDataRange(ctx);
keywordTarget, keyword, limit, page, if (dataRange.userIds && dataRange.userIds.length === 0) {
orderBy, orderDirection, ctx.status = 200;
overtimeDayStatisticStartDate: startDate, ctx.body = {
overtimeDayStatisticendDate: endDate, count: 0,
overtimeCountStatistic: true, rows: []
overtimeCountStatisticStartDate: startDate, }
overtimeCountStatisticendDate: endDate, } else {
}) const userRes = await memberList({
keywordTarget, keyword, limit, page,
let { packageUser: returnD, pepUserIds } = await packageUserData(userRes) orderBy, orderDirection,
overtimeDayStatisticStartDate: startDate,
const sumRes = await overtimeStatisticListDayType({ overtimeDayStatisticendDate: endDate,
startDate, endDate, pepUserIds overtimeCountStatistic: true,
}) overtimeCountStatisticStartDate: startDate,
overtimeCountStatisticendDate: endDate,
returnD.forEach(u => { userIds: dataRange.userIds
let overtimeStatistic = sumRes.filter(s => s.pepUserId == u.pepUserId) })
u.overtimeDuration = overtimeStatistic.reduce((sum, os) => sum + os.duration, 0)
u.overtimeStatistic = overtimeStatistic let { packageUser: returnD, pepUserIds } = await packageUserData(userRes)
})
ctx.status = 200; const sumRes = await overtimeStatisticListDayType({
ctx.body = { startDate, endDate, pepUserIds
count: userRes.count, })
rows: returnD
} returnD.forEach(u => {
} catch (error) { let overtimeStatistic = sumRes.filter(s => s.pepUserId == u.pepUserId)
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); u.overtimeDuration = overtimeStatistic.reduce((sum, os) => sum + os.duration, 0)
ctx.status = 400; u.overtimeStatistic = overtimeStatistic
ctx.body = { })
message: typeof error == 'string' ? error : undefined ctx.status = 200;
} ctx.body = {
} count: userRes.count,
rows: 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 exportOvertimeStatistic(ctx) { async function exportOvertimeStatistic(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { clickHouse } = ctx.app.fs const { clickHouse } = ctx.app.fs
const { memberList, packageUserData, overtimeStatisticListDayType, simpleExcelDown, dayType, overtimeType } = ctx.app.fs.utils const { memberList, packageUserData, overtimeStatisticListDayType, simpleExcelDown, dayType, overtimeType } = ctx.app.fs.utils
const { const {
keywordTarget, keyword, limit, page, orderBy, orderDirection, keywordTarget, keyword, limit, page, orderBy, orderDirection,
startDate, endDate, startDate, endDate,
} = ctx.query } = ctx.query
let returnD = null;
const userRes = await memberList({ let dataRange = await getDataRange(ctx);
keywordTarget, keyword, limit, page, if (dataRange.userIds && dataRange.userIds.length === 0) {
orderBy, orderDirection, returnD = [];
overtimeDayStatisticStartDate: startDate, } else {
overtimeDayStatisticendDate: endDate, const userRes = await memberList({
overtimeCountStatistic: true, keywordTarget, keyword, limit, page,
overtimeCountStatisticStartDate: startDate, orderBy, orderDirection,
overtimeCountStatisticendDate: endDate, overtimeDayStatisticStartDate: startDate,
}) overtimeDayStatisticendDate: endDate,
overtimeCountStatistic: true,
let { packageUser: returnD, pepUserIds } = await packageUserData(userRes) overtimeCountStatisticStartDate: startDate,
overtimeCountStatisticendDate: endDate,
const sumRes = await overtimeStatisticListDayType({ userIds: dataRange.userIds
startDate, endDate, pepUserIds })
})
let { packageUser, pepUserIds } = await packageUserData(userRes)
returnD.forEach(d => { returnD = packageUser;
d.departmrnt = d.departmrnt.map(dep => dep.name).join('、') const sumRes = await overtimeStatisticListDayType({
d.role = d.role.map(r => r.name).join('、') startDate, endDate, pepUserIds
let overtimeStatistic = sumRes.filter(s => s.pepUserId == d.pepUserId)
let totalDuration = 0
for (let dayTypeKey in dayType) {
for (let overtimeTypeKey in overtimeType) {
let corOverTimeSta = overtimeStatistic.find(o => o.compensate == overtimeTypeKey && o.dayType == dayTypeKey)
if (corOverTimeSta) {
d[overtimeTypeKey + dayTypeKey] = (corOverTimeSta.duration / 3600)
// .toFixed(1)
totalDuration += corOverTimeSta.duration / 3600
}
}
}
for (let overtimeTypeKey in overtimeType) {
d['sum' + overtimeTypeKey] = overtimeStatistic.reduce((sum, o) => {
if (o.compensate == overtimeTypeKey) {
sum += o.duration / 3600
// totalDuration += o.duration / 3600
}
return sum
}, 0)
}
d.totalDuration = totalDuration
})
let overtimeTypeHeader = []
let sumOvertimeTypeHeader = []
for (let overtimeTypeKey in overtimeType) {
sumOvertimeTypeHeader.push({
title: `合计${overtimeType[overtimeTypeKey]}加班时长(小时)`,
key: 'sum' + overtimeTypeKey,
defaultValue: '0'
})
for (let dayTypeKey in dayType) {
overtimeTypeHeader.push({
title: dayType[dayTypeKey] + '-' + overtimeType[overtimeTypeKey],
key: overtimeTypeKey + dayTypeKey,
defaultValue: '0'
}) })
}
} returnD.forEach(d => {
d.departmrnt = d.departmrnt.map(dep => dep.name).join('、')
const header = [{ d.role = d.role.map(r => r.name).join('、')
title: '员工编号', let overtimeStatistic = sumRes.filter(s => s.pepUserId == d.pepUserId)
key: 'userCode', let totalDuration = 0
}, { for (let dayTypeKey in dayType) {
title: '姓名', for (let overtimeTypeKey in overtimeType) {
key: 'userName', let corOverTimeSta = overtimeStatistic.find(o => o.compensate == overtimeTypeKey && o.dayType == dayTypeKey)
}, { if (corOverTimeSta) {
title: '所属部门', d[overtimeTypeKey + dayTypeKey] = (corOverTimeSta.duration / 3600)
key: 'departmrnt', // .toFixed(1)
}, { totalDuration += corOverTimeSta.duration / 3600
title: '职位', }
key: 'role', }
}, { }
title: '加班次数(次)', for (let overtimeTypeKey in overtimeType) {
key: 'overtimeCount', d['sum' + overtimeTypeKey] = overtimeStatistic((sum, o) => {
},] if (o.compensate == overtimeTypeKey) {
.concat(overtimeTypeHeader) sum += o.duration / 3600
.concat(sumOvertimeTypeHeader) // totalDuration += o.duration / 3600
.concat([{ }
title: '合计加班时长(小时)', return sum
key: 'totalDuration', }, 0)
defaultValue: '0' }
}]) d.totalDuration = totalDuration
})
const fileName = `加班统计_${startDate ? moment(startDate).format('YYYY-MM-DD') : ''}${startDate && endDate ? '-' : ''}${endDate ? moment(endDate).format('YYYY-MM-DD') : ''}${startDate || endDate ? '_' : ''}${moment().format('YYYYMMDDHHmmss')}` + '.csv' }
const filePath = await simpleExcelDown({ data: returnD, header, fileName: fileName }) let overtimeTypeHeader = []
const fileData = fs.readFileSync(filePath); let sumOvertimeTypeHeader = []
for (let overtimeTypeKey in overtimeType) {
ctx.status = 200; sumOvertimeTypeHeader.push({
ctx.set('Content-Type', 'application/x-xls'); title: `合计${overtimeType[overtimeTypeKey]}加班时长(小时)`,
ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName)); key: 'sum' + overtimeTypeKey,
ctx.body = fileData; defaultValue: '0'
} catch (error) { })
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); for (let dayTypeKey in dayType) {
ctx.status = 400; overtimeTypeHeader.push({
ctx.body = { title: dayType[dayTypeKey] + '-' + overtimeType[overtimeTypeKey],
message: typeof error == 'string' ? error : undefined key: overtimeTypeKey + dayTypeKey,
} defaultValue: '0'
} })
}
}
const header = [{
title: '员工编号',
key: 'userCode',
}, {
title: '姓名',
key: 'userName',
}, {
title: '所属部门',
key: 'departmrnt',
}, {
title: '职位',
key: 'role',
}, {
title: '加班次数(次)',
key: 'overtimeCount',
},]
.concat(overtimeTypeHeader)
.concat(sumOvertimeTypeHeader)
.concat([{
title: '合计加班时长(小时)',
key: 'totalDuration',
defaultValue: '0'
}])
const fileName = `加班统计_${startDate ? moment(startDate).format('YYYY-MM-DD') : ''}${startDate && endDate ? '-' : ''}${endDate ? moment(endDate).format('YYYY-MM-DD') : ''}${startDate || endDate ? '_' : ''}${moment().format('YYYYMMDDHHmmss')}` + '.csv'
const filePath = await simpleExcelDown({ data: returnD, header, fileName: fileName })
const fileData = fs.readFileSync(filePath);
ctx.status = 200;
ctx.set('Content-Type', 'application/x-xls');
ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName));
ctx.body = fileData;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
} }
async function vacateType(ctx) { async function vacateType(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { clickHouse } = ctx.app.fs const { clickHouse } = ctx.app.fs
const vacateTypeRes = await clickHouse.hr.query(` const vacateTypeRes = await clickHouse.hr.query(`
SELECT type FROM vacate GROUP BY type ORDER BY type DESC SELECT type FROM vacate GROUP BY type ORDER BY type DESC
`).toPromise() `).toPromise()
ctx.status = 200; ctx.status = 200;
ctx.body = vacateTypeRes ctx.body = vacateTypeRes
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
ctx.status = 400; ctx.status = 400;
ctx.body = { ctx.body = {
message: typeof error == 'string' ? error : undefined message: typeof error == 'string' ? error : undefined
} }
} }
} }
async function vacateStatistic(ctx) { async function vacateStatistic(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { clickHouse } = ctx.app.fs const { clickHouse } = ctx.app.fs
const { judgeHoliday, memberList, packageUserData, vacateStatisticListDayType } = ctx.app.fs.utils const { judgeHoliday, memberList, packageUserData, vacateStatisticListDayType } = ctx.app.fs.utils
const { const {
keywordTarget, keyword, limit, page, orderBy, orderDirection, keywordTarget, keyword, limit, page, orderBy, orderDirection,
startDate, endDate, startDate, endDate,
} = ctx.query } = ctx.query
const userRes = await memberList({ let dataRange = await getDataRange(ctx);
keywordTarget, keyword, limit, page, if (dataRange.userIds && dataRange.userIds.length === 0) {
orderBy, orderDirection, ctx.status = 200;
vacateDayStatisticStartDate: startDate, ctx.body = {
vacateDayStatisticendDate: endDate, count: 0,
vacateDurationStatistic: true, rows: []
vacateCountStatistic: true, }
vacateCountStatisticStartDate: startDate, } else {
vacateCountStatisticendDate: endDate, const userRes = await memberList({
}) keywordTarget, keyword, limit, page,
orderBy, orderDirection,
let { packageUser: returnD, pepUserIds } = await packageUserData(userRes) vacateDayStatisticStartDate: startDate,
vacateDayStatisticendDate: endDate,
const sumRes = await vacateStatisticListDayType({ vacateDurationStatistic: true,
startDate, endDate, pepUserIds vacateCountStatistic: true,
}) vacateCountStatisticStartDate: startDate,
vacateCountStatisticendDate: endDate,
const remarkList = await models.VacateRemark.findAll({}); //查询备注 userIds: dataRange.userIds
})
returnD.forEach(u => {
let vacateStatistic = sumRes.filter(s => s.pepUserId == u.pepUserId) let { packageUser: returnD, pepUserIds } = await packageUserData(userRes)
let remarkData = remarkList.filter(e => e.pepUserId == u.pepUserId)
u.vacateDuration = vacateStatistic.reduce((sum, vs) => sum + vs.duration, 0) const sumRes = await vacateStatisticListDayType({
u.vacateStatistic = vacateStatistic startDate, endDate, pepUserIds
u.remark = remarkData.length ? remarkData[0].remark : null })
})
ctx.status = 200; const remarkList = await models.VacateRemark.findAll({}); //查询备注
ctx.body = {
count: userRes.count, returnD.forEach(u => {
rows: returnD let vacateStatistic = sumRes.filter(s => s.pepUserId == u.pepUserId)
} let remarkData = remarkList.filter(e => e.pepUserId == u.pepUserId)
} catch (error) { u.vacateDuration = vacateStatistic.reduce((sum, vs) => sum + vs.duration, 0)
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); u.vacateStatistic = vacateStatistic
ctx.status = 400; u.remark = remarkData.length ? remarkData[0].remark : null
ctx.body = { })
message: typeof error == 'string' ? error : undefined ctx.status = 200;
} ctx.body = {
} count: userRes.count,
rows: 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 exportVacateStatistic(ctx) { async function exportVacateStatistic(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { clickHouse } = ctx.app.fs const { clickHouse } = ctx.app.fs
const { memberList, packageUserData, vacateStatisticListDayType, overtimeStatisticListDayType, simpleExcelDown, dayType, overtimeType } = ctx.app.fs.utils const { memberList, packageUserData, vacateStatisticListDayType, overtimeStatisticListDayType, simpleExcelDown, dayType, overtimeType } = ctx.app.fs.utils
const { const {
keywordTarget, keyword, limit, page, orderBy, orderDirection, keywordTarget, keyword, limit, page, orderBy, orderDirection,
startDate, endDate, startDate, endDate,
} = ctx.query } = ctx.query
const vacateTypeRes = await clickHouse.hr.query(` const vacateTypeRes = await clickHouse.hr.query(`
SELECT type FROM vacate GROUP BY type ORDER BY type DESC SELECT type FROM vacate GROUP BY type ORDER BY type DESC
`).toPromise() `).toPromise()
let returnD = null;
let dataRange = await getDataRange(ctx);
if (dataRange.userIds && dataRange.userIds.length === 0) {
returnD = [];
} else {
const userRes = await memberList({
keywordTarget, keyword, limit, page,
orderBy, orderDirection,
vacateDayStatisticStartDate: startDate,
vacateDayStatisticendDate: endDate,
vacateDurationStatistic: true,
vacateCountStatistic: true,
vacateCountStatisticStartDate: startDate,
vacateCountStatisticendDate: endDate,
userIds: dataRange.userIds
})
const userRes = await memberList({ let { packageUser, pepUserIds } = await packageUserData(userRes)
keywordTarget, keyword, limit, page, returnD = packageUser;
orderBy, orderDirection, const sumRes = await vacateStatisticListDayType({
vacateDayStatisticStartDate: startDate, startDate, endDate, pepUserIds
vacateDayStatisticendDate: endDate, })
vacateDurationStatistic: true,
vacateCountStatistic: true, const remarkList = await models.VacateRemark.findAll({}); //查询备注
vacateCountStatisticStartDate: startDate,
vacateCountStatisticendDate: endDate, returnD.forEach(u => {
}) u.departmrnt = u.departmrnt.map(dep => dep.name).join('、')
u.role = u.role.map(r => r.name).join('、')
let { packageUser: returnD, pepUserIds } = await packageUserData(userRes)
let vacateStatistic = sumRes.filter(s => s.pepUserId == u.pepUserId)
const sumRes = await vacateStatisticListDayType({ for (let { type } of vacateTypeRes) {
startDate, endDate, pepUserIds u[type] = vacateStatistic.reduce((sum, v) => {
}) if (v.type == type) {
sum += v.duration / 3600
const remarkList = await models.VacateRemark.findAll({}); //查询备注 }
return sum
returnD.forEach(u => { }, 0)
u.departmrnt = u.departmrnt.map(dep => dep.name).join('、') }
u.role = u.role.map(r => r.name).join('、') u.vacateDayStatisticDuration = (u.vacateDayStatisticDuration || 0) / 3600
u.userActiveStatus = u.userActiveStatus == 1 ? '在职' : u.userActiveStatus == 2 ? '离职' : '特殊状态-特殊账号'
let vacateStatistic = sumRes.filter(s => s.pepUserId == u.pepUserId) let remarkData = remarkList.filter(e => e.pepUserId == u.pepUserId)
for (let { type } of vacateTypeRes) { u.remark = remarkData.length ? (remarkData[0].remark == '' ? '无' : remarkData[0].remark) : '无'
u[type] = vacateStatistic.reduce((sum, v) => { })
if (v.type == type) { }
sum += v.duration / 3600 const header = [{
} title: '员工编号',
return sum key: 'userCode',
}, 0) }, {
} title: '姓名',
u.vacateDayStatisticDuration = (u.vacateDayStatisticDuration || 0) / 3600 key: 'userName',
u.userActiveStatus = u.userActiveStatus == 1 ? '在职' : u.userActiveStatus == 2 ? '离职' : '特殊状态-特殊账号' }, {
let remarkData = remarkList.filter(e => e.pepUserId == u.pepUserId) title: '所属部门',
u.remark = remarkData.length ? (remarkData[0].remark == '' ? '无' : remarkData[0].remark) : '无' key: 'departmrnt',
}) }, {
title: '职位',
const header = [{ key: 'role',
title: '员工编号', }, {
key: 'userCode', title: '在职状态',
}, { key: 'userActiveStatus',
title: '姓名', },]
key: 'userName', .concat(vacateTypeRes.map(v => {
}, { return {
title: '所属部门', title: `${v.type}请假时长(小时)`,
key: 'departmrnt', key: v.type,
}, { defaultValue: '0',
title: '职位', }
key: 'role', }))
}, { .concat(
title: '在职状态', [{
key: 'userActiveStatus', title: '合计请假次数(次)',
},] key: 'vacateCount',
.concat(vacateTypeRes.map(v => { defaultValue: '0',
return { }, {
title: `${v.type}请假时长(小时)`, title: '合计请假时长(小时)',
key: v.type, key: 'vacateDayStatisticDuration',
defaultValue: '0', defaultValue: '0',
} }]
})) )
.concat( .concat(
[{ [{
title: '合计请假次数(次)', title: '备注',
key: 'vacateCount', key: 'remark',
defaultValue: '0', }]
}, { )
title: '合计请假时长(小时)',
key: 'vacateDayStatisticDuration', const fileName = `请假统计_${startDate ? moment(startDate).format('YYYY-MM-DD') : ''}${startDate && endDate ? '-' : ''}${endDate ? moment(endDate).format('YYYY-MM-DD') : ''}${startDate || endDate ? '_' : ''}${moment().format('YYYYMMDDHHmmss')}` + '.csv'
defaultValue: '0', const filePath = await simpleExcelDown({ data: returnD, header, fileName: fileName })
}] const fileData = fs.readFileSync(filePath);
)
.concat( ctx.status = 200;
[{ ctx.set('Content-Type', 'application/x-xls');
title: '备注', ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName));
key: 'remark', ctx.body = fileData;
}] } catch (error) {
) ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
ctx.status = 400;
const fileName = `请假统计_${startDate ? moment(startDate).format('YYYY-MM-DD') : ''}${startDate && endDate ? '-' : ''}${endDate ? moment(endDate).format('YYYY-MM-DD') : ''}${startDate || endDate ? '_' : ''}${moment().format('YYYYMMDDHHmmss')}` + '.csv' ctx.body = {
const filePath = await simpleExcelDown({ data: returnD, header, fileName: fileName }) message: typeof error == 'string' ? error : undefined
const fileData = fs.readFileSync(filePath); }
}
ctx.status = 200;
ctx.set('Content-Type', 'application/x-xls');
ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName));
ctx.body = fileData;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
} }
// 添加请假统计备注 // 添加请假统计备注
async function vacateRemark(ctx) { async function vacateRemark(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { remark, pepUserId } = ctx.query; const { remark, pepUserId } = ctx.query;
let oldData = await models.VacateRemark.findOne({ where: { pepUserId: pepUserId } }); let oldData = await models.VacateRemark.findOne({ where: { pepUserId: pepUserId } });
if (oldData) { if (oldData) {
await models.VacateRemark.update({ remark, pepUserId }, { where: { pepUserId: pepUserId } }); await models.VacateRemark.update({ remark, pepUserId }, { where: { pepUserId: pepUserId } });
} else { } else {
await models.VacateRemark.create({ remark, pepUserId }); await models.VacateRemark.create({ remark, pepUserId });
} }
ctx.status = 200; ctx.status = 200;
ctx.body = '添加备注成功'; ctx.body = '添加备注成功';
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;
ctx.body = { ctx.body = {
"message": "添加备注失败" "message": "添加备注失败"
} }
} }
} }
module.exports = { module.exports = {
overtimeStatistic, overtimeStatistic,
exportOvertimeStatistic, exportOvertimeStatistic,
vacateType, vacateType,
vacateStatistic, vacateStatistic,
exportVacateStatistic, exportVacateStatistic,
vacateRemark vacateRemark
}; };

18
api/app/lib/controllers/departmentTrain/index.js

@ -1,17 +1,25 @@
'use strict'; 'use strict';
const moment = require("moment/moment"); const moment = require("moment");
const { getDataRange } = require('../auth/index')
async function get(ctx) { async function get(ctx) {
try { try {
const { limit, page } = ctx.query; const { limit, page } = ctx.query;
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
let res = await models.DeptTraining.findAndCountAll({ const findObj = {
where: {}, where: {},
offset: Number(page) * Number(limit),
limit: Number(limit),
order: [['id', 'ASC']] order: [['id', 'ASC']]
}) };
let dataRange = await getDataRange(ctx);
if (dataRange.departmentNames) {
findObj.where.departmentName = { $in: dataRange.departmentNames || [] }
}
if (Number(limit) > 0 && Number(page) >= 0) {
findObj.limit = Number(limit);
findObj.offset = Number(page) * Number(limit);
}
let res = await models.DeptTraining.findAndCountAll(findObj)
ctx.status = 200; ctx.status = 200;
ctx.body = res; ctx.body = res;
} catch (error) { } catch (error) {

32
api/app/lib/controllers/employeeCommunicate/index.js

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('fs');
const moment = require('moment'); const moment = require('moment');
const { getDataRange } = require('../auth/index')
/** /**
* 查询员工沟通统计数据 * 查询员工沟通统计数据
* @param {*} ctx ctx ctx.query:{keywordTarget-关键字项keyword-关键字内容timeRange-沟通时间limit-页宽, page-页码} * @param {*} ctx ctx ctx.query:{keywordTarget-关键字项keyword-关键字内容timeRange-沟通时间limit-页宽, page-页码}
@ -9,19 +10,26 @@ async function get(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { keywordTarget, keyword, timeRange, limit, page } = ctx.query; const { keywordTarget, keyword, timeRange, limit, page } = ctx.query;
const where = {}; const findObj = {
where: {},
order: [['id', 'ASC']]
};
if (keywordTarget && keyword) { if (keywordTarget && keyword) {
where[keywordTarget] = { $iLike: `%${keyword}%` }; findObj.where[keywordTarget] = { $iLike: `%${keyword}%` };
} }
if (timeRange) { if (timeRange) {
where.communicateDate = { $between: timeRange.split(',') }; findObj.where.communicateDate = { $between: timeRange.split(',') };
} }
let employeeCommunicate = await models.EmployeeCommunicate.findAndCountAll({ let dataRange = await getDataRange(ctx);
where: where, //相较于用户所属部门,用户名称更少改动
offset: Number(page) * Number(limit), if (dataRange.userNames) {
limit: Number(limit), findObj.where.personalName = { $in: dataRange.userNames || [] }
order: [['id', 'ASC']] }
}); if (Number(limit) > 0 && Number(page) >= 0) {
findObj.limit = Number(limit);
findObj.offset = Number(page) * Number(limit);
}
let employeeCommunicate = await models.EmployeeCommunicate.findAndCountAll(findObj);
ctx.status = 200 ctx.status = 200
ctx.body = employeeCommunicate; ctx.body = employeeCommunicate;
} catch (error) { } catch (error) {
@ -55,7 +63,13 @@ async function exportData(ctx) {
header.push({ title: columnKeys[key], key: key }); header.push({ title: columnKeys[key], key: key });
attributes.push(key); attributes.push(key);
}) })
const where = {};
let dataRange = await getDataRange(ctx);
if (dataRange.userNames) {
where.personalName = { $in: dataRange.userNames || [] }
}
let exportData = await models.EmployeeCommunicate.findAll({ let exportData = await models.EmployeeCommunicate.findAll({
where: where,
attributes: attributes, attributes: attributes,
order: [['id', 'ASC']] order: [['id', 'ASC']]
}); });

17
api/app/lib/controllers/personalTrain/index.js

@ -1,14 +1,23 @@
'use strict'; 'use strict';
const { getDataRange } = require('../auth/index')
async function getPersonalTrainRecordList(ctx) { async function getPersonalTrainRecordList(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { limit, page } = ctx.query; const { limit, page } = ctx.query;
let personalTrainDetail = await models.PersonalTraining.findAndCountAll({ const findObj = {
offset: Number(page) * Number(limit), where: {},
limit: Number(limit),
order: [['id', 'ASC']] order: [['id', 'ASC']]
}); };
let dataRange = await getDataRange(ctx);
if (dataRange.userNames) {
findObj.where.personalName = { $in: dataRange.userNames || [] }
}
if (Number(limit) > 0 && Number(page) >= 0) {
findObj.limit = Number(limit);
findObj.offset = Number(page) * Number(limit);
}
let personalTrainDetail = await models.PersonalTraining.findAndCountAll(findObj);
ctx.status = 200 ctx.status = 200
ctx.body = personalTrainDetail; ctx.body = personalTrainDetail;
} catch (error) { } catch (error) {

201
api/app/lib/controllers/userRole/index.js

@ -3,97 +3,146 @@ const moment = require('moment')
const fs = require('fs'); const fs = require('fs');
async function get(ctx) { async function get(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
let userRoleList = await models.UserRole.findAndCountAll({ let userRoleList = await models.UserRole.findAndCountAll({
order: [['id', 'desc']] order: [['id', 'desc']]
}); });
ctx.status = 200 ctx.status = 200
ctx.body = userRoleList; ctx.body = userRoleList;
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path:${ctx.path},error:${error}`) ctx.fs.logger.error(`path:${ctx.path},error:${error}`)
ctx.status = 400; ctx.status = 400;
ctx.body = { name: 'FindError', message: '查询用户信息失败' } ctx.body = { name: 'FindError', message: '查询用户信息失败' }
} }
} }
async function add(ctx) { async function add(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { userId, roleId } = ctx.request.body const { userId, roleId } = ctx.request.body
await models.UserRole.destroy({ await models.UserRole.destroy({
where: { roleId: roleId } where: { roleId: roleId }
}) })
let storageData = userId.map(e => { let storageData = userId.map(e => {
return { return {
roleId: roleId, roleId: roleId,
userId: e userId: e
} }
}) })
await models.UserRole.bulkCreate(storageData); await models.UserRole.bulkCreate(storageData);
ctx.status = 204; ctx.status = 204;
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;
ctx.body = { ctx.body = {
message: typeof error == 'string' ? error : undefined message: typeof error == 'string' ? error : undefined
} }
} }
} }
async function edit(ctx) { async function edit(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
// const { pepUserId, provinces, cities, businessLines } = ctx.request.body // const { pepUserId, provinces, cities, businessLines } = ctx.request.body
const { id, name } = ctx.request.body const { id, name } = ctx.request.body
const role = await models.Role.findOne({ const role = await models.Role.findOne({
where: { id } where: { id }
}) })
if (!role) { if (!role) {
throw '当前角色不存在' throw '当前角色不存在'
} }
let storageData = { name } let storageData = { name }
await models.Role.update(storageData, { await models.Role.update(storageData, {
where: { id } where: { id }
}) })
ctx.status = 204; ctx.status = 204;
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;
ctx.body = { ctx.body = {
message: typeof error == 'string' ? error : undefined message: typeof error == 'string' ? error : undefined
} }
} }
} }
async function del(ctx) { async function del(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { id } = ctx.request.body const { id } = ctx.request.body
await models.SalesDistribution.update({ await models.SalesDistribution.update({
delete: false delete: false
}, },
{ {
where: { id } where: { id }
}) })
ctx.status = 204; ctx.status = 204;
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;
ctx.body = { ctx.body = {
message: typeof error == 'string' ? error : undefined message: typeof error == 'string' ? error : undefined
} }
} }
} }
async function getUserResources(ctx) {
try {
const { adminHr } = ctx.fs.api.userInfo;
const { models } = ctx.fs.dc;
const { userId } = ctx.params;
let codes = [];
//人资管理员-所有权限
if (adminHr.some(admin => admin.id == userId)) {
const resource = await models.Resource.findAll({
attributes: ['code'],
order: [['id', 'asc']]
})
codes = resource.map(r => r.code);
} else {
const userResources = await models.RoleResource.findAll({
attributes: ['id', 'resId'],
include: [{
required: true,
model: models.Role,
attributes: [],
include: [{
model: models.UserRole,
attributes: [],
where: { userId: userId }
}],
}, {
required: true,
model: models.Resource,
attributes: ['id', 'name', 'code'],
}],
order: [['id', 'asc']]
});
let rslt = [];
userResources && userResources.map(ur => {
if (!rslt.some(r => r.id === ur.resId)) {
rslt.push(ur.resource);
codes.push(ur.resource.code);
}
})
}
ctx.status = 200;
ctx.body = codes;
} catch (error) {
ctx.fs.logger.error(`path:${ctx.path},error:${error}`)
ctx.status = 400;
ctx.body = { name: 'FindError', message: '获取用户角色资源失败' }
}
}
module.exports = { module.exports = {
get, get,
add, add,
edit, edit,
del, del,
getUserResources
} }

8
api/app/lib/models/role_resource.js

@ -39,5 +39,13 @@ module.exports = dc => {
indexes: [] indexes: []
}); });
dc.models.RoleResource = RoleResource; dc.models.RoleResource = RoleResource;
const Role = dc.models.Role;
RoleResource.belongsTo(Role, { foreignKey: 'roleId', targetKey: 'id' });
Role.hasMany(RoleResource, { foreignKey: 'roleId', sourceKey: 'id' });
const Resource = dc.models.Resource;
RoleResource.belongsTo(Resource, { foreignKey: 'resId', targetKey: 'id' });
Resource.hasMany(RoleResource, { foreignKey: 'resId', sourceKey: 'id' });
return RoleResource; return RoleResource;
}; };

11
api/app/lib/routes/userRole/index.js

@ -4,11 +4,14 @@ const userRole = require('../../controllers/userRole');
module.exports = function (app, router, opts) { module.exports = function (app, router, opts) {
app.fs.api.logAttr['GET/roleUser/list'] = { content: '角色用户列表', visible: true }; app.fs.api.logAttr['GET/roleUser/list'] = { content: '角色用户列表', visible: true };
router.get('/roleUser/list', userRole.get); router.get('/roleUser/list', userRole.get);
app.fs.api.logAttr['POST/roleUser/add'] = { content: '角色添加用户成功', visible: true }; app.fs.api.logAttr['POST/roleUser/add'] = { content: '角色添加用户成功', visible: true };
router.post('/roleUser/add', userRole.add); router.post('/roleUser/add', userRole.add);
app.fs.api.logAttr['GET/user/:userId/role/resources'] = { content: '获取用户角色资源', visible: true };
router.get('/user/:userId/role/resources', userRole.getUserResources);
}; };
Loading…
Cancel
Save