政务数据资源中心(Government data Resource center) 03专项3期主要建设内容
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
12 KiB

'use strict';
const moment = require('moment')
function getStandardDocFolders (opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
2 years ago
const { keyword, parent } = ctx.query;
let errMsg = { message: '获取标准文档目录列表失败' }
try {
let option = {
where: { parent: parent || null },
2 years ago
order: [["id", "desc"]]
}
2 years ago
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
}
}
}
2 years ago
function postStandardDocFolders (opts) {
return async function (ctx, next) {
2 years ago
let message = '标准文档目录新增失败'
try {
const models = ctx.fs.dc.models;
const { name, parent } = ctx.request.body;
2 years ago
let findOne = await models.StandardDocFolder.findOne({ where: { name, parent: parent || null } })
if (findOne) {
message = '文件夹名重复'
throw ''
}
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;
2 years ago
ctx.body = { message: message }
}
}
}
// 新增标准文档
function postStandardDocs (opts) {
return async function (ctx, next) {
2 years ago
let message = '新增标准文档失败'
try {
const models = ctx.fs.dc.models;
const { docName, standardType, tags, folder, path } = ctx.request.body;
2 years ago
let findOne = await models.StandardDoc.findOne({ where: { docName, folder: folder || null } })
if (findOne) {
message = '同一目录下文件名重复'
throw ''
}
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;
2 years ago
ctx.body = { message: 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;
2 years ago
let findOne = await models.BusinessRule.findOne({ where: { name: name } })
if ((!id && findOne) || (id && findOne && findOne.id != id)) {
message = '业务规则名称重复'
throw ''
}
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
}
}
}
2 years ago
function fetchFiles (opts) {
return async function (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { folderId } = ctx.request.body;
let fileAll = await models.StandardDoc.findAll({})
let folderAll = await models.StandardDocFolder.findAll({})
const recursive = (id) => {
let levelId = []
folderAll.map(d => {
if (id.includes(d.parent)) {
levelId.push(d.id)
folderId.push(d.id)
}
})
if (levelId.length > 0) {
recursive(levelId)
}
}
recursive(folderId)
let res = fileAll.filter(s => folderId.includes(s.folder)).map(x => ({ name: x.docName, url: x.path }))
ctx.status = 200;
ctx.body = res;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '获取文件夹下文件失败' }
}
}
}
module.exports = {
getStandardDocFolders,
postStandardDocFolders,
postStandardDocs,
getStandardDocs,
postBusinessRules,
getBusinessRules,
delBusinessRules,
getRegularBasis,
2 years ago
postFolderFile,
fetchFiles
}