<?php

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

/**
 *
 */
class ReportController extends Controller {
	function Index(Req $req) {

        $request=array();
        $request['account_name']=isset($req->account_name)?trim($req->account_name):null;
        $request['betnum_small']=isset($req->betnum_small)?trim($req->betnum_small):null;
        $request['betnum_big']=isset($req->betnum_big)?trim($req->betnum_big):null;
        $request['betmoney_small']=isset($req->betmoney_small)?trim($req->betmoney_small):null;
        $request['betmoney_big']=isset($req->betmoney_big)?trim($req->betmoney_big):null;
        $request['settle_status']=isset($req->settle_status)?trim($req->settle_status):null;
        $request['name']=isset($req->name)?trim($req->name):null;
        $request['betres_small']=isset($req->betres_small)?trim($req->betres_small):null;
        $request['betres_big']=isset($req->betres_big)?trim($req->betres_big):null;
        $request['win_small']=isset($req->win_small)?trim($req->win_small):null;
        $request['win_big']=isset($req->win_big)?trim($req->win_big):null;
        $request['star_time']=isset($req->star_time)?trim($req->star_time):null;
        $request['end_time']=isset($req->end_time)?trim($req->end_time):null;



		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Report/single');
		$dt->setLang('report');
		$dt->setPriKey('game_name');
		$dt->addColsFields('name', array('templet' => '#betdetail'));
		$dt->addColsFields('munBet');
		$dt->addColsFields('betmoney');
//		$dt->addColsFields('effectivemoney');
		$dt->addColsFields('prizemoney');
		$dt->addColsFields('winmoney', array('templet' => '#betde'));
		$dt->addColsFields('star_time');
		$dt->addColsFields('end_time');
		// $dt->setToolBar(array('view'), array('toolbar' => '#query'));
		return view('admin.report/index', $dt->render($request));
	}
	//报表查看
	function View(Req $req) {
		$game = $req->game_name;
		$star_time = !empty($req->star_time) ? $req->star_time : '';
		$end_time = !empty($req->end_time) ? $req->end_time : '';
		if (empty($game)) {
			abort(404);
		}
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Report/getDatil?game=' . $game . '&star_time=' . $star_time . '&end_time=' . $end_time);
		$dt->setLang('report');
		$dt->addColsFields('name');
		$dt->addColsFields('account_name');
		$dt->addColsFields('munBet');
		$dt->addColsFields('betmoney');
		$dt->addColsFields('winmoney');
		$dt->addColsFields('star_time');
		$dt->addColsFields('end_time');
		return view('admin.report/datial', $dt->render());
	}
	//获取游戏按照个人统计数据
	function getDatil() {
		$game = Request::has('game') ? Request::get('game') : '';
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : '';
		$star_time = Request::get('star_time') ? Request::get('star_time').' 00:00:00' : '';
		$end_time = Request::get('end_time') ? Request::get('end_time').' 23:59:59' : '';
		$where = array();
		if (!empty($star_time)) {
			$where[] = array('money_time', '>=', $star_time);
		}
		if (!empty($end_time)) {
			$where[] = array('money_time', '<=', $end_time);
		}
		$buy_db = new \App\Models\MoneyBuy;
		$data = $buy_db->getTotalByUser($game, $page, $list, $where);
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	/**
	 *注单明细报表
	 * 'FunctionName description'
	 */
	function single() {
		$page = Request::has('page') ? Request::get('page') : 1;
		$list = Request::has('limit') ? Request::get('limit') : 10;

		$name = Request::has('name') ? Request::get('name') : '';
		$account_name = Request::has('account_name') ? Request::get('account_name') : '';
		$settle_status = Request::has('settle_status') ? Request::get('settle_status') : '';
		$betnum_small = Request::has('betnum_small') ? Request::get('betnum_small') : '';
		$betnum_big = Request::has('betnum_big') ? Request::get('betnum_big') : '';
		$betmoney_small = Request::has('betmoney_small') ? Request::get('betmoney_small') : '';
		$betmoney_big = Request::has('betmoney_big') ? Request::get('betmoney_big') : '';
		$betres_small = Request::has('betres_small') ? Request::get('betres_small') : '';
		$betres_big = Request::has('betres_big') ? Request::get('betres_big') : '';
		$win_small = Request::has('win_small') ? Request::get('win_small') : '';
		$win_big = Request::has('win_big') ? Request::get('win_big') : '';
		$star_time = Request::get('star_time') ? Request::get('star_time').' 00:00:00' : '';
		$end_time = Request::get('end_time') ? Request::get('end_time').' 23:59:59' : '';
		$where = array();
		$having = array();
		$where[] = array('money_buy.status', '<>', 4);
		if (!empty($account_name)) {
			$where[] = array('account_name', '=', $account_name);
		}
		/*if (!empty($settle_status) && $settle_status!=3) {
			$where[] = array('game_status', '<>', '0');
			$where[] = array('settle_status', '=', $settle_status);
		}*/
		if ($settle_status == 1) {
			$where[] = array('game_status', '=', '0');
		}
		if ($settle_status == 2) {
			$where[] = array('game_status', '<>', '0');
		}
		if (!empty($name)) {
			$where[] = array('game_name', '=', $name);
		}
		if (!empty($star_time)) {
			$where[] = array('money_time', '>=', $star_time);
		}
		if (!empty($end_time)) {
			$where[] = array('money_time', '<=', $end_time);
		}
		if (!empty($betnum_small)) {
			$having[] = 'count("order_id")>=' . $betnum_small;
		}
		if (!empty($betnum_big)) {
			$having[] = 'count("order_id")<=' . $betnum_big;
		}
		if (!empty($betmoney_small)) {
			$having[] = 'sum("money")>=' . $betmoney_small;
		}
		if (!empty($betmoney_big)) {
			$having[] = 'sum("money")<=' . $betmoney_big;
		}
		if (!empty($betres_small)) {
			$having[] = 'sum("get_money")>=' . $betres_small;
		}
		if (!empty($betres_big)) {
			$having[] = 'sum("get_money")<=' . $betres_big;
		}
		if (!empty($win_small)) {
			$having[] = 'sum("prize_money")>=' . $win_small;
		}
		if (!empty($win_big)) {
			$having[] = 'sum("prize_money")<=' . $win_big;
		}

		$db = new \App\Models\MoneyBuy;
		$data = $db->getTotal($list, $page, $where, $having);
		$allcount = array(
			'allnum' => 0,
			'allbetmoney' => 0,
			'allbetres' => 0,
			'allwinmoney' => 0,
			'alleffectivemoney' => 0,
		);
		
		$gamedata = array();
		$tempgd = array();
		$sl = ($page-1)*$list;
		$i = 0;
		if (!($data < 0)) {
		    /*************有效金额************************/
		    //获取游戏列表
           /* $nodatas=array();
            $effectivemoney=array();
            if (empty($name)) {
                $gameList=\App\Model\GameType::getGameType();
                foreach ($gameList as $kg=>$vg){
                    $nodata=DB::Select("select info_no from game_".$vg["gameCode"]." where open_time >='".$star_time."' and open_time <='".$end_time."'");
                    $nodatas=array_merge($nodatas,array_column($nodata,'info_no'));
                    $effectivemoney[$vg['gameCode']]=0;
                }
            }else{
                $nodata=DB::Select("select info_no from game_".$name." where open_time >='".$star_time."' and open_time <='".$end_time."'");
                $nodatas=array_merge($nodatas,array_column($nodata,'info_no'));
                $effectivemoney[$name]=0;
            }

            //按游戏统计有效金额
            $effective=array();
            if($nodatas){
                $effective=$db->getEffectiveMoney($list, $page, $nodatas,$name);
            }
            foreach ($effective as $k=>$v){
                $effectivemoney[$v['game_name']]=$v['money'];
            }*/
		    /*************有效金额************************/
			foreach ($data as $k => $v) {

				if(isset($tempgd[$v['game_name']])){
                    $tempgd[$v['game_name']]['effectivemoney'] = 0;
					$tempgd[$v['game_name']]['winmoney'] += $v['winmoney'];
					$tempgd[$v['game_name']]['prizemoney'] += $v['prizemoney'];
					$tempgd[$v['game_name']]['betmoney'] += $v['betmoney'];
					$tempgd[$v['game_name']]['munBet'] += $v['munBet'];
					$tempgd[$v['game_name']]['end_time'] = ($v['end_time']>$tempgd[$v['game_name']]['end_time'])?$v['end_time']:$tempgd[$v['game_name']]['end_time'];
					$tempgd[$v['game_name']]['star_time'] = ($v['star_time']<$tempgd[$v['game_name']]['star_time'])?$v['star_time']:$tempgd[$v['game_name']]['star_time'];

//					$tempgd[$v['game_name']]['effectivemoney'] += ($v['game_status']!=0)?$v['betmoney']:0;

				}else{
					$tempgd[$v['game_name']]['winmoney'] = $data[$k]['winmoney'];
					$tempgd[$v['game_name']]['prizemoney'] = $v['prizemoney'];
					$tempgd[$v['game_name']]['betmoney'] = $v['betmoney'];
					$tempgd[$v['game_name']]['munBet'] = $v['munBet'];
					$tempgd[$v['game_name']]['end_time'] = $v['end_time'];
					$tempgd[$v['game_name']]['star_time'] = $v['star_time'];
					$tempgd[$v['game_name']]['game_name'] = $v['game_name'];
					$tempgd[$v['game_name']]['name'] = $v['name'];
                    $tempgd[$v['game_name']]['effectivemoney'] = 0;
//					$tempgd[$v['game_name']]['effectivemoney'] = ($v['game_status']!=0)?$v['betmoney']:0;
				}
			}
			foreach ($tempgd as $v) {
				if($i<$sl){
					$i++;
					continue;
				}
				if(($i-$sl)>=$list)break;
				$gamedata[$i] = $v;
				$gamedata[$i]['winmoney'] = ($v['winmoney'] > 0) ? -$v['winmoney'] : abs($v['winmoney']);
				$allcount['allnum'] += $v['munBet'];
				$allcount['allbetmoney'] += $v['betmoney'];
				$allcount['allbetres'] += $v['prizemoney'];
				$allcount['allwinmoney'] += $v['winmoney'];
				$allcount['alleffectivemoney'] += $v['effectivemoney'];
				$i++;
			}

			foreach ($gamedata as $k => $v) {
				$gamedata[$k]['allnum'] = $allcount['allnum'];
				$gamedata[$k]['allbetmoney'] = $allcount['allbetmoney'];
				$gamedata[$k]['allbetres'] = $allcount['allbetres'];
				$gamedata[$k]['allwinmoney'] = $allcount['allwinmoney'];
				$gamedata[$k]['alleffectivemoney'] = $allcount['alleffectivemoney'];
			}
		}
		$data = arrayToNumber($gamedata, array('betmoney', 'prizemoney', 'winmoney','effectivemoney', 'allbetmoney', 'allbetres', 'allwinmoney','alleffectivemoney'));

		return \App\Lib\DataTable\DataTable::init()->toJson($gamedata, count($tempgd));
	}

}
?>