refill_order($fShowStat == 1); } public function refill_order($fShowStat = false) { $model_refill_order = Model('refill_order'); $condition['inner_status'] = 0; $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d H:i:s", strtotime("-1 hours")); if (!empty($_GET['order_sn'])) { $condition['refill_order.order_sn'] = $_GET['order_sn']; } if (!empty($_GET['mch_order'])) { $condition['refill_order.mch_order'] = $_GET['mch_order']; } if (!empty($_GET['ch_trade_no'])) { $condition['refill_order.ch_trade_no'] = $_GET['ch_trade_no']; } if (!empty($_GET['card_no'])) { $condition['refill_order.card_no'] = $_GET['card_no']; } if (!empty($_GET['no_mchid'])) { $no_mchid = explode(',', $_GET['no_mchid']); $condition['refill_order.mchid'] = ['not in', $no_mchid]; } if (!empty($_GET['no_amount'])) { $no_amount = explode(',', $_GET['no_amount']); $condition['refill_order.refill_amount'] = ['not in', $no_amount]; } if (!empty($_GET['mchid'])) { $condition['refill_order.mchid'] = $_GET['mchid']; } if (!empty($_GET['channel_name'])) { $condition['refill_order.channel_name'] = $_GET['channel_name']; } if (!empty($_GET['store_id'])) { $condition['vr_order.store_id'] = $_GET['store_id']; } if (!empty($_GET['refill_amount'])) { $condition['refill_order.refill_amount'] = $_GET['refill_amount']; } if (!empty($_GET['quality'])) { $condition['refill_order.quality'] = $_GET['quality']; } if(!empty($_GET['official_status'])) { if($_GET['official_status'] == 1) { $condition['official_sn'] = ''; } elseif ($_GET['official_status'] == 2) { $condition['official_sn'] = ['neq', '']; } } if (!empty($_GET['card_type'])) { if (in_array($_GET['card_type'], ['1', '2', '4', '5', '6', '7'])) { $condition['refill_order.card_type'] = intval($_GET['card_type']); } if ($_GET['card_type'] == 'oil') { $condition['refill_order.card_type'] = ['in', [1, 2]]; } if ($_GET['card_type'] == 'phone') { $condition['refill_order.card_type'] = ['in', [4, 5, 6]]; } } $fToday = false; $start_unixtime = intval(strtotime($_GET['query_start_time'])); $end_unixtime = intval(strtotime($_GET['query_end_time'])); if($end_unixtime <= 0) { $end_unixtime = time(); } if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) { $condition['refill_order.order_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and']; $condition['vr_order.add_time'] = [['egt', $start_unixtime],['lt', $end_unixtime], 'and']; } elseif ($start_unixtime > 0) { $condition['refill_order.order_time'] = ['egt', $start_unixtime]; $condition['vr_order.add_time'] = ['egt', $start_unixtime]; } elseif ($end_unixtime > 0) { $condition['refill_order.order_time'] = ['lt', $end_unixtime]; } else { $start = time() - 3600; $_GET['query_start_time'] = date("Y-m-d H:i:s",$start); $condition['refill_order.order_time'] = ['egt', $start]; $condition['vr_order.add_time'] = ['egt', $start-1]; $fToday = true; } if(in_array($_GET['cardno_state'], ['0','1', '2', '4', '5'])) { $condition['refill_order.cardno_state'] = $_GET['cardno_state']; } if(in_array($_GET['is_transfer'], ['0','1'])) { $condition['refill_order.is_transfer'] = $_GET['is_transfer']; } if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40'])) { $condition['vr_order.order_state'] = $_GET['order_state']; if ($_GET['order_state'] == ORDER_STATE_SEND) { if ($_GET['time'] == 1) { $condition['refill_order.order_time'] = ['between', [(time() - 3600), (time() - 1800)]]; } if ($_GET['time'] == 2) { $condition['refill_order.order_time'] = ['lt', (time() - 3600)]; } } } if (!empty($_GET['export']) || !empty($_GET['export_stats'])) { $this->RefillOrderExport($condition); exit; } if (!empty($_GET['mch_notify'])) { $this->mch_notify($condition); exit; } if (!empty($_GET['order_query'])) { $this->updateOrderSend($condition); exit; } $merchants = []; $merchant_list = $this->merchants(); foreach ($merchant_list as $value) { $merchants[$value['mchid']] = $value; } $order_list = $model_refill_order->getMerchantOrderList($condition, 200, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc'); foreach ($order_list as $order_id => $order_info) { $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']); $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name']; if ($order_info['notify_time'] > 0) { $diff_time = $order_info['notify_time'] - $order_info['order_time']; } else { $diff_time = time() - $order_info['order_time']; } $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time); $order_list[$order_id]['diff_time'] = $diff_time; $order_list[$order_id]['quality_text'] = $this->quality_format($order_info['quality'],$order_info['card_type']); $order_list[$order_id]['org_quality_text'] = $this->quality_format($order_info['org_quality'],$order_info['card_type']); } $provider_list = $this->providers(); if ($fShowStat) { $stat = $this->all_order_state_stat($condition); Tpl::output('stat', $stat); } $dispatcher_queue_length = refill\util::dispatcher_queue_length(); Tpl::output('dispatcher_queue_length', $dispatcher_queue_length); Tpl::output('admin_info', $this->getAdminInfo()); Tpl::output('ftoday', $fToday); Tpl::output('order_list', $order_list); Tpl::output('provider_list', $provider_list); Tpl::output('merchant_list', $merchant_list); Tpl::output('show_page', $model_refill_order->showpage()); Tpl::showpage('refill.order.index'); } private function RefillOrderExport($condition) { if(!empty($_GET['export_stats'])) { if(!empty($condition['refill_order.order_time'])) { $condition['refill_order.notify_time'] = $condition['refill_order.order_time']; unset($condition['refill_order.order_time']); unset($condition['vr_order.add_time']); } } $result = $this->getAllOrders($condition); $this->createExcel($result); } private function createExcel($data = array()) { Language::read('export'); import('libraries.excel'); $excel_obj = new Excel(); $excel_data = array(); //设置样式 $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1'))); //header $excel_data[0][] = array('styleid' => 's_title', 'data' => '商户号'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '客户订单号'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '平台单号'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '面额'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '充值卡号'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '充值卡类型'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '下单日期'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '完成日期'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '官方流水号'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '订单状态'); $excel_data[0][] = array('styleid' => 's_title', 'data' => '扣款金额'); //data foreach ((array)$data as $k => $v) { $tmp = array(); $tmp[] = array('data' => $v['mchid']); $tmp[] = array('data' => $v['mch_order']); $tmp[] = array('data' => $v['order_sn']); $tmp[] = array('data' => $v['refill_amount']); $tmp[] = array('data' => $v['card_no']); $tmp[] = array('data' => $this->scard_type($v['card_type'])); $tmp[] = array('data' => date('Y-m-d H:i:s', $v['order_time'])); if (empty($v['notify_time'])) { $tmp[] = array('data' => ''); } else { $tmp[] = array('data' => date('Y-m-d H:i:s', $v['notify_time'])); } $tmp[] = array('data' => $v['official_sn']); $tmp[] = array('data' => orderState($v)); $tmp[] = array('data' => $v['mch_amount']); $excel_data[] = $tmp; } $excel_data = $excel_obj->charset($excel_data, CHARSET); $excel_obj->addArray($excel_data); $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET)); $excel_obj->generateXML($excel_obj->charset(L('exp_od_order'), CHARSET) . date('Y-m-d-H', time())); exit; } 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); exit; } 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); exit; } 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'; $order_list = $model_refill_order->getMerchantOrderList($condition, 1000, $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) { $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']); } else { 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 = 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 = $this->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 = $this->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('操作成功'); } private function getAllOrders($condition): array { $len = 1000; $i = 0; $orders = []; while (true) { $start = $i * $len; $items = Model('')->table('refill_order,vr_order') ->field('refill_order.*,vr_order.order_state') ->join('inner') ->on('refill_order.order_id=vr_order.order_id') ->where($condition) ->order('refill_order.order_time desc') ->limit("{$start},{$len}") ->select(); $orders = array_merge($orders,$items); if (empty($items) || count($items) < $len) { break; } $i++; } return $orders; } }