xiaoyu 3 anni fa
parent
commit
63788fc182

+ 28 - 14
admin/control/orderstats.php

@@ -142,7 +142,13 @@ class orderstatsControl extends SystemControl
     public function refill_taskOp()
     {
         $model = Model('refill_task');
-        $condition = [];
+        $condition['is_del'] = 0;
+        if(!empty($_GET['type'])) {
+            $condition['type'] = $_GET['type'];
+        }
+        if(!empty($_GET['cid'])) {
+            $condition['cid'] = $_GET['cid'];
+        }
         $task_list = $model->getRefillTaskList($condition, 50);
         foreach ($task_list as $key => $value) {
             $task_list[$key]['condition'] = unserialize($value['task_condition']);
@@ -154,6 +160,7 @@ class orderstatsControl extends SystemControl
         Tpl::output('task_type', $this->task_type);
         Tpl::output('type', $this->type);
         Tpl::output('task_state', $this->task_state);
+        Tpl::output('order_time_type', $this->order_time_type);
         Tpl::showpage('refill.task');
     }
 
@@ -165,7 +172,7 @@ class orderstatsControl extends SystemControl
             $obj_validate->validateparam = [
                 ["input" => $_POST["task_type"], "require" => "true", "message" => '请选择任务类型'],
                 ["input" => $_POST["type"], "require" => "true", "message" => '请选择主体类型'],
-                ["input" => $_POST["cid"], "require" => "true", "message" => '请选择统计主体'],
+                ["input" => $_POST["body"], "require" => "true", "message" => '请选择统计主体'],
                 ["input" => $_POST["order_time_type"], "require" => "true", "message" => '请选择日期类型'],
                 ["input" => $_POST["query_start_time"], "require" => "true", "message" => '请选择统计日期'],
             ];
@@ -173,23 +180,18 @@ class orderstatsControl extends SystemControl
             if ($error != '') {
                 showMessage($error, '', '', 'error');
             }
-
+            $body = explode('-', $_POST['body']);
             $insert['task_type'] = $_POST['task_type'];
             $insert['type'] = $_POST['type'];
-            $insert['cid'] = $_POST['cid'];
+            $insert['cid'] = $body[0];
+            $insert['cname'] = $body[1];
             $insert['add_time'] = time();
 
             $order_time_type = $_POST['order_time_type'];
-            $start_unixtime = intval(strtotime($_POST['query_start_time']));
-            $end_unixtime = intval(strtotime($_POST['query_end_time']));
-            if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
-                $condition["refill_order.{$order_time_type}"] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
-            } elseif ($start_unixtime > 0) {
-                $condition["refill_order.{$order_time_type}"] = ['egt', $start_unixtime];
-            }
-            if(!empty($_POST['order_state'])) {
-                $condition['order_state'] = $_POST['order_state'];
-            }
+            $condition['start_unixtime'] = intval(strtotime($_POST['query_start_time']));
+            $condition['end_unixtime'] = intval(strtotime($_POST['query_end_time']));
+            $condition['order_time_type'] = $order_time_type;
+            $condition['order_state'] = $_POST['order_state'] ?? '';
 
             $hash = $this->task_hash_check($condition, $insert['task_type'], $insert['type']);
             if($hash === false) {
@@ -226,4 +228,16 @@ class orderstatsControl extends SystemControl
             return false;
         }
     }
+
+    public function task_delOp()
+    {
+        $task_id = $_GET['task_id'];
+        $model = Model('refill_task');
+        $resp = $model->TaskDel($task_id);
+        if($resp) {
+            showMessage('删除成功', 'index.php?act=OrderStats&&op=refill_task');
+        }else{
+            showMessage('任务删除失败');
+        }
+    }
 }

+ 5 - 5
admin/templates/default/refill.task.add.php

@@ -86,7 +86,7 @@
             <tr class="noborder" >
                 <td class="layui-form">
                     <label for="selection"></label>
-                    <select name="cid" id="selection" lay-search></select>
+                    <select name="body" id="selection" lay-search></select>
                 </td>
                 <td class="vatop tips"></td>
             </tr>
@@ -182,7 +182,7 @@
                         res = JSON.parse(res) 
                         let html = '';
                         res.forEach(item => {
-                        html += `<option value="${item.value}">${item.name}</option>`;
+                        html += `<option value="${item.value}-${item.name}">${item.name}</option>`;
                         })
                         let select_type = `<select>${html}</select>`
                         $('#selection').html(select_type);
@@ -193,7 +193,7 @@
                         res = JSON.parse(res) 
                         let html = '';
                         res.forEach(item => {
-                        html += `<option value="${item.value}">${item.name}</option>`;
+                        html += `<option value="${item.value}-${item.name}">${item.name}</option>`;
                         })
                         let select_type = `<select>${html}</select>`
                         $('#selection').html(select_type);
@@ -216,7 +216,7 @@
                 order_time_type: {
                     required: true
                 },
-                cid: {
+                body: {
                     required: true
                 },
                 task_type: {
@@ -233,7 +233,7 @@
                 order_time_type: {
                     required: '日期类型未选择'
                 },
-                cid: {
+                body: {
                     required: '未选择统计主体'
                 },
                 task_type: {

+ 90 - 38
admin/templates/default/refill.task.php

@@ -53,7 +53,7 @@
         min-width: 883% !important;
         max-height: 280px !important;
         padding: 14px 0 !important;
-        left: -599px !important;
+        left: 0 !important;
     }
     .layui-form-select {
         width: 45%;
@@ -123,52 +123,28 @@
     </div>
     <div class="fixed-empty"></div>
     <form method="get" action="index.php" name="formSearch" id="formSearch">
-        <input type="hidden" name="act" value="order_search"/>
-        <input type="hidden" name="op" value="index"/>
+        <input type="hidden" name="act" value="orderstats"/>
+        <input type="hidden" name="op" value="refill_task"/>
         <table class="tb-type1 noborder search">
             <tr>
-                <th><label for="query_start_time">下单时间</label></th>
-                <td style="width: 327px">
-                    <input class="txt date" type="text" value="<?php echo $_GET['query_start_time']; ?>"
-                           id="startTime" name="query_start_time" autocomplete="off" style="width:120px" />
-                    <label for="query_start_time">~</label>
-                    <input class="txt date" type="text" value="<?php echo $_GET['query_end_time']; ?>"
-                           id="endTime" name="query_end_time" autocomplete="off" style="width:120px" />
-                </td>
-                <th><label>订单状态</label></th>
-                <td class="layui-form type">
-                    <select name="order_state" class="querySelect">
-                        <option value=""><?php echo $lang['nc_please_choose']; ?></option>
-                        <option value="10"
-                                <?php if ($_GET['order_state'] == '10'){ ?>selected<?php } ?>><?php echo $lang['order_state_new']; ?></option>
-                        <option value="20"
-                                <?php if ($_GET['order_state'] == '20'){ ?>selected<?php } ?>><?php echo $lang['order_state_pay']; ?></option>
-                        <option value="30"
-                                <?php if ($_GET['order_state'] == '30'){ ?>selected<?php } ?>><?php echo $lang['order_state_send']; ?></option>
-                        <option value="40"
-                                <?php if ($_GET['order_state'] == '40'){ ?>selected<?php } ?>><?php echo $lang['order_state_success']; ?></option>
-                        <option value="0"
-                                <?php if ($_GET['order_state'] == '0'){ ?>selected<?php } ?>><?php echo $lang['order_state_cancel']; ?></option>
-                    </select>
-                </td>
                 <th><label>主体类型</label></th>
                 <td class="layui-form type">
                     <select name="type" id="type" lay-filter="type">
                         <option value="">请选择</option>
                         <?php foreach ($output['type'] as $key => $value){?>
-                        <option value="<?php echo $key;?>"><?php echo $value;?></option>
+                        <option value="<?php echo $key;?>" <?php if($_GET['type'] === $key) { echo 'selected'; }?>><?php echo $value;?></option>
                         <?php }?>
                     </select>
                 </td>
                 <th><label>主体选择</label></th>
                 <td class="layui-form selection">
-                    <select name="cid" id="selection" lay-search></select>
+                    <select name="cid" id="cid" lay-filter="cid" lay-search></select>
                 </td>
-                <!-- <td>
+                <td>
                     <a href="javascript:void(0);" id="ncsubmit" class="btn-search "
                        title="<?php echo $lang['nc_query']; ?>">&nbsp;
                     </a>
-                </td> -->
+                </td>
             </tr>
         </table>
     </form>
@@ -181,6 +157,7 @@
             <th class="align-center">任务类型</th>
             <th class="align-center">主体类型</th>
             <th class="align-center">主体ID</th>
+            <th class="align-center">主体名称</th>
             <th class="align-center">任务条件</th>
             <th class="align-center">任务状态</th>
             <th class="align-center">生成日期</th>
@@ -199,14 +176,34 @@
                     <td class="align-center"><?php echo $output['task_type'][$value['task_type']]; ?></td>
                     <td class="align-center"><?php echo $output['type'][$value['type']]; ?></td>
                     <td class="align-center"><?php echo $value['cid']; ?></td>
+                    <td class="align-center"><?php echo $value['cname']; ?></td>
                     <td class="align-center">
-
+                        <p>统计日期类型:<?php echo $output['order_time_type'][$value['condition']['order_time_type']];?></p>
+                        <p>
+                            统计日期范围:<?php echo date("Y-m-d H:i:s", $value['condition']['start_unixtime'])?>
+                            <?php if (!empty($value['condition']['end_unixtime'])) {
+                                echo '~' . date("Y-m-d H:i:s", $value['condition']['end_unixtime']);
+                            }
+                            ?>
+                        </p>
+                        <p>订单状态:
+                            <?php if ($value['condition']['order_state'] === '') {
+                                echo '/';
+                            } else {
+                                echo orderState($value['condition']);
+                            }
+                            ?>
+                        </p>
                     </td>
                     <td class="align-center"><?php echo $output['task_state'][$value['task_state']]; ?></td>
                     <td class="align-center"><?php echo $value['add_time'] ? date('Y-m-d H:i:s', $value['add_time']) : '/'; ?></td>
                     <td class="align-center"><?php echo $value['dispose_time'] ? date('Y-m-d H:i:s', $value['dispose_time']) : '/'; ?></td>
                     <td class="align-center"><?php echo $value['finish_time'] ? date('Y-m-d H:i:s', $value['finish_time']) : '/'; ?></td>
-                    <td class="align-center"></td>
+                    <td class="align-center">
+                        <?php if($value['task_state'] == 3 && !empty($value['task_result'])){?>
+                            <a target="_blank" href="<?php echo UPLOAD_SITE_URL . '/' . ATTACH_TASK . DS . $value['task_result']; ?>">下载</a>
+                        <?php }?>
+                    </td>
                 </tr>
             <?php } ?>
         <?php } else { ?>
@@ -235,6 +232,7 @@
       <script type="text/javascript" src="<?php echo ADMIN_TEMPLATES_URL;?>/layui/layui.js"></script>
 <link rel="stylesheet" type="text/css" href="<?php echo ADMIN_TEMPLATES_URL; ?>/layui/css/layui.css"/>
 <script type="text/javascript">
+
     $(function () {
         $('#ncsubmit').click(function () {
             $('#formSearch').submit();
@@ -268,11 +266,55 @@
                 $(this).css('color', 'red')
             }
         })
-            //主体选择
+            
+        let selectValue = ''
+        let selectRes = {}
+    
+        let selectNew = $("#cid").next().find("input")
+                    
+                    for (let i = 0; i < selectRes.length; i++) {
+                        if (selectRes[i].value == selectValue) {
+                            console.log(selectValue, 12333);
+                            $(selectNew).val(selectRes[i].value)
+                            selectRes[i].selected = true
+                    }
+                }
+
+
+
+        //主体选择 
         layui.use(['form'], function () {
             let form = layui.form;
+            let default_type = $('select[name="type"]').val();
+                if (default_type === 'merchant') {
+                $.get('index.php?act=refill_order&op=merchant_data', function(res) {
+                    res = JSON.parse(res) 
+                    let html = '';
+                    res.forEach(item => {
+                    html += `<option value="${item.value}">${item.name}</option>`;
+                    })
+                    let select_type = `<select>${html}</select>`
+                    $('#cid').html(select_type);
+                    form.render('select')
+                })
+            } else if (default_type === 'provider'){
+                $.get('index.php?act=refill_order&op=provider_data', function(res) {
+                    res = JSON.parse(res) 
+                    let html = '';
+                    res.forEach(item => {
+                    html += `<option value="${item.value}">${item.name}</option>`;
+                    })
+                    let select_type = `<select>${html}</select>`
+                    $('#cid').html(select_type);
+                    form.render('select')
+                })
+            } else {
+                    $('#cid').html('');
+                    form.render('select')
+                }
             form.on('select(type)', function (data) {
             let form_type = data.value;
+            selectValue = data.value
             form.render('select')
                 if (form_type === 'merchant') {
                 $.get('index.php?act=refill_order&op=merchant_data', function(res) {
@@ -282,22 +324,32 @@
                     html += `<option value="${item.value}">${item.name}</option>`;
                     })
                     let select_type = `<select>${html}</select>`
-                    $('#selection').html(select_type);
+                    $('#cid').html(select_type);
                     form.render('select')
+                    
                 })
             } else if (form_type === 'provider'){
                 $.get('index.php?act=refill_order&op=provider_data', function(res) {
-                    res = JSON.parse(res) 
+                    res = JSON.parse(res)
+                    selectRes = res 
                     let html = '';
                     res.forEach(item => {
                     html += `<option value="${item.value}">${item.name}</option>`;
                     })
                     let select_type = `<select>${html}</select>`
-                    $('#selection').html(select_type);
+                    $('#cid').html(select_type);
+                    form.on('select(cid)', function (data) {
+                    let form_type = data.value;
+                    console.log('xuang',form_type);
+                    $("#cid dl").find("dd[lay-value=" +form_type+"]").attr("selected",true);
                     form.render('select')
+                    
+            })
+            form.render('select')
+
                 })
             } else {
-                    $('#selection').html('');
+                    $('#cid').html('');
                     form.render('select')
                 }
             })

+ 174 - 0
crontab/control/minutes.php

@@ -14,6 +14,7 @@ require_once(BASE_HELPER_PATH . '/refill/util.php');
 require_once(BASE_HELPER_PATH . '/refill/divert_account.php');
 require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
 require_once(BASE_CORE_PATH . '/framework/function/http.php');
+require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
 
 
 class minutesControl extends BaseCronControl
@@ -379,6 +380,7 @@ class minutesControl extends BaseCronControl
             do{
                 try {
                     $ret = $this->_cron_common([8]);
+                    $this->_refill_task();
                     perfor_clear();
                 } catch (Exception $ex) {
                     Log::record($ex->getMessage(),Log::ERR);
@@ -639,4 +641,176 @@ class minutesControl extends BaseCronControl
             }
         }
     }
+
+    public function _refill_task(): bool
+    {
+        Log::record(__FUNCTION__,Log::DEBUG);
+        $model_refill_task = Model('refill_task');
+
+        $task = $model_refill_task->TaskUnDispose('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->TaskDispose($task_id);
+        [$state, $task_result] = $this->order_export($condition, $type, $task_id);
+        if ($state) {
+            $model_refill_task->TaskDisposeFinish($task_id, $task_result);
+        } else {
+            $model_refill_task->TaskDisposeErr($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;
+    }
 }

+ 13 - 5
data/model/refill_task.model.php

@@ -13,7 +13,7 @@ class refill_taskModel extends Model
         parent::__construct('refill_task');
     }
 
-    public function getRefillTaskList($condition, $pagesize = '', $field = '*', $order = 'add_time desc', $limit = '', $master = false)
+    public function getRefillTaskList($condition, $pagesize = '', $field = '*', $order = 'add_time desc', $limit = '', $master = false): array
     {
         $condition['is_del'] = 0;
         $list = $this->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->master($master)->select();
@@ -33,7 +33,7 @@ class refill_taskModel extends Model
         return empty($this->where($condition)->find());
     }
 
-    public function TaskUnDispose($task_type)
+    public function TaskUnDispose($task_type): array
     {
         $condition['is_del'] = 0;
         $condition['task_state'] = 1;
@@ -48,7 +48,7 @@ class refill_taskModel extends Model
         $condition['is_del'] = 0;
         $condition['task_id'] = $task_id;
         return $this->where($condition)->update(
-            ['task_state' => self::DisposeState]
+            ['task_state' => self::DisposeState, 'dispose_time' => time()]
         );
     }
 
@@ -57,7 +57,7 @@ class refill_taskModel extends Model
         $condition['is_del'] = 0;
         $condition['task_id'] = $task_id;
         return $this->where($condition)->update(
-            ['task_state' => self::ErrState, 'task_result' => $err_msg]
+            ['task_state' => self::ErrState, 'task_result' => $err_msg, 'finish_time' => time()]
         );
     }
 
@@ -66,7 +66,15 @@ class refill_taskModel extends Model
         $condition['is_del'] = 0;
         $condition['task_id'] = $task_id;
         return $this->where($condition)->update(
-            ['task_state' => self::FinishState, 'task_result' => $result]
+            ['task_state' => self::FinishState, 'task_result' => $result, 'finish_time' => time()]
+        );
+    }
+
+    public function TaskDel($task_id)
+    {
+        $condition['task_id'] = $task_id;
+        return $this->where($condition)->update(
+            ['is_del' => 1]
         );
     }
 }

+ 1 - 0
global.php

@@ -64,6 +64,7 @@ define('ATTACH_CIRCLE','circle');
 define('ATTACH_CMS','cms');
 define('ATTACH_LIVE','live');
 define('ATTACH_UPFILE','upfile');
+define('ATTACH_TASK','task');
 define('ATTACH_REFILL_EVIDENCE','refill_evidence');
 define('ATTACH_MALBUM', ATTACH_PATH.'/member');
 define('ATTACH_MQRCODE',ATTACH_PATH.'/qrcode');

+ 91 - 32
test/TestExportOrder.php

@@ -80,11 +80,11 @@ class TestExportOrder extends TestCase
         $model_refill_task = Model('refill_task');
 
         $task = $model_refill_task->TaskUnDispose('order_export');
-        if(empty($task)) return false;
+        if (empty($task)) return false;
 
         $task_id = $task['task_id'];
         $task_condition = unserialize($task['task_condition']);
-        if(empty($task_condition)) {
+        if (empty($task_condition)) {
             Log::record("refill task condition unserialize err, task_id:{$task_id}");
             return false;
         }
@@ -92,57 +92,76 @@ class TestExportOrder extends TestCase
         $type = $task['type'];
         $cid = $task['cid'];
         if ($type === 'merchant') {
-            $task_condition['refill_order.mchid'] = $cid;
+            $condition['refill_order.mchid'] = $cid;
         } elseif ($type === 'provider') {
-            $task_condition['vr_order.store_id'] = $cid;
+            $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->TaskDispose($task_id);
-        [$state,$task_result] = $this->order_export($task_condition, $type, $task_id);
-        if($state) {
+        [$state, $task_result] = $this->order_export($condition, $type, $task_id);
+        if ($state) {
             $model_refill_task->TaskDisposeFinish($task_id, $task_result);
-        }else{
+        } else {
             $model_refill_task->TaskDisposeErr($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);
-        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
-        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
-        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
-        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
-        $objPHPExcel->getActiveSheet()
-            ->setCellValue('A1', '第一列')
-            ->setCellValue('B1', '第二列')
-            ->setCellValue('C1', '第三列')
-            ->setCellValue('D1', '第四列');
+        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) {
-            $field1 = 'A'.($k+2);
-            $objPHPExcel->getActiveSheet()->setCellValueExplicit($field1, $order['order_id'],
-                PHPExcel_Cell_DataType::TYPE_STRING);
-
-            $field2 = 'B'.($k+2);
-            $objPHPExcel->getActiveSheet()->setCellValueExplicit($field2, $order['order_sn'],
-                PHPExcel_Cell_DataType::TYPE_STRING);
-
-            $field3 = 'C'.($k+2);
-            $objPHPExcel->getActiveSheet()->setCellValueExplicit($field3, $order['card_no'],
-                PHPExcel_Cell_DataType::TYPE_STRING);
-
-            $field4 = 'D'.($k+2);
-            $objPHPExcel->getActiveSheet()->setCellValueExplicit($field4,$order['mch_order'],
-                PHPExcel_Cell_DataType::TYPE_STRING);
+            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/";
@@ -187,6 +206,46 @@ class TestExportOrder extends TestCase
         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;}';