<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Illuminate\Support\Facades\DB;
use Request;

/**
 *
 */
class UserController extends Controller
{
    /**
     *
     * [Colse description]
     */
    public function index(Req $req)
    {

        $request = array();
        $request['account'] = isset($req->account) ? trim($req->account) : null;
        $request['agent_name'] = isset($req->agent_name) ? trim($req->agent_name) : null;
        $request['select_type'] = isset($req->select_type) ? trim($req->select_type) : null;
        $request['select_value'] = isset($req->select_value) ? trim($req->select_value) : null;
        $request['grade'] = isset($req->grade) ? trim($req->grade) : null;
        $request['statuss'] = isset($req->statuss) ? trim($req->statuss) : null;
        $request['rechargenum_small'] = isset($req->rechargenum_small) ? trim($req->rechargenum_small) : null;
        $request['rechargenum_big'] = isset($req->rechargenum_big) ? trim($req->rechargenum_big) : null;
        $request['takenum_small'] = isset($req->takenum_small) ? trim($req->takenum_small) : null;
        $request['takenum_big'] = isset($req->takenum_big) ? trim($req->takenum_big) : null;
        $request['takenum_small'] = isset($req->takenum_small) ? trim($req->takenum_small) : null;
        $request['last_startime'] = isset($req->last_startime) ? trim($req->last_startime) : null;
        $request['last_endtime'] = isset($req->last_endtime) ? trim($req->last_endtime) : null;
        $request['regist_startime'] = isset($req->regist_startime) ? trim($req->regist_startime) : null;
        $request['fanshui'] = isset($req->fanshui) ? trim($req->fanshui) : '';
        $request['regist_endtime'] = isset($req->regist_endtime) ? trim($req->regist_endtime) : null;
        $request['status'] = isset($req->status) ? trim($req->status) : null;
        $request['group'] = isset($req->group) ? intval($req->group) : null;
        $request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
        //用户组数组
        $request['select_group']=\App\Models\Account_group::getAllInfo();
        $Account = new \App\Models\Account();
        $request['gradelist']=$Account->getUserGrade();
        $page = Request::has('page') ? Request::get('page') : '';
        $list = Request::has('limit') ? Request::get('limit') : '';
        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/user/user?page=' . $page . '&limit=' . $list);
        $dt->setLang('user');
        // $dt->addColsFields('id', array('sort' => false));
        $dt->addColsFields('account_grade', array('templet' => '#userdetail', 'width'=>120,'sort' => false));
        $dt->addColsFields('cash', array('templet' => '#moneyrecord','width'=>90));
        $dt->addColsFields('name', array('templet' => '#selectname', 'width'=>90,'sort' => false));
        $dt->addColsFields('register_last_time',array('width'=>160,'templet'=>'#register_last', 'sort' => false)); //'templet' => '#selectname',
        $dt->addColsFields('register_log_ip', array('templet' => '#selectip','width'=>130, 'sort' => false));
        $dt->addColsFields('register_log_url', array('width'=>150,'sort' => false, 'templet' => '#selecturl','display'=>'inline-block','overflow'=>'hidden'));
        $dt->addColsFields('phone', array( 'sort' => false, 'width'=>120));
        // $dt->addColsFields('qq', array('width'=>120, 'sort' => false));
        // $dt->addColsFields('wechat', array('width'=>120, 'sort' => false));
        //邀请码显示
        $dt->addColsFields('open_invitation', array('templet' => '#invitation','width'=>120, 'sort' => false));
        // $dt->addColsFields('last_time',array('width'=>90)); //'templet' => '#selectphone',
        //$dt->addColsFields('money_num', array('templet' => '#moneynum', 'sort' => false, 'width' => 110));
        $dt->addColsFields('statuss', array('templet' => '#openstatus', 'sort' => false,'width'=>100));
        $dt->addColsFields('isfs', array('sort' => false, 'templet' => '#agent','width'=>100,));
        $dt->addColsFields('action_user', array('templet' => '#waterTool', 'sort' => false,'width'=>160));
        $dt->setPriKey('id');
        return view('admin.user/index', $dt->render($request));
    }

    //改变反水状态
    function ajaxfsup(){
        $data = array();
        $data['fanshui'] = $_POST['type'];
        return \App\Models\Account::where('id',$_POST['id'])->update($data);
    }

    /**
     * 会员存取反水列表
     * [detail description]
     * @return [type] [description]
     */
    public function moneyrecord()
    {
        $account_name = Request::has('account_name') ? Request::get('account_name') : '';
        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/money/moneyList?account_name=' . $account_name . '&sureblur=精确');
        $dt->setLang('money');
        $dt->setPriKey('trade_id');
        $dt->addColsFields('id');
        $dt->addColsFields('account_name');
        $dt->addColsFields('trade_id');
        $dt->addColsFields('trade_type');
        $dt->addColsFields('money_time');
        $dt->addColsFields('money');
        $dt->addColsFields('system_user');
        $dt->addColsFields('reason');
        $dt->disableCheckBox();
        return view('admin.user/viewip', $dt->render());
    }

    /**
     * 同一实名用户列表
     * [detail description]
     * @return [type] [description]
     */
    public function userlist()
    {
        $name = Request::has('name') ? Request::get('name') : '';
        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/user/user?name=' . $name . '&nameblur=精确');
        $dt->setLang('state');
        $dt->setLang('user');
        $dt->addColsFields('account_grade', array('sort' => false, 'width' => 140));
        $dt->addColsFields('cash', array('width' => 120));
        $dt->addColsFields('name_registtime', array('sort' => false, 'width' => 160));
        $dt->addColsFields('register_log_ip', array('sort' => false, 'width' => 140));
        $dt->addColsFields('register_log_url', array('sort' => false, 'width' => 133));
        $dt->addColsFields('phone', array('sort' => false, 'width' => 140));
        $dt->addColsFields('statuss', array('sort' => false, 'width' => 80));
        $dt->setPriKey('id');
        $dt->setToolBar(array('view'));
        return view('admin.view', $dt->render());
    }

    /**
     * 会员列表
     * 'user description'
     * @return 'type' 'description'
     */
    public function user()
    {
        $page = Request::has('page') ? Request::get('page') : 1;
        $list = Request::has('limit') ? Request::get('limit') : 10;
        $field = Request::has('field') ? Request::get('field') : 'id';
        $order = Request::has('order') ? Request::get('order') : 'desc';
        $status = Request::has('status') ? Request::get('status') : '';
        $agent_name = Request::has('agent_name') ? Request::get('agent_name') : '';
        $fanshui = Request::has('fanshui') ? Request::get('fanshui') : '';

        $account = Request::has('account') ? Request::get('account') : '';
        $select_type = Request::has('select_type') ? trim(Request::get('select_type')) : '';
        $select_value = Request::has('select_value') ? trim(Request::get('select_value')) : '';
        /*$name = Request::has('name') ? Request::get('name') : '';
        $register_ip = Request::has('register_ip') ? Request::get('register_ip') : '';
        $last_ip = Request::has('last_ip') ? Request::get('last_ip') : '';
        $register_url = Request::has('register_url') ? Request::get('register_url') : '';
        $last_url = Request::has('last_url') ? Request::get('last_url') : '';
        $phone = Request::has('phone') ? Request::get('phone') : '';*/
        $grade = Request::has('grade') ? Request::get('grade') : '';
        $group = Request::has('group') ? Request::get('group') : '';
        $regist_startime = Request::get('regist_startime') ? Request::get('regist_startime') . ' 00:00:00' : '';
        $regist_endtime = Request::get('regist_endtime') ? Request::get('regist_endtime') . ' 23:59:59' : '';
        $last_startime = Request::get('last_startime') ? Request::get('last_startime') . ' 00:00:00' : '';
        $last_endtime = Request::get('last_endtime') ? Request::get('last_endtime') . ' 23:59:59' : '';
        $statuss = Request::has('statuss') ? Request::get('statuss') : '';
        $cash_small = Request::has('cash_small') ? Request::get('cash_small') : '';
        $cash_big = Request::has('cash_big') ? Request::get('cash_big') : '';
        $sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'off';
        $nameblur = Request::has('nameblur') ? Request::get('nameblur') : '';
        $rechargenum_small = Request::has('rechargenum_small') ? Request::get('rechargenum_small') : '';
        $rechargenum_big = Request::has('rechargenum_big') ? Request::get('rechargenum_big') : '';
        $takenum_small = Request::has('takenum_small') ? Request::get('takenum_small') : '';
        $takenum_big = Request::has('takenum_big') ? Request::get('takenum_big') : '';

        $where = ' ';
        $curtime = time() - 300;
        $rflag = -1;
        $tflag = -1;
        if (!empty($account)) {
            $account = strtolower($account);
            $where .= ' AND account';
            if (empty($sureblur) || $sureblur == 'off') {
                $where .= " LIKE '%" . $account . "%'";
            } else {
                $where .= "='" . $account . "'";
            }
        }

        if (!empty($select_value)) {
            if ($select_type == 'name') {
                $where .= ' AND name';
                if (empty($nameblur) || $nameblur == '模糊') {
                    $where .= " LIKE '%" . $select_value . "%'";
                } else {
                    $where .= "='" . $select_value . "'";
                }
            } elseif (in_array($select_type, array('register_ip','last_ip','open_invitation','invitation'))) {
                $where .= " AND " . $select_type . "='" . $select_value . "'";
            } elseif ($select_type == 'register_url') {
                $where .= " AND register_url LIKE '%" . $select_value . "%'";
            } elseif ($select_type == 'last_url') {
                $where .= " AND last_url LIKE '%" . $select_value . "%'";
            } elseif ($select_type == 'phone') {
                $where .= " AND b.phone LIKE '%{$select_value}%'";
            } elseif ($select_type == 'email') {
                $where .= " AND email LIKE '%" . $select_value . "%'";
            }elseif (in_array($select_type,['qq','wechat'])) {
                $where .= " AND b.{$select_type} LIKE '%". trim($select_value) . "%'";
            }elseif($select_type == 'agent'){
                $where .= " AND c.invite ='" . $select_value . "'";
            }

        }

        if (!empty($grade)) {
            $where .= ' AND b.grade=';
            $where .= ($grade == 10) ? '0' : $grade;
        }

        if (!empty($invite)) {
            $where .= " AND open_invitation='" . $invite . "'";
        }
        if(!empty($group)){
            $where .= " AND group_code like '%,".$group. ",%'";
        }
        if (!empty($regist_startime)) {
            $regist_startime = date('Y-m-d H:i:s', strtotime($regist_startime));
            $where .= " AND b.register_time>='" . $regist_startime . "'";
        }
        if (!empty($regist_endtime)) {
            $regist_endtime = date('Y-m-d H:i:s', strtotime($regist_endtime));
            $where .= " AND b.register_time<='" . $regist_endtime . "'";
        }
        if (!empty($last_startime)) {
            $last_startime = date('Y-m-d H:i:s', strtotime($last_startime));
            //$last_startime = strtotime($last_startime)-300;
            $where .= " AND last_time>='" . $last_startime . "'";
            //$where .= " AND ta.effective_time>=" . $last_startime;
        }
        if (!empty($last_endtime)) {
            $last_endtime = date('Y-m-d H:i:s', strtotime($last_endtime));
            //$last_endtime = strtotime($last_endtime)-300;
            $where .= " AND last_time<='" . $last_endtime . "'";
            //$where .= " AND ta.effective_time<=" . $last_endtime;
        }
        if (!empty($statuss)) {
            $statuss = ($statuss == 2) ? '0' : $statuss;
            $where .= " AND statuss=" . $statuss;
        }

        if (!empty($status)) {
            $status = ($status == 5) ? '-1' : $status;
            $where .= " AND status=" . $status;
            $where .= " or status=" . '2';
        }

        if(!empty($fanshui)){
            $where .= " AND fanshui=" . $fanshui;
        }
        if (!empty($cash_small)) {
            $where .= " AND cash>=" . $cash_small;
        }
        if (!empty($cash_big)) {
            $where .= " AND cash<=" . $cash_big;
        }
        if (!empty($rechargenum_small)) {
            $rflag = 1;
            $where .= ' AND (mc.recharge_num >=' . $rechargenum_small;
        }
        if (!empty($rechargenum_big)) {
            $rflag = 1;
            $where .= empty($rechargenum_small) ? 'AND (' : 'AND ';
            $where .= ' mc.recharge_num <=' . $rechargenum_big;
        }
        if ((empty($rechargenum_small) || $rechargenum_small <= 0) && (empty($rechargenum_big) || $rechargenum_big >= 0)) {
            if (!(empty($rechargenum_small) && empty($rechargenum_big))) {
                $where .= " OR mc.recharge_num ISNULL)";
            }
        } else {
            $where .= ')';
        }

        if (!empty($takenum_small)) {
            $tflag = 1;
            $where .= ' AND (tc.take_num >=' . $takenum_small;
        }
        if (!empty($takenum_big)) {
            $tflag = 1;
            $where .= empty($takenum_small) ? 'AND (' : 'AND ';
            $where .= ' tc.take_num <=' . $takenum_big;
        }
        if ((empty($takenum_small) || $takenum_small <= 0) && (empty($takenum_big) || $takenum_big >= 0)) {
            if (!(empty($takenum_small) && empty($takenum_big))) {
                $where .= " OR tc.take_num ISNULL)";
            }
        } else {
            $where .= ')';
        }
// var_dump($where);die;
        $db = new \App\Models\Account;
        $data = $db->account($list, $page, $where, $field, $order, $rflag, $tflag);
        $gradelist = $db->getUserGrade();
        $data['data'] = arrayToNumber($data['data'], ['cash']);
        $has_root = 0;
        $has_roots = 0;
        $show_name=0;
        if (\App\Models\Role::hasRoot('/enable/show/mobile') < 0) {
            $has_root = 1;
            // $data['data'][$k]['phone'] = phoneHide($v['phone']);
        }
        if (\App\Models\Role::hasRoot('/enable/show/email') < 0) {
            $has_roots = 1;
            // $data['data'][$k]['email'] = emailHide($v['email']);
        }
        if (\App\Models\Role::hasRoot('/enable/show/name') < 0) {
            $show_name = 1;
            // $data['data'][$k]['email'] = emailHide($v['email']);
        };
        $letime = time()-60*30;
        // foreach ($data['data'] as $k => $v) {
        //     $findId[] = $v['agent_identity'];
        //     $data['data'][$k]['online'] = ($v['statuss'] == 0) ? '离线' : '在线';
        //     if($v['statuss']){
        //         $linetime = \App\Model\Account_Token::where('effective_time','>=',$letime)->where('account_identity',$v['identity'])->first();
        //         if($linetime){}else{
        //             \App\Model\Account_detailed::where('account_identity',$v['identity'])->update(['statuss'=>'0']);
        //             if(!empty($statuss) && $statuss==1){
        //                 unset($data['data'][$k]);
        //                 continue;
        //             }
        //             $data['data'][$k]['online'] = '离线';
        //             $data['data'][$k]['statuss'] = '0';
        //         }
        //     }
        //     $data['data'][$k]['invite'] = ($v['invite']=='null' || empty($v['invite']))?'':$v['invite'];
        //     $data['data'][$k]['agent_id'] = '无上级';
        //     $data['data'][$k]['register_ip'] = str_replace(',','/',$v['register_ip']);
        //     $data['data'][$k]['last_ip'] = str_replace(',','/',$v['last_ip']);
        //     $has_root == 1 ? $data['data'][$k]['phone'] = phoneHide($v['phone']) : null;
        //     $has_roots == 1 ? $data['data'][$k]['email'] = emailHide($v['email']) : null;

        //     $show_name == 1 ? $data['data'][$k]['account'] = userHide($v['account']) : null;
        //     $data['data'][$k]['grade_name'] = $gradelist[$v['grade']]??'';
        //     // if($v['grade'] >=0){
        //     //     $data['data'][$k]['grade_name'] = $gradelist[$v['grade']]??'';
        //     // }elseif($v['grade'] ==-1){
        //     //     $data['data'][$k]['grade_name'] = '申请vip审核中';
        //     // }elseif($v['grade'] ==-2){
        //     //     $data['data'][$k]['grade_name'] = 'vip0';
        //     // }

        // }

        // if (!empty($findId)){
        //     $db = new \App\Model\Agent_detailed;
        //     $asd = $db->findId($findId);

        //     foreach ($data['data'] as $k => $v) {
        //         foreach ($asd as $val) {
        //             if ($v['agent_identity'] == $val['agent_identity']) {
        //                 $data['data'][$k]['agent_id'] = $val['id'];
        //             }
        //         }
        //     }
        // }

        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total'], 0, $where);
    }

    //修改用户密码
    public function SetPwdview(Req $req)
    {
        return $this->EditPwd($req);
    }

    //修改会员密码
    public function EditPwd(Req $req)
    {
        $id = $req->id;
        $parentid = isset($req->parentid)?$req->parentid:'';
        if (empty($id)) {
            abort(404);
        }
        $pwdtype = empty($req->pwdtype) ? '' : $req->pwdtype;
        $u_db = new \App\Models\Account();
        $where = array();
        $where[] = array('id', '=', $id);
        $data = $u_db::where($where)->first();
        if (!$data) {
            abort(404);
        }
        $data = $data->toArray();
        if(!(empty($parentid) || is_null($parentid) || $parentid == 'null')){
            $udetail = \App\Models\Nagent_detailed::select('agent_identity','invite')->where('agent_identity',$parentid)->first();
            if(!empty($udetail) && count($udetail->toArray())>0 && !empty($udetail->invite)){
                $data['invite']=$udetail->invite;
            }
        }

        $uname = $data['account'];
        unset($data['password']);
        if (!$req->isMethod('post')) {
            $aid = session('adminInfo.admin_id');
            $arootid = \App\Models\System_user::select('root_id')->where('id',$aid)->first();
            $data['arootid'] = $arootid->root_id;
            return view('admin.user/editpwd', ['data' => $data]);
        } else {
            //修改代理父级
            if(!empty($pwdtype) && $pwdtype==2){
                DB::beginTransaction();
                $invite = $req->invite;
                //$agent_id = $req->agent_id;
                //修改代理父级
                $res = $this->updateAgentParent($data,$invite);
                if($res<0){
                    DB::rollBack();
                }else{
                    DB::commit();
                }
                return responseToJson($res);
            }

            $pwd = $req->password;
            $repwd = $req->repassword;
            $updatetype = '';
            if (!empty($pwd)) {
                if ($pwd != $repwd) {
                    return responseToJson(-4010000502); //两次密码不一致
                }
                $updatevalue = $pwd;
                $pwddata = GenPassword($pwd);
                $remark = empty($req->remark) ? '' : $req->remark;
                $status = empty($req->status) ? '' : $req->status;
                $editdata = array();
                $editdata['account_password'] = $pwddata['password'];
                $editdata['encryption'] = $pwddata['encryption'];
            }
            if (!empty($pwdtype) && $pwdtype == 1) {
                $updatetype = '支付密码';
                $paydata = array(
                    'pay_password' => $editdata['account_password'],
                    'encryption' => $editdata['encryption'],
                );
                $db = new \App\Models\Pay_password;
                $res = $db->updateInfo($paydata, $data['identity']);
            } else {
                $updatetype = '密码';
                $db = new \App\Models\Account_password;
                if (isset($editdata)) {
                    $res = $db->updateInfo($editdata, $data['identity']);
                }
            }
            $identity=$data['identity'];
            $status_type = $req->status_type;
            $data = array(
                'remark' => $req->remark,
                'status' => $req->status,
            );
            if (!empty($status_type) && $status_type == 1) {
                $updatetype = '备注信息、状态';
                $sch = ($data['status']==1)?'启用':'禁用';
                $updatevalue = $data['remark'].'、'.$sch;
                $db = new \App\Models\Account;
                $res = $db->updateInfo($data, $id);
            }
            if($res>0){
                \App\Models\Account_detailed::where('account_identity',$identity)->update(['token'=>createToken()]);
            }
            $log = array(
                session('adminInfo.admin_name'),
                $uname,
                $updatetype,
                $updatevalue,
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'update_userinfo', $log);
            return responseToJson($res);
        }
    }

    //修改代理父级
    private function updateAgentParent($data,$invite){
        if(empty($invite))return -4010010102;
        //验证代理是否存在
        $check_agent = \App\Models\Nagent_detailed::select('agent_identity as identity','agent_user','parent_path')->where('invite',$invite)->first();
        if(!$check_agent || count($check_agent->toArray())<1 || empty($check_agent->identity))return -4010010202;
        $agent_name = $check_agent->agent_user;
        if($agent_name == $data['account'])return -4010010802;

        //验证会员下级关系中是否存在该父级
        $conchild = \App\Models\Nagent_detailed::select('id')->where('parent_path','like',$data['identity'])->where('agent_identity',$check_agent->identity)->first();
        if($conchild && count($conchild->toArray())>0)return -4010010302;

        //验证相同代理关系是否已存在
        $check_conhas = \App\Models\NagentChild::select('id')->where('agent_name',$agent_name)->where('account_identity',$data['identity'])->first();
        if($check_conhas && count($check_conhas->toArray())>0 && !empty($check_conhas->id))return -4010010302;

        $pdata = array(
            'parent_id' => $check_agent->identity,
            'parent_path' => $check_agent->parent_path.','.$check_agent->identity,
        );
        $res = \App\Models\Account_detailed::where('account_identity',$data['identity'])->update($pdata);
        if($res<1)return -4010010602;
        $res = \App\Models\Nagent_detailed::where('agent_identity',$data['identity'])->update($pdata);
        if($res<1)return -4010010902;
        $updatesub = $this->updateChildinfo($pdata['parent_path'],$data['identity']);
        if($updatesub<0)return $updatesub;
        //关联信息处理
        $coninfo = array(
            'agent_identity' => $check_agent->identity,
            'account_identity' => $data['identity'],
            'agent_name' => $agent_name,
            'account_name' => $data['account'],
        );

        //获取用户原父级信息
        $ypinfo = \App\Models\NagentChild::select('agent_name','agent_identity')->where('account_identity',$data['identity'])->first();
        if($ypinfo && count($ypinfo->toArray())>0 && !empty($ypinfo->agent_name)){
            $res = \App\Models\NagentChild::where('account_identity',$data['identity'])->update($coninfo);
            if($res<0)return -4010011002;
            $ypinvite = \App\Models\Nagent_detailed::select('invite')->where('agent_identity',$ypinfo->agent_identity)->first();
            if(!$ypinvite || !isset($ypinvite->invite))return -4010011102;
            $log = array(
                session('adminInfo.admin_name'),
                $data['account'],
                $ypinfo->agent_name,
                $ypinvite->invite,
                $agent_name,
                $invite,
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'update_parentagent', $log);
        }else{
            $res = \App\Models\NagentChild::where('account_identity',$data['identity'])->insert($coninfo);
            if($res<0)return -4010010702;
            $log = array(
                session('adminInfo.admin_name'),
                $data['account'],
                $agent_name,
                $invite,
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'add_parentagent', $log);
        }

        return 1;
    }

    //更新代理下级信息
    private function updateChildinfo($ppath,$pid){
        $childinfo = \App\Models\Nagent_detailed::select('agent_identity','parent_path')->where('parent_path','like','%' . $pid . '%')->get();
        if(!$childinfo || count($childinfo->toArray())<1)return 1;
        $childinfo = $childinfo->toArray();
        foreach ($childinfo as $k => $v) {
            $subpath = strstr($v['parent_path'],$pid);
            $pdata = array('parent_path'=>$ppath.','.$subpath);
            $res = \App\Models\Account_detailed::where('account_identity',$v['agent_identity'])->update($pdata);
            if($res<1)return -4010010602;
            $res = \App\Models\Nagent_detailed::where('agent_identity',$v['agent_identity'])->update($pdata);
            if($res<1)return -4010010602;
        }
    }

    //修改代理父级invite
    /*private function updateAgentParent($data,$agent_id){
        if(empty($agent_id))return -4010010102;
        //验证代理是否存在
        $check_agent = \App\Model\Agent_detailed::select('agent_identity as identity','agent_user')->where('id',$agent_id)->first();
        if(!$check_agent || count($check_agent->toArray())<1 || empty($check_agent->identity))return -4010010202;
        $agent_name = $check_agent->agent_user;
        if($agent_name == $data['account'])return -4010010802;
        //验证相同代理关系是否已存在
        $check_conhas = \App\Model\AgentChild::select('id')->where('agent_name',$agent_name)->where('account_identity',$data['identity'])->first();
        if($check_conhas && count($check_conhas->toArray())>0 && !empty($check_conhas->id))return -4010010302;

        //关联信息处理
        $coninfo = array(
            'agent_identity' => $check_agent->identity,
            'account_identity' => $data['identity'],
            'agent_name' => $agent_name,
            'account_name' => $data['account'],
        );

        //获取用户原父级信息
        $ypinfo = \App\Model\AgentChild::select('agent_name','agent_identity')->where('account_identity',$data['identity'])->first();
        if($ypinfo && count($ypinfo->toArray())>0 && !empty($ypinfo->agent_name)){
            $res = \App\Model\AgentChild::where('account_identity',$data['identity'])->update($coninfo);
            if($res<0)return -4010010602;
            $log = array(
                session('adminInfo.admin_name'),
                $data['account'],
                $ypinfo->agent_name,
                $ypinfo->agent_identity,
                $agent_name,
                $check_agent->identity,
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'update_parentagent', $log);
        }else{
            $res = \App\Model\AgentChild::where('account_identity',$data['identity'])->insert($coninfo);
            if($res<0)return -4010010702;
            $log = array(
                session('adminInfo.admin_name'),
                $data['account'],
                $agent_name,
                $check_agent->identity,
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'add_parentagent', $log);
        }

        return 1;
    }*/

    public function View(Req $req)
    {
        return $this->Edit($req);
    }

    //修改会员信息
    public function Edit(Req $req)
    {
        $id = $req->id;
        $identity = !empty($req->identity) ? $req->identity : '';
        //echo 'id:'.$id.';account:'.$account;exit;
        if (empty($id) && empty($identity)) {
            abort(404);
        }
        $u_db = new \App\Models\Account();
        $u_details = new \App\Models\Account_detailed();
        $bank = new \App\Models\Account_bank();
        $groupinfo = \App\Models\Account_group::getList();
        $groupdata = $groupinfo['data'];
        $gradelist = $u_db->getUserGrade();
        if (!$req->isMethod('post')) {
            $where = array();
            if (!empty($id)) {
                $where[] = array('id', '=', $id);
            } elseif (!empty($identity)) {
                $where[] = array('identity', '=', $identity);
            }
            $data = $u_db::where($where)->first();
            if (!$data) {
                abort(404);
            }
            $data = $data->toArray();
            $data_details = array();
            $data_details = $u_details->getInfoBy($data['identity']);
            if ($data_details < 0) {
                abort(404);
            }
            $data_details['id'] = $data['id'];
            $data_details['user_type'] = $data['user_type'];
            $data_details['account'] = $data['account'];
            $data_details['remark'] = $data['remark'];
            $data_details['group'] = $groupdata;
            $data_details['gradelist'] = $gradelist;
            $bank_data = $bank->getInfoByUid($data['identity']);
            $data_details['bank_name'] = isset($bank_data['bank_name']) ? $bank_data['bank_name'] : '';
            $data_details['bank_address'] = isset($bank_data['bank_address']) ? $bank_data['bank_address'] : '';
            $data_details['bank_number'] = isset($bank_data['bank_number']) ? $bank_data['bank_number'] : '';
            if (\App\Models\Role::hasRoot('/enable/show/bankcard') < 0) {
                $data_details['bank_number'] = bankHide($data_details['bank_number']);
            }
            if (\App\Models\Role::hasRoot('/enable/show/mobile') < 0) {
                $data_details['phone'] = phoneHide($data_details['phone']);
            }
            if (\App\Models\Role::hasRoot('/enable/show/name') < 0) {
                $data_details['account'] = userHide($data_details['account']);
            }
            if(\App\Models\Role::hasRoot('/enable/show/qq') < 0){
                $data_details['qq'] = phoneHide($data_details['qq']);
            }
            $data_details['register_time'] = isset($data_details['register_time']) ? $data_details['register_time'] : '';
            $data_details['register_ip'] = isset($data_details['register_ip']) ? $data_details['register_ip'] : '';
            $data_details['last_ip'] = isset($data_details['last_ip']) ? $data_details['last_ip'] : '';
            $data_details['cash'] = isset($data_details['cash']) ? $data_details['cash'] : '';
            $data_details['og_type'] = isset($data_details['og_type']) ? $data_details['og_type'] : '';
            if (\App\Models\Role::hasRoot('/enable/show/email') < 0) {
                $data_details['email'] = emailHide($data_details['email']);
            }

            $data_details['name'] = isset($data_details['name']) ? $data_details['name'] : '';
            unset($data_details['password']);
            //获取用户存取款笔数
            $moneydetail = new \App\Models\Money_details();
            $Withdraw_rule = new \App\Models\Withdraw_rule();
            $mdata = $moneydetail->getUserMoneynum($data['identity']);
            $data_details['recharge_num'] = $mdata['recharge_num'];
            $data_details['take_num'] = $mdata['take_num'];
            $withdrawRule = $Withdraw_rule::check($data_details['account_identity']);
            if(!empty($withdrawRule)){
                $withdrawRule = $withdrawRule->toArray();
                $data_details['bettingMoney'] = $withdrawRule['bettingMoney'];
                $data_details['needBettingMoney'] = $withdrawRule['needBettingMoney'];
            }else{
                $data_details['bettingMoney'] = $data_details['cash'];
                $data_details['needBettingMoney'] = $data_details['cash'];
            }
            return view('admin.user/view', ['data' => $data_details]);
            //return view('admin.menu/userAdd', ['data' => $data_details]);
        } else {
            if (empty($id)) {
                return responseToJson(-4010100102);
            }
            $data = $u_db::where('id', $id)->first();
            if (!$data) {
                return responseToJson(-4010100202);
            }
            $data = $data->toArray();
            $uname = $data['account'];
            $groups=$req->groups?$req->groups:'';
            //选择的组
            $group_codes=array_keys($groups);
            //获取组名的
            $groups_names='| ';
            foreach ($groupdata as $k=>$v){
                if(in_array($v['group_code'],$group_codes))
                    $groups_names.=$v['group_name'].'| ';
            }
            $updatedata = array(
                'name' => $req->input('name'),
                'grade' => $req->input('grade'),
                'email'=>$req->input('email'),
                'phone'=>$req->input('phone'),
                'qq'=>$req->input('qq'),
                'wechat'=>$req->input('wechat'),
                'og_type'=>$req->input('og_type'),
                'group_code' => '0,'.implode(',',$group_codes).',',
                'group_name' => $groups_names,
            );

            if (strpos($updatedata['phone'], '*')!==false) {
                unset($updatedata['phone']);
            }
            if (strpos($updatedata['email'], '*')!==false) {
                unset($updatedata['email']);
            }
            if (strpos($updatedata['qq'], '*')!==false) {
                unset($updatedata['qq']);
            }
            $UserInfo  = array(
                'user_type' => is_numeric($req->user_type)?trim($req->user_type):1,
                'remark' => $req->remark,
             );
            $log = array(
                session('adminInfo.admin_name'),
                $uname,
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'update_userinfos', $log);
            $res = $u_details->updateInfo($updatedata, $data['identity']);
            if ($res < 0) {
                return responseToJson($res);
            }
            $res=$u_db->upUserInfo($UserInfo,$data['identity']);
            if ($res < 0) {
                return responseToJson($res);
            }
            $bankObj = new \App\Models\Account_bank();
            $checkbank = $bankObj->checkHasBank($data['identity']);
            if ($checkbank < 0) {
                return responseToJson($res);
            }
            $res = $bankObj->updateOne(array('account_name' => $updatedata['name']), $data['identity']);

            return responseToJson($res);
        }
    }

    /**
     * 批量删除
     * [delete description]
     * @return [type] [description]
     */
    public function delete(Req $req)
    {
        $id = $req->input('id');
        if (empty($id)) {
            return responseToJson(-2001); //id����Ϊ��
        }
        $ids = explode(',', $id);
        if (!is_array($ids) && intval($ids) < 0) {
            return responseToJson(-2002); //id����
        }
        if (is_array($ids) && count($ids) > 0) {
            foreach ($ids as $k => $v) {
                if (intval($v) < 1) {
                    unset($ids[$k]);
                }
            }
        }
        $rows = \App\Models\Account::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003); //id����
        }
        return responseToJson(1, trans('menu.delete_success')); //id����
    }

    //启用/禁用会员信息
    public function changeStatus(Req $req)
    {
        $id = Request::has('id') ? Request::get('id') : '';
        $account = Request::has('account') ? Request::get('account') : '';
        if (empty($id)) {
            return responseToJson(-4010100102); //未提交用户ID
        }
        $u_db = new \App\Models\Account();
        $where = array();
        $where[] = array('id', '=', $id);
        $data = $u_db::where($where)->first();
        if (!$data) {
            return responseToJson(-4010100202); //用户不存在
        }
        $data = $data->toArray();
        unset($data['password']);
        if (!$req->isMethod('post')) {
            return view('admin.user/changeStatus', ['data' => $data]);
        } else {
            $status = $req->status;
            $password = $req->password;

            if (!empty($password)) {
                $admin_id = session('adminInfo.admin_id');
                $adminObj = new \App\Models\System_user();
                $admininfo = $adminObj->getAdminInfo($admin_id);
                if ($admininfo < 0 || empty($admininfo['password_2'])) {
                    return responseToJson(-7010200102); //未获取到二次密码
                }
                $checkpwd = VerPassword($password, $admininfo['encryption_2'], $admininfo['password_2']);
                if (!$checkpwd) {
                    return responseToJson(-7010200202); //二次密码错误
                }
            } else {
                return responseToJson(-7010200302); //二次密码不能为空
            }

            $status = is_numeric($req->status) ? intval($req->status) : '';
            if (empty($id) || !is_numeric($status)) {
                return responseToJson(-2020100102);
            }
            $u_db = new \App\Models\Account();

            $data = array(
                'status' => $status,
            );
            $res = $u_db->updateInfo($data, $id);
            $log = array(
                session('adminInfo.admin_name'),
                $account,
            );
            OperationLog(session('adminInfo.admin_id'), 'changeStatus', $log);
            return responseToJson($res);
        }
    }

    //用户踢线
    public function changeStatuss(Req $req)
    {
        $id = $req->id;
        if (empty($id)) {
            return responseToJson(-4010100102); //未提交用户ID
        }
        $u_db = new \App\Models\Account();
        $u_details = new \App\Models\Account_detailed();
        $where = array();
        $where[] = array('id', '=', $id);
        $data = $u_db::where($where)->first();
        if (!$data) {
            return responseToJson(-4010100202); //用户不存在
        }
        $data_details = $u_details::where($where)->first();
        $data = $data->toArray();
        $data_details = $u_details->getInfoBy($data['identity']);
        $data_details['account'] = $data['account'];
        $uname = $data['account'];
        unset($data['password']);
        if (!$req->isMethod('post')) {
            return view('admin.user/changeStatuss', ['data' => $data_details]);
        } else {
            $password = $req->password;
            $ids = $req->ids;

            if (!empty($password)) {
                $admin_id = session('adminInfo.admin_id');
                $adminObj = new \App\Models\System_user();
                $admininfo = $adminObj->getAdminInfo($admin_id);
                if ($admininfo < 0 || empty($admininfo['password_2'])) {
                    return responseToJson(-7010200102); //未获取到二次密码
                }
                $checkpwd = VerPassword($password, $admininfo['encryption_2'], $admininfo['password_2']);
                if (!$checkpwd) {
                    return responseToJson(-7010200202); //二次密码错误
                }
            } else {
                return responseToJson(-7010200302); //二次密码不能为空
            }

            $status = is_numeric($req->statuss) ? intval($req->statuss) : '';
            if (empty($ids) || !is_numeric($status)) {
                return responseToJson(-2020100102);
            }
            $u_db = new \App\Models\Account_detailed();
            $u_dbs = new \App\Models\Account_Token();
            $datas = $u_dbs->getTime($data['identity']);
            if ($datas != 1) {
                return responseToJson(-27349826); //未获取到二次密码
            }
            $data = array(
                'statuss' => $status,
                'token' => '',
            );
            $res = $u_db->updateInfos($data, $ids);
            $log = array(
                session('adminInfo.admin_name'),
                $uname,
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'letuser_outline', $log);
            return responseToJson($res);
        }

    }
    /**
     * 用户添加
     * [add description]
     */
    public function userAdd()
    {
        return view('admin.menu/userAdd');
    }

    /**
     * 会员列表
     * [getCloseTable description]
     * @return [type] [description]
     */
    public function getUserTable()
    {
        $table = new \App\Lib\Biz\Game\Table\UserTable();
        $data = $table->getUserTh();
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
    }

    /**
     * 资金流水
     * 'money description'
     * @return 'type' 'description'
     */
    public function money()
    {
        $db = new \App\Models\Money_details;
        $page = Request::has('statpageus') ? Request::get('page') : '20';
        $identity = Request::has('account_identity') ? Request::get('account_identity') : '45b7d4e9-2bb9-16b3-9a50-6fd6f62235f8';
        if (empty($identity)) {
            return -2020162003; //用户不存在
        }
        $status = Request::has('status') ? Request::get('status') : '1';
        if ($status == 1) {
            $data = $db->money($page, $identity, $status);
            return responseToJson($data);
        } else {
            return -2020162103; //账户异常
        }
    }

    /**
     * 会员余额统计
     * 'money description'
     * @return 'type' 'description'
     */
    public function userCashCount()
    {
        $where = Request::has('where') ? Request::get('where') : '';
        $db = new \App\Models\Account;

        $data = $db->userCount($where);
        return responseToJson($data);
    }

    public function test()
    {
        $detail = new \App\Models\Account_detailed;
        $userdata = $detail->getUinfo();
        if ($userdata < 0) {
            return responseToJson($userdata);
        }
        //print_r($userdata);
        $account_identity = '';
        foreach ($userdata as $k => $v) {
            $account_identity .= ($k == 0) ? "('" . $v['account_identity'] . "'" : ",'" . $v['account_identity'] . "'";
        }
        $account_identity .= empty($account_identity) ? '' : ')';
        //echo $account_identity.'<hr>';
        if (!empty($account_identity)) {
            $url = new \App\Models\Logfile;
            $urldata = $url->getUrl($account_identity);
            if (empty($urldata) || $urldata < 0) {
                foreach ($userdata as $k => $v) {
                    $res = $detail->updateInfo(array('last_url' => 'xin83888.com', 'register_url' => 'xin83888.com'), $v['account_identity']);
                    echo 1;
                }
            }

            if (!($urldata < 0)) {
                foreach ($urldata as $sk => $sv) {
                    $res = $detail->updateInfo(array('last_url' => $sv['last_url'], 'register_url' => $sv['register_url'], 'last_time' => $sv['maxtime'], 'last_ip' => $sv['ip']), $sv['account_identity']);
                    echo $sv['account'] . '<br>';
                }
            }
            if ($res > 0) {
                return responseToJson(1);
            }
        } else {
            return responseToJson(2);
        }
    }

   //  public function checkPwd(Req $req){
   //  	//密码验证

   //      // return responseToJson('/admin/User/export');
   // //  	if (!$req->isMethod('post')) {
   // //  		return view('admin.user/export');
   // //  	}else {
   // //  		$password = $req->password;
   // //  		$admin_info = \App\Model\System_user::where('id', session('adminInfo.admin_id'))->first();
			// // if (!$admin_info) {
			// // 	return responseToJson(-7010100202);
			// // }
			// // $admin_info = $admin_info->toArray();
			// // if (!VerPassword($password, $admin_info['encryption_2'], $admin_info['password_2'])) {
			// // 	return responseToJson(-7010001622);
			// // }
   //  		return redirect('/admin/User/export');
   // //  	}

   //  }
    function checkPwd(){
        $page = Request::has('page') ? Request::get('page') : 1;
        $list = Request::has('limit') ? Request::get('limit') : '1000';
        $field = Request::has('field') ? Request::get('field') : 'id';
        $order = Request::has('order') ? Request::get('order') : 'desc';
        $status = Request::has('status') ? Request::get('status') : '';
        $agent_name = Request::has('agent_name') ? Request::get('agent_name') : '';

        $account = Request::has('account') ? Request::get('account') : '';
        $select_type = Request::has('select_type') ? Request::get('select_type') : '';
        $select_value = Request::has('select_value') ? Request::get('select_value') : '';
        $grade = Request::has('grade') ? Request::get('grade') : '';
        $group = Request::has('group') ? Request::get('group') : '';
        $regist_startime = Request::get('regist_startime') ? Request::get('regist_startime') . ' 00:00:00' : '';
        $regist_endtime = Request::get('regist_endtime') ? Request::get('regist_endtime') . ' 23:59:59' : '';
        $last_startime = Request::get('last_startime') ? Request::get('last_startime') . ' 00:00:00' : '';
        $last_endtime = Request::get('last_endtime') ? Request::get('last_endtime') . ' 23:59:59' : '';
        $statuss = Request::has('statuss') ? Request::get('statuss') : '';
        $cash_small = Request::has('cash_small') ? Request::get('cash_small') : '';
        $cash_big = Request::has('cash_big') ? Request::get('cash_big') : '';
        $sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'on';
        $nameblur = Request::has('nameblur') ? Request::get('nameblur') : '';
        $rechargenum_small = Request::has('rechargenum_small') ? Request::get('rechargenum_small') : '';
        $rechargenum_big = Request::has('rechargenum_big') ? Request::get('rechargenum_big') : '';
        $takenum_small = Request::has('takenum_small') ? Request::get('takenum_small') : '';
        $takenum_big = Request::has('takenum_big') ? Request::get('takenum_big') : '';

        $where = '/admin/User/export?';
        if (!empty($account)) {
            $account = strtolower($account);
            $where .= '&account='.$account;
        }
        if (!empty($select_type)) {
             $where .= '&select_type='.$select_type;
        }
        if (!empty($select_value)) {
             $where .= '&select_value='.$select_value;
        }
        if (!empty($group)) {
             $where .= '&group='.$group;
        }
        if (!empty($grade)) {
            $where .= '&grade='.$grade;
        }
        if (!empty($statuss)) {
            $where .= "&statuss=" . $statuss;
        }
        if (!empty($regist_startime)) {
            $where .= '&regist_startime='. $regist_startime;
        }
        if (!empty($regist_endtime)) {
            $where .= '&regist_endtime=' . $regist_endtime;
        }
        if (!empty($last_startime)) {
            $where .= '&last_startime=' . $last_startime;
        }
        if (!empty($last_endtime)) {
            $where .= '&last_endtime=' . $last_endtime;
        }
        if (!empty($takenum_small)) {
            $where .= "&takenum_small=" . $takenum_small;
        }
        if (!empty($takenum_big)) {
            $where .= "&takenum_big=" . $takenum_big;
        }
        if (!empty($rechargenum_small)) {
            $where .= '&rechargenum_small=' . $rechargenum_small;
        }
        if (!empty($rechargenum_big)) {
            $where .= '&rechargenum_big=' . $rechargenum_big;
        }
        if (!empty($sureblur)) {
            $where .= '&sureblurs=' . $sureblur;
        }
        return responseToJson($where);

    }

    //Excel文件导出功能 By Laravel学院
    public function export() {
        $page = Request::has('page') ? Request::get('page') : 1;
        $list = Request::has('limit') ? Request::get('') : '';
        $field = Request::has('field') ? Request::get('field') : 'id';
        $order = Request::has('order') ? Request::get('order') : 'desc';
        $status = Request::has('status') ? Request::get('status') : '';
        $agent_name = Request::has('agent_name') ? Request::get('agent_name') : '';

        $account = Request::has('account') ? Request::get('account') : '';
        $select_type = Request::has('select_type') ? Request::get('select_type') : '';
        $select_value = Request::has('select_value') ? Request::get('select_value') : '';
        $grade = Request::has('grade') ? Request::get('grade') : '';
        $group = Request::has('group') ? Request::get('group') : '';
        $regist_startime = Request::get('regist_startime') ? Request::get('regist_startime') : '';
        $regist_endtime = Request::get('regist_endtime') ? Request::get('regist_endtime')  : '';
        $last_startime = Request::get('last_startime') ? Request::get('last_startime')  : '';
        $last_endtime = Request::get('last_endtime') ? Request::get('last_endtime')  : '';
        $statuss = Request::has('statuss') ? Request::get('statuss') : '';
        $cash_small = Request::has('cash_small') ? Request::get('cash_small') : '';
        $cash_big = Request::has('cash_big') ? Request::get('cash_big') : '';
        $sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'on';
        $nameblur = Request::has('nameblur') ? Request::get('nameblur') : '';
        $rechargenum_small = Request::has('rechargenum_small') ? Request::get('rechargenum_small') : '';
        $rechargenum_big = Request::has('rechargenum_big') ? Request::get('rechargenum_big') : '';
        $takenum_small = Request::has('takenum_small') ? Request::get('takenum_small') : '';
        $takenum_big = Request::has('takenum_big') ? Request::get('takenum_big') : '';

        $where = ' ';
        $curtime = time() - 300;
        $rflag = -1;
        $tflag = -1;
        if (!empty($account)) {
            $account = strtolower($account);
            $where .= ' AND account';
            if (empty($sureblur) || $sureblur == 'off') {
                $where .= " LIKE '%" . $account . "%'";
            } else {
                $where .= "='" . $account . "'";
            }
        }
        if (!empty($select_value)) {
            if ($select_type == 'name') {
                $where .= ' AND name';
                if (empty($nameblur) || $nameblur == '模糊') {
                    $where .= " LIKE '%" . $select_value . "%'";
                } else {
                    $where .= "='" . $select_value . "'";
                }
            } elseif ($select_type == 'register_ip') {
                $where .= " AND register_ip='" . $select_value . "'";
            } elseif ($select_type == 'last_ip') {
                $where .= " AND last_ip='" . $select_value . "'";
            } elseif ($select_type == 'register_url') {
                $where .= " AND register_url LIKE '%" . $select_value . "%'";
            } elseif ($select_type == 'last_url') {
                $where .= " AND last_url LIKE '%" . $select_value . "%'";
            } elseif ($select_type == 'phone') {
                $where .= " AND phone LIKE '%" . $select_value . "%'";
            } elseif ($select_type == 'email') {
                $where .= " AND email LIKE '%" . $select_value . "%'";
            }
        }

        if (!empty($grade)) {
            $where .= ' AND grade=';
            $where .= ($grade == 10) ? '0' : $grade;
        }
        if(!empty($group)){
            $where .= " AND group_code like '%,".$group. ",%'";
        }
        if (!empty($regist_startime)) {
            $regist_startime = date('Y-m-d H:i:s', strtotime($regist_startime));
            $where .= " AND register_time>='" . $regist_startime . "'";
        }
        if (!empty($regist_endtime)) {
            $regist_endtime = date('Y-m-d H:i:s', strtotime($regist_endtime));
            $where .= " AND register_time<='" . $regist_endtime . "'";
        }
        if (!empty($last_startime)) {
            $last_startime = date('Y-m-d H:i:s', strtotime($last_startime));
            //$last_startime = strtotime($last_startime)-300;
            $where .= " AND last_time>='" . $last_startime . "'";
            //$where .= " AND ta.effective_time>=" . $last_startime;
        }
        if (!empty($last_endtime)) {
            $last_endtime = date('Y-m-d H:i:s', strtotime($last_endtime));
            //$last_endtime = strtotime($last_endtime)-300;
            $where .= " AND last_time<='" . $last_endtime . "'";
            //$where .= " AND ta.effective_time<=" . $last_endtime;
        }
        if (!empty($statuss)) {
            $statuss = ($statuss == 2) ? '0' : $statuss;
            $where .= " AND statuss=" . $statuss;
        }

        if (!empty($status)) {
            $status = ($status == 5) ? '-1' : $status;
            $where .= " AND status=" . $status;
            $where .= " or status=" . '2';
        }
        if (!empty($cash_small)) {
            $where .= " AND cash>=" . $cash_small;
        }
        if (!empty($cash_big)) {
            $where .= " AND cash<=" . $cash_big;
        }
        if (!empty($rechargenum_small)) {
            $rflag = 1;
            $where .= ' AND (mc.recharge_num >=' . $rechargenum_small;
        }
        if (!empty($rechargenum_big)) {
            $rflag = 1;
            $where .= empty($rechargenum_small) ? 'AND (' : 'AND ';
            $where .= ' mc.recharge_num <=' . $rechargenum_big;
        }
        if ((empty($rechargenum_small) || $rechargenum_small <= 0) && (empty($rechargenum_big) || $rechargenum_big >= 0)) {
            if (!(empty($rechargenum_small) && empty($rechargenum_big))) {
                $where .= " OR mc.recharge_num ISNULL)";
            }
        } else {
            $where .= ')';
        }

        if (!empty($takenum_small)) {
            $tflag = 1;
            $where .= ' AND (tc.take_num >=' . $takenum_small;
        }
        if (!empty($takenum_big)) {
            $tflag = 1;
            $where .= empty($takenum_small) ? 'AND (' : 'AND ';
            $where .= ' tc.take_num <=' . $takenum_big;
        }
        if ((empty($takenum_small) || $takenum_small <= 0) && (empty($takenum_big) || $takenum_big >= 0)) {
            if (!(empty($takenum_small) && empty($takenum_big))) {
                $where .= " OR tc.take_num ISNULL)";
            }
        } else {
            $where .= ')';
        }


        $db = new \App\Models\Account;
        $data = $db->account($list, $page, $where, $field, $order, $rflag, $tflag);

        if ($data < 0) {
            return -1;
        }
        $data = $data['data'];
        // // 打开PHP文件句柄，php://output 表示直接输出到浏览器
        // dump($data);
        // // 输出Excel列名信息
        $head = '编号, 用户名/等级, 余额, 姓名, 注册时间, 注册IP/登录IP, 注册网址/登录网址, 手机号码, 邮箱, 最后登录时间, 状态, 代理';


        $log = array(
            session('adminInfo.admin_name'),
        );
        OperationLog(session('adminInfo.admin_id'), 'export_userlist', $log);
        $str = iconv('utf-8', 'gbk',  $head) . chr(10) . chr(9);
        foreach ($data as $value) {
            $row = array();
            $row[] = $value['id'];
            $row[] = $value['account'];
            $row[] = $value['cash'];
            $row[] = $value['name'];
            $row[] = $value['register_time'];
            $row[] = str_replace(',','-',$value['register_ip']) . '/' . str_replace(',','-',$value['last_ip']);
            $row[] = $value['register_url'] . '/' . $value['last_url'];
            $row[] = $value['phone'];
            $row[] = $value['email'];
            $row[] = $value['last_time'];
            $row[] = ($value['status']== 1) ? '启用' : '禁用';
            $row[] = $value['agent_name'];
            $str .= iconv('UTF-8', 'GBK//TRANSLIT', implode(',', $row)) . chr(10) . chr(9);
        }
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="user.csv"');
        header('Cache-Control: max-age=0');
        echo $str;

    }


    function getUser(Req $req){
        $account_name=$req->account_name?trim($req->account_name):'';
        if(empty($account_name)){
            return responseToJson(-4010100201);
        }
        $acccount_db=new \App\Models\Account_list;
        $data=$acccount_db->getMsg($account_name);
        if($data<0){
            return responseToJson($data);
        }
        //获取次数
        $recharge_db=new \App\Models\Money_details;
        $rech_data=$recharge_db->countUserNum($account_name);
        $data['take_num']=0;
        $data['recharge_num']=0;
        foreach ($rech_data as $v){
            if($v['trade_type']==5){
                $data['take_num']=$v['num'];
            }else{
                $data['recharge_num']+=$v['num'];
            }
        }
        //获取金额
        $data['recharge']=$recharge_db->sumRecharge($account_name);
        return responseToJson($data);

    }
}
