mchid(); $cond['inner_status'] = 0; 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'])) { $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'])) { $cond['refill_order.mch_order'] = $_GET['mch_order']; } if (!empty($_GET['order_sn'])) { $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; $start = intval($_GET['start_time']); $end = intval($_GET['end_time']); if($start <= 0) { $start = $cur_time - 86400 * 2; } if($end <= 0) { $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'], ['0', '30', '40'])) { $cond['vr_order.order_state'] = $_GET['order_state']; } if ($_GET['stat']) { $stat = $this->orderStatsHour($cond); return self::outsuccess($stat); } $fields = 'refill_order.*,vr_order.order_state'; $total = $this->getTotal($start, $end); $order_list = $model_vr_order->getMerchantOrderList($cond, $this->page, $total, $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); } private function getTotal($start,$end) { $model_detail = Model('refill_detail'); $cond['order_time'] = [['egt', $start], ['lt', $end], 'and']; $cond['mchid'] = $this->mchid(); $items= $model_detail->field('count(*) as ncount')->where($cond)->select(); foreach($items as $item) { $count = intval($item['ncount']); return $count; } return 0; } private function orderStatsHour($cond) { $stats = Model('')->table('refill_order,vr_order')->join('inner') ->on('refill_order.order_id=vr_order.order_id') ->field("FROM_UNIXTIME(order_time,'%d-%H') as hour, count(*) as order_count, sum(refill_amount) as refill_amounts, sum(mch_amount) as mch_amounts, order_state") ->where($cond)->group('hour, order_state')->select(); $cond['refill_order.channel_name'] = ''; $excp_stats = Model('')->table('refill_order,vr_order')->join('inner') ->on('refill_order.order_id=vr_order.order_id') ->field("FROM_UNIXTIME(order_time,'%d-%H') as hour, count(*) as order_count") ->where($cond)->group('hour')->select(); $data = []; $except = []; $result = []; $all = [ 'success_count' => 0, 'success_amounts' => 0, 'cancel_count' => 0, 'except_count' => 0 ]; if(!empty($excp_stats)) { foreach ($excp_stats as $value) { $except[$value['hour']] = $value['order_count']; } } if(!empty($stats)) { foreach ($stats as $stat) { $hour = $stat['hour']; if ($stat['order_state'] == ORDER_STATE_SUCCESS) { $data[$hour]['success_count'] = $stat['order_count']; $data[$hour]['success_amounts'] = $stat['refill_amounts']; } elseif ($stat['order_state'] == ORDER_STATE_CANCEL) { $data[$hour]['cancel_count'] = $stat['order_count']; } if (array_key_exists($hour, $except)) { $data[$hour]['except_count'] = $except[$hour]; } } foreach ($data as $hour => $val) { $success_count = $val['success_count'] ?? 0; $success_amounts = $val['success_amounts'] ?? 0; $cancel_count = $val['cancel_count'] ?? 0; $except_count = $val['except_count'] ?? 0; $result[] = [$hour, $success_count, $success_amounts, $cancel_count, $except_count]; $all['success_count'] += $success_count; $all['success_amounts'] += ncPriceFormat($success_amounts); $all['cancel_count'] += ncPriceFormat($cancel_count); $all['except_count'] += ncPriceFormat($except_count); } $result[] = ['总计', $all['success_count'], $all['success_amounts'], $all['cancel_count'], $all['except_count']]; } return $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']; $cond['mchid'] = $this->mchid(); 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); } }