<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;

/**
 *
 */
class IndexController extends Controller {

    function welcome() {
        $total = array(
            'total_user' => $this->countTodayUser(),
            'total_bet' => $this->countBet(),
            'today_take' => $this->countTodayTake(),
            'today_recharge' => $this->countTodayRecharge(),
            'today_return' => $this->countTodayReturn(),
            'today_rimit' => $this->countTodayRimt(),
        );
        if (\App\Models\Role::hasRoot('/enable/show/get_money_count') < 0) {
            $get_money_count = "*****";
        } else {
            $get_money_count = $total['today_recharge']['count'];
        }
        $notice = $this->getNotice();
        $message = $this->Message();
        return view('admin/welcome', ['total' => $total, 'notice' => $notice, 'message' => $message, 'get_money_count' => $get_money_count]);
    }

    //返回用户数量
    function countUser() {
        return \App\Models\Account_detailed::count();
    }

    //今日注册用户数量
    function countTodayUser() {
        $time1 = date('Y-m-d 00:00:00'); //今日0时
        $time2 = date('Y-m-d 23:59:59'); //
        $timearea = [$time1, $time2];
        if (\App\Models\Role::hasRoot('/enable/show/today_user') < 0) {
            $user_data['today_user'] = "*****";
        } else {
            $user_data['today_user'] = \App\Models\Account_detailed::whereBetween('register_time', $timearea)->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('status', '1')->count();
        }
        if (\App\Models\Role::hasRoot('/enable/show/last_online') < 0) {
            $user_data['last_online'] = "*****";
        } else {
            $user_data['last_online'] = \App\Models\Account_Token::whereBetween('last_time', $timearea)->count();
        }
        if (\App\Models\Role::hasRoot('/enable/show/user_count') < 0) {
            $user_data['user_count'] = "*****";
        } else {
            $user_data['user_count'] = \App\Models\Account_detailed::count();
        }
        return $user_data;
    }

    //注单总数量
    function countBet() {
        $time1 = date('Y-m-d 00:00:00'); //今日0时
        $time2 = date('Y-m-d 23:59:59'); //
        $timearea = [$time1, $time2];
        if (\App\Models\Role::hasRoot('/enable/show/bet_count') < 0) {
            $bet_count['count'] = "*****";
        } else {
            $bet_count['count'] = \App\Models\MoneyBuy::where('money_buy.status', '<>', 4)->join('account', 'money_buy.account_identity', '=', 'account.identity')->where('account.status', '<>', '4')->where('account.user_type','1')->whereBetween('money_time', $timearea)->count();
        }
        if (\App\Models\Role::hasRoot('/enable/show/bet_money') < 0) {
            $bet_money = \App\Models\MoneyBuy::where('money_buy.status', '<>', 4)->join('account', 'money_buy.account_identity', '=', 'account.identity')->where('account.status', '<>', '4')->where('account.user_type','1')->whereBetween('money_time', $timearea)->sum('money');
            $bet_count['money_count'] = "*****";
        } else {
            $bet_money = \App\Models\MoneyBuy::where('money_buy.status', '<>', 4)->join('account', 'money_buy.account_identity', '=', 'account.identity')->where('account.status', '<>', '4')->where('account.user_type','1')->whereBetween('money_time', $timearea)->sum('money');
            $bet_count['money_count'] = round($bet_money, 2);
        }
        // DB::connection()->enableQueryLog();
        if (\App\Models\Role::hasRoot('/enable/show/countBet') < 0) {
            $db = new \App\Models\MoneyBuy;
            $bet_count['user_count'] = "*****";
        } else {
            $db = new \App\Models\MoneyBuy;
            $bet_count['user_count'] = $db->countUser($timearea);
        }
        // $queries = DB::getQueryLog();
        // print_r($queries);
        return $bet_count;
    }

    //今日提现
    function countTodayTake() {
        $time1 = date('Y-m-d 00:00:00'); //今日0时
        $time2 = date('Y-m-d 23:59:59'); //
        $timearea = [$time1, $time2];

        if (\App\Models\Role::hasRoot('/enable/show/count') < 0) {
            $take_count['count'] = "*****";
        } else {
            $take_count['count'] = \App\Models\Money_take::whereBetween('apply_time', $timearea)->where('status', 1)->count();
        }
        if (\App\Models\Role::hasRoot('/enable/show/money_count') < 0) {
            $take_count['money_count'] = "*****";
        } else {
            $take_count['money_count'] = \App\Models\Money_take::whereBetween('apply_time', $timearea)->where('status', 1)->sum('money');
        }
//        DB::connection()->enableQueryLog();
        if (\App\Models\Role::hasRoot('/enable/show/user_count') < 0) {
            $db = new \App\Models\Money_take;
            $take_count['user_count'] = "*****";
        } else {
            $db = new \App\Models\Money_take;
            $take_count['user_count'] = $db->countUser($timearea);
        }
//        $queries = DB::getQueryLog();
//        dump($queries);
//        dump($take_count);
        return $take_count;
    }

    //今日充值笔数
    function countTodayRecharge() {
        $time1 = date('Y-m-d 00:00:00'); //今日0时
        $time2 = date('Y-m-d 23:59:59'); //
        $timearea = [$time1, $time2];
//        DB::connection()->enableQueryLog();
        if (\App\Models\Role::hasRoot('/enable/show/countTodayRecharge') < 0) {
            $rech_data['count'] = "*****";
        } else {
            $rech_data['count'] = \App\Models\Money_details::whereBetween('money_time', $timearea)->whereIn('trade_type', array(6, 11))->where('status', 1)->count();
        }
//        $queries = DB::getQueryLog();
//        print_r($queries);
        if (\App\Models\Role::hasRoot('/enable/show/countTodayRecharge_money') < 0) {
            $money_count = \App\Models\Money_details::whereBetween('money_time', $timearea)->whereIn('trade_type', array(6, 11))->where('status', 1)->sum('money');
            $rech_data['money_count'] = "*****";
        } else {
            $money_count = \App\Models\Money_details::whereBetween('money_time', $timearea)->whereIn('trade_type', array(6, 11))->where('status', 1)->sum('money');
            $rech_data['money_count'] = round($money_count, 2);
        }
//        DB::connection()->enableQueryLog();
        if (\App\Models\Role::hasRoot('/enable/show/countTodayRecharge_user') < 0) {
            $db = new \App\Models\Money_details;
            $count = $db->countUser($timearea, array(6, 11));
            $rech_data['user_count'] = "*****";
        } else {
            $db = new \App\Models\Money_details;
            $count = $db->countUser($timearea, array(6, 11));
            $rech_data['user_count'] = $count;
        }
//        $queries = DB::getQueryLog();
//        dump($queries);
        return $rech_data;
    }

    //	今日回水笔数
    function countTodayReturn() {
        $time1 = date('Y-m-d 00:00:00'); //今日0时
        $time2 = date('Y-m-d 23:59:59'); //
        $timearea = [$time1, $time2];
        if (\App\Models\Role::hasRoot('/enable/show/countTodayReturn') < 0) {
            $return['count'] = "*****";
        } else {
            $return['count'] = \App\Models\Money_return::whereBetween('processing_time', $timearea)->count();
        }
        if (\App\Models\Role::hasRoot('/enable/show/countTodayReturn_money') < 0) {
            $return['money_count'] = "*****";
        } else {
            $return['money_count'] = \App\Models\Money_return::whereBetween('processing_time', $timearea)->sum('money');
        }
//		$return['user_count'] = \App\Model\Money_return::whereBetween('processing_time', $timearea)->groupBy('account_identity')->count();
        if (\App\Models\Role::hasRoot('/enable/show/countTodayReturn_user') < 0) {
            $db = new \App\Models\Money_return;
            $return['user_count'] = "*****";
        } else {
            $db = new \App\Models\Money_return;
            $return['user_count'] = $db->countUser($timearea);
        }
        return $return;
    }

    //	今日反水笔数
    function countTodayBack() {
        $time1 = date('Y-m-d 00:00:00'); //今日0时
        $time2 = date('Y-m-d 23:59:59'); //
        $timearea = [$time1, $time2];
        if (\App\Models\Role::hasRoot('/enable/show/countTodayBack') < 0) {
            $return['count'] = "*****";
        } else {
            $return['count'] = \App\Models\Money_back::whereBetween('processing_time', $timearea)->count();
        }
        if (\App\Models\Role::hasRoot('/enable/show/countTodayBack_money') < 0) {
            $return['money_count'] = "*****";
        } else {
            $return['money_count'] = \App\Models\Money_back::whereBetween('processing_time', $timearea)->sum('money');
        }
        if (\App\Models\Role::hasRoot('/enable/show/countTodayBack_user') < 0) {
            $return['user_count'] = "*****";
        } else {
            $return['user_count'] = \App\Models\Money_back::whereBetween('processing_time', $timearea)->groupBy('account_identity')->count();
        }
        return $return;
    }

    //今日汇款
    function countTodayRimt() {
        $time1 = date('Y-m-d 00:00:00'); //今日0时
        $time2 = date('Y-m-d 23:59:59'); //
        $timearea = [$time1, $time2];
        if (\App\Models\Role::hasRoot('/enable/show/countTodayRimt') < 0) {
            $rimit['count'] = "*****";
        } else {
            $rimit['count'] = \App\Models\Money_recharge::whereBetween('apply_time', $timearea)->where('recharge_type', '汇款')->where('status', 1)->count();
        }
        if (\App\Models\Role::hasRoot('/enable/show/countTodayRimt_money') < 0) {
            $rimit['money_count'] = "*****";
        } else {
            $rimit['money_count'] = \App\Models\Money_recharge::whereBetween('apply_time', $timearea)->where('recharge_type', '汇款')->where('status', 1)->sum('money');
        }
        if (\App\Models\Role::hasRoot('/enable/show/countTodayRimt_user') < 0) {
            $db = new \App\Models\Money_recharge;
            $rimit['user_count'] = "*****";
        } else {
            $db = new \App\Models\Money_recharge;
            $rimit['user_count'] = $db->countRimitUser($timearea);
        }
        return $rimit;
    }

    //公告信息
    function getNotice() {
        $db = new \App\Models\Article();
        $data = $db->getMsg(2, 2);
        return $data;
    }

    //当前在线用户饼状图
    function onlineUser() {
//		$db = new \App\Model\Logfile;
//		$db=new \App\Model\Account_Token;
        $db = new \App\Models\Account_detailed;
        $data_log = $db->getOnlineUser();
//		$data_log = $db->getUser();

        $array['data'] = $data_log;
        if (empty($data_log)) {
            $array['data'][0]['name'] = '没有';
            $array['data'][0]['value'] = 0;
        }

        //组装标
        $count = 0;
        foreach ($array['data'] as $k => $v) {
            $array['title'][$k] = trim($v['name']);
            $array['data'][$k]['name'] = trim($v['name']);
            $count += $v['value'];
        }
        //总在线数量、
        $array['text'] = date('Y-m-d') . trans('common.today_user_num') . $count;
        return responseToJson($array);
//        dump($array);

    }

    //今日投注统计饼状图
    function getBetUrl() {
        $db = new \App\Models\OrderUrl;
        $data = $db->getUrlBet();
        $array['data'] = $data;
        if (empty($data)) {
            $array['data'][0]['name'] = '没有';
            $array['data'][0]['value'] = 0;
        }
        //组装标
        foreach ($array['data'] as $k => $v) {
            $array['title'][$k] = $v['name'];
        }
        //添加标题
        //总投注数量
        $count = $db->gettotal();
        $array['text'] = date('Y-m-d') . trans('common.today_bet_num') . $count;
//        dump($array);
        return responseToJson($array);
    }

    //时间区
    function getTime() {
        $time = array(
            'addrss' => date_default_timezone_get(),
            'time' => date("Y-m-d H:i:s", time() + 3600 * 8),
        );
        // print_r($time['time']);die;
        return responseToJson($time);
    }

    //最近7天会员注册情况
    function regUser() {
        $time2 = date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 30);
        $time1 = date('Y-m-d H:i:s', time());
        $timearea = [$time2, $time1];
        // $rech_data['money_with'] = DB::table('account_detailed')->join('account', 'account.identity', '=', 'money_details.account_identity')->select(DB::Raw('sum(money) as with'))->whereBetween('money_time', $timearea)->groupBy('account')->where('trade_type', 5)->get();
        $rimit = \App\Models\Account_list::where('account_status', 1)
            ->select(DB::Raw('date(register_time) as name, count(identity) as value'))
            ->whereBetween('register_time', $timearea)->orderBy(DB::Raw('date(register_time)'))->groupBy(DB::Raw('date(register_time)'))->get();
        $data = $rimit->toArray();
        foreach ($data as $k => $v) {
            $array['title'][$k] = $v['name'];
            $array['data']['num'][$k] = $v['value'];
        }
        $array['legend'][] = '最近注册用户记录';
        return responseToJson($array);
    }

    //最近7天财务情况
    function money() {
        $res_array = array();

        for ($i = 6; $i >= 0; $i--) {
            $secondes = $i * 24 * 60 * 60;
            $res_array['title'][] = date('Y-m-d', (time() - $secondes));
            $res_array['data']['money_with'][] = 0;
            $res_array['data']['money_recharge'][] = 0;
            $res_array['data']['money_remittance'][] = 0;
            $res_array['data']['money_trans'][] = 0;
            $res_array['data']['profit'][] = 0;
        }
        $res_array['legend']['money_with'] = trans('user.money_with');
        $res_array['legend']['money_recharge'] = trans('user.money_recharge');
        $res_array['legend']['money_remittance'] = trans('user.money_remittance');
        $res_array['legend']['money_trans'] = trans('user.money_trans');
        $res_array['legend']['profit'] = '盈利';
        $db = new \App\Models\Money_details;
        $data = $db->getBet();
        if ($data < 0) {
            return responseToJson($res_array);
        }

        $trade_type = $this->getTradetype();
        foreach ($data as $k => $v) {
            foreach ($res_array['title'] as $tk => $tv) {
                if ($v['date'] == $tv) {
                    $res_array['data'][$trade_type[$v['trade_type']]][$tk] += $v['value'];
                    $res_array['data'][$trade_type[1]][$tk] += ($v['trade_type'] == 5) ? -$v['value'] : +$v['value'];
                    break;
                }
            }
        }
        return responseToJson($res_array);
        /*print_r($data);exit;

        $i = 0;
        $array['title']=array();
        foreach ($data as $k => $v) {
            if (is_array($v) && count($v) > 0) {
                foreach ($v as $ka => $va) {
                    $array['title'][$i] = $va['date'];
                    $temp[$k][$va['date']] = $va['value'];
                    $i++;
                }
            } else {
                $temp[$k] = array();
            }
            $array['legend'][$k] = trans('user.' . $k);
        }
        $array['title']=array_unique($array['title']);
        asort($array['title']);
        $tem=array();
        foreach($array['title'] as $v){
            $tem[]=$v;
        }
        $array['title']=$tem;
        $te = array();
        foreach ($array['title'] as $k => $v) {
            if (array_key_exists($v, $temp['money_with'])) {
                $te['money_with'][] = $temp['money_with'][$v];
                $money_with=$temp['money_with'][$v];
            } else {
                $te['money_with'][] = 0;
                $money_with=0;
            }
            if (array_key_exists($v, $temp['money_recharge'])) {
                $te['money_recharge'][] = $temp['money_recharge'][$v];
                $money_recharge=$temp['money_recharge'][$v];
            } else {
                $te['money_recharge'][] = 0;
                $money_recharge=0;
            }
            if (array_key_exists($v, $temp['money_remittance'])) {
                $te['money_remittance'][] = $temp['money_remittance'][$v];
                $money_remittance=$temp['money_remittance'][$v];
            } else {
                $te['money_remittance'][] = 0;
                $money_remittance=0;
            }
            if (array_key_exists($v, $temp['money_trans'])) {
                $te['money_trans'][] = $temp['money_trans'][$v];
                $money_trans=$temp['money_trans'][$v];
            } else {
                $te['money_trans'][] = 0;
                $money_trans=0;
            }
            $te['profit'][]=$money_trans+$money_remittance+$money_recharge-$money_with;
        }
        $array['legend']['profit']='盈利';
        $array['data'] = $te;
        return responseToJson($array);*/
    }

    private function getTradetype() {
        $tradetype = array(
            '5' => 'money_with',
            '6' => 'money_recharge',
            '11' => 'money_trans',
            '14' => 'money_remittance',
            '1' => 'profit',
        );
        return $tradetype;
    }

    //每天每个游戏注单数
    function TodayGameNote() {
        $time1 = date('Y-m-d 00:00:00');//今日零时
        $time2 = date('Y-m-d 23:59:59');//今日末时
//        $time1='2017-09-14 00:00:00';
//        $time2='2017-09-14 23:59:59';
        $timearea = [$time1, $time2];
        $data = \App\Models\MoneyBuy::select('game_name as name', DB::Raw('count(id) as value'))->whereBetween('money_time', $timearea)->groupBy('game_name')->get();
        $data = $data->toArray();
        if (count($data) == 0) {
            $data = array(array('name' => 'no_bet', 'value' => 0));
        }
        $array = array();
        foreach ($data as $k => $v) {
            $data[$k]['name'] = trans('common.' . $v['name']);
            $array['title'][$k] = trans('common.' . $v['name']);
        }
        $array['data'] = $data;
        $array['text'] = '今日游戏注单数量:' . date('Y-m-d');
        return responseToJson($array);
    }

    //拉时间
    function pullTime() {
        //获取前台域名
        $setInfo = new \App\Models\Setinfo();
        $info = $setInfo->getInfo(18);
        if ($info < 0) {
            return responseToJson($info);
        }
        //$a=file_get_contents($info['infocontent'].'api-common/getTime');
        $url = $info['infocontent'] . 'api-common/getTime';
        $timeout = 30;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //避免data数据过长问题
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array());

        $a = curl_exec($ch);
        curl_close($ch);
        echo $a;
    }

    //通知消息
    function Message() {

        $admin_id = session('adminInfo.admin_id');
        $db = new \App\Models\MessageRead;
        $dbs = new \App\Models\Manager;
        $where = [
            'type',
            '4'
        ];
        $datas = $dbs->getlistd($where);
        $data = $db->getlist($admin_id);
        if (!empty($data)) {
            foreach ($datas as $k => $v) {
                foreach ($data as $val) {
                    if ($v['id'] == $val['message_id']) {
                        $datas[$k]['status'] = '1';
                    }
                }
            }
        }
        return $datas;
    }

    //刷新缓存
    public function refresh() {
        return view('admin.refresh');
    }
}

