'use strict'; const moment = require('moment') async function getResourceClassify(ctx) { try { const { models } = ctx.fs.dc; let rlst = []; rlst = [{ label: "公司培训资料", value: "company", key: "公司培训资料", operation: false, children: [] }, { label: "部门培训资料", value: 'dept', key: '部门培训资料', operation: false, children: [] }]; const findObj = { order: [["departmentName", "asc"]] } const filterData = (arrayData, arrIndex, operation) => { if (arrayData.length) { for (let level of arrayData) { const { departmentName, trainDate } = level.dataValues; /** departmentName:2级,trainDate:3级 */ if (departmentName) { if (trainDate) { const trainDateLabel = 'object' == typeof trainDate ? moment(trainDate).format('YYYY-MM') : trainDate; const depKey = rlst[arrIndex].label + '/' + departmentName //区分2级是否记录 if (rlst[arrIndex].children) { const depIndex = rlst[arrIndex].children.findIndex(r => r.key == depKey); if (depIndex > -1) { const dateKey = rlst[arrIndex].label + '/' + rlst[arrIndex].children[depIndex].label + '/' + trainDateLabel; if (rlst[arrIndex].children[depIndex].children) { const dateIndex = rlst[arrIndex].children[depIndex].children.findIndex(r => r.key == dateKey); if (dateIndex == -1) { rlst[arrIndex].children[depIndex].children.push({ label: trainDateLabel, value: dateKey, key: dateKey, operation }); } } else { rlst[arrIndex].children[depIndex].children = [{ label: trainDateLabel, value: dateKey, key: dateKey, operation }] } } else { rlst[arrIndex].children.push({ label: departmentName, value: depKey, key: depKey, operation, children: [{ label: trainDateLabel, value: depKey + '/' + trainDateLabel, key: depKey + '/' + trainDateLabel, operation }] }); } } else { rlst[arrIndex].children = [{ label: departmentName, value: depKey, key: depKey, operation, children: [{ label: trainDateLabel, value: depKey + '/' + trainDateLabel, key: depKey + '/' + trainDateLabel, operation }] }] } } else { //只有2级目录的情况 if (rlst[arrIndex].children) { const depIndex = rlst[arrIndex].children.findIndex(r => r.key == rlst[arrIndex].label + '/' + departmentName); if (depIndex == -1) { rlst[arrIndex].children.push({ label: departmentName, value: rlst[arrIndex].label + '/' + departmentName, key: rlst[arrIndex].label + '/' + departmentName, operation, children: [] }) } } else { rlst[arrIndex].children = [{ value: rlst[arrIndex].label + '/' + departmentName, key: rlst[arrIndex].label + '/' + departmentName, operation, children: [] }] } } } } } } const deptTraining = await models.DeptTraining.findAll(findObj); if (deptTraining.length) { filterData(deptTraining, 1, false); } const trainingInformation = await models.TrainingInformation.findAll(findObj); if (trainingInformation.length) { filterData(trainingInformation, 0, true); } ctx.status = 200; ctx.body = rlst; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '查询培训资源储备库分类目录失败' } } } /**查询培训资源储备库文件列表 * query * limit、offset * type : 文件分类,必填,{公司培训资料/部门培训资料} * departmentName:二级目录 * trainDate:三级目录 */ async function getResourceFileList(ctx, next) { try { const { models } = ctx.fs.dc; const { limit, page, type, departmentName, trainDate, startTime, endTime, keyword } = ctx.query; let rlst = []; if (["公司培训资料", "部门培训资料"].includes(type)) { const findObj = { attributes: ["id", "fileType", "fileName", "fileSize", "updateDate", "attachPath"], order: [["updateDate", "desc"]] }; if (Number(limit) > 0 && Number(page) >= 0) { findObj.limit = Number(limit); findObj.offset = Number(page) * Number(limit); } const where = { fileName: { $not: null } }; if (departmentName) { where.departmentName = departmentName; } if (startTime && endTime) { where.updateDate = { $between: [startTime, endTime] }; } if (keyword) { where.fileName = { $like: `%${keyword}%` }; } findObj.where = where; if ("公司培训资料" == type) { if (trainDate) { findObj.where.trainDate = trainDate; } rlst = await models.TrainingInformation.findAndCountAll(findObj); } else { if (trainDate) { findObj.where.trainDate = { $between: [moment(trainDate).startOf('month').format('YYYY-MM-DD HH:mm:ss'), moment(trainDate).endOf('month').format('YYYY-MM-DD HH:mm:ss')] }; } rlst = await models.DeptTraining.findAndCountAll(findObj); } } else { ctx.throw("培训资料范围为公司或部门"); } ctx.status = 200; ctx.body = rlst; } catch (err) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: err.message || '查询培训资源储备库文件列表失败' } } } async function postResourceClassify(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const { models } = ctx.fs.dc; const { departmentName, trainDate } = ctx.request.body; if (departmentName && '' != departmentName) { const where = { departmentName: departmentName }; if (trainDate && '' != trainDate) { where.trainDate = trainDate; } const oldData = await models.TrainingInformation.findOne({ where }); if (oldData) { ctx.throw("该文件夹已存在"); } else { await models.TrainingInformation.create({ departmentName: departmentName, trainDate: trainDate }, { transaction }) } } await transaction.commit(); ctx.status = 204; } catch (err) { await transaction.rollback(); ctx.status = 400; ctx.body = { message: err.message || '新增培训资源储备库文件夹失败' } } } async function putResourceClassify(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const { models } = ctx.fs.dc; const { oldDepName, oldTrainDate, departmentName, trainDate } = ctx.request.body; if (departmentName && '' != departmentName && oldDepName && '' != oldDepName) { const where = { departmentName: oldDepName }; if (trainDate && '' != trainDate && oldTrainDate && '' != oldTrainDate) { where.trainDate = oldTrainDate; await models.TrainingInformation.update({ trainDate: trainDate }, { where, transaction }); //三级目录 } else { //二级目录 await models.TrainingInformation.update({ departmentName: departmentName }, { where, transaction }); } } await transaction.commit(); ctx.status = 204; } catch (err) { await transaction.rollback(); ctx.status = 400; ctx.body = { message: err.message || '编辑培训资源储备库文件夹失败' }; } } async function delResourceClassify(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const { departmentName, trainDate } = ctx.query; const { models } = ctx.fs.dc; if (departmentName && '' != departmentName) { const where = { departmentName }; if (trainDate && '' != trainDate) { where.trainDate = trainDate; } await models.TrainingInformation.destroy({ where, transaction }); } await transaction.commit(); ctx.status = 204; } catch (err) { await transaction.rollback(); ctx.status = 400; ctx.body = { message: err.message || '新增培训资源储备库文件夹失败' } } } //新增公司培训资料文件 async function postResourceFile(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const { fileList, currentSelect } = ctx.request.body; const { models } = ctx.fs.dc; const files = fileList && fileList.length && fileList.filter(f => f.fileName && f.fileType && f.fileSize && f.attachPath); const splitArr = currentSelect.split("/"); if (files.length && splitArr.length == 3 && "公司培训资料" == splitArr[0]) { const dataToSave = files.map(r => { return { ...r, departmentName: splitArr[1], trainDate: splitArr[2], updateDate: moment() } }); await models.TrainingInformation.bulkCreate(dataToSave, { transaction }); } else { ctx.throw("新增公司培训资料文件内容不合法,请检查"); } await transaction.commit(); ctx.status = 204; } catch (err) { await transaction.rollback(); ctx.status = 400; ctx.body = { message: err.message || '新增公司培训资料文件失败' } } } //删除公司培训资料文件 async function delResourceFile(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const { id } = ctx.params; const { models } = ctx.fs.dc; const old = await models.TrainingInformation.findOne({ where: { id: id } }); if (old) { await models.TrainingInformation.destroy({ where: { id: id }, transaction }); } await transaction.commit(); ctx.status = 204; } catch (err) { await transaction.rollback(); ctx.status = 400; ctx.body = { message: err.message || '删除培训资源储备库文件夹失败' } } } module.exports = { getResourceClassify, getResourceFileList, postResourceClassify, putResourceClassify, delResourceClassify, postResourceFile, delResourceFile }