This commit is contained in:
2025-09-17 14:00:46 +08:00
parent ecc9e3046e
commit cb0b401b6e
4 changed files with 25 additions and 205 deletions

View File

@@ -171,163 +171,6 @@ router.get('/unreasonable-matches', auth, adminAuth, async (req, res) => {
}
});
/**
* @swagger
* /api/matching-admin/fix-unreasonable-match/{allocationId}:
* post:
* summary: 修复不合理的匹配记录
* tags: [MatchingAdmin]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: allocationId
* required: true
* schema:
* type: integer
* description: 分配ID
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* action:
* type: string
* enum: [cancel, reassign]
* description: 修复操作类型(取消或重新分配)
* required:
* - action
* responses:
* 200:
* description: 修复成功
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* message:
* type: string
* 400:
* description: 参数错误或无需修复
* 401:
* description: 未授权
* 403:
* description: 无管理员权限
* 404:
* description: 分配记录不存在
* 500:
* description: 服务器错误
*/
router.post('/fix-unreasonable-match/:allocationId', auth, adminAuth, async (req, res) => {
try {
const { allocationId } = req.params;
const { action } = req.body; // 'cancel' 或 'reassign'
const connection = await db.getDB().getConnection();
await connection.query('START TRANSACTION');
try {
// 获取分配详情
const [allocationResult] = await connection.execute(
`SELECT oa.*, u_to.balance as to_user_balance, u_to.username as to_username
FROM transfers oa
JOIN users u_to ON oa.to_user_id = u_to.id
WHERE oa.source_type = 'allocation' AND oa.id = ?`,
[allocationId]
);
if (allocationResult.length === 0) {
await connection.query('ROLLBACK');
connection.release();
return res.status(404).json({ message: '分配记录不存在' });
}
const allocation = allocationResult[0];
if (allocation.to_user_balance <= 0) {
await connection.query('ROLLBACK');
connection.release();
return res.status(400).json({ message: '该用户余额已为负数,无需修复' });
}
if (action === 'cancel') {
// 获取当前时间
const currentTime = new Date();
// 取消分配
await connection.execute(
'UPDATE transfers SET status = "cancelled", updated_at = ? WHERE id = ?',
[currentTime, allocationId]
);
// 记录操作日志
await connection.execute(
'INSERT INTO admin_operation_logs (admin_id, operation_type, target_type, target_id, description, created_at) VALUES (?, "fix_matching", "allocation", ?, ?, ?)',
[req.user.id, allocationId, `取消不合理匹配:正余额用户${allocation.to_username}(余额${allocation.to_user_balance}元)被匹配${allocation.amount}`, currentTime]
);
} else if (action === 'reassign') {
// 重新分配给负余额用户
const usedTargetUsers = new Set([allocation.to_user_id]);
const newTargetUser = await matchingService.getMatchingTargetExcluding(allocation.from_user_id, usedTargetUsers);
if (!newTargetUser) {
await connection.query('ROLLBACK');
connection.release();
return res.status(400).json({ message: '没有可用的负余额用户进行重新分配' });
}
// 获取当前时间
const currentTime = new Date();
// 更新分配目标
await connection.execute(
'UPDATE transfers SET to_user_id = ?, updated_at = ? WHERE id = ?',
[newTargetUser, currentTime, allocationId]
);
// 获取新目标用户信息
const [newUserResult] = await connection.execute(
'SELECT username, balance FROM users WHERE id = ?',
[newTargetUser]
);
const newUser = newUserResult[0];
// 记录操作日志
await connection.execute(
'INSERT INTO admin_operation_logs (admin_id, operation_type, target_type, target_id, description, created_at) VALUES (?, "fix_matching", "allocation", ?, ?, ?)',
[req.user.id, allocationId, `修复不合理匹配:从正余额用户${allocation.to_username}(余额${allocation.to_user_balance}元)重新分配给负余额用户${newUser.username}(余额${newUser.balance}元)`, currentTime]
);
} else {
await connection.query('ROLLBACK');
connection.release();
return res.status(400).json({ message: '无效的操作类型' });
}
await connection.query('COMMIT');
connection.release();
res.json({
success: true,
message: action === 'cancel' ? '已取消不合理匹配' : '已重新分配给负余额用户'
});
} catch (innerError) {
await connection.query('ROLLBACK');
connection.release();
throw innerError;
}
} catch (error) {
console.error('修复不合理匹配失败:', error);
res.status(500).json({ message: error.message || '修复不合理匹配失败' });
}
});
/**
* @swagger