search.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. require 'config.php';
  3. if (isset($_GET['debtorName'])) {
  4. $debtorName = $_GET['debtorName'];
  5. // 先尝试从Redis获取缓存
  6. if ($REDIS_ENABLED) {
  7. $cacheKey = "debt_info:" . $debtorName;
  8. $cachedResult = $redis->get($cacheKey);
  9. if ($cachedResult) {
  10. echo $cachedResult;
  11. exit;
  12. }
  13. }
  14. // 如果没有缓存,则查询数据库
  15. $stmt = $pdo->prepare("SELECT * FROM Debtors WHERE debtor_name = :debtorName");
  16. $stmt->execute(['debtorName' => $debtorName]);
  17. $debtor = $stmt->fetch(PDO::FETCH_ASSOC);
  18. if ($debtor) {
  19. // 获取还款记录
  20. $repaymentStmt = $pdo->prepare("SELECT * FROM Repayments WHERE debtor_name = :debtorName ORDER BY repayment_date DESC");
  21. $repaymentStmt->execute(['debtorName' => $debtorName]);
  22. $repayments = $repaymentStmt->fetchAll(PDO::FETCH_ASSOC);
  23. // 获取视图中的欠款信息
  24. $viewStmt = $pdo->prepare("SELECT * FROM DebtStatus WHERE 姓名 = :debtorName");
  25. $viewStmt->execute(['debtorName' => $debtorName]);
  26. $viewData = $viewStmt->fetch(PDO::FETCH_ASSOC);
  27. // 构建输出
  28. $output = "<div class='search-result'>";
  29. $output .= "<div class='result-header'>";
  30. $output .= "<h2 class='debtor-name'>{$debtor['debtor_name']}</h2>";
  31. $output .= "<p class='remaining-debt'>当前欠款余额: <span class='amount'>¥{$viewData['剩余欠款金额']}</span></p>";
  32. $output .= "</div>";
  33. $output .= "<div class='repayment-section'>";
  34. $output .= "<h3>还款记录</h3>";
  35. if ($repayments) {
  36. $output .= "<div class='repayment-list'>";
  37. foreach ($repayments as $repayment) {
  38. $output .= "<div class='repayment-item'>";
  39. $output .= "<div class='repayment-date'><span class='label'>日期:</span> {$repayment['repayment_date']}</div>";
  40. $output .= "<div class='repayment-details'>";
  41. $output .= "<div class='repayment-amount'><span class='label'>金额:</span> ¥{$repayment['repayment_amount']}</div>";
  42. $output .= "<div class='repayment-method'><span class='label'>支付方式:</span> {$repayment['repayment_method']}</div>";
  43. if (!empty($repayment['comments'])) {
  44. $output .= "<div class='repayment-comment'><span class='label'>备注:</span> {$repayment['comments']}</div>";
  45. }
  46. $output .= "</div>";
  47. $output .= "</div>";
  48. }
  49. $output .= "</div>";
  50. } else {
  51. $output .= "<p class='no-repayments'>暂无还款记录</p>";
  52. }
  53. $output .= "</div>";
  54. $output .= "</div>";
  55. // 如果Redis可用,缓存结果
  56. if ($REDIS_ENABLED) {
  57. $redis->setex($cacheKey, 60, $output);
  58. }
  59. echo $output;
  60. } else {
  61. echo "<p class='no-result'>未找到相关记录</p>";
  62. }
  63. }
  64. ?>