<?php
class asynclogModel extends CI_Model
{
	public function __construct(){
        parent::__construct();
        $this->load->model('oo');
    }
	
	public function cacheIntoDb(){
		// 转盘流水
        $log = [
        	date("YmdH",strtotime("-4 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("-3 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("-2 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	//前一个小时
        	date("YmdH",strtotime("-1 hour")) => [
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
            //当前小时
            date("YmdH") => [
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("+1 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("+2 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("+3 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("+4 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("+5 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("+6 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("+7 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
        	date("YmdH",strtotime("+8 hour")) => [     
                'length'=> 50000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ]
        ];
        $sqlArr = [];
        foreach ($log as $logClass=>$classInfo){
            $ret = self::pop($logClass,$classInfo['length']);
            if(empty($ret)){
                continue;
            }
            $sqlKeys  = array_keys($ret[0]);
            $usersArr = [];
            foreach ($ret as $user){
                $index              = $user['uid'] % 100;
                $usersArr[$index][] = $user;
            }
            $sqlKeyStr = "";
            foreach ($sqlKeys as $sqlKey){
                $sqlKeyStr .= "`{$sqlKey}`,";
            }
            $sqlKeyStr = trim($sqlKeyStr,',');
            $sqlArr = [];
            $date = date("Ymd",strtotime((intval($logClass / 100))));
            foreach ($usersArr as $userIndex => $userArr){
                //$tb = $classInfo['tbType']?$classInfo['tb'].$date:$classInfo['tb'];
                //$sql= "INSERT INTO {$tb} ({$sqlKeyStr}) VALUE ";
                //$sqlValueStr = "";
                foreach ($userArr as $row){
                    $tb = $classInfo['tbType']?otable::Assetslog($date, $row['uid']):$classInfo['tb'];
                    if(!isset($sqlArr[$tb])) {
                        $sqlArr[$tb] = "INSERT INTO {$tb} ({$sqlKeyStr}) VALUE ";
                    }
                    $sqlArr[$tb] .= "(";
                    foreach ($sqlKeys as $sqlKey){
                        $sqlArr[$tb] .= "'{$row[$sqlKey]}',";
                    }
                    $sqlArr[$tb] = trim($sqlArr[$tb], ',');
                    $sqlArr[$tb] .= "),";
                }
            }
            foreach($sqlArr as $tb => $sql){
                $sql = trim($sql,',');
                oo::commonOprDb('log')->query($sql);
            }

        }
    }

    public function actCacheInfoDb(){
	    $log = [
            date("YmdH",strtotime("-4 hour")) => [
                'length'=> 50000,
                'tb'    => otable::ActAssetslog(date("Ymd",strtotime("-4 hour")),-1),
            ],
            date("YmdH",strtotime("-3 hour")) => [
                'length'=> 50000,
                'tb'    => otable::ActAssetslog(date("Ymd",strtotime("-3 hour")),-1),
            ],
            date("YmdH",strtotime("-2 hour")) => [
                'length'=> 50000,
                'tb'    => otable::ActAssetslog(date("Ymd",strtotime("-2 hour")),-1),
            ],
            //前一个小时
            date("YmdH",strtotime("-1 hour")) => [
                'length'=> 50000,
                'tb'    => otable::ActAssetslog(date("Ymd",strtotime("-1 hour")),-1),
            ],
            //当前小时
            date("YmdH") => [
                'length'=> 50000,
                'tb'    => otable::ActAssetslog(date("Ymd"),-1),
            ],
            date("YmdH",strtotime("+1 hour")) => [
                'length'=> 50000,
                'tb'    => otable::ActAssetslog(date("Ymd",strtotime("+1 hour")),-1),
            ],
            date("YmdH",strtotime("+2 hour")) => [
                'length'=> 50000,
                'tb'    => otable::ActAssetslog(date("Ymd",strtotime("+2 hour")),-1),
            ],
        ];
        foreach ($log as $logClass=>$classInfo){
            $ret = self::pop($logClass,$classInfo['length'],'act');
//            if(empty($ret)){
                continue;
//            }
            if(isset($ret[0]['game_version']))unset($ret[0]['game_version']);
            $sqlKeys  = array_keys($ret[0]);
            $sqlKeyStr = "";
            foreach ($sqlKeys as $sqlKey){
                $sqlKeyStr .= "`{$sqlKey}`,";
            }
            $sqlKeyStr = trim($sqlKeyStr,',');
            $sqlArr = [];
            foreach ($ret as $row){
                $tb = $classInfo['tb'];
                if(!isset($sqlArr[$tb])) {
                    $sqlArr[$tb] = "INSERT INTO {$tb} ({$sqlKeyStr}) VALUE ";
                }
                $sqlArr[$tb] .= "(";
                foreach ($sqlKeys as $sqlKey){
                    $sqlArr[$tb] .= "'{$row[$sqlKey]}',";
                }
                $sqlArr[$tb] = trim($sqlArr[$tb], ',');
                $sqlArr[$tb] .= "),";
            }
//            var_dump($sqlArr);
            foreach($sqlArr as $tb => $sql){
                $sql = trim($sql,',');
                $res =oo::commonOprDb('log')->query($sql);
            }
        }
    }
    /**
     * 入队
     * @param $class
     * @param $param
     * @return bool
     * Created by: Owen
     * Created on: 2020/3/19 17:58
     */
    public function push($class,$param){
        if(is_array($param)){
            $param = json_encode($param);
        }
        $key = okeys::assetsLog($class);
        $ret = oo::commonOprRedis('statistics')->rPush($key,$param);
        if($ret){
            return true;
        }
        return false;
    }

    /**
     * 出队
     * @param $class
     * @param int $length
     * @return array
     * Created by: Owen
     * Created on: 2020/3/23 14:18
     */
    public function pop($class,$length = 1000,$type=''){
        if($type=='act'){
            $key = okeys::actAssetsLog($class);
        }else{
            $key = okeys::assetsLog($class);
        }
        $ret  = oo::commonOprRedis('statistics')->lGetRange($key,0,$length);
        $return = [];
        foreach ($ret as $row){
            $return[] = json_decode($row,true);
        }
        oo::commonOprRedis('statistics')->lTrim($key,$length,-1);
        return $return;
    }
    
    
    //插入埋点数据
    public function cacheIntoPointDb(){
		// 转盘流水
        $log = [
            //当前小时
            date("Ymd") => [
                'length'=> 10000,
                'tb'    => otable::buryingPointLog(),
                'tbType'=> false,
            ]
        ];
        foreach ($log as $logClass=>$classInfo){
        	
            $ret = self::dataPop($logClass,$classInfo['length'],'buryingPointLog');
            
            if(empty($ret)){
                continue;
            }
            $sqlKeys  = array_keys($ret[0]);
            $usersArr = [];
            foreach ($ret as $user){
                $index              = $user['uid'] % 100;
                $usersArr[$index][] = $user;
            }
            $sqlKeyStr = "";
            foreach ($sqlKeys as $sqlKey){
                $sqlKeyStr .= "`{$sqlKey}`,";
            }
            $sqlKeyStr = trim($sqlKeyStr,',');
            foreach ($usersArr as $userIndex => $userArr){
                $tb = $classInfo['tbType']?$classInfo['tb'].$logClass:$classInfo['tb'];
                $sql= "INSERT INTO {$tb} ({$sqlKeyStr}) VALUE ";
                $sqlValueStr = "";
                foreach ($userArr as $row){
                    $sqlValueStr .= "(";
                    foreach ($sqlKeys as $sqlKey){
                        $sqlValueStr .= "'{$row[$sqlKey]}',";
                    }
                    $sqlValueStr = trim($sqlValueStr,',');
                    $sqlValueStr .= "),";
                }
                $sqlValueStr = trim($sqlValueStr,',');
                $sql .= $sqlValueStr;
                oo::commonOprDb('log')->query($sql);
            }
        }
    }
    
    /**
     * 数据出队 复用
     * @param $class
     * @param int $length
     * @return array
     * Created by: Hss
     * Created on: 2020/9/22 14:29
     */
    public function dataPop($class,$length = 1000,$type = 'buryingPointLog'){
    	if($type == 'buryingPointLog'){
    		$key = okeys::buryingPointLog($class);
    	}else{
    		return [];
    	}
    	
        $ret  = oo::commonOprRedis('Statistics')->lGetRange($key,0,$length);
        $return = [];
        foreach ($ret as $row){
            $return[] = json_decode($row,true);
        }
        oo::commonOprRedis('Statistics')->lTrim($key,$length,-1);
        return $return;
    }
}
