orderstats.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. require_once(BASE_HELPER_PATH . '/task/task_helper.php');
  3. class orderstatsControl extends SystemControl
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. }
  9. public function indexOp()
  10. {
  11. $type = $_GET['type'] ?? 'system';
  12. $page = "{$type}.order.stats";
  13. $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d 00:00:00", strtotime("-1 day"));
  14. $model_refill_order = Model('refill_order');
  15. $condition = $this->ct_cond($_GET, 'time_stamp');
  16. if (!empty($_GET['order_time_type'])) {
  17. $condition['order_time_type'] = $_GET['order_time_type'];
  18. }
  19. if(!empty($_GET['reload'])) {
  20. $stats_list = $model_refill_order->getAllRefillStats($condition);
  21. $this->stats_reload($stats_list);
  22. $json_str = json_encode($condition);
  23. $this->log("对账管理,批量重新统计,条件:{$json_str}", 1);
  24. showMessage('操作完成');
  25. }
  26. $stats_list = $model_refill_order->getOrderStatsList($condition, 50, '*', 'time_stamp desc, cname asc');
  27. if($type == 'merchant') {
  28. foreach ($stats_list as $key => $stats) {
  29. $time = date("Y-m-d",$stats['time_stamp']+86400);
  30. $mch_cache = rcache("merchant_balance_{$time}", 'refill-');
  31. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  32. if(empty($caches)) continue;
  33. $stats_list[$key]['available'] = ncPriceFormat($caches[$stats['cid']]) ?? '';
  34. }
  35. }
  36. $total_stats = $this->stats($condition);
  37. $order_time_type_text = ['notify_time' => '回调时间', 'order_time' => '下单时间'];
  38. Tpl::output('total_stats', $total_stats);
  39. Tpl::output('stats_list', $stats_list);
  40. Tpl::output('order_time_type_text', $order_time_type_text);
  41. Tpl::output('show_page', $model_refill_order->showpage());
  42. Tpl::showpage($page);
  43. }
  44. private function stats_reload($stats_list)
  45. {
  46. foreach ($stats_list as $stats)
  47. {
  48. if($stats['send_count'] == 0) continue;
  49. $cond = [
  50. 'type' => $stats['type'],
  51. 'time_stamp' => $stats['time_stamp'],
  52. 'cid' => $stats['cid'],
  53. 'order_time_type' => $stats['order_time_type']
  54. ];
  55. $manager = new task\manager();
  56. $manager->add_task('order_stat_reload',$cond,0,3600);
  57. }
  58. }
  59. private function ct_cond($input,$time_stamp)
  60. {
  61. $condition['type'] = $input['type'] ?? 'system';
  62. if (!empty($input['cid'])) {
  63. $condition['cid'] = ['in', $input['cid']];
  64. }
  65. $start_unixtime = intval(strtotime($input['query_start_time']));
  66. $end_unixtime = intval(strtotime($input['query_end_time']));
  67. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  68. $condition[$time_stamp] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  69. } elseif ($start_unixtime > 0) {
  70. $condition[$time_stamp] = ['egt', $start_unixtime];
  71. } elseif ($end_unixtime > 0) {
  72. $condition[$time_stamp] = ['lt', $end_unixtime];
  73. }
  74. return $condition;
  75. }
  76. public function ExportDataOp()
  77. {
  78. $type = $_GET['type'] ?? 'system';
  79. $model_refill_order = Model('refill_order');
  80. $condition['type'] = $type;
  81. if (!empty($_GET['cid'])) {
  82. $condition['cid'] = ['in', $_GET['cid']];
  83. }
  84. if (!empty($_GET['order_time_type'])) {
  85. $condition['order_time_type'] = $_GET['order_time_type'];
  86. }
  87. $start_unixtime = intval($_GET['query_start_time']);
  88. $end_unixtime = intval($_GET['query_end_time']);
  89. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  90. $condition['time_stamp'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  91. } elseif ($start_unixtime > 0) {
  92. $condition['time_stamp'] = ['egt', $start_unixtime];
  93. } elseif ($end_unixtime > 0) {
  94. $condition['time_stamp'] = ['lt', $end_unixtime];
  95. }
  96. $stats_list = $this->all_orderstats_data($condition);
  97. $total_stats = $this->stats($condition);
  98. $stats = $total_stats[$_GET['order_time_type']];
  99. if(empty($stats)) {
  100. $stats = $total_stats['notify_time'];
  101. }
  102. $result['data'] = $stats_list;
  103. $result['total_stats'] = $stats;
  104. echo(json_encode($result));
  105. }
  106. private function stats($condition)
  107. {
  108. $order_time_type = ['notify_time','order_time'];
  109. foreach ($order_time_type as $type){
  110. $condition['order_time_type'] = $type;
  111. $stats = Model('')->table('refill_stats')
  112. ->field('sum(success_count) as success_count,sum(success_refill_amounts) as success_refill_amounts,sum(success_mch_amounts) as success_mch_amounts,
  113. sum(success_channel_amounts) as success_channel_amounts,sum(profit_amounts) as profit_amounts,sum(send_count) as send_count')
  114. ->where($condition)
  115. ->find();
  116. $total_stats[$type] = [
  117. 'success_count_total' => $stats['success_count'],
  118. 'success_refill_amounts_total' => ncPriceFormat($stats['success_refill_amounts']),
  119. 'success_mch_amounts_toatl' => ncPriceFormat($stats['success_mch_amounts']),
  120. 'success_channel_amounts_total' => ncPriceFormat($stats['success_channel_amounts']),
  121. 'profit_amounts_total' => ncPriceFormat($stats['profit_amounts']),
  122. 'send_count_total' => $stats['send_count'],
  123. ];
  124. }
  125. return $total_stats;
  126. }
  127. private function all_orderstats_data($condition): array
  128. {
  129. $i = 0;
  130. $result = [];
  131. while (true) {
  132. $start = $i * 1000;
  133. $list = Model('')->table('refill_stats')->field('*')
  134. ->where($condition)->order('time_stamp desc')->limit("{$start},1000")->select();
  135. if (empty($list)) {
  136. break;
  137. }
  138. $i++;
  139. foreach ($list as $value) {
  140. $result[] = $value;
  141. }
  142. }
  143. return $result;
  144. }
  145. public function refill_balanceOp()
  146. {
  147. $mod = Model('refill_balance');
  148. $type = $_GET['type'] ?? 'system';
  149. $page = "{$type}.refill.balance";
  150. $cond = $this->ct_cond($_GET, 'end_stamp');
  151. if (!empty($_GET['order_time_type'])) {
  152. $cond['time_type'] = $_GET['order_time_type'];
  153. }
  154. $balance_list = $mod->getBalanceList($cond, 200);
  155. $order_time_type_text = ['notify_time' => '回调时间', 'order_time' => '下单时间'];
  156. Tpl::output('stats_list', $balance_list);
  157. Tpl::output('order_time_type_text', $order_time_type_text);
  158. Tpl::output('show_page', $mod->showpage());
  159. Tpl::showpage($page);
  160. }
  161. public function refill_balance_stat_allOp()
  162. {
  163. $end_unixtime = intval(strtotime($_GET['selectEndDate']));
  164. if(empty($end_unixtime)) {
  165. exit(json_encode(['code' => false, 'msg' => '结束日期不能为空!']));
  166. }
  167. $condition = ['end' => $end_unixtime];
  168. $manager = new task\manager();
  169. $task = $manager->add_task('refill_balance_stat_all',$condition,0);
  170. if ($task->completed() && $task->success()) {
  171. exit(json_encode(['code' => true, 'msg' => '操作成功,此记录已新建成功。']));
  172. } else {
  173. exit(json_encode(['code' => true, 'msg' => '操作成功,后台任务已开始新建,请稍后查看新数据']));
  174. }
  175. }
  176. public function refill_balance_createOp()
  177. {
  178. $balance_id = $_GET['balance_id'];
  179. $mod = Model('refill_balance');
  180. $balance = $mod->getBalance(['balance_id' => $balance_id]);
  181. if(empty($balance )) {
  182. exit(json_encode(['code' => false, 'msg' => '记录不存在!']));
  183. }
  184. $start_unixtime = intval(strtotime($_GET['selectStartDate']));
  185. $end_unixtime = intval(strtotime($_GET['selectEndDate']));
  186. if(empty($start_unixtime)) {
  187. $start_unixtime = $balance['end_stamp'];
  188. }
  189. if(empty($end_unixtime)) {
  190. exit(json_encode(['code' => false, 'msg' => '结束日期不能为空!']));
  191. }
  192. $condition = [
  193. 'type' => $balance['type'], 'cid' => $balance['cid'], 'start' => $start_unixtime,
  194. 'end' => $end_unixtime, 'time_type' => $balance['time_type'], 'parent_id' => $balance_id
  195. ];
  196. $manager = new task\manager();
  197. $task = $manager->add_task('refill_balance_create',$condition,0);
  198. if ($task->completed() && $task->success()) {
  199. exit(json_encode(['code' => true, 'msg' => '操作成功,此记录已新建成功。']));
  200. } else {
  201. exit(json_encode(['code' => true, 'msg' => '操作成功,后台任务已开始新建,请稍后查看新数据']));
  202. }
  203. }
  204. public function refill_balance_rebuildOp()
  205. {
  206. $balance_id = $_GET['balance_id'];
  207. $mod = Model('refill_balance');
  208. $balance = $mod->getBalance(['balance_id' => $balance_id]);
  209. if(empty($balance )) {
  210. showMessage('记录不存在!');
  211. }
  212. $condition['balance_id'] = $balance_id;
  213. $manager = new task\manager();
  214. $task = $manager->add_task('refill_balance_rebuild',$condition,0);
  215. if ($task->completed() && $task->success()) {
  216. showMessage('操作成功,此记录已重新生成!');
  217. } else {
  218. showMessage('操作成功,后台任务已开始重新生成,请稍后查看新数据!');
  219. }
  220. }
  221. public function refill_balance_saveOp()
  222. {
  223. showMessage('操作成功!');
  224. }
  225. public function refill_balance_confirmOp()
  226. {
  227. $balance_id = $_GET['balance_id'];
  228. $mod = Model('refill_balance');
  229. $balance = $mod->getBalance(['balance_id' => $balance_id]);
  230. if(empty($balance )) {
  231. showMessage('记录不存在!');
  232. }
  233. $ret = $mod->BalanceConfirm($balance_id);
  234. if ($ret) {
  235. showMessage('操作成功!');
  236. } else {
  237. showMessage('操作失败!');
  238. }
  239. }
  240. }