SdkOrderLogic.php 4.2 KB

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