Browse Source

培训资料库:左侧分类查询

master
周沫沫历险记 2 years ago
parent
commit
6492daac6b
  1. 105
      api/app/lib/controllers/resourceRepository/index.js
  2. 6
      api/app/lib/models/training_information_level.js
  3. 9
      api/app/lib/routes/resourceRepository/index.js

105
api/app/lib/controllers/resourceRepository/index.js

@ -0,0 +1,105 @@
'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 = { plain: false, group: ["type", "id"] }
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", operation: true,
}, { label: "部门培训资料", value: 'dept', operation: false }];
for (let level of trainingInformationLevel) {
const { id, type, departmentname, traindate } = level;
/**type:1级, departmentname:2级,traindate:3级 */
const arrIndex = rlst.findIndex(item => item.label == type)
if (arrIndex > -1) {//一级分类名称正确
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,
})
} else {
rlst[arrIndex].children[depIndex].children = [{
label: moment(traindate).format('YYYY-MM'),
value: id,
key: id
}]
}
} else {
rlst[arrIndex].children.push({
label: departmentname,
children: {
label: moment(traindate).format('YYYY-MM'),
value: id,
key: id
}
});
}
} else {
rlst[arrIndex].children = [{
label: departmentname,
children: [{
label: moment(traindate).format('YYYY-MM'),
value: id,
key: id,
}]
}]
}
} 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;
}
} else {
rlst[arrIndex].children.push({
label: departmentname,
value: id,
key: id
})
}
} else {
rlst[arrIndex].children = [{
label: departmentname,
value: id,
key: id
}]
}
}
}
}
}
}
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
}

6
api/app/lib/models/training_information_level.js

@ -28,16 +28,16 @@ module.exports = dc => {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: '部门(2级目录)',
primaryKey: false, primaryKey: false,
field: "departmentname", field: "departmentname",
autoIncrement: false autoIncrement: false
}, },
traindate: { traindate: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, allowNull: true,
defaultValue: null, defaultValue: null,
comment: null, comment: '培训时间(3级目录)',
primaryKey: false, primaryKey: false,
field: "traindate", field: "traindate",
autoIncrement: false autoIncrement: false

9
api/app/lib/routes/resourceRepository/index.js

@ -0,0 +1,9 @@
'use strict';
/**培训资料库 */
const resourceRepository = require('../../controllers/resourceRepository');
module.exports = function (app, router, opts) {
app.fs.api.logAttr['GET/train/trainFiles/resourceRepository/classify'] = { content: '查询培训资源储备库分类目录', visible: false };
router.get('/train/trainFiles/resourceRepository/classify', resourceRepository.getResourceClassify);
};
Loading…
Cancel
Save