SdkOrderLogic.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\v1\logic\customer;
  3. use plugin\saiadmin\basic\BaseLogic;
  4. use plugin\saiadmin\exception\ApiException;
  5. use plugin\saiadmin\utils\Helper;
  6. use app\v1\model\customer\SdkOrder;
  7. use DateTime;
  8. use support\Redis;
  9. use support\think\Db;
  10. /**
  11. * 用户订单记录逻辑层
  12. */
  13. class SdkOrderLogic extends BaseLogic
  14. {
  15. /**
  16. * 构造函数
  17. */
  18. public function __construct()
  19. {
  20. $this->model = new SdkOrder();
  21. }
  22. /**
  23. * 获取订单列表
  24. */
  25. public function getOrderList($where)
  26. {
  27. $startDate = $where['pay_date'][0];
  28. $endDate = $where['pay_date'][1];
  29. $page = max(1, (int)($where['page'] ?? 1));
  30. $limit = max(1, (int)($where['limit'] ?? 10));
  31. $offset = ($page - 1) * $limit;
  32. $orderBy = $where['orderBy'] ?: 'pay_date';
  33. $orderType = $where['orderType'] ?: 'DESC';
  34. $orderBy = $orderBy . ' ' . $orderType;
  35. // 获取年月
  36. $table_name_array = $this->queryOrdersByDateRange($startDate, $endDate);
  37. // 合并表 union all
  38. $where_sql = "";
  39. if (!empty($where['game_id'])) {
  40. $where_sql .= " AND game_id IN({$where['game_id']})";
  41. }
  42. if (!empty($where['user_name'])) {
  43. $where_sql .= " AND user_name = '{$where['user_name']}'";
  44. }
  45. if (!empty($where['order_id'])) {
  46. $where_sql .= " AND order_id = '{$where['order_id']}'";
  47. }
  48. if (!empty($where['trade_id'])) {
  49. $where_sql .= " AND trade_id = '{$where['trade_id']}'";
  50. }
  51. if (!empty($where['role_id'])) {
  52. $where_sql .= " AND role_id = '{$where['role_id']}'";
  53. }
  54. if (!empty($where['role_name'])) {
  55. $where_sql .= " AND role_name = '{$where['role_name']}'";
  56. }
  57. if (!empty($where['pay_date'])) {
  58. $where_sql .= " AND pay_date BETWEEN '{$startDate} 00:00:00' AND '{$endDate} 23:59:59'";
  59. }
  60. $sql_parts = [];
  61. foreach ($table_name_array as $table_name) {
  62. // 安全过滤,避免 SQL 注入
  63. if (!preg_match('/^\d{6}$/', $table_name)) {
  64. continue;
  65. }
  66. $sql_parts[] = "SELECT * FROM sdk_order_{$table_name} WHERE 1=1 {$where_sql}";
  67. }
  68. if (empty($sql_parts)) {
  69. return []; // 或抛异常,或返回空结果
  70. }
  71. $unionSql = implode(" UNION ALL ", $sql_parts);
  72. // 外层包裹分页、排序
  73. $finalSql = "
  74. SELECT * FROM ( {$unionSql} ) AS all_orders
  75. ORDER BY {$orderBy}
  76. LIMIT {$offset}, {$limit}
  77. ";
  78. // 统计总数(用于分页)
  79. $countSql = "
  80. SELECT COUNT(*) AS total FROM ( {$unionSql} ) AS count_orders
  81. ";
  82. // 总金额查询
  83. $sumSql = "
  84. SELECT ROUND(SUM(money), 2) AS money_sum FROM (
  85. {$unionSql}
  86. ) AS sum_orders
  87. ";
  88. $count = Db::connect('db_game_log')->query($countSql)[0]['total'] ?? 0;
  89. $data = Db::connect('db_game_log')->query($finalSql);
  90. $moneySum = Db::connect('db_game_log')->query($sumSql)[0]['money_sum'] ?? 0;
  91. $data = $this->trandformListColumn($data, ['game', 'pay_channel']);
  92. return [
  93. 'total' => $count,
  94. 'current_page' => $page,
  95. 'per_page' => $limit,
  96. 'last_page' => ceil($count / $limit),
  97. 'has_more' => $page < ceil($count / $limit),
  98. 'data' => $data,
  99. 'totalRow' => $moneySum
  100. ];
  101. }
  102. /**
  103. * 获取年月
  104. */
  105. public function queryOrdersByDateRange($start, $end)
  106. {
  107. $start = new DateTime($start);
  108. $end = new DateTime($end);
  109. $end->modify('first day of next month');
  110. $months = [];
  111. while ($start < $end) {
  112. $months[] = $start->format('Ym'); // 例如 202505
  113. $start->modify('+1 month');
  114. }
  115. return $months;
  116. }
  117. /**
  118. * 补发
  119. */
  120. public function send($order_id)
  121. {
  122. return Redis::lpush("request_cp_callback_queue", $order_id);
  123. }
  124. }