orderstats.php 5.3 KB

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