123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- <?php
- require_once(BASE_HELPER_PATH . '/refill/util.php');
- require_once(BASE_HELPER_PATH . '/task/task_helper.php');
- class refill_orderControl extends SystemControl
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function indexOp()
- {
- $fShowStat = $_GET['fShowStat'];
- $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d H:i:s", strtotime("-1 hours"));
- $this->refill_order($_GET, $fShowStat == 1);
- }
- public function refill_order($input, $fShowStat = false)
- {
- $time_type = $input['time_type'];
- if(empty($time_type) || !in_array($time_type,['order_time','notify_time'])) {
- $time_type = 'order_time';
- }
- $normal_cond = function ($input)
- {
- $cond['inner_status'] = 0;
- if (!empty($input['order_sn'])) {
- $cond['refill_order.order_sn'] = $input['order_sn'];
- }
- if (!empty($input['mch_order'])) {
- $cond['refill_order.mch_order'] = $input['mch_order'];
- }
- if (!empty($input['ch_trade_no'])) {
- $cond['refill_order.ch_trade_no'] = $input['ch_trade_no'];
- }
- if (!empty($input['card_no'])) {
- $cond['refill_order.card_no'] = $input['card_no'];
- }
- if (!empty($input['no_mchid'])) {
- $no_mchid = explode(',', $input['no_mchid']);
- $cond['refill_order.mchid'] = ['not in', $no_mchid];
- }
- if (!empty($input['no_amount'])) {
- $no_amount = explode(',', $input['no_amount']);
- $cond['refill_order.refill_amount'] = ['not in', $no_amount];
- }
- if (!empty($input['mchid'])) {
- $cond['refill_order.mchid'] = $input['mchid'];
- }
- if (!empty($input['channel_name'])) {
- $cond['refill_order.channel_name'] = $input['channel_name'];
- }
- if (!empty($input['store_id'])) {
- $cond['vr_order.store_id'] = $input['store_id'];
- }
- if (!empty($input['refill_amount'])) {
- $cond['refill_order.refill_amount'] = $input['refill_amount'];
- }
- if (!empty($input['quality'])) {
- $cond['refill_order.quality'] = $input['quality'];
- }
- if(!empty($input['official_status']))
- {
- if($input['official_status'] == 1) {
- $cond['official_sn'] = '';
- } elseif ($input['official_status'] == 2) {
- $cond['official_sn'] = ['neq', ''];
- }
- }
- if (!empty($input['card_type'])) {
- if (in_array($input['card_type'], ['1', '2', '4', '5', '6', '7'])) {
- $cond['refill_order.card_type'] = intval($input['card_type']);
- }
- if ($input['card_type'] == 'oil') {
- $cond['refill_order.card_type'] = ['in', [1, 2]];
- }
- if ($input['card_type'] == 'phone') {
- $cond['refill_order.card_type'] = ['in', [4, 5, 6]];
- }
- }
- if(in_array($input['cardno_state'], ['0','1', '2', '4', '5'])) {
- $cond['refill_order.cardno_state'] = $input['cardno_state'];
- }
- if(in_array($input['is_transfer'], ['0','1'])) {
- $cond['refill_order.is_transfer'] = $input['is_transfer'];
- }
- if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40']))
- {
- $cond['vr_order.order_state'] = $_GET['order_state'];
- }
- return $cond;
- };
- if(isset($input['export'])) {
- $scope['export_time'] = 'order_time';
- } else {
- $scope['export_time'] = 'notify_time';
- }
- $time_scope = function ($input,$time_type)
- {
- $start = intval(strtotime($input['query_start_time']));
- $end = intval(strtotime($input['query_end_time']));
- $today = strtotime(date('Y-m-d',time()));
- $tomorrow = $today + 86400;
- if($start <= 0) {
- $start = $today;
- }
- $cur = time();
- $cur = $cur - ($cur % 300) + 300;
- if($end <= 0) {
- $end = $cur;
- }
- $end_date = strtotime(date('Y-m-d',$end));
- $start_date = strtotime(date('Y-m-d',$start));
- if ($end >= $start)
- {
- if ($time_type === 'order_time')
- {
- $add_end = $end_date + 86400 * 5;
- if($add_end > $tomorrow) {
- $add_end = $tomorrow;
- }
- return ['order_time' => [$start, $end], 'add_time' => [$start, $add_end]];
- } elseif ($time_type === 'notify_time') {
- $add_begin = $start_date - 86400 * 5;
- return ['order_time' => [$add_begin, $end], 'add_time' => [$add_begin, $end], 'notify_time' => [$start, $end]];
- }
- }
- return [];
- };
- $time_cond = function ($scope)
- {
- $cond = [];
- [$start,$end] = $scope['order_time'];
- $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
- [$start,$end] = $scope['add_time'];
- $cond['vr_order.add_time'] = [['egt', $start], ['elt', $end], 'and'];
- if(isset($scope['notify_time'])) {
- [$start,$end] = $scope['notify_time'];
- $cond['refill_order.notify_time'] = [['egt', $start], ['lt', $end], 'and'];
- }
- return $cond;
- };
- $scope = $time_scope($input,$time_type);
- if(empty($scope)) {
- showMessage('结束日期必须大于起始日期.');
- }
- $normal = $normal_cond($input);
- if (!empty($input['export'])) {
- $this->export_execl($normal, $scope, $input);
- }
- $tmcond = $time_cond($scope);
- $cond = array_merge($normal,$tmcond);
- if (!empty($input['mch_notify'])) {
- $this->mch_notify($cond);
- return;
- }
- if (!empty($input['order_query'])) {
- $this->updateOrderSend($cond);
- return;
- }
- $merchants = $this->merchants();
- $mod_refill = Model('refill_order');
- $order_list = $mod_refill->getMerchantOrderList($cond, 200, 5000,'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
- $order_list = $this->OrderDataFormat($order_list, $merchants);
- $provider_list = $this->providers();
- $periodor = function ($start, $end)
- {
- $delta = $end - $start;
- if($delta < 0) return 300;
- if($delta / 86400 >= 5) {
- $ret = 12 * 3600;
- }
- elseif($delta / 86400 >= 1) {
- $ret = 2 * 3600;
- }
- elseif($delta / 3600 > 12) {
- $ret = 1800;
- }
- elseif($delta / 3600 > 3) {
- $ret = 900;
- }
- else {
- $ret = 300;
- }
- return $ret;
- };
- if ($fShowStat)
- {
- [$start,$end] = $scope[$time_type];
- $period = $periodor($start,$end);
- $manager = new task\manager();
- if($end - $start >= 86400) {
- $task = $manager->add_task('refill_order_stat_ex', ['normal' => $normal,'time_scope' => $scope],0,$period);
- }else{
- $task = $manager->add_task('refill_order_stat',$cond,0,$period);
- }
- $task_id = $task->task_id();
- if($task->waiting()) {
- $sec = time() - $task->add_time();
- $comment = "task={$task_id} 已经加入后台任务,已经等待{$sec}秒";
- } elseif($task->processing()) {
- $sec = time() - $task->act_time();
- $comment = "task={$task_id} 正在处理中,已经执行{$sec}秒";
- } else {
- $sec = $task->fini_time() - $task->act_time();
- $comment = "task={$task_id} 执行完成,共耗时{$sec}秒";
- }
- Tpl::output('stat_tip', $comment);
- if($task->completed() && $task->success())
- {
- $stat = $task->result();
- Tpl::output('stat', $stat);
- Tpl::output('end_unixtime', $end);
- }
- }
- $dispatcher_queue_length = refill\util::dispatcher_queue_length();
- Tpl::output('dispatcher_queue_length', $dispatcher_queue_length);
- Tpl::output('admin_info', $this->getAdminInfo());
- Tpl::output('order_list', $order_list);
- Tpl::output('provider_list', $provider_list);
- Tpl::output('merchant_list', $merchants);
- $exmechs = function ($merchants) {
- $result = [];
- foreach ($merchants as $value) {
- $data['name'] = $value['company_name'] ?? $value['name'];
- $data['value'] = $value['mchid'];
- $data['alpha'] = $value['alpha'];
- $data['color'] = false;
- $result[] = $data;
- }
- return base64_encode(json_encode($result));
- };
- Tpl::output('exmechs', $exmechs($merchants));
- Tpl::output('show_page', $mod_refill->showpage());
- Tpl::showpage('refill.order.index');
- }
- private function export_execl($normal_cond, $time_scope, $input)
- {
- $title = $input['task_title'] ?? '';
- $manager = new task\manager();
- $cond = ['normal' => $normal_cond,'time_scope' => $time_scope,'export_type'=> $input['export_type']];
- $task = $manager->add_task('refill_order_export',$cond,1,-1,$title);
- if ($task->completed() && $task->success()) {
- $file_name = $task->result();
- $file_path = UPLOAD_SITE_URL . '/' . ATTACH_TASK . DS . $file_name;
- header("Content-Disposition: attachment; filename={$file_name}");
- readfile($file_path);
- } else {
- $task_id = $task->task_id();
- showMessage("录入成功,任务ID:{$task_id},请稍后以相同条件再次导出,或在任务列表直接下载。","index.php?act=task&op=index&task_id={$task_id}");
- }
- }
- private function all_order_state_stat($condition)
- {
- $counts = Model('')->table('refill_order,vr_order')->join('inner')
- ->on('refill_order.order_id=vr_order.order_id')
- ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
- ->where($condition)
- ->group('order_state')
- ->select();
- $all = [];
- $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
- $sending = $success = $cancel = $data;
- foreach ($counts as $count) {
- if ($count['order_state'] == ORDER_STATE_SEND) {
- $sending = $count;
- } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
- $success = $count;
- } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
- $cancel = $count;
- }
- $all['order_count'] += $count['order_count'];
- $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
- $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
- $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
- }
- return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
- }
- public function merchant_dataOp()
- {
- $merchant_list = $this->merchants();
- $result = [];
- foreach ($merchant_list as $value) {
- $data['name'] = $value['company_name'] ?? $value['name'];
- $data['value'] = $value['mchid'];
- $data['alpha'] = $value['alpha'];
- $data['color'] = false;
- $result[] = $data;
- }
- echo json_encode($result);
- }
- public function provider_dataOp()
- {
- $provider_list = $this->providers();
- $result = [];
- foreach ($provider_list as $value) {
- $data['name'] = $value['store_name'] ?? $value['name'];
- $data['value'] = $value['store_id'];
- $result[] = $data;
- }
- echo json_encode($result);
- }
- public function refill_third_infoOp()
- {
- $order_id = $_GET['order_id'];
- $model_refill_order = Model('refill_order');
- $order_info = $model_refill_order->getOrderInfo(['order_id' => $order_id]);
- if (empty($order_info) || $order_info['card_type'] != mtopcard\ThirdRefillCard) {
- $res = ['state' => -1, 'msg' => '订单不存在或类型错误'];
- exit(json_encode($res));
- }
- $third_refill = Model('thrid_refill');
- $data = $third_refill->getThird($order_id);
- $data['card_info'] = $data['card_info'] ?? '';
- $account_type_text = ['手机号','QQ号','微信号'];
- $data['account_type_text'] = $account_type_text[$data['account_type']-1];
- $ret_state_text = ['初始化','已解析','解析失败'];
- $data['ret_state_text'] = $ret_state_text[$data['ret_state']];
- $res = ['state' => 1, 'data' => $data];
- exit(json_encode($res));
- }
- public function showOrdersOp()
- {
- $order_sn = $_GET['order_sn'] ?? '';
- $model_refill_order = Model('refill_order');
- $refill_info = $model_refill_order->getOrderInfo(['order_sn' => $order_sn]);
- if (empty($refill_info)) {
- $res = ['state' => -1, 'msg' => '订单不存在'];
- exit(json_encode($res));
- }
- $mch_order = $refill_info['mch_order'];
- $order_time = $refill_info['order_time'];
- $mchid = $refill_info['mchid'];
- $condition['refill_order.mch_order'] = $mch_order;
- $condition['refill_order.order_time'] = ['egt', $order_time];
- $condition['refill_order.mchid'] = $mchid;
- $field = 'refill_order.order_sn,refill_order.commit_time,refill_order.notify_time,refill_order.channel_name,vr_order.order_state,refill_order.err_msg,vr_order.close_reason';
- $order_list = $model_refill_order->getMerchantOrderList($condition, 1000,0, $field);
- foreach ($order_list as $order_id => $order_info)
- {
- $order_list[$order_id]['order_state_text'] = orderState($order_info);
- $order_list[$order_id]['notify_time_text'] = '/';
- $order_list[$order_id]['commit_time_text'] = date("Y-m-d H:i:s", $order_info['commit_time']);
- if ($order_info['notify_time'] > 0)
- {
- if($order_info['commit_time'] == 0) {
- $diff_time = 0;
- $order_list[$order_id]['order_state_text'] = '提交失败';
- $order_list[$order_id]['commit_time_text'] = '/';
- }else{
- $diff_time = $order_info['notify_time'] - $order_info['commit_time'];
- $order_list[$order_id]['notify_time_text'] = date("Y-m-d H:i:s", $order_info['notify_time']);
- }
- }
- elseif($order_info['commit_time'] == 0) {
- $diff_time = 0;
- $order_list[$order_id]['order_state_text'] = '提交失败';
- $order_list[$order_id]['commit_time_text'] = '/';
- }
- else
- {
- $diff_time = time() - $order_info['commit_time'];
- if($order_info['order_state'] != ORDER_STATE_SEND) {
- $diff_time = 0;
- $order_list[$order_id]['order_state_text'] = '提交失败';
- }
- }
- if($diff_time != 0) {
- $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
- } else {
- $order_list[$order_id]['diff_time_text'] = '/';
- }
- $order_list[$order_id]['diff_time'] = $diff_time;
- }
- $mch_notify_state = ['未确认','已确认','回调失败'];
- $res = ['state' => 1,
- 'data' => $order_list,
- 'count' => count($order_list),
- 'mch_notify_times' => $refill_info['mch_notify_times'],
- 'mch_notify_state' => $mch_notify_state[$refill_info['mch_notify_state']]
- ];
- exit(json_encode($res));
- }
- private function mch_notify($condition)
- {
- $condition['mch_notify_state'] = 0;
- $condition['inner_status'] = 0;
- $condition['is_retrying'] = 0;
- $orders = Model('refill_order')->getAllOrders($condition);
- if (!empty($orders))
- {
- foreach ($orders as $order)
- {
- $order_id = $order['order_id'];
- if ($order['order_state'] == ORDER_STATE_SEND) {
- QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
- } else {
- QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => true]);
- }
- }
- }
- showMessage('操作成功');
- }
- private function updateOrderSend($condition)
- {
- $condition['order_state'] = ORDER_STATE_SEND;
- $orders = Model('refill_order')->getAllOrders($condition);
- if (!empty($orders)) {
- foreach ($orders as $order) {
- $order_id = $order['order_id'];
- Log::record("updateOrderSend order_id={$order_id}",Log::DEBUG);
- QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
- }
- }
- showMessage('操作成功');
- }
- }
|