123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- use PHPUnit\Framework\TestCase;
- use statistics\stat_refill;
- define('APP_ID', 'test');
- define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
- require_once(BASE_ROOT_PATH . '/global.php');
- require_once(BASE_CORE_PATH . '/lrlz.php');
- require_once(BASE_ROOT_PATH . '/fooder.php');
- require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
- class TestExportOrder extends TestCase
- {
- public static function setUpBeforeClass() : void
- {
- Base::run_util();
- }
- public function testXingzhiyu()
- {
- $end_time = strtotime("2021-06-15");
- $this->exoprt(10132,null,$end_time);
- }
- public function exoprt($mchid,$start_time = null,$end_time = null)
- {
- $cond['mchid'] = $mchid;
- $cond['vr_order.order_state'] = 40;
- if(!is_null($start_time) && !is_null($end_time)) {
- $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $start_time], ['lt', $end_time]];
- }
- elseif(!is_null($start_time)) {
- $cond["refill_order.notify_time"] = ['egt', $start_time];
- }
- elseif(!is_null($end_time)) {
- $cond["refill_order.notify_time"] = ['lt', $end_time];
- }
- $path = BASE_DATA_PATH . "/log/{$mchid}.csv";
- $fp = fopen($path, 'w');
- fputcsv($fp,['序号','商户订单号','卡号','面额','流水号',"下单时间","回调时间"]);
- $count = 1000;
- $i = 0;
- $index = 0;
- while (true)
- {
- $start = $i * $count;
- $items = Model('')->table('refill_order,vr_order')
- ->field('mch_order,refill_order.card_no,refill_amount,mch_amount,official_sn,from_unixtime(order_time) as sorder_time,from_unixtime(notify_time) as snotify_time')
- ->join('inner')
- ->on('refill_order.order_id=vr_order.order_id')
- ->where($cond)
- ->limit("{$start},{$count}")
- ->select();
- if(empty($items)) {
- break;
- }
- $i++;
- foreach ($items as $item)
- {
- $index++;
- $data = [$index,$item['mch_order'],$item['card_no'],$item['refill_amount'],$item['mch_amount'],$item['official_sn'],$item['sorder_time'],$item['snotify_time']];
- fputcsv($fp,$data);
- }
- }
- fclose($fp);
- }
- public function testRefillTask()
- {
- $model_refill_task = Model('refill_task');
- $task = $model_refill_task->UnDispose('order_export');
- if (empty($task)) return false;
- $task_id = $task['task_id'];
- $task_condition = unserialize($task['task_condition']);
- if (empty($task_condition)) {
- Log::record("refill task condition unserialize err, task_id:{$task_id}");
- return false;
- }
- $type = $task['type'];
- $cid = $task['cid'];
- if ($type === 'merchant') {
- $condition['refill_order.mchid'] = $cid;
- } elseif ($type === 'provider') {
- $condition['vr_order.store_id'] = $cid;
- } else {
- Log::record("refill task type err, task_id:{$task_id}");
- return false;
- }
- $order_time_type = $task_condition['order_time_type'];
- $start_unixtime = $task_condition['start_unixtime'];
- $end_unixtime = $task_condition['end_unixtime'];
- $order_state = $task_condition['order_state'];
- if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
- $condition["refill_order.{$order_time_type}"] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
- $condition['vr_order.add_time'] = ['egt', $start_unixtime-1];
- } elseif ($start_unixtime > 0) {
- $condition["refill_order.{$order_time_type}"] = ['egt', $start_unixtime];
- $condition['vr_order.add_time'] = ['egt', $start_unixtime-1];
- }
- if (in_array($order_state, ['0', '10', '20', '30', '40'], true)) {
- $condition['order_state'] = $order_state;
- }
- $model_refill_task->Dispose($task_id);
- [$state, $task_result] = $this->order_export($condition, $type, $task_id);
- if ($state) {
- $model_refill_task->DisposeFinish($task_id, $task_result);
- } else {
- $model_refill_task->DisposeErr($task_id, $task_result);
- }
- return true;
- }
- /**
- * @throws PHPExcel_Exception
- */
- private function order_export($condition, $type, $task_id)
- {
- $orders = $this->getAllOrders($condition);
- if(empty($orders)) {
- return [false, '统计数据为空'];
- }
- $orders = $this->orderFormat($orders);
- $objPHPExcel = new PHPExcel();
- if ($type === 'merchant') {
- $column_keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
- $column_values = ['商户号', '客户订单号', '平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额'];
- $data_keys = ['mchid', 'mch_order', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'mch_amount'];
- } elseif ($type === 'provider') {
- $column_keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'];
- $column_values = ['上游订单号', '平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额'];
- $data_keys = ['ch_trade_no', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'channel_amount'];
- } else {
- return [false, '主体类型错误'];
- }
- $objPHPExcel->setActiveSheetIndex(0);
- $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
- foreach ($column_keys as $key => $column_key) {
- $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(25);
- $cell_value = $column_key . 1;
- $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
- }
- foreach ($orders as $k => $order) {
- foreach ($column_keys as $key => $column_key) {
- $field = $column_key.($k+2);
- $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]],
- PHPExcel_Cell_DataType::TYPE_STRING);
- }
- }
- try {
- $path = BASE_ROOT_PATH . "/data/upload/task/";
- if(!is_dir($path)){
- mkdir($path, 0755);
- }
- $filename = date('YmdHis',time())."-任务导出-任务ID:{$task_id}.xlsx";
- $file_path = $path . $filename;
- $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
- $objWriter->save($file_path);
- return [true, $filename];
- } catch (Exception $e) {
- return [false, $e->getMessage()];
- }
- }
- 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 orderFormat($orders): array
- {
- $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
- $orderState = function ($order_info) {
- $is_retrying = $order_info['is_retrying'];
- switch ($order_info['order_state']) {
- case ORDER_STATE_CANCEL:
- if ($is_retrying) {
- $order_state = '重试中';
- } else {
- $order_state = '已取消';
- }
- break;
- case ORDER_STATE_NEW:
- $order_state = '待付款';
- break;
- case ORDER_STATE_PAY:
- $order_state = '待发货';
- break;
- case ORDER_STATE_SEND:
- $order_state = '待收货';
- break;
- case ORDER_STATE_SUCCESS:
- $order_state = '交易完成';
- break;
- default :
- $order_state = '未知状态';
- }
- return $order_state;
- };
- foreach($orders as $key => $order)
- {
- $orders[$key]['card_type_text'] = $card_type_texts[$order['card_type']];
- $orders[$key]['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
- $orders[$key]['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
- $orders[$key]['order_state_text'] = $orderState($order);
- }
- return $orders;
- }
- public function testStrSer()
- {
- $str = 'a:4:{s:9:"qualities";a:3:{i:1;a:3:{s:8:"day_secs";i:3600;s:10:"night_secs";i:1800;s:5:"times";i:50;}i:2;a:3:{s:8:"day_secs";i:600;s:10:"night_secs";i:300;s:5:"times";i:10;}i:3;a:3:{s:8:"day_secs";i:900;s:10:"night_secs";i:600;s:5:"times";i:1;}}s:11:"lower_ratio";a:2:{s:5:"ratio";d:0.9;s:6:"period";i:3600;}s:12:"profit_ratio";d:0.03;s:6:"opened";b:1;}';
- $str = 'a:3:{s:11:"is_transfer";b:1;s:11:"card_states";a:2:{i:0;i:0;i:1;i:5;}s:10:"card_types";a:1:{i:0;i:4;}}';
- $arr = unserialize($str);
- }
- }
- //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestExportOrder::testXingzhiyu)( .*)?$/" --test-suffix TestExportOrder.php /var/www/html/test
|