123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <?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 = "order.stats.{$type}";
- $_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)
- {
- $condes = [];
- foreach ($stats_list as $stats)
- {
- $cond = [
- 'type' => $stats['type'],
- 'time_stamp' => $stats['time_stamp'],
- 'cid' => $stats['cid'],
- 'order_time_type' => $stats['order_time_type']
- ];
- $condes[] = $cond;
- }
- $manager = new task\manager();
- $manager->add_task('order_stat_reload_some',$condes,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 = "refill.balance.{$type}";
- $cond = $this->ct_cond($_GET, 'end_stamp');
- if (!empty($_GET['order_time_type'])) {
- $cond['time_type'] = $_GET['order_time_type'];
- }
- $add_type_text = self::ADD_TYPE_TEXT;
- $balance_list = $mod->getBalanceList($cond, 200);
- foreach ($balance_list as &$item) {
- $transfer_detail_data = [];
- if(!empty($item['transfer_detail'])) {
- $transfer_detail = json_decode($item['transfer_detail'], true);
- foreach ($transfer_detail as $add_type => $value) {
- $transfer_detail_data[$add_type] = [
- 'name' => $add_type_text[$add_type],
- 'amount' => $value
- ];
- }
- }
- $item['transfer_detail_data'] = $transfer_detail_data;
- }
-
- $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];
- $end_txt = date("Y-m-d",$end_unixtime);
- $manager = new task\manager();
- $task = $manager->add_task('refill_balance_stat_all',$condition,1,-1,"{$end_txt}-对账统计");
- 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->confirm($balance_id);
- if ($ret) {
- showMessage('操作成功!');
- } else {
- showMessage('操作失败!');
- }
- }
- public function refill_balance_editOp()
- {
- $balance_id = $_POST['balance_id'];
- $mod = Model('refill_balance');
- $balance = $mod->getBalance(['balance_id' => $balance_id]);
- if(empty($balance )) {
- showMessage('记录不存在!');
- }
- $success_count = $_POST['success_count'] ?? 0;
- $refill_amount = $_POST['refill_amount'] ?? 0;
- $mch_amount = $_POST['mch_amount'] ?? 0;
- $channel_amount = $_POST['channel_amount'] ?? 0;
- $profit_amount = $_POST['profit_amount'] ?? 0;
- $refund_amount = $_POST['refund_amount'] ?? 0;
- $except_amount = $_POST['except_amount'] ?? 0;
- $accumulate_balance = $_POST['accumulate_balance'] ?? 0;
- $balance = $_POST['balance'] ?? 0;
- $remark = $_POST['remark'] ?? '';
- $transfer_in = $_POST['transfer_in'] ?? 0;
- $transfer_out = $_POST['transfer_out'] ?? 0;
- $update = [
- 'success_count' => $success_count, 'transfer_in' => $transfer_in, 'transfer_out' => $transfer_out,
- 'refill_amount' => $refill_amount, 'mch_amount' => $mch_amount, 'channel_amount' => $channel_amount,
- 'profit_amount' => $profit_amount, 'refund_amount' => $refund_amount, 'except_amount' => $except_amount,
- 'accumulate_balance' => $accumulate_balance, 'balance' => $balance, 'remark' => $remark
- ];
- $resp = $mod->editBalance($balance_id, $update);
- if($resp) {
- showMessage('编辑成功!');
- }else{
- showMessage('操作失败!');
- }
- }
- public function refill_balance_exportOp()
- {
- $balance_ids = $_GET['balance_ids'];
- showMessage('操作成功!');
- }
- public function refill_balance_stat_cfgOp()
- {
- $type = $_POST['type'];
- $items = $result = [];
- if ($type === 'provider') {
- $items = $this->providers();
- $cid_field = 'store_id';
- $cache_field = 'provider';
- } elseif ($type === 'merchant') {
- $items = $this->merchants();
- $cid_field = 'mchid';
- $cache_field = 'merchant';
- } else {
- echo json_encode(['status' => 'fail','message' => '类型错误']);
- }
- $statType = $_POST['statType'];
- if(empty($statType)) {
- echo json_encode(['status' => 'success','message' => '操作成功']);
- }
- foreach ($items as $item)
- {
- $cid = $item[$cid_field];
- if (!empty($statType[$cid]) && array_key_exists($cid, $statType)) {
- foreach ($statType[$cid] as $value) {
- $result[$cid][] = $value;
- }
- }
- }
- wcache('balance-cfg', [$cache_field => serialize($result)], 'refill-');
- echo json_encode(['status' => 'success','message' => '操作成功']);
- }
- public function balance_cfg_dataOp()
- {
- $type = $_GET['type'];
- $result = [];
- if ($type === 'provider')
- {
- $items = $this->providers();
- $cfgs = $this->read_balance_cfg('balance-cfg','provider');
- foreach ($items as $item)
- {
- $data['name'] = $item['store_name'] ?? $item['name'];
- $data['value'] = $item['store_id'];
- if (!empty($cfgs[$item['store_id']]) && array_key_exists($item['store_id'], $cfgs)) {
- $data['order_time_type'] = $cfgs[$item['store_id']];
- } else {
- $data['order_time_type'] = [];
- }
- $result[] = $data;
- }
- }
- elseif ($type === 'merchant')
- {
- $items = $this->merchants();
- $cfgs = $this->read_balance_cfg('balance-cfg','merchant');
- foreach ($items as $item)
- {
- $data['name'] = $item['company_name'] ?? $item['name'];
- $data['value'] = $item['mchid'];
- if (!empty($cfgs[$item['mchid']]) && array_key_exists($item['mchid'], $cfgs)) {
- $data['order_time_type'] = $cfgs[$item['mchid']];
- } else {
- $data['order_time_type'] = [];
- }
- $result[] = $data;
- }
- }
- echo json_encode($result);
- }
- private function read_balance_cfg($name,$field)
- {
- $data = rcache($name, 'refill-',$field);
- $val = $data[$field] ?? serialize([]);
- return unserialize($val);
- }
- }
|