123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- require_once(BASE_HELPER_PATH . '/task/task_helper.php');
- class orderstatsControl extends SystemControl
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function indexOp()
- {
- $type = $_GET['type'] ?? 'system';
- $page = "{$type}.order.stats";
- $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d 00:00:00", strtotime("-1 day"));
- $model_refill_order = Model('refill_order');
- $condition = $this->ct_cond($_GET, 'time_stamp');
- if (!empty($_GET['order_time_type'])) {
- $condition['order_time_type'] = $_GET['order_time_type'];
- }
- if(!empty($_GET['reload'])) {
- $stats_list = $model_refill_order->getAllRefillStats($condition);
- $this->stats_reload($stats_list);
- $json_str = json_encode($condition);
- $this->log("对账管理,批量重新统计,条件:{$json_str}", 1);
- showMessage('操作完成');
- }
- $stats_list = $model_refill_order->getOrderStatsList($condition, 50, '*', 'time_stamp desc, cname asc');
- if($type == 'merchant') {
- foreach ($stats_list as $key => $stats) {
- $time = date("Y-m-d",$stats['time_stamp']+86400);
- $mch_cache = rcache("merchant_balance_{$time}", 'refill-');
- $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
- if(empty($caches)) continue;
- $stats_list[$key]['available'] = ncPriceFormat($caches[$stats['cid']]) ?? '';
- }
- }
-
- $total_stats = $this->stats($condition);
- $order_time_type_text = ['notify_time' => '回调时间', 'order_time' => '下单时间'];
- Tpl::output('total_stats', $total_stats);
- Tpl::output('stats_list', $stats_list);
- Tpl::output('order_time_type_text', $order_time_type_text);
- Tpl::output('show_page', $model_refill_order->showpage());
- Tpl::showpage($page);
- }
- private function stats_reload($stats_list)
- {
- foreach ($stats_list as $stats)
- {
- if($stats['send_count'] == 0) continue;
- $cond = [
- 'type' => $stats['type'],
- 'time_stamp' => $stats['time_stamp'],
- 'cid' => $stats['cid'],
- 'order_time_type' => $stats['order_time_type']
- ];
- $manager = new task\manager();
- $manager->add_task('order_stat_reload',$cond,0,3600);
- }
- }
- private function ct_cond($input,$time_stamp)
- {
- $condition['type'] = $input['type'] ?? 'system';
- if (!empty($input['cid'])) {
- $condition['cid'] = ['in', $input['cid']];
- }
- $start_unixtime = intval(strtotime($input['query_start_time']));
- $end_unixtime = intval(strtotime($input['query_end_time']));
- if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
- $condition[$time_stamp] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
- } elseif ($start_unixtime > 0) {
- $condition[$time_stamp] = ['egt', $start_unixtime];
- } elseif ($end_unixtime > 0) {
- $condition[$time_stamp] = ['lt', $end_unixtime];
- }
- return $condition;
- }
- public function ExportDataOp()
- {
- $type = $_GET['type'] ?? 'system';
- $model_refill_order = Model('refill_order');
- $condition['type'] = $type;
- if (!empty($_GET['cid'])) {
- $condition['cid'] = ['in', $_GET['cid']];
- }
- if (!empty($_GET['order_time_type'])) {
- $condition['order_time_type'] = $_GET['order_time_type'];
- }
- $start_unixtime = intval($_GET['query_start_time']);
- $end_unixtime = intval($_GET['query_end_time']);
- if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
- $condition['time_stamp'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
- } elseif ($start_unixtime > 0) {
- $condition['time_stamp'] = ['egt', $start_unixtime];
- } elseif ($end_unixtime > 0) {
- $condition['time_stamp'] = ['lt', $end_unixtime];
- }
- $stats_list = $this->all_orderstats_data($condition);
- $total_stats = $this->stats($condition);
- $stats = $total_stats[$_GET['order_time_type']];
- if(empty($stats)) {
- $stats = $total_stats['notify_time'];
- }
- $result['data'] = $stats_list;
- $result['total_stats'] = $stats;
- echo(json_encode($result));
- }
- private function stats($condition)
- {
- $order_time_type = ['notify_time','order_time'];
- foreach ($order_time_type as $type){
- $condition['order_time_type'] = $type;
- $stats = Model('')->table('refill_stats')
- ->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,sum(send_count) as send_count')
- ->where($condition)
- ->find();
- $total_stats[$type] = [
- 'success_count_total' => $stats['success_count'],
- 'success_refill_amounts_total' => ncPriceFormat($stats['success_refill_amounts']),
- 'success_mch_amounts_toatl' => ncPriceFormat($stats['success_mch_amounts']),
- 'success_channel_amounts_total' => ncPriceFormat($stats['success_channel_amounts']),
- 'profit_amounts_total' => ncPriceFormat($stats['profit_amounts']),
- 'send_count_total' => $stats['send_count'],
- ];
- }
- return $total_stats;
- }
- private function all_orderstats_data($condition): array
- {
- $i = 0;
- $result = [];
- while (true) {
- $start = $i * 1000;
- $list = Model('')->table('refill_stats')->field('*')
- ->where($condition)->order('time_stamp desc')->limit("{$start},1000")->select();
- if (empty($list)) {
- break;
- }
- $i++;
- foreach ($list as $value) {
- $result[] = $value;
- }
- }
- return $result;
- }
- public function refill_balanceOp()
- {
- $mod = Model('refill_balance');
- $type = $_GET['type'] ?? 'system';
- $page = "{$type}.refill.balance";
- $cond = $this->ct_cond($_GET, 'end_stamp');
- if (!empty($_GET['order_time_type'])) {
- $cond['time_type'] = $_GET['order_time_type'];
- }
- $balance_list = $mod->getBalanceList($cond, 200);
- $order_time_type_text = ['notify_time' => '回调时间', 'order_time' => '下单时间'];
- Tpl::output('stats_list', $balance_list);
- Tpl::output('order_time_type_text', $order_time_type_text);
- Tpl::output('show_page', $mod->showpage());
- Tpl::showpage($page);
- }
- public function refill_balance_stat_allOp()
- {
- $end_unixtime = intval(strtotime($_GET['selectEndDate']));
- if(empty($end_unixtime)) {
- exit(json_encode(['code' => false, 'msg' => '结束日期不能为空!']));
- }
- $condition = ['end' => $end_unixtime];
- $manager = new task\manager();
- $task = $manager->add_task('refill_balance_stat_all',$condition,0);
- if ($task->completed() && $task->success()) {
- exit(json_encode(['code' => true, 'msg' => '操作成功,此记录已新建成功。']));
- } else {
- exit(json_encode(['code' => true, 'msg' => '操作成功,后台任务已开始新建,请稍后查看新数据']));
- }
- }
- public function refill_balance_createOp()
- {
- $balance_id = $_GET['balance_id'];
- $mod = Model('refill_balance');
- $balance = $mod->getBalance(['balance_id' => $balance_id]);
- if(empty($balance )) {
- exit(json_encode(['code' => false, 'msg' => '记录不存在!']));
- }
- $start_unixtime = intval(strtotime($_GET['selectStartDate']));
- $end_unixtime = intval(strtotime($_GET['selectEndDate']));
- if(empty($start_unixtime)) {
- $start_unixtime = $balance['end_stamp'];
- }
- if(empty($end_unixtime)) {
- exit(json_encode(['code' => false, 'msg' => '结束日期不能为空!']));
- }
- $condition = [
- 'type' => $balance['type'], 'cid' => $balance['cid'], 'start' => $start_unixtime,
- 'end' => $end_unixtime, 'time_type' => $balance['time_type'], 'parent_id' => $balance_id
- ];
- $manager = new task\manager();
- $task = $manager->add_task('refill_balance_create',$condition,0);
- if ($task->completed() && $task->success()) {
- exit(json_encode(['code' => true, 'msg' => '操作成功,此记录已新建成功。']));
- } else {
- exit(json_encode(['code' => true, 'msg' => '操作成功,后台任务已开始新建,请稍后查看新数据']));
- }
- }
- public function refill_balance_rebuildOp()
- {
- $balance_id = $_GET['balance_id'];
- $mod = Model('refill_balance');
- $balance = $mod->getBalance(['balance_id' => $balance_id]);
- if(empty($balance )) {
- showMessage('记录不存在!');
- }
- $condition['balance_id'] = $balance_id;
- $manager = new task\manager();
- $task = $manager->add_task('refill_balance_rebuild',$condition,0);
- if ($task->completed() && $task->success()) {
- showMessage('操作成功,此记录已重新生成!');
- } else {
- showMessage('操作成功,后台任务已开始重新生成,请稍后查看新数据!');
- }
- }
- public function refill_balance_saveOp()
- {
- showMessage('操作成功!');
- }
- public function refill_balance_confirmOp()
- {
- $balance_id = $_GET['balance_id'];
- $mod = Model('refill_balance');
- $balance = $mod->getBalance(['balance_id' => $balance_id]);
- if(empty($balance )) {
- showMessage('记录不存在!');
- }
- $ret = $mod->BalanceConfirm($balance_id);
- if ($ret) {
- showMessage('操作成功!');
- } else {
- showMessage('操作失败!');
- }
- }
- }
|