<?php

/**
 * 异步流水
 * Class ModelAsyncLog
 * Created by: Owen
 * Created on: 2020/3/18 19:32
 */
class ModelAsynclog{
    public function cacheIntoDb(){
        $log = [
            'SLOT'     => [     // 老虎机流水
                'length'=> 10000,
                'tb'    => otable::slotlog(),
                'tbType'=> true,
            ],
            'ASSETS'   => [     // 财产流水
                'length'=> 10000,
                'tb'    => otable::Assetslog(),
                'tbType'=> true,
            ],
            'ABNORMAL' => [     // 异常流水
                'length'=> 10000,
                'tb'    => otable::abnormalSta(),
                'tbType'=> false,
            ],
        ];
        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,',');
            foreach ($usersArr as $userIndex => $userArr){
                $tb = $classInfo['tbType']?$classInfo['tb'].$userIndex%100:$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('common')->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::CacheIntoDb($class);
        $ret = oo::commonOprRedis('common')->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){
        $key  = okeys::CacheIntoDb($class);
        $ret  = oo::commonOprRedis('common')->lGetRange($key,0,$length);
        $return = [];
        foreach ($ret as $row){
            $return[] = json_decode($row,true);
        }
        oo::commonOprRedis('common')->lTrim($key,$length,-1);
        return $return;
    }
}
