<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class GameController extends Controller {

    function betMoney(){
       //统计最近7天投注总额按天进行分组
       $start_time = date('Y-m-d', strtotime('-7 days'));
       $end_time = date('Y-m-d',time());

       //计算日期内天数
       $stimestamp = strtotime($start_time);
       $etimestamp = strtotime($end_time);
       //计算日期段内有多少天
       $days = ($etimestamp - $stimestamp) / 86400;
       //保存每天日期
       $date = array();
       for($i = 1;$i <= $days;$i++){
           $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
       }
       //单式投注
       $bet_money_s =DB::table('money_buy_simplex')->whereBetween('money_time',[$start_time,$end_time.' 23:59:59'])
          ->selectRaw('date(money_time) as name,sum(money) as value')
          ->groupBy('money_time')->get();

        //串式投注
        $bet_money_str =DB::table('money_buy_str')->whereBetween('money_time',[$start_time,$end_time.' 23:59:59'])
        ->selectRaw('date(money_time) as name,sum(money) as value')
        ->groupBy('money_time')->get();

        
        if(!empty($bet_money_s)){
            $betData_d = [];
            foreach($bet_money_s as $k=>$v){
                $betData_d[$k]['name'] = $v->name; 
                $betData_d[$k]['value'] = $v->value; 
            }
        }
        if(!empty($bet_money_str)){
            $betData_r = [];
            foreach($bet_money_str as $k=>$v){
                $betData_r[$k]['name'] = $v->name; 
                $betData_r[$k]['value'] = $v->value; 
            }
        }

        $bet_money = array_merge($betData_d,$betData_r);
        if(!empty($bet_money)){
            $newData = [];
            foreach($bet_money as $k=>$v){
                if(!isset($newData[$v['name']])){
                    $newData[$v['name']] = $v;
                }else{
                    $newData[$v['name']]['value'] += $v['value'];
                }
            }
        }

       //循环补全日期
       foreach ($date as $key => $val){
           $data[$key] = [
           'name' => $val,
           'value' => 0
           ];
           foreach ($newData as $item => $value){
               if($val == $value['name']){
                   $data[$key] = $value;
               }
           }
       }

       foreach ($data as $k => $v) {
           $array['title'][$k] = $v['name'];
           $array['data']['num'][$k] = $v['value'];
       }
       $array['legend'][] = '最近7天投注总额趋势图';

       return responseToJson($array);
    }

}