'use strict'; const moment = require('moment') function getStandardDocFolders (opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; const { keyword, parent } = ctx.query; let errMsg = { message: '获取标准文档目录列表失败' } try { let option = { where: { parent: parent || null }, order: [["id", "desc"]] } let folderData = await models.StandardDocFolder.findAll(option) || [] let fileAll = await models.StandardDoc.findAll({}) let folderAll = await models.StandardDocFolder.findAll({}) const recursive = (id) => { let show = false let fileOne = fileAll.filter(f => f.folder == id && (f.docName.indexOf(keyword) != -1 || f.standardType.indexOf(keyword) != -1)) if (fileOne.length > 0) { return true } let folderList = folderAll.filter(f => f.parent == id) if (folderList.length > 0) { let data = [] folderList.map(d => { let datum = recursive(d.id) if (datum) { data.push(d) } }) if (data.length > 0) { show = true } } else { return false } return show } let res = [] if (folderData.length > 0 && keyword) { folderData.map(d => { let findOne = recursive(d.id) if (findOne) { res.push(d) } }) } else { res = folderData } ctx.status = 200; ctx.body = res; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } // 标准文档目录新增失败 function postStandardDocFolders (opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { name, parent } = ctx.request.body; await models.StandardDocFolder.create({ name, parent, createAt: moment().format('YYYY-MM-DD HH:mm:ss') }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '标准文档目录新增失败' } } } } // 新增标准文档 function postStandardDocs (opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { docName, standardType, tags, folder, path } = ctx.request.body; await models.StandardDoc.create({ docName, standardType, tags, folder, path, createAt: moment().format('YYYY-MM-DD HH:mm:ss') }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '新增标准文档失败' } } } } function getStandardDocs (opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; const { keyword, folder } = ctx.query; let errMsg = { message: '获取标准文档列表失败' } try { let option = { where: { folder: folder || null }, order: [["id", "desc"]], } let type = ['国家标准', '行业标准', '地方标准'] if (keyword) { option.where['$or'] = [{ docName: { $iLike: `%${keyword}%` } }, { standardType: { $in: type.filter(v => v.indexOf(keyword) != -1) } }] } const res = await models.StandardDoc.findAll(option); ctx.status = 200; ctx.body = res; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } function postBusinessRules (opts) { return async function (ctx, next) { let message = (ctx.request.body.id ? '编辑' : '新增') + '业务规则失败' try { const models = ctx.fs.dc.models; const { id, name, description, problemType, problemLevel, ruleBasis } = ctx.request.body; if (id) { await models.BusinessRule.update({ name, description, problemType, problemLevel, ruleBasis }, { where: { id: id } }) } else { await models.BusinessRule.create({ name, description, problemType, problemLevel, ruleBasis, createAt: moment().format('YYYY-MM-DD HH:mm:ss') }) } ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: message } } } } function getBusinessRules (opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; const { page, limit, keyword, } = ctx.query; let errMsg = { message: '查询业务规则列表失败' } try { let option = { where: {}, order: [["id", "desc"]], distinct: true, include: [{ model: models.StandardDoc, attributes: ['id', 'docName', 'path'] }] } if (keyword) { option.where.name = { $iLike: `%${keyword}%` } } if (limit) { option.limit = Number(limit) } if (page && limit) { option.offset = Number(page) * Number(limit) } const res = await models.BusinessRule.findAndCount(option); ctx.status = 200; ctx.body = res; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } function delBusinessRules (opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { id } = ctx.params; await models.BusinessRule.destroy({ where: { id: id } }) ctx.status = 204; ctx.body = { message: '删除业务规则成功' } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '删除业务规则失败' } } } } function getRegularBasis (opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; const { } = ctx.query; try { let folders = await models.StandardDocFolder.findAll({ attributes: ['id', 'name', 'parent'] }) || [] let files = await models.StandardDoc.findAll({ attributes: ['id', 'docName', 'folder'] }) || [] let res = [] let carousel = (id) => { let list = [] let data = folders.filter(f => f.parent == id) if (data.length > 0) { data.map(c => { list.push({ value: c.id, title: c.name, disabled: true, children: carousel(c.id) }) }) } let filedata = files.filter(f => f.folder == id) if (filedata.length > 0) { filedata.map(f => { list.push({ value: f.id, title: f.docName }) }) } return list } if (folders.length > 0) { folders.map(v => { if (v.dataValues.parent == null) { res.push({ value: v.id, title: v.name, disabled: true, children: carousel(v.id) }) } }) } if (files.length > 0) { files.map(v => { if (v.dataValues.folder == null) { res.push({ value: v.id, title: v.docName }) } }) } ctx.status = 200; ctx.body = res || []; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '查询规则依据列表失败' } } } } function postFolderFile (opts) { return async function (ctx, next) { let body = { message: '删除业务规则失败' } try { const models = ctx.fs.dc.models; const { folderId, fileId } = ctx.request.body; let folderIds = folderId let fileIds = fileId let folderLists = await models.StandardDocFolder.findAll() || [] let carousel = (id) => { let folderListId = [] folderLists.filter(d => { if (id.includes(d.parent)) { folderIds.push(d.id) folderListId.push(d.id) return true } else { return false } }) if (folderListId.length > 0) { carousel(folderListId) } } carousel(folderIds) folderIds = [...new Set(folderIds)] let fileList = await models.StandardDoc.findAll({ where: { $or: [{ folder: { $in: folderIds } }, { id: { $in: fileIds } }] }, distinct: true, include: [{ model: models.BusinessRule }] }) let url = [] fileList.map(v => { if (v.businessRules.length > 0) { body.data = v.businessRules body.message = '当前创建的业务规则与标准文档关联' throw '前创建的业务规则与标准文档关联' } fileIds.push(v.id) url.push(v.path) }) fileIds = [...new Set(fileIds)] await models.StandardDocFolder.destroy({ where: { id: { $in: folderIds } } }) await models.StandardDoc.destroy({ where: { id: { $in: fileIds } } }) ctx.status = 200; ctx.body = url } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = body } } } module.exports = { getStandardDocFolders, postStandardDocFolders, postStandardDocs, getStandardDocs, postBusinessRules, getBusinessRules, delBusinessRules, getRegularBasis, postFolderFile }