'use strict'; const moment = require('moment') async function getResourceClassify(ctx) { try { const { models } = ctx.fs.dc; const { limit, offset } = ctx.query; let rlst = []; const findObj = {} if (Number(limit) > 0 && Number(offset) >= 0) { findObj.limit = Number(limit); findObj.offset = Number(offset); } const trainingInformationLevel = await models.TrainingInformationLevel.findAll(findObj); if (trainingInformationLevel.length) { rlst = [{ label: "公司培训资料", value: "company", key: "company", operation: true, }, { label: "部门培训资料", value: 'dept', key: 'dept', operation: false }]; for (let level of trainingInformationLevel) { const { id, type, departmentname, traindate, origin } = level; /**type:1级, departmentname:2级,traindate:3级 */ const arrIndex = rlst.findIndex(item => item.label == type) if (arrIndex > -1) {//一级分类名称正确 const operation = "import" == origin ? true : false; if (departmentname) { if (traindate) { //区分2级是否记录 if (rlst[arrIndex].children) { const depIndex = rlst[arrIndex].children.findIndex(r => r.label == departmentname); if (depIndex > -1) { if (rlst[arrIndex].children[depIndex].children) { rlst[arrIndex].children[depIndex].children.push({ label: moment(traindate).format('YYYY-MM'), value: id, key: id, operation }) } else { rlst[arrIndex].children[depIndex].children = [{ label: moment(traindate).format('YYYY-MM'), value: id, key: id, operation }] } } else { rlst[arrIndex].children.push({ label: departmentname, children: [{ label: moment(traindate).format('YYYY-MM'), value: id, key: id, operation }] }); } } else { rlst[arrIndex].children = [{ label: departmentname, children: [{ label: moment(traindate).format('YYYY-MM'), value: id, key: id, operation }] }] } } else { if (rlst[arrIndex].children) { const depIndex = rlst[arrIndex].children.findIndex(r => r.label == departmentname); if (depIndex > -1) { if (!rlst[arrIndex].children[depIndex].value) { rlst[arrIndex].children[depIndex].value = id; rlst[arrIndex].children[depIndex].key = id; rlst[arrIndex].children[depIndex].operation = operation; } } else { rlst[arrIndex].children.push({ label: departmentname, value: id, key: id, operation }) } } else { rlst[arrIndex].children = [{ label: departmentname, value: id, key: id, operation }] } } } } } } ctx.status = 200; ctx.body = rlst; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '查询培训资源储备库分类目录失败' } } } module.exports = { getResourceClassify }