100 lines
3.8 KiB
JavaScript
100 lines
3.8 KiB
JavaScript
import express from 'express'
|
|
import { orgs, nextOrgId, buildOrgTree, collectDescendantIds } from '../data/orgs.js'
|
|
import { users } from '../data/users.js'
|
|
import { requireAuth } from '../middleware/requireAuth.js'
|
|
|
|
const router = express.Router()
|
|
router.use(requireAuth)
|
|
|
|
// ---------------- 机构管理 ----------------
|
|
|
|
router.get('/org/list', (req, res) => {
|
|
const { name, id } = req.query || {}
|
|
let list = orgs
|
|
if (name || id) {
|
|
const matched = orgs.filter(
|
|
(o) => (name && o.name.includes(name)) || (id && o.id.includes(id))
|
|
)
|
|
const idSet = new Set()
|
|
matched.forEach((m) => {
|
|
let cur = m
|
|
while (cur) {
|
|
idSet.add(cur.id)
|
|
cur = orgs.find((o) => o.id === cur.parentId)
|
|
}
|
|
})
|
|
list = orgs.filter((o) => idSet.has(o.id))
|
|
}
|
|
res.json({ code: 200, msg: 'ok', data: buildOrgTree(list) })
|
|
})
|
|
|
|
router.post('/org', (req, res) => {
|
|
const { name, parentId, remark } = req.body || {}
|
|
if (!name) return res.json({ code: 40004, msg: '机构名称不能为空', data: null })
|
|
if (!parentId) return res.json({ code: 40004, msg: '请选择上级机构', data: null })
|
|
const parent = orgs.find((o) => o.id === parentId)
|
|
if (!parent) return res.json({ code: 40004, msg: '上级机构不存在', data: null })
|
|
const duplicate = orgs.some((o) => o.parentId === parentId && o.name === name)
|
|
if (duplicate) return res.json({ code: 40005, msg: '同一上级机构下机构名称不可重复', data: null })
|
|
|
|
const org = {
|
|
id: nextOrgId(),
|
|
name,
|
|
parentId,
|
|
level: parent.level + 1,
|
|
remark: remark || '',
|
|
builtin: false,
|
|
createdAt: new Date().toISOString().slice(0, 19).replace('T', ' ')
|
|
}
|
|
orgs.push(org)
|
|
res.json({ code: 200, msg: 'ok', data: org })
|
|
})
|
|
|
|
router.put('/org/:id', (req, res) => {
|
|
const { id } = req.params
|
|
const { name, parentId, remark } = req.body || {}
|
|
const org = orgs.find((o) => o.id === id)
|
|
if (!org) return res.json({ code: 40404, msg: '机构不存在', data: null })
|
|
if (!name) return res.json({ code: 40004, msg: '机构名称不能为空', data: null })
|
|
|
|
let nextParentId = org.parentId
|
|
let level = org.level
|
|
if (org.parentId !== null) {
|
|
if (!parentId) return res.json({ code: 40004, msg: '请选择上级机构', data: null })
|
|
if (parentId === id) return res.json({ code: 40006, msg: '上级机构不可选择自身', data: null })
|
|
const descendantIds = collectDescendantIds(orgs, id)
|
|
if (descendantIds.includes(parentId)) {
|
|
return res.json({ code: 40006, msg: '上级机构不可选择自身下级', data: null })
|
|
}
|
|
const parent = orgs.find((o) => o.id === parentId)
|
|
if (!parent) return res.json({ code: 40004, msg: '上级机构不存在', data: null })
|
|
nextParentId = parentId
|
|
level = parent.level + 1
|
|
}
|
|
|
|
const duplicate = orgs.some((o) => o.id !== id && o.parentId === nextParentId && o.name === name)
|
|
if (duplicate) return res.json({ code: 40005, msg: '同一上级机构下机构名称不可重复', data: null })
|
|
|
|
org.name = name
|
|
org.parentId = nextParentId
|
|
org.level = level
|
|
org.remark = remark || ''
|
|
res.json({ code: 200, msg: 'ok', data: org })
|
|
})
|
|
|
|
router.delete('/org/:id', (req, res) => {
|
|
const { id } = req.params
|
|
const index = orgs.findIndex((o) => o.id === id)
|
|
if (index === -1) return res.json({ code: 40404, msg: '机构不存在', data: null })
|
|
const org = orgs[index]
|
|
if (org.builtin) return res.json({ code: 40007, msg: '内置一级机构不可删除', data: null })
|
|
const hasChildren = orgs.some((o) => o.parentId === id)
|
|
if (hasChildren) return res.json({ code: 40008, msg: '该机构存在下级机构,不可删除', data: null })
|
|
const hasUsers = users.some((u) => u.orgId === id)
|
|
if (hasUsers) return res.json({ code: 40009, msg: '该机构已关联用户,不可删除', data: null })
|
|
orgs.splice(index, 1)
|
|
res.json({ code: 200, msg: 'ok', data: null })
|
|
})
|
|
|
|
export default router
|