<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

use User\Api\UserApi;
use Com\Wechat;
use Com\WechatAuth;

/**
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class UserActionLogController extends AdminController
{

   public function index($p = 0) {

       $page = intval($p);
       $page = $page ? $page : 1; //默认显示第一页数据
       $arraypage = $page;

       if (isset($_REQUEST['row'])) {
           $row = $_REQUEST['row'];
       } else {
           $row = 10;
       }

       $map = [];

       if ($_REQUEST['time_start']&&!$_REQUEST['time_end']) {
           $map['create_time'] = ['egt',strtotime($_REQUEST['time_start'])];
       }

       if (!$_REQUEST['time_start']&&$_REQUEST['time_end']) {
           $map['create_time'] = ['elt',strtotime($_REQUEST['time_end'])+86399];
       }

       if ($_REQUEST['time_start']&&$_REQUEST['time_end']) {
           $map['create_time'] = ['between',[strtotime($_REQUEST['time_start']),strtotime($_REQUEST['time_end'])+86399]];
//           $map['create_time'] = ['elt',strtotime($_REQUEST['time_end'])];
       }

       if ($_REQUEST['username']) {
           $map['account'] = ['like',"%{$_REQUEST['username']}%"];
       }

       if ($_REQUEST['game_name']) {
           $map['game_name'] = ['like',"%{$_REQUEST['game_name']}%"];
       }

       $data = M('user_action_log','tab_')->where($map)->page($page,$row)->order('create_time DESC')->select();

       foreach($data as $key=>$value) {
           $remarks = json_decode($value['remarks'],true);

           $action = json_decode($value['action'],true);

           if ($value['type'] == 3) {
                $data[$key]['action_title'] = '登录 '.($remarks['server_name']?$remarks['server_name']:'无').'-'.($remarks['role_name']?$remarks['role_name']:'无').'-'.($remarks['role_level']?$remarks['role_level']:'无');
            } else if ($value['type'] == 4) {
                $data[$key]['action_title'] = '退出 '.($remarks['server_name']?$remarks['server_name']:'无').'-'.($remarks['role_name']?$remarks['role_name']:'无').'-'.($remarks['role_level']?$remarks['role_level']:'无');
            } else if ($value['type'] == 0) {
               $data[$key]['action_title'] = '修改密码';
               if (!$action['old_data']) {
                   $data[$key]['action_content'] = "-------------------";
               } else {
                   $data[$key]['action_content'] = "旧值:{$action['old_data']}<br>新值:{$action['new_data']}";
               }

           } else if ($value['type'] == 1) {
               $data[$key]['action_title'] = '修改/绑定手机号';
               $data[$key]['action_content'] = "手机号:{$action['phone']}";
           } else if ($value['type'] == 2) {
               $data[$key]['action_title'] = '修改身份信息';
               $data[$key]['action_content'] = $action['old_data']?"旧姓名:{$action['old_data']['real_name']} - 新姓名:{$action['new_data']['real_name']}<br>旧身份证:{$action['old_data']['idcard']} - 新身份证:{$action['new_data']['idcard']}":"无历史记录";
           } else if ($value['type'] == 5) {
               $data[$key]['action_title'] = '上报角色 '.($remarks['server_name']?$remarks['server_name']:'无').'-'.($remarks['role_name']?$remarks['role_name']:'无').'-'.($remarks['role_level']?$remarks['role_level']:'无');
           } else if ($value['type'] == 6) {
               $data[$key]['action_title'] = '点击推广链接下载';
               $data[$key]['action_content'] = "下载地址:".$value['action'];;
           } else if ($value['type'] == 7){
               $data[$key]['action_title'] = '修改角色名';
               $data[$key]['action_content'] = "旧值:{$action['old_data']}<br>新值:{$action['new_data']}";
           } else if ($value['type'] == 8){
               $data[$key]['action_title'] = '解绑手机号';
               $data[$key]['action_content'] = "手机号:{$action['phone']}";
           }
           else if ($value['type'] == 11) {
               $data[$key]['action_title'] = '修改绑定上级推广员';
           } else {
               $data[$key]['action_title'] = '未知';
           }

       }
//       die();

       $count = M('user_action_log','tab_')->where($map)->order('create_time DESC')->count();

       $page = set_pagination($count, $row);
       if ($page) {
           $this->assign('_page', $page);//分页
       }

       $this->checkListOrCountAuthRestMap($map,[]);
       $show_data_power = (is_administrator()|| session('user_auth')['show_data']);
       $this->assign('show_data_power', $show_data_power);
       $this->assign('data',$data);

       $this->display();

   }

   public function del() {

       if (!$_REQUEST['id']) {
           $this->ajaxReturn(['status'=>1001]);
       }

       $map['id'] = $_REQUEST['id'];

       $is_del = M('user_action_log','tab_')->where($map)->delete();

       if ($is_del) {
           $this->ajaxReturn(['status'=>1]);
       } else {
           $this->ajaxReturn(['status'=>0]);
       }

   }

    public function export() {

        $xlsName = $_REQUEST['xlsname'];

        $xlsCell = array(
            '编号','行为','内容','游戏','玩家','操作时间','操作IP地址'
        );

        $map = [];

        if ($_REQUEST['time_start']&&!$_REQUEST['time_end']) {
            $map['create_time'] = ['egt',strtotime($_REQUEST['time_start'])];
        }

        if (!$_REQUEST['time_start']&&$_REQUEST['time_end']) {
            $map['create_time'] = ['elt',strtotime($_REQUEST['time_end']+86399)];
        }

        if ($_REQUEST['time_start']&&$_REQUEST['time_end']) {
//            $map['create_time'] = ['egt',strtotime($_REQUEST['time_start'])];
//            $map['create_time'] = ['elt',strtotime($_REQUEST['time_end'])];
            $map['create_time'] = ['between',[strtotime($_REQUEST['time_start']),strtotime($_REQUEST['time_end'])+86399]];
        }

        if ($_REQUEST['username']) {
            $map['account'] = ['like',"%{$_REQUEST['username']}%"];
        }

        if ($_REQUEST['game_name']) {
            $map['game_name'] = ['like',"%{$_REQUEST['game_name']}%"];
        }

        $csvFileName = $xlsName.'.csv';
        //设置好告诉浏览器要下载excel文件的headers
        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="'. $csvFileName .'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        $fp = fopen('php://output', 'a');//打开output流
        mb_convert_variables('GBK', 'UTF-8', $xlsCell);
        fputcsv($fp, $xlsCell);//将数据格式化为CSV格式并写入到output流中

        $accessNum=M('user_action_log','tab_')->where($map)->order('create_time DESC')->count();

        $perSize = 5000;//每次查询的条数
        $pages   = ceil($accessNum / $perSize);

        for($i = 1; $i <= $pages; $i++) {
            /* 获取频道列表 */

            $xlsData = M('user_action_log','tab_')
                ->field("id,'' as action_title,'' as action_content,game_name,account,create_time,ip,type,remarks,action")
                ->where($map)
                ->limit(($i-1)*$perSize  ,$perSize)
                ->order('create_time DESC')->select();

            foreach($xlsData as $key => $value) {

                $value['create_time'] = date("Y-m-d H:i:s", $value['create_time']);

//                $remarks = json_decode($value['remarks'],true);
////           dump($data);
//
//                if ($value['type'] == 3) {
//                    $value['action'] = '登录 '.($remarks['server_name']?$remarks['server_name']:'无').'-'.($remarks['role_name']?$remarks['role_name']:'无').'-'.($remarks['role_level']?$remarks['role_level']:'无');
//                } else if ($value['type'] == 4) {
//                    $value['action'] = '退出 '.($remarks['server_name']?$remarks['server_name']:'无').'-'.($remarks['role_name']?$remarks['role_name']:'无').'-'.($remarks['role_level']?$remarks['role_level']:'无');
//                } else if ($value['type'] == 0) {
//                    $value['action'] = '修改密码';
//                } else if ($value['type'] == 1) {
//                    $value['action'] = '修改/绑定手机号';
//                } else if ($value['type'] == 2) {
//                    $value['action'] = '修改身份信息';
//                } else if ($value['type'] == 5) {
//                    $value['action'] = '上报角色 '.($remarks['server_name']?$remarks['server_name']:'无').'-'.($remarks['role_name']?$remarks['role_name']:'无').'-'.($remarks['role_level']?$remarks['role_level']:'无');
//                } else if ($value['type'] == 11) {
//                    $value['action'] = '修改绑定上级推广员';
//                } else {
//                    $value['action'] = '未知';
//                }

                $remarks = json_decode($value['remarks'],true);

                $action = json_decode($value['action'],true);

                if ($value['type'] == 3) {
                    $value['action_title'] = '登录 '.($remarks['server_name']?$remarks['server_name']:'无').'-'.($remarks['role_name']?$remarks['role_name']:'无').'-'.($remarks['role_level']?$remarks['role_level']:'无');
                } else if ($value['type'] == 4) {
                    $value['action_title'] = '退出 '.($remarks['server_name']?$remarks['server_name']:'无').'-'.($remarks['role_name']?$remarks['role_name']:'无').'-'.($remarks['role_level']?$remarks['role_level']:'无');
                } else if ($value['type'] == 0) {
                    $value['action_title'] = '修改密码';
                    if ($action['account']) {
                        $value['action_content'] = "-------------------";
                    } else {
                        $value['action_content'] = "旧值:{$action['old_data']}<br>新值:{$action['new_data']}";
                    }

                } else if ($value['type'] == 1) {
                    $value['action_title'] = '修改/绑定手机号';
                    $value['action_content'] = "手机号:{$action['phone']}";
                } else if ($value['type'] == 2) {
                    $value['action_title'] = '修改身份信息';
                    $value['action_content'] = $action['old_data']?"旧姓名:{$action['old_data']['real_name']} - 新姓名:{$action['new_data']['real_name']}<br>旧身份证:{$action['old_data']['idcard']} - 新身份证:{$action['new_data']['idcard']}":"无历史记录";
                } else if ($value['type'] == 5) {
                    $value['action_title'] = '上报角色 '.($remarks['server_name']?$remarks['server_name']:'无').'-'.($remarks['role_name']?$remarks['role_name']:'无').'-'.($remarks['role_level']?$remarks['role_level']:'无');
                } else if ($value['type'] == 6) {
                    $value['action_title'] = '点击推广链接下载';
                    $value['action_content'] = "下载地址:".$value['action'];;
                } else if ($value['type'] == 7){
                    $value['action_title'] = '修改角色名';
                    $value['action_content'] = "旧值:{$action['old_data']}<br>新值:{$action['new_data']}";
                } else if ($value['type'] == 8){
                    $value['action_title'] = '解绑手机号';
                    $value['action_content'] = "手机号:{$action['phone']}";
                }
                else if ($value['type'] == 11) {
                    $value['action_title'] = '修改绑定上级推广员';
                } else {
                    $value['action_title'] = '未知';
                }

                unset($value['type']);
                unset($value['remarks']);
                unset($value['action']);

                mb_convert_variables('GBK', 'UTF-8', $value);
                fputcsv($fp, $value);

            }
            unset($xlsData);//释放变量的内存
            //刷新输出缓冲到浏览器
            ob_flush();
            flush();//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
        }
        fclose($fp);

        $getData = $_GET;
        unset($getData['xlsname']);

        addOperationLog(['op_type'=>3,'key'=>getNowDate(),'op_name'=>'导出操作日志','url'=>U('UserActionLog/index',$getData),'menu'=>'站点-日志管理-操作日志-导出操作日志']);

        exit();

    }

    public function operateLog($p = 0) {

        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $arraypage = $page;

        if (isset($_REQUEST['row'])) {
            $row = $_REQUEST['row'];
        } else {
            $row = 10;
        }

        $map = [];

        if ($_REQUEST['time_start']&&!$_REQUEST['time_end']) {
            $map['create_time'] = ['egt',strtotime($_REQUEST['time_start'])];
        }

        if (!$_REQUEST['time_start']&&$_REQUEST['time_end']) {
            $map['create_time'] = ['elt',strtotime($_REQUEST['time_end'])+86399];
        }

        if ($_REQUEST['time_start']&&$_REQUEST['time_end']) {
//            $map['create_time'] = ['egt',strtotime($_REQUEST['time_start'])];
//            $map['create_time'] = ['elt',strtotime($_REQUEST['time_end'])];
            $map['create_time'] = ['between',[strtotime($_REQUEST['time_start']),strtotime($_REQUEST['time_end'])+86399]];
        }

        if ($_REQUEST['adminname']) {
            $map['admin_account'] = ['like',"%{$_REQUEST['adminname']}%"];
        }

        if ($_REQUEST['op_type']||$_REQUEST['op_type']=='0') {
            $map['op_type'] = $_REQUEST['op_type'];
        }

        if ($_REQUEST['menu']) {
            $map['menu'] = ['like',"%{$_REQUEST['menu']}%"];
        }

        $data = M('operation_log','tab_')
            ->where($map)
            ->page($page,$row)
            ->order('create_time DESC')
            ->select();

        $count = M('operation_log','tab_')->where($map)->order('create_time DESC')->count();

        $page = set_pagination($count, $row);
        if ($page) {
            $this->assign('_page', $page);//分页
        }

        $this->checkListOrCountAuthRestMap($map,[]);

        $this->assign('data',$data);

        $this->display();

    }

    public function operateLogDel() {

        if (!$_REQUEST['id']) {
            $this->ajaxReturn(['status'=>1001]);
        }

        $map['id'] = $_REQUEST['id'];

        $is_del = M('operation_log','tab_')->where($map)->delete();

        if ($is_del) {
            $this->ajaxReturn(['status'=>1]);
        } else {
            $this->ajaxReturn(['status'=>0]);
        }

    }

    public function operateLoExport() {

        $xlsName = $_REQUEST['xlsname'];

        $xlsCell = array(
            '编号','位置','类型','操作人','操作时间','操作IP地址'
        );

        $map = [];

        if ($_REQUEST['time_start']&&!$_REQUEST['time_end']) {
            $map['create_time'] = ['egt',strtotime($_REQUEST['time_start'])];
        }

        if (!$_REQUEST['time_start']&&$_REQUEST['time_end']) {
            $map['create_time'] = ['elt',strtotime($_REQUEST['time_end'])+86399];
        }

        if ($_REQUEST['time_start']&&$_REQUEST['time_end']) {
//            $map['create_time'] = ['egt',strtotime($_REQUEST['time_start'])];
//            $map['create_time'] = ['elt',strtotime($_REQUEST['time_end'])];
            $map['create_time'] = ['between',[strtotime($_REQUEST['time_start']),strtotime($_REQUEST['time_end'])+86399]];
        }

        if ($_REQUEST['adminname']) {
            $map['admin_account'] = ['like',"%{$_REQUEST['adminname']}%"];
        }

        if ($_REQUEST['op_type']) {
            $map['op_type'] = $_REQUEST['op_type'];
        }

        $csvFileName = $xlsName.'.csv';
        //设置好告诉浏览器要下载excel文件的headers
        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="'. $csvFileName .'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        $fp = fopen('php://output', 'a');//打开output流
        mb_convert_variables('GBK', 'UTF-8', $xlsCell);
        fputcsv($fp, $xlsCell);//将数据格式化为CSV格式并写入到output流中

        $accessNum=M('operation_log','tab_')->where($map)->order('create_time DESC')->count();

        $perSize = 5000;//每次查询的条数
        $pages   = ceil($accessNum / $perSize);

        for($i = 1; $i <= $pages; $i++) {
            /* 获取频道列表 */

            $xlsData = M('operation_log','tab_')
                ->field("id,menu,key,op_name,admin_account,create_time,op_ip")
                ->where($map)
                ->limit(($i-1)*$perSize  ,$perSize)
                ->order('create_time DESC')->select();

            foreach($xlsData as $key => $value) {

                $value['menu'] = $value['menu'].'-'.$value['key'];
                $value['create_time'] = date("Y-m-d H:i:s", $value['create_time']);
                unset($value['key']);

                mb_convert_variables('GBK', 'UTF-8', $value);
                fputcsv($fp, $value);

            }
            unset($xlsData);//释放变量的内存
            //刷新输出缓冲到浏览器
            ob_flush();
            flush();//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
        }
        fclose($fp);

        $getData = $_GET;
        unset($getData['xlsname']);

        addOperationLog(['op_type'=>3,'key'=>getNowDate(),'op_name'=>'导出行为日志','url'=>U('UserActionLog/operateLog',$getData),'menu'=>'站点-日志管理-操作日志-导出行为日志']);

        exit();

    }

}