政务数据资源中心(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.
 
 
 
 

185 lines
4.5 KiB

'use strict';
const moment = require('moment')
//获取REST服务失败
function getServiceManagement(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
const { page, limit, keyword } = ctx.query;
try {
let option = {
where: {},
order: [["id", "desc"]],
include: [{
model: models.ResourceConsumption,
},
{
model: models.ResourceCatalog,
attributes: ['id', 'name'],
include: [{
model: models.Organization,
attributes: ['id', 'name'],
}]
}
],
distinct: true
}
if (keyword) {
option.where.name = { $iLike: `%${keyword}%` }
}
if (limit) {
option.limit = Number(limit)
}
if (page && limit) {
option.offset = Number(page) * Number(limit)
}
let res = await models.RestfulApi.findAndCount(option)
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '获取REST服务失败' }
}
}
}
//编辑REST服务
function postServiceManagement(opts) {
return async function (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { id, name, enabled } = ctx.request.body;
await models.RestfulApi.update({ name: name, enabled: enabled }, { where: { id: id } })
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '编辑REST服务失败' }
}
}
}
//编辑REST服务
function delServiceManagement(opts) {
return async function (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { id } = ctx.params;
await models.RestfulApi.destroy({
where: {
id: id
}
})
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '删除REST服务失败' }
}
}
}
//获取表字段
function getLookField(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
const { table, fields } = ctx.query;
try {
let option = {
where: {
code: table,
},
order: [["id", "desc"]],
include: [{
model: models.MetadataDatabase,
where: { code: fields, },
distinct: true
}],
distinct: true
}
let list = await models.MetadataDatabase.findOne(option)
let res = await models.MetadataDatabase.findAll({
where: {
type: '字段',
parent: list && list.metadataDatabases[0] && list.metadataDatabases[0].id
}
})
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '获取表字段失败' }
}
}
}
//获取rest服务申请次数
function getResourceConsumptionCount(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
let errMsg = { message: '获取接口资源申请次数失败' }
try {
const rslt = await models.ResourceConsumption.count({
attributes: [
'rest_service_id', // 分组的字段
],
group: ['rest_service_id'], // 分组的字段
where: {
resourceType: '数据服务'
}
});
const rests = await models.RestfulApi.findAll()
let arr = []
rslt.map(s => {
let rest = rests.find(x => x.id == s.rest_service_id)
if (rest) {
arr.push({
count: s.count,
name: rest.name
})
}
})
ctx.status = 200;
ctx.body = arr;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = errMsg
}
}
}
module.exports = {
getServiceManagement,
postServiceManagement,
delServiceManagement,
getLookField,
getResourceConsumptionCount
}