orderstats.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. class orderstatsControl extends SystemControl
  3. {
  4. private $task_type = [
  5. 'refill_order_export' => '订单导出'
  6. ];
  7. private $task_state = [
  8. 1 => '待处理',
  9. 2 => '处理中',
  10. 3 => '已处理',
  11. 4 => '处理错误'
  12. ];
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. }
  17. public function indexOp()
  18. {
  19. $type = $_GET['type'] ?? 'system';
  20. $page = "{$type}.order.stats";
  21. $model_refill_order = Model('refill_order');
  22. $condition['type'] = $type;
  23. if (!empty($_GET['cid'])) {
  24. $condition['cid'] = ['in', $_GET['cid']];
  25. }
  26. if (!empty($_GET['order_time_type'])) {
  27. $condition['order_time_type'] = $_GET['order_time_type'];
  28. }
  29. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  30. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  31. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  32. $condition['time_stamp'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  33. } elseif ($start_unixtime > 0) {
  34. $condition['time_stamp'] = ['egt', $start_unixtime];
  35. } elseif ($end_unixtime > 0) {
  36. $condition['time_stamp'] = ['lt', $end_unixtime];
  37. }
  38. $stats_list = $model_refill_order->getOrderStatsList($condition, 50, '*', 'time_stamp desc, cname asc');
  39. if($type == 'merchant') {
  40. foreach ($stats_list as $key => $stats) {
  41. $time = date("Y-m-d",$stats['time_stamp']+86400);
  42. $mch_cache = rcache("merchant_balance_{$time}", 'refill-');
  43. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  44. if(empty($caches)) continue;
  45. $stats_list[$key]['available'] = ncPriceFormat($caches[$stats['cid']]) ?? '';
  46. }
  47. }
  48. $total_stats = $this->stats($condition);
  49. $order_time_type_text = ['notify_time' => '回调时间', 'order_time' => '下单时间'];
  50. Tpl::output('total_stats', $total_stats);
  51. Tpl::output('stats_list', $stats_list);
  52. Tpl::output('order_time_type_text', $order_time_type_text);
  53. Tpl::output('show_page', $model_refill_order->showpage());
  54. Tpl::showpage($page);
  55. }
  56. public function ExportDataOp()
  57. {
  58. $type = $_GET['type'] ?? 'system';
  59. $model_refill_order = Model('refill_order');
  60. $condition['type'] = $type;
  61. if (!empty($_GET['cid'])) {
  62. $condition['cid'] = ['in', $_GET['cid']];
  63. }
  64. if (!empty($_GET['order_time_type'])) {
  65. $condition['order_time_type'] = $_GET['order_time_type'];
  66. }
  67. $start_unixtime = intval($_GET['query_start_time']);
  68. $end_unixtime = intval($_GET['query_end_time']);
  69. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  70. $condition['time_stamp'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  71. } elseif ($start_unixtime > 0) {
  72. $condition['time_stamp'] = ['egt', $start_unixtime];
  73. } elseif ($end_unixtime > 0) {
  74. $condition['time_stamp'] = ['lt', $end_unixtime];
  75. }
  76. $stats_list = $this->all_orderstats_data($condition);
  77. $total_stats = $this->stats($condition);
  78. $stats = $total_stats[$_GET['order_time_type']];
  79. if(empty($stats)) {
  80. $stats = $total_stats['notify_time'];
  81. }
  82. $result['data'] = $stats_list;
  83. $result['total_stats'] = $stats;
  84. echo(json_encode($result));
  85. exit;
  86. }
  87. private function stats($condition)
  88. {
  89. $order_time_type = ['notify_time','order_time'];
  90. foreach ($order_time_type as $type){
  91. $condition['order_time_type'] = $type;
  92. $stats = Model('')->table('refill_stats')
  93. ->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')
  94. ->where($condition)
  95. ->find();
  96. $total_stats[$type] = [
  97. 'success_count_total' => $stats['success_count'],
  98. 'success_refill_amounts_total' => ncPriceFormat($stats['success_refill_amounts']),
  99. 'success_mch_amounts_toatl' => ncPriceFormat($stats['success_mch_amounts']),
  100. 'success_channel_amounts_total' => ncPriceFormat($stats['success_channel_amounts']),
  101. 'profit_amounts_total' => ncPriceFormat($stats['profit_amounts'])
  102. ];
  103. }
  104. return $total_stats;
  105. }
  106. private function all_orderstats_data($condition): array
  107. {
  108. $i = 0;
  109. $result = [];
  110. while (true) {
  111. $start = $i * 1000;
  112. $list = Model('')->table('refill_stats')->field('*')
  113. ->where($condition)->order('time_stamp desc')->limit("{$start},1000")->select();
  114. if (empty($list)) {
  115. break;
  116. }
  117. $i++;
  118. foreach ($list as $value) {
  119. $result[] = $value;
  120. }
  121. }
  122. return $result;
  123. }
  124. public function refill_taskOp()
  125. {
  126. $model = Model('task');
  127. $condition['is_show'] = 1;
  128. if(!empty($_GET['type'])) {
  129. $condition['type'] = $_GET['type'];
  130. }
  131. if(!empty($_GET['task_id'])) {
  132. $condition['task_id'] = $_GET['task_id'];
  133. }
  134. if(!empty($_GET['state'])) {
  135. $condition['state'] = $_GET['state'];
  136. }
  137. $task_list = $model->getList($condition, 50);
  138. foreach ($task_list as $key => $value) {
  139. $task_list[$key]['condition'] = unserialize($value['task_params']);
  140. $task_list[$key]['result'] = unserialize($value['result']);
  141. }
  142. Tpl::output('list', $task_list);
  143. Tpl::output('task_type', $this->task_type);
  144. Tpl::output('task_state', $this->task_state);
  145. Tpl::showpage('refill.task');
  146. }
  147. public function task_delOp()
  148. {
  149. $task_id = $_GET['task_id'];
  150. $model = Model('task');
  151. $resp = $model->Del($task_id);
  152. if($resp) {
  153. showMessage('删除成功', 'index.php?act=OrderStats&&op=refill_task');
  154. }else{
  155. showMessage('任务删除失败');
  156. }
  157. }
  158. }