orderstats.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. class orderstatsControl extends SystemControl
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. }
  8. public function indexOp()
  9. {
  10. $type = $_GET['type'] ?? 'system';
  11. $page = "{$type}.order.stats";
  12. $model_refill_order = Model('refill_order');
  13. $condition['type'] = $type;
  14. if (!empty($_GET['cid'])) {
  15. $condition['cid'] = ['in', $_GET['cid']];
  16. }
  17. if (!empty($_GET['order_time_type'])) {
  18. $condition['order_time_type'] = $_GET['order_time_type'];
  19. }
  20. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  21. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  22. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  23. $condition['time_stamp'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  24. } elseif ($start_unixtime > 0) {
  25. $condition['time_stamp'] = ['egt', $start_unixtime];
  26. } elseif ($end_unixtime > 0) {
  27. $condition['time_stamp'] = ['lt', $end_unixtime];
  28. }
  29. $stats_list = $model_refill_order->getOrderStatsList($condition, 50, '*', 'time_stamp desc, cname asc');
  30. if($type == 'merchant') {
  31. foreach ($stats_list as $key => $stats) {
  32. $time = date("Y-m-d",$stats['time_stamp']+86400);
  33. $mch_cache = rcache("merchant_balance_{$time}", 'refill-');
  34. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  35. if(empty($caches)) continue;
  36. $stats_list[$key]['available'] = ncPriceFormat($caches[$stats['cid']]) ?? '';
  37. }
  38. }
  39. $total_stats = $this->stats($stats_list);
  40. $order_time_type_text = ['notify_time' => '回调时间', 'order_time' => '下单时间'];
  41. Tpl::output('total_stats', $total_stats);
  42. Tpl::output('stats_list', $stats_list);
  43. Tpl::output('order_time_type_text', $order_time_type_text);
  44. Tpl::output('show_page', $model_refill_order->showpage());
  45. Tpl::showpage($page);
  46. }
  47. public function ExportDataOp()
  48. {
  49. $type = $_GET['type'] ?? 'system';
  50. $model_refill_order = Model('refill_order');
  51. $condition['type'] = $type;
  52. if (!empty($_GET['cid'])) {
  53. $condition['cid'] = ['in', $_GET['cid']];
  54. }
  55. if (!empty($_GET['order_time_type'])) {
  56. $condition['order_time_type'] = $_GET['order_time_type'];
  57. }
  58. $start_unixtime = intval($_GET['query_start_time']);
  59. $end_unixtime = intval($_GET['query_end_time']);
  60. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  61. $condition['time_stamp'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  62. } elseif ($start_unixtime > 0) {
  63. $condition['time_stamp'] = ['egt', $start_unixtime];
  64. } elseif ($end_unixtime > 0) {
  65. $condition['time_stamp'] = ['lt', $end_unixtime];
  66. }
  67. $stats_list = $this->all_orderstats_data($condition);
  68. $total_stats = $this->stats($stats_list);
  69. $stats = $total_stats[$_GET['order_time_type']];
  70. if(empty($stats)) {
  71. $stats = $total_stats['notify_time'];
  72. }
  73. $result['data'] = $stats_list;
  74. $result['total_stats'] = $stats;
  75. echo(json_encode($result));
  76. exit;
  77. }
  78. private function stats($stats_list): array
  79. {
  80. $order_time_type = ['notify_time','order_time'];
  81. foreach ($order_time_type as $type){
  82. $success_count_total = $success_refill_amounts_total = $success_mch_amounts_total = $success_channel_amounts_total = $profit_amounts_total = 0;
  83. foreach ($stats_list as $stats) {
  84. if($stats['order_time_type'] != $type) continue;
  85. $success_count_total += $stats['success_count'];
  86. $success_refill_amounts_total += $stats['success_refill_amounts'];
  87. $success_mch_amounts_total += $stats['success_mch_amounts'];
  88. $success_channel_amounts_total += $stats['success_channel_amounts'];
  89. $profit_amounts_total += $stats['profit_amounts'];
  90. }
  91. $total_stats[$type] = [
  92. 'success_count_total' => ncPriceFormat($success_count_total),
  93. 'success_refill_amounts_total' => ncPriceFormat($success_refill_amounts_total),
  94. 'success_mch_amounts_toatl' => ncPriceFormat($success_mch_amounts_total),
  95. 'success_channel_amounts_total' => ncPriceFormat($success_channel_amounts_total),
  96. 'profit_amounts_total' => ncPriceFormat($profit_amounts_total)
  97. ];
  98. }
  99. return $total_stats;
  100. }
  101. private function all_orderstats_data($condition): array
  102. {
  103. $i = 0;
  104. $result = [];
  105. while (true) {
  106. $start = $i * 1000;
  107. $list = Model('')->table('refill_stats')->field('*')
  108. ->where($condition)->order('time_stamp desc')->limit("{$start},1000")->select();
  109. if (empty($list)) {
  110. break;
  111. }
  112. $i++;
  113. foreach ($list as $value) {
  114. $result[] = $value;
  115. }
  116. }
  117. return $result;
  118. }
  119. }