'use strict'; const Hex = require('crypto-js/enc-hex'); const MD5 = require('crypto-js/md5'); const CryptoJS = require('crypto-js'); function getOrganizationList(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; const { page, limit, name } = ctx.query; const Op = ctx.fs.dc.ORM.Op; let errMsg = { message: '获取机构失败' } try { let searchWhere = { } let option = { where: searchWhere, order: [["id", "desc"]], attributes: { exclude: ['password'] }, } if (name) { searchWhere.name = { $like: '%' + name + '%' }; } option.where = searchWhere let limit_ = limit || 10; let page_ = page || 1; let offset = (page_ - 1) * limit_; if (limit && page) { option.limit = limit_ option.offset = offset } const res = await models.Organization.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 addOrganization(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; try { const { name, code } = ctx.request.body const checkName = await models.Organization.findOne({ where: { name } }); const checkCode = await models.Organization.findOne({ where: { code } }); if (checkName || checkCode) { ctx.status = 400; ctx.body = { message: "机构名称或代码不能重复" } } else { let rslt = ctx.request.body; await models.Organization.create(Object.assign({}, rslt)) ctx.status = 204; ctx.body = { message: '新建机构成功' } } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '新建机构失败' } } } } // 修改机构 function editOrganization(opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { id } = ctx.params; const body = ctx.request.body; const { name, code } = ctx.request.body const checkName = await models.Organization.findOne({ where: { id: { $not: id }, name } }); const checkCode = await models.Organization.findOne({ where: { id: { $not: id }, code } }); if (checkName || checkCode) { ctx.status = 400; ctx.body = { message: '机构名称或代码不能重复' } } else { await models.Organization.update( body, { 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 deleteOrganization(opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { id } = ctx.params; // const checkName1 = await models.MetadataDatabase.findOne({ where: { createBy: id } }); // const checkName2 = await models.MetadataFile.findOne({ where: { createBy: id } }); // const checkName3 = await models.MetadataRestapi.findOne({ where: { createBy: id } }); // if (checkName1 || checkName2 || checkName3) { // ctx.status = 400; // ctx.body = { message: '该机构下存在依赖资源无法删除!' } // } else { await models.Organization.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: '删除机构失败' } } } } module.exports = { getOrganizationList, addOrganization, editOrganization, deleteOrganization, }