import express from 'express' import { orgs } from '../data/orgs.js' import { channels, nextChannelId } from '../data/channels.js' import { requireAuth } from '../middleware/requireAuth.js' const router = express.Router() router.use(requireAuth) function orgName(orgId) { return orgs.find((o) => o.id === orgId)?.name || '' } function nowStr() { return new Date().toISOString().slice(0, 19).replace('T', ' ') } // ---------------- 核心企业管理 ---------------- router.get('/channel/list', (req, res) => { const { orgId, channelCode, channelName, page = 1, pageSize = 10 } = req.query || {} let list = channels if (orgId) list = list.filter((c) => c.orgId === orgId) if (channelCode) list = list.filter((c) => c.channelCode.includes(channelCode)) if (channelName) list = list.filter((c) => c.name.includes(channelName)) const total = list.length const start = (Number(page) - 1) * Number(pageSize) const pageList = list.slice(start, start + Number(pageSize)).map((c) => ({ id: c.id, name: c.name, channelCode: c.channelCode, appCode: c.appCode, motherAccountBank: c.motherAccountBank, orgId: c.orgId, orgName: orgName(c.orgId), createdAt: c.createdAt })) res.json({ code: 200, msg: 'ok', data: { list: pageList, total } }) }) router.post('/channel', (req, res) => { const { name, channelCode, appCode, motherAccountBank, orgId } = req.body || {} if (!name) return res.json({ code: 40025, msg: '请输入渠道名称', data: null }) if (!channelCode) return res.json({ code: 40026, msg: '请输入渠道编号', data: null }) if (!appCode) return res.json({ code: 40027, msg: '请输入应用编号', data: null }) if (!motherAccountBank) return res.json({ code: 40028, msg: '请输入母户开户行', data: null }) if (!orgId || !orgs.some((o) => o.id === orgId)) { return res.json({ code: 40029, msg: '请选择有效的所属机构', data: null }) } const channel = { id: nextChannelId(), name, channelCode, appCode, motherAccountBank, orgId, createdAt: nowStr() } channels.push(channel) res.json({ code: 200, msg: 'ok', data: channel }) }) router.put('/channel/:id', (req, res) => { const { id } = req.params const { name, channelCode, appCode, motherAccountBank, orgId } = req.body || {} const channel = channels.find((c) => c.id === id) if (!channel) return res.json({ code: 40404, msg: '核心企业不存在', data: null }) if (!name) return res.json({ code: 40025, msg: '请输入渠道名称', data: null }) if (!channelCode) return res.json({ code: 40026, msg: '请输入渠道编号', data: null }) if (!appCode) return res.json({ code: 40027, msg: '请输入应用编号', data: null }) if (!motherAccountBank) return res.json({ code: 40028, msg: '请输入母户开户行', data: null }) if (!orgId || !orgs.some((o) => o.id === orgId)) { return res.json({ code: 40029, msg: '请选择有效的所属机构', data: null }) } channel.name = name channel.channelCode = channelCode channel.appCode = appCode channel.motherAccountBank = motherAccountBank channel.orgId = orgId res.json({ code: 200, msg: 'ok', data: channel }) }) router.delete('/channel/batch', (req, res) => { const { ids } = req.body || {} if (!Array.isArray(ids) || ids.length === 0) { return res.json({ code: 40024, msg: '请选择要删除的核心企业', data: null }) } const successIds = [] const failed = [] ids.forEach((id) => { const channel = channels.find((c) => c.id === id) if (!channel) { failed.push({ id, name: id, reason: '核心企业不存在' }) return } successIds.push(id) }) successIds.forEach((id) => { const index = channels.findIndex((c) => c.id === id) if (index !== -1) channels.splice(index, 1) }) res.json({ code: 200, msg: 'ok', data: { successIds, failed } }) }) export default router