SdkOrderLogic.php 4.5 KB

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