const express = require('express'); const router = express.Router(); const db = require('../db'); const auth = require('../middleware/auth'); router.use(auth); // 获取所有租户 (含搜索) router.get('/', async (req, res) => { try { const { search } = req.query; let query = 'SELECT * FROM tenants WHERE user_id = ?'; let params = [req.user.username]; if (search) { query += ' AND name LIKE ?'; params.push(`%${search}%`); } query += ' ORDER BY created_at DESC'; const [rows] = await db.query(query, params); res.json(rows); } catch (err) { res.status(500).json({ error: err.message }); } }); // 新增租户 router.post('/', async (req, res) => { try { const tenant = req.body; // 特殊逻辑:标品PP下 entCode 默认等于 apiKey if (tenant.type === '标品PP' && !tenant.entCode) { tenant.entCode = tenant.apiKey; } const [result] = await db.query( 'INSERT INTO tenants (user_id, name, type, app_key, app_secret, api_key, ent_code, bu_id, ent_id, private_key, public_key) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ req.user.username, tenant.name, tenant.type, tenant.appKey || null, tenant.appSecret || null, tenant.apiKey || null, tenant.entCode || null, tenant.buId || null, tenant.entId || null, tenant.privateKey || null, tenant.publicKey || null ] ); res.json({ id: result.insertId, ...tenant }); } catch (err) { res.status(500).json({ error: err.message }); } }); // 更新租户 router.put('/:id', async (req, res) => { try { const { id } = req.params; const tenant = req.body; await db.query( `UPDATE tenants SET name = ?, type = ?, app_key = ?, app_secret = ?, api_key = ?, ent_code = ?, bu_id = ?, ent_id = ?, private_key = ?, public_key = ? WHERE id = ? AND user_id = ?`, [ tenant.name, tenant.type, tenant.appKey || null, tenant.appSecret || null, tenant.apiKey || null, tenant.entCode || null, tenant.buId || null, tenant.entId || null, tenant.privateKey || null, tenant.publicKey || null, id, req.user.username ] ); res.json({ message: 'Updated successfully' }); } catch (err) { res.status(500).json({ error: err.message }); } }); // 删除租户 router.delete('/:id', async (req, res) => { try { const { id } = req.params; await db.query('DELETE FROM tenants WHERE id = ? AND user_id = ?', [id, req.user.username]); res.json({ message: 'Deleted successfully' }); } catch (err) { res.status(500).json({ error: err.message }); } }); // 复制租户 router.post('/copy/:id', async (req, res) => { try { const { id } = req.params; const [rows] = await db.query('SELECT * FROM tenants WHERE id = ? AND user_id = ?', [id, req.user.username]); if (rows.length === 0) { return res.status(404).json({ error: 'Tenant not found' }); } const tenant = rows[0]; const [result] = await db.query( 'INSERT INTO tenants (user_id, name, type, app_key, app_secret, api_key, ent_code, bu_id, ent_id, private_key, public_key) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ req.user.username, `${tenant.name} (Copy)`, tenant.type, tenant.app_key, tenant.app_secret, tenant.api_key, tenant.ent_code, tenant.bu_id, tenant.ent_id, tenant.private_key, tenant.public_key ] ); res.json({ id: result.insertId, message: 'Copied successfully' }); } catch (err) { res.status(500).json({ error: err.message }); } }); module.exports = router;