orderstats.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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($condition);
  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($condition);
  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. }
  77. private function stats($condition)
  78. {
  79. $order_time_type = ['notify_time','order_time'];
  80. foreach ($order_time_type as $type){
  81. $condition['order_time_type'] = $type;
  82. $stats = Model('')->table('refill_stats')
  83. ->field('sum(success_count) as success_count,sum(success_refill_amounts) as success_refill_amounts,sum(success_mch_amounts) as success_mch_amounts,sum(success_channel_amounts) as success_channel_amounts,sum(profit_amounts) as profit_amounts')
  84. ->where($condition)
  85. ->find();
  86. $total_stats[$type] = [
  87. 'success_count_total' => $stats['success_count'],
  88. 'success_refill_amounts_total' => ncPriceFormat($stats['success_refill_amounts']),
  89. 'success_mch_amounts_toatl' => ncPriceFormat($stats['success_mch_amounts']),
  90. 'success_channel_amounts_total' => ncPriceFormat($stats['success_channel_amounts']),
  91. 'profit_amounts_total' => ncPriceFormat($stats['profit_amounts'])
  92. ];
  93. }
  94. return $total_stats;
  95. }
  96. private function all_orderstats_data($condition): array
  97. {
  98. $i = 0;
  99. $result = [];
  100. while (true) {
  101. $start = $i * 1000;
  102. $list = Model('')->table('refill_stats')->field('*')
  103. ->where($condition)->order('time_stamp desc')->limit("{$start},1000")->select();
  104. if (empty($list)) {
  105. break;
  106. }
  107. $i++;
  108. foreach ($list as $value) {
  109. $result[] = $value;
  110. }
  111. }
  112. return $result;
  113. }
  114. }