123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- <?php
- use task\manager;
- require_once(BASE_ROOT_PATH . '/mobile/control/merchantweb.php');
- require_once(BASE_HELPER_PATH . '/task/task_helper.php');
- class merchant_orderControl extends mbMerchantControl
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function listOp()
- {
- $model_vr_order = Model('refill_order');
- $cond['mchid'] = $this->mchid();
- $cond['inner_status'] = 0;
- $fSingle = false;
- if (!empty($_GET['card_type'])) {
- if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
- $cond['refill_order.card_type'] = $_GET['card_type'];
- }
- if($_GET['card_type'] == 'oil') {
- $cond['refill_order.card_type'] = ['in' , ['1' , '2']];
- }
- if($_GET['card_type'] == 'phone') {
- $cond['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
- }
- }
- if (!empty($_GET['card_no'])) {
- $fSingle = true;
- $cond['refill_order.card_no'] = $_GET['card_no'];
- }
- if (!empty($_GET['refill_amount'])) {
- $cond['refill_order.refill_amount'] = $_GET['refill_amount'];
- }
- if (!empty($_GET['mch_order'])) {
- $fSingle = true;
- $cond['refill_order.mch_order'] = $_GET['mch_order'];
- }
- if (!empty($_GET['order_sn'])) {
- $fSingle = true;
- $cond['refill_order.order_sn'] = $_GET['order_sn'];
- }
- if (!empty($_GET['quality'])) {
- $cond['refill_order.quality'] = $_GET['quality'];
- }
- $cur_time = time();
- $end_date = strtotime(date('Y-m-d',$cur_time));
- $add_end = $end_date + 86400 * 5;
- if($fSingle)
- {
- $start = intval($_GET['start_time']);
- $end = intval($_GET['end_time']);
- if($start <= 0) {
- $start = $cur_time - 3600;
- }
- if($end <= 0) {
- $end = $cur_time;
- }
- }
- else{
- $start = $cur_time - 3600;
- $end = $cur_time;
- }
- $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
- $cond['vr_order.add_time'] = [['egt', $start], ['elt', $add_end], 'and'];
- if (in_array($_GET['order_state'], array('0', '30', '40'))) {
- $cond['vr_order.order_state'] = $_GET['order_state'];
- if($_GET['order_state'] == 30 && $_GET['time'] == 1){
- $cond['refill_order.order_time'] = ['lt', (time() - 3600)];
- }
- }
- $fields = 'refill_order.*,vr_order.order_state';
- $order_list = $model_vr_order->getMerchantOrderList($cond, $this->page, 5000, $fields, 'refill_order.order_time desc');
- $order_list = $this->merchant_order_format($order_list);
- $result['data'] = $order_list;
- $result['total'] = $model_vr_order->gettotalpage();
- return self::outsuccess($result);
- }
- public function OrderStatsOp()
- {
- if(empty($_GET['time_type']) || empty($_GET['start_time']) || empty($_GET['end_time'])) {
- return self::outerr(errcode::ErrInputParam, "参数错误.");
- }
- $cond['inner_status'] = 0;
- $time_type = $_GET['time_type'];
- $start = $_GET['start_time'];
- $end = $_GET['end_time'];
- $start_date = strtotime(date('Y-m-d',$start));
- $end_date = strtotime(date('Y-m-d',$end));
- if ($time_type == 'order_time') {
- $add_end = $end_date + 86400 * 5;
- $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
- $cond['vr_order.add_time'] = [['egt', $start], ['elt', $add_end], 'and'];
- } elseif ($time_type == 'notify_time') {
- $add_begin = $start_date - 86400 * 5;
- $cond['refill_order.order_time'] = [['egt', $add_begin], ['lt', $end], 'and'];
- $cond['vr_order.add_time'] = [['egt', $add_begin], ['elt', $end], 'and'];
- $cond['refill_order.notify_time'] = [['egt', $start], ['elt', $end], 'and'];
- } else {
- return self::outerr(errcode::ErrInputParam, "筛选日期类型错误.");
- }
- $cond['mchid'] = $this->mchid();
- $stats = 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(mch_amount) as mch_amounts, order_state')
- ->where($cond)->group('order_state')->select();
- $result['count'] = $result['sendCount'] = $result['errorCount'] = $result['successCount'] = $result['refill_amounts'] = $result['mch_amounts'] = 0;
- foreach ($stats as $stat) {
- $result['count'] += $stat['order_count'];
- if($stat['order_state'] == ORDER_STATE_SEND) {
- $result['sendCount'] = $stat['order_count'];
- }
- if($stat['order_state'] == ORDER_STATE_CANCEL) {
- $result['errorCount'] = $stat['order_count'];
- }
- if($stat['order_state'] == ORDER_STATE_SUCCESS) {
- $result['successCount'] = $stat['order_count'];
- $result['mch_amounts'] = $stat['mch_amounts'];
- $result['refill_amounts'] = $stat['refill_amounts'];
- }
- }
- return self::outsuccess($result);
- }
- private function merchant_order_format($orders)
- {
- $data = [];
- foreach ($orders as $order) {
- if($order['notify_time'] > 0)
- {
- $order['diff_time_text'] = $this->elapse_time($order['notify_time'] - $order['order_time']);
- $order['diff_time'] = $order['notify_time'] - $order['order_time'];
- }
- else
- {
- $order['diff_time_text'] = $this->elapse_time(time() - $order['order_time']);
- $order['diff_time'] = time() - $order['order_time'];
- }
- if (isset($order['order_time'])) {
- $order['order_time'] = date('Y-m-d H:i:s', $order['order_time']);
- }
- if (isset($order['notify_time'])) {
- $order['notify_time'] = date('Y-m-d H:i:s', $order['notify_time']);
- }
- if($order['is_retrying'] == 1) {
- $order['order_state'] = ORDER_STATE_SEND;
- }
- if ($order['order_state'] == ORDER_STATE_NEW || $order['order_state'] == ORDER_STATE_PAY) {
- $order['order_state'] = ORDER_STATE_SEND;
- }
- $order['order_state_text'] = $this->_orderState($order['order_state']);
- $order['card_type_name'] = $this->scard_type($order['card_type']);
- $data[] = $order;
- }
- return $data;
- }
- /**
- * 取得订单状态文字输出形式
- *
- * @param int $order_state 订单数组
- * @return string
- */
- private function _orderState($order_state)
- {
- switch ($order_state) {
- case ORDER_STATE_CANCEL:
- $text = '已取消';
- break;
- case ORDER_STATE_NEW:
- $text = '新订单';
- break;
- case ORDER_STATE_SEND:
- $text = '充值中';
- break;
- case ORDER_STATE_PAY:
- $text = '支付成功';
- break;
- case ORDER_STATE_SUCCESS:
- $text = '充值成功';
- break;
- case 'retrying':
- $text = '重试中';
- break;
- default:
- $text = '未知状态';
- }
- return $text;
- }
- private function scard_type(int $card_type)
- {
- if ($card_type == mtopcard\PetroChinaCard) { //中石油
- return '中石油';
- } elseif ($card_type == mtopcard\SinopecCard) { //中石化
- return '中石化';
- } elseif ($card_type == mtopcard\ChinaMobileCard) { //中国移动
- return '中国移动';
- } elseif ($card_type == mtopcard\ChinaUnicomCard) { //中国联通
- return '中国联通';
- } elseif ($card_type == mtopcard\ChinaTelecomCard) { //中国电信
- return '中国电信';
- } elseif ($card_type == mtopcard\ThirdRefillCard) { //中国电信
- return '增值业务';
- } else {
- return 'unknown';
- }
- }
- private function elapse_time($seconds)
- {
- $minutes = intval($seconds / 60);
- $second = intval($seconds % 60);
- if($minutes >= 60) {
- $minute = $minutes % 60;
- $hours = intval($minutes / 60);
- $result = "{$minute}:{$second}";
- }
- else {
- if($minutes > 0){
- $result = "{$minutes}:{$second}";
- }else{
- $result = "{$second}";
- }
- }
- if(isset($hours))
- {
- $result = "{$hours}:" . $result;
- }
- return $result;
- }
- public function OrderExportOp()
- {
- $cond['mchid'] = $this->mchid();
- $cond['inner_status'] = 0;
- $fSingle = false;
- if (!empty($_GET['card_type'])) {
- if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
- $cond['refill_order.card_type'] = $_GET['card_type'];
- }
- if($_GET['card_type'] == 'oil') {
- $cond['refill_order.card_type'] = ['in' , ['1' , '2']];
- }
- if($_GET['card_type'] == 'phone') {
- $cond['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
- }
- }
- if (!empty($_GET['card_no'])) {
- $fSingle = true;
- $cond['refill_order.card_no'] = $_GET['card_no'];
- }
- if (!empty($_GET['refill_amount'])) {
- $cond['refill_order.refill_amount'] = $_GET['refill_amount'];
- }
- if (!empty($_GET['mch_order'])) {
- $fSingle = true;
- $cond['refill_order.mch_order'] = $_GET['mch_order'];
- }
- if (!empty($_GET['order_sn'])) {
- $fSingle = true;
- $cond['refill_order.order_sn'] = $_GET['order_sn'];
- }
- if (!empty($_GET['quality'])) {
- $cond['refill_order.quality'] = $_GET['quality'];
- }
- $cur_time = time();
- $end_date = strtotime(date('Y-m-d',$cur_time));
- $add_end = $end_date + 86400 * 5;
- if($fSingle)
- {
- $start = intval($_GET['start_time']);
- $end = intval($_GET['end_time']);
- if($start <= 0) {
- $start = $cur_time - 3600;
- }
- if($end <= 0) {
- $end = $cur_time;
- }
- }
- else{
- $start = $cur_time - 3600;
- $end = $cur_time;
- }
- $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
- $cond['vr_order.add_time'] = [['egt', $start], ['elt', $add_end], 'and'];
- if (in_array($_GET['order_state'], array('0', '30', '40'))) {
- $cond['vr_order.order_state'] = $_GET['order_state'];
- if($_GET['order_state'] == 30 && $_GET['time'] == 1){
- $cond['refill_order.order_time'] = ['lt', (time() - 3600)];
- }
- }
- $order_list = $this->getAllOrders($cond);
- $order_list = $this->merchant_order_format($order_list);
- $result = $this->export_order_exec($order_list);
- return self::outsuccess($result);
- }
- 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;
- }
- private function export_order_exec($order_list)
- {
- $card_type = ['1'=>'中石油' , '2' =>'中石化' , '4' => '移动' , '5' => '联通' , '6' => '电信'];
- $datas = [];
- if(defined('COMPANY_NAME') && COMPANY_NAME === 'LZKJ_COMPANY') {
- $title = [
- ['value' => '代理商账号'],
- ['value' => '商品名称'],
- ['value' => '交易账号'],
- ['value' => '交易金额'],
- ['value' => '交易面值'],
- ['value' => '交易日期'],
- ['value' => '交易状态'],
- ['value' => '处理时间'],
- ['value' => '第三方流水']
- ];
- foreach ($order_list as $order) {
- $data = [];
- $official_sn = $order['official_sn'];
- $notify_time = $order['$notify_time'];
- if(empty($order['official_sn'])) {
- $official_sn = '';
- }
- if(empty($order['$notify_time'])) {
- $notify_time = '';
- }
- $data[] = ['value' => $order['mchid']];
- $data[] = ['value' => "{$card_type[$order['card_type']]}{$order['refill_amount']}元"];
- $data[] = ['value' => $order['card_no']];
- $data[] = ['value' => $order['mch_amount']];
- $data[] = ['value' => $order['refill_amount']];
- $data[] = ['value' => $order['order_time']];
- $data[] = ['value' => $order['order_state_text']];
- $data[] = ['value' => $notify_time];
- $data[] = ['value' => $official_sn];
- $datas[] = $data;
- }
- }
- else
- {
- $title = [
- ['value' => '商户号'],
- ['value' => '客户订单号'],
- ['value' => '平台单号'],
- ['value' => '面额'],
- ['value' => '充值卡号'],
- ['value' => '充值卡类型'],
- ['value' => '下单日期'],
- ['value' => '完成日期'],
- ['value' => '官方流水号'],
- ['value' => '订单状态'],
- ['value' => '扣款金额']
- ];
- foreach ($order_list as $order) {
- $data = [];
- $data[] = ['value'=>$order['mchid']];
- $data[] = ['value'=>$order['mch_order']];
- $data[] = ['value'=>$order['order_sn']];
- $data[] = ['value'=>$order['refill_amount']];
- $data[] = ['value'=>$order['card_no']];
- $data[] = ['value'=>$order['card_type_name']];
- $data[] = ['value'=>$order['order_time']];
- $data[] = ['value'=>$order['notify_time']];
- $data[] = ['value'=>$order['official_sn']];
- $data[] = ['value'=>$order['order_state_text']];
- $data[] = ['value'=>$order['mch_amount']];
- $datas[] = $data;
- }
- }
- return ['title' => $title , 'data' => $datas];
- }
- private function checkSend($start, $end)
- {
- $order_state_send = ORDER_STATE_SEND;
- $order_state_queue = ORDER_STATE_QUEUE;
- $cond['order_state'] = ['in',"{$order_state_send},{$order_state_queue}"];
- $cond['order_time'] = [['egt', $start], ['lt', $end], 'and'];
- return Model('refill_detail')->where($cond)->select();
- }
- private function is_timestamp($timestamp) {
- $timestamp = intval($timestamp);
- if(strtotime(date('Y-m-d H:i:s',$timestamp)) === $timestamp) {
- return $timestamp;
- } else return false;
- }
- public function create_taskOp()
- {
- if(empty($_GET['time_type']) || empty($_GET['start_time']) || empty($_GET['end_time'])) {
- return self::outerr(errcode::ErrInputParam, "参数错误.");
- }
- if(!$this->is_timestamp($_GET['start_time']) || !$this->is_timestamp($_GET['end_time'])) {
- return self::outerr(errcode::ErrInputParam, "时间戳类型错误.");
- }
- $task_title_create = function ($start, $end, $time_type){
- $start = date('Y-m-d H:i:s', $start);
- $end = date('Y-m-d H:i:s', $end);
- if ($time_type === 'order_time') {
- $task_title = "订单导出: 下单时间 {$start} - {$end}";
- } else {
- $task_title = "订单导出: 回调时间 {$start} - {$end}";
- }
- return $task_title;
- };
- $time_type = $_GET['time_type'];
- $start = $_GET['start_time'];
- $end = $_GET['end_time'];
- if (!empty($this->checkSend($start, $end))) {
- return self::outerr(errcode::ErrInputParam, "还有正在充值中的订单,请稍后再试.");
- }
- $start_date = strtotime(date('Y-m-d',$start));
- $end_date = strtotime(date('Y-m-d',$end));
- $today = strtotime(date('Y-m-d',time()));
- $tomorrow = $today + 86400;
- if ($time_type === 'order_time')
- {
- $add_end = $end_date + 86400 * 5;
- if($add_end > $tomorrow) {
- $add_end = $tomorrow;
- }
- $scope = ['order_time' => [$start, $end], 'add_time' => [$start, $add_end]];
- } elseif ($time_type === 'notify_time') {
- $add_begin = $start_date - 86400 * 5;
- $scope = ['order_time' => [$add_begin, $end], 'add_time' => [$add_begin, $end], 'notify_time' => [$start, $end]];
- } else {
- return self::outerr(errcode::ErrInputParam, "筛选日期类型错误.");
- }
- $normal_cond['inner_status'] = 0;
- $normal_cond['order_state'] = ORDER_STATE_SUCCESS;
- $normal_cond['mchid'] = $this->mchid();
- $task_title = $task_title_create($start, $end, $time_type);
- $cond = ['normal' => $normal_cond, 'time_scope' => $scope, 'export_type' => 'merchant'];
- $manager = new manager();
- $task = $manager->add_task('refill_order_export',$cond,1,-1, $task_title, $this->mchid());
- $file_path = '';
- if ($task->completed() && $task->success()) {
- $file_name = $task->result();
- $file_path = UPLOAD_SITE_URL . '/' . ATTACH_TASK . DS . $file_name;
- }
- return self::outsuccess(['file_path' => $file_path]);
- }
- public function task_listOp()
- {
- $model = Model('task');
- $condition['is_show'] = 1;
- $condition['mchid'] = $this->mchid();
- $task_list = $model->getList($condition, $this->page, '*', 'add_time desc', 20);
- foreach ($task_list as $key => $value)
- {
- if($value['state'] == 3 && !empty($value['result']))
- {
- $task_list[$key]['file_path'] = UPLOAD_SITE_URL . '/' . ATTACH_TASK . DS . unserialize($value['result']);
- }else{
- $task_list[$key]['file_path'] = '';
- }
- }
- $result['data'] = $task_list;
- $result['total'] = $model->gettotalpage();
- return self::outsuccess($result);
- }
- }
|