Browse Source

数据导出

xiaosongshu007 4 years ago
parent
commit
1f9008728b

+ 148 - 0
Application/Admin/Controller/ExportDataController.class.php

@@ -0,0 +1,148 @@
+<?php
+
+namespace Admin\Controller;
+
+use Think\Model;
+
+/**
+ * 数据导出模块
+ * @author   xusong
+ * @version  0.0.1
+ * @datetime 2016-12-01T21:51:08+0800
+ */
+class ExportDataController extends CommonController {
+
+    /**
+     * [_initialize 前置操作-继承公共前置方法]
+     * @author   Devil
+     * @blog     http://gong.gg/
+     * @version  0.0.1
+     * @datetime 2016-12-03T12:39:08+0800
+     */
+    private $actions; 
+    public function _initialize() {
+        // 调用父类前置方法
+        parent::_initialize();
+        $this->actions = $this->getRegisterAction();
+    }
+
+    public function index()
+    {
+        $this->display();
+    }
+
+    private function getRegisterAction()
+    {
+        return $actions = array(
+            'user_record'=>'用户数据',
+        );
+    }
+
+    public function doAction()
+    {   
+        $action = I('action');
+        if($this->actions[$action]){
+            $this->$action();
+        }else{
+            echo '请注册操作';
+        }
+    }
+    
+    /**
+     * 导出片单
+     * @param string start_date '2019-01-18'
+     * @param string end_date   '2019-02-22'
+     */
+    public function user_record()
+    {   
+        Vendor('PHPExcel.PHPExcel');
+        $obpe = new \PHPExcel();
+
+        if(!$uid = trim(I('uid'))){
+            exit('用户不存在');
+        }
+
+        //登录历史
+        $data['login'] = M('iptv_user_login_log')->field('uid,created_at')->where(['uid'=>$uid])->order('created_at asc')->select();
+        $login_title = ['账号','登录时间'];
+        array_unshift($data['login'], $login_title);
+        
+        //订购历史
+        $data['order'] = M('iptv_order')->field('uid,created_at')->where(['uid'=>$uid])->order('created_at asc')->select();
+        $order_title = ['账号','订购时间'];
+        array_unshift($data['order'], $order_title);
+
+        //观看历史
+        $data['watch'] = M('iptv_watch_log wl')
+            ->field('wl.uid,wl.created_at,s.name as source_name')
+            ->join('__SOURCE__ s on wl.source_id = s.id','left')
+            ->where(['wl.uid'=>$uid])
+            ->order('wl.created_at asc')
+            ->select();
+        $watch_title = ['账号','观看时间','节目名称'];
+        array_unshift($data['watch'], $watch_title);
+                   
+        /* @func 设置文档基本属性 */
+        // $obpe_pro = $obpe->getProperties();
+        // $obpe_pro->setCreator('midoks')//设置创建者
+        //          ->setLastModifiedBy('2013/2/16 15:00')//设置时间
+        //          ->setTitle('data')//设置标题
+        //          ->setSubject('beizhu')//设置备注
+        //          ->setDescription('miaoshu')//设置描述
+        //          ->setKeywords('keyword')//设置关键字 | 标记
+        //          ->setCategory('catagory');//设置类别
+
+        $index = 0;
+
+        foreach ($data as $type => $value) {
+
+            if($index != 0){
+                $obpe->createSheet();
+            }
+            $obpe->setactivesheetindex($index);
+            if($type == 'watch'){
+                $obpe->getActiveSheet()->setTitle('观看记录');
+            }
+            if($type == 'login'){
+                $obpe->getActiveSheet()->setTitle('登录记录');
+            }
+            if($type == 'order'){
+                $obpe->getActiveSheet()->setTitle('订购记录');
+            }
+            $number = 0;
+            foreach($value as $key2=>$value2){
+                /* @func 设置列 */
+                $key = 0;
+                $number = $number + 1;
+                foreach ($value2 as $value3) {
+                    $code = strtoupper(chr(65 + $key));//输出大写字母
+                    $obpe->getactivesheet()->setcellvalue($code.$number, $value3);
+                    $key = $key+1;
+                }
+            }
+            $index ++;
+        }
+        //写入类容
+        $obwrite = \PHPExcel_IOFactory::createWriter($obpe, 'Excel5');
+        //保存文件
+        $fileName = '投诉'.$uid.date('YmdHis').'.xls';
+        $obwrite->save($fileName);
+
+        header( 'Content-Description: File Transfer' );  
+        header( 'Content-Type: application/octet-stream' );  
+        header( 'Content-Disposition: attachment;filename = ' . $fileName);  
+        header( 'Content-Transfer-Encoding: binary' );  
+        header( 'Expires: 0' );  
+        header( 'Cache-Control: must-revalidate, post-check = 0, pre-check = 0' );  
+        header( 'Pragma: public' );  
+        header( 'Content-Length: ' . filesize( $fileName ) );  
+        ob_clean();  
+        flush(); 
+        readfile( $fileName ); 
+
+        unlink($fileName);
+
+    }
+    
+    
+}

+ 10 - 5
Application/Admin/Controller/PrizeLogV2Controller.class.php

@@ -50,7 +50,11 @@ class PrizeLogV2Controller extends CommonController {
         if($prize_status>-1){
             $where['pl.prize_status'] = ['egt',1];
         }
-        $listRows = I('listRows',100,'intval');
+        if(I('model')=='exportExcel'){
+            $listRows = I('listRows',100000,'intval');
+        }else{
+            $listRows = I('listRows',100,'intval');
+        }
         if($p = I('p',1,'intval')){
             $offset = ($p-1)*$listRows;
         }
@@ -85,6 +89,7 @@ class PrizeLogV2Controller extends CommonController {
         }
 //        var_dump($activity_info);die;
         foreach ($List as $key=>$row) {
+            $List[$key]['id'] = $key + 1;
             $List[$key]['activity_name'] = $activity_info[$row['act_id']]['activity_name'];
             $List[$key]['prize_name'] = $activity_info[$row['act_id']]['prize_list'][$row['prize_id']]['prize_name'];
         }
@@ -114,15 +119,15 @@ class PrizeLogV2Controller extends CommonController {
     public function exportPrizeLog($data)
     {   
         $title = array(
-            'id'                                 =>  array('col' => 'A', 'name' => '抽奖记录ID'),
-            'iptv_user_id'                       =>  array('col' => 'B', 'name' => '用户ID'),
-            'prize_name'                         =>  array('col' => 'C', 'name' => '品'),
+            'id'                                 =>  array('col' => 'A', 'name' => '序号'),
+            'uid'                                =>  array('col' => 'B', 'name' => '用户ID'),
+            'prize_name'                         =>  array('col' => 'C', 'name' => '奖品名称'),
             'user_phone'                         =>  array('col' => 'D', 'name' => '联系方式'),
             'activity_name'                      =>  array('col' => 'E', 'name' => '活动名称'),
             'created_at'                         =>  array('col' => 'F', 'name' => '中奖时间'),
         );    
         // Excel驱动导出数据
-        $excel = new \My\Excel(array('filename' => '中奖记录', 'title' => $title, 'data' => $data, 'msg' => L('common_not_data_tips')));
+        $excel = new \My\Excel(array('filename' => '芒果移动活动中奖记录', 'title' => $title, 'data' => $data, 'msg' => L('common_not_data_tips')));
         $excel->Export();          
     }
 

+ 10 - 10
Application/Admin/View/Default/PrizeLogV2/Index.html

@@ -4,7 +4,7 @@
 
 <!-- right content start  -->
 <div class="content-right">
-	<div class="content">
+    <div class="content">
             <!-- form start -->
             <form class="am-form view-list" action="{{:U('Admin/PrizeLogV2/Index')}}" method="post">
                 <div class="am-g">
@@ -27,7 +27,7 @@
                     <button type="submit" class="am-btn am-btn-secondary am-btn-sm am-radius form-submit">查询</button>
                 </div>
             </form>
-		<table class="am-table am-table-striped am-table-hover am-text-middle m-t-10">
+        <table class="am-table am-table-striped am-table-hover am-text-middle m-t-10">
                     <thead>
                         <tr>
                             <th>ID</th>
@@ -64,13 +64,13 @@
                                     <tr><td colspan="10" class="table-no">{{$act_id ? '没有记录':'请先选择活动'}}</td></tr>
                             </if>
                     </tbody>
-		</table>
-		<!-- list end -->
+        </table>
+        <!-- list end -->
 
-		<!-- page start -->
-		<div id="pages" class="p">{{$page}}</div>
-		<!-- page end -->
-	</div>
+        <!-- page start -->
+        <div id="pages" class="p">{{$page}}</div>
+        <!-- page end -->
+    </div>
 </div>
 <!-- right content end  -->
 <script type="text/javascript">
@@ -80,11 +80,11 @@ $("#export").on("click",function(){
         var act_id = $("select[name=act_id]").val();
         
         var params = '&keyword='+keyword+'&prize_status='+prize_status+'&act_id='+act_id;
-        var url = 'index.php?m=Admin&c=PrizeLog&a=Index&model=exportExcel'+params;
+        var url = 'index.php?m=Admin&c=PrizeLogV2&a=Index&model=exportExcel'+params;
 //        alert(url);return ;
         window.open(url);
 });    
-</script>		
+</script>       
 <!-- footer start -->
 <include file="Public/Footer" />
 <!-- footer end