<?php
class ModelSendrewardtool {
    /**
     * Notes:异常发放流水入库
     * User: wsc
     * Time: 2022/3/23 23:38
     */
    public function sendErrLogIntoDb(){
        $key = okeys::sendErrLog();
        $len = oo::commonOprRedis('statistics')->lLen($key);
        if($len>0){
            if($len>10000){
                $len = 10000;
            }
            //{"uid":2537661,"rid":3,"reward":[{"propsId":14,"num":1200000000,"skin":0,"jokerCardExpireTime":0,"cards":null,"chestProps":null,"skinResourceId":0}],"msg":"攻击:100582,基础:800000000,宠物加成:0,vip加成:0,金币大爆炸加成:400000000","crateTime":1648049069}
            $tb = "crazygod_api_log.send_err_log";
            $list = oo::commonOprRedis('statistics')->lGetRange($key,0,$len-1);

            $fields = ["uid","rid","reward","msg","crateTime"];

            $sql = funs::db_intsertArrSQL($tb,$fields,$list,1);
            $res = oo::commonOprDb('api_log')->query($sql);
            oo::commonOprRedis('statistics')->lTrim($key,$len,-1);
        }
    }

    /**
     * Notes:通用发不发邮件
     * User: wsc
     * Time: 2022/3/23 18:26
     * @param $uid
     * @param $rewards
     * @param int $msgId 邮件模板 id
     * @param int $typ 0 试用于老版本php奖励结构  1 fox-prizeshow 结构
     * @return bool
     */
    public function sendRewardEmail($uid,$rewards,$msgId =131,$typ=1){
        $tb = otable::userSysMail($uid);
        $key = okeys::sysMsgGet($uid);
        $rewardNew = [];
        if($typ==1){
            foreach ($rewards as $k=>$v) {
                //go fox 结构体
                $one = [
                    "propsId" => $v['propsId'],
                    "num" => $v['num'],
                    "jokerCardExpireTime" => 0,
                    "cards" => null,
                    "chestProps" => null,
                    "skinResourceId" => 0,
                    "skin" => 0,
                ];
                if ($v["propsId"] == 19) {
                    $one["skin"] = $v["value"]['skin'];
                    $one["skinResourceId"] = $one["skin"];
                }
                array_push($rewardNew, $one);
            }
        }else{
            $rewardNew = $rewards;
        }

        $data = [
            "uid"=>$uid,
            "msgid"=>$msgId,
            "got"=>0,
            "rewards"=>json_encode($rewardNew),
            "expire"=>time()+86400*16,
            "create_time"=>time(),
        ];
        $sql = funs::db_insertSQL($tb,$data);
        $ret = oo::commonOprDb('common')->query($sql,1);
        if($ret){
            $eid = oo::commonOprDb('common')->insertID();
            $data['id'] = $eid;
            $data['rewards'] = json_decode($data['rewards'],true);
            $field = "{$msgId}_{$eid}";
            oo::commonOprRedis('common')->hSet($key,$field,json_encode($data));
            oo::commonOprRedis('common')->expire($key,time()+86400*3);
            return true;
        }
        return false;
    }

    /**
     * Notes:通用获取 fox-prizeshow 结构
     * User: wsc
     * Time: 2022/2/26 14:17
     * @param $uid
     * @param $aid
     * @param $actType
     * @param $lv
     * @param $prizes ["id"=>1,"num"=>1,"type"=>1,"rtype"=>1,"skin"=>0]
     * @return array
     */
    public function getPropsShow($uid,$aid,$actType,$prizes){

        $propsId = $prizes['type'];
        $num = $prizes['num'];
        $jokerCardExpireTime = 0;
        $cards = null;
        switch ($prizes['type']){
            case 13:
                if($prizes['rtype'] == 1){//固定金卡处理

                }
                break;
            case 14://金币处理
                if ($prizes['rtype'] == 2) {
                    $level = oo::commonOprModel('member')->getUserLevel($uid);
                    $lv = intval($level['levelId'])>0?intval($level['levelId']):1;
                    $betCoin =oo::commonOprModel('activitynew')->getCoinRewardByTypeAndLevel($actType,$lv);
                    $num = intval($betCoin * $prizes['num']);
                }
                break;
            case 18://小丑卡
                $jokerCardExpireTime = time()+86400;
                break;
            case 19://皮肤宝箱

            default:
                break;
        }
        $propsShow =[
            "propsId"=>$propsId,
            "num"=>$num,
            "jokerCardExpireTime"=>$jokerCardExpireTime,
            "cards"=>$cards,
            "chestProps"=>null,
            "skinResourceId"=>$prizes["skin"],
            "skin"=>$prizes["skin"],
        ];
        return $propsShow;
    }

//============锦标赛结算==================
   //锦标赛结算发奖补发--专用邮件
   public function sendRankEndRewardEmail($uid,$rewards,$msgId =131){
        $tb = otable::userSysMail($uid);
        $key = okeys::sysMsgGet($uid);
        $rewardNew = [];
        foreach ($rewards as $k=>$v){
            //go fox 结构体
            $one = [
                "propsId"=>$v['propsId'],
                "num"=>$v['num'],
                "jokerCardExpireTime"=>0,
                "cards"=>null,
                "chestProps"=>null,
                "skinResourceId"=>0,
                "skin"=>0,
            ];
            if($v["propsId"]==19){
                $one["skin"] = $v["value"]['skin'];
                $one["skinResourceId"] = $one["skin"];
            }
            array_push($rewardNew,$one);
        }
        $data = [
            "uid"=>$uid,
            "msgid"=>$msgId,
            "got"=>0,
            "rewards"=>json_encode($rewardNew),
            "expire"=>time()+86400*3,
            "create_time"=>time(),
        ];
        $sql = funs::db_insertSQL($tb,$data);
        $ret = oo::commonOprDb('common')->query($sql,1);
        if($ret){
            $eid = oo::commonOprDb('common')->insertID();
            $data['id'] = $eid;
            $data['rewards'] = json_decode($data['rewards'],true);
            $field = "{$msgId}_{$eid}";
            oo::commonOprRedis('common')->hSet($key,$field,json_encode($data));
            oo::commonOprRedis('common')->expire($key,time()+86400*16);
        }else{
            echo "err:{$uid},ret:{$ret}\r\n";
        }
    }

   public function checkRankReward($uid,$aid){
        $actInfo = oo::commonOprModel('activitynew')->getActivity($aid);
        $type =$actInfo['type'];
        $msgId = 131;
        $roomId = oo::commonOprModel('activitynew')->getUserRankRoomId($uid,$aid);
        $key = okeys::activityUserRankList($aid,$roomId);
        $ranking  = intval(oo::commonOprRedis('activity')->zRevRank($key,$uid))+1;//排名
        $num =  intval(oo::commonOprRedis('activity')->zScore($key,$uid));//分数
        if(!$roomId||$num<5){
            echo "uid:{$uid},num:{$num}\r\n";
            return false;
        }
        $cache = oo::commonOprRedis('common')->get(okeys::activityUserRankEndReward($uid,$type));
        if($cache){
            echo "uid:{$uid},cache:{$cache}\r\n";
            return false;
        }
        $reward = oo::commonOprModel('activitynew')->getRankRewardByLimit($uid,$aid,$ranking);//获取当前排行奖励
//        var_dump($reward);
        if(!empty($reward)){
            // sendRewardEmail2(3060095,$reward,$msgId);
            $this->sendRankEndRewardEmail($uid,$reward,$msgId);
            echo "sendSuccess:{$uid},type:{$type}\r\n";
            return false;
        }
        echo "rewardsErr:{$uid}\r\n";
        return $reward;
    }


//===========金币老虎机任务=================
    public function getStageRewardsCfg($uid,$aid,$actType="a",$t=1){
        $key1 = "ACT_STAGE_REWARDS_KEY:{$aid}";
        $ukey = "USER:STAGE:{$aid}:{$uid}";
        $stageCfgStr = oo::commonOprRedis('common')->get($ukey);
        $conf = oo::commonOprRedis("common")->hGet($key1,$stageCfgStr);
        $arr =json_decode($conf,true);
        return array_column($arr,"prize","id");
    }
    /**
     * Notes:补发金币老虎机任务奖励
     * User: wsc
     * Time: 2022/3/23 18:30
     * @param $uid
     * @param $aid
     * @param $stage
     */
   public function checkTreasureReward($uid,$aid,$stage){
        $actType = "treasure";
        $msgId = 133;
        $rewardCfgArr = $this->getStageRewardsCfg($uid,$aid);
        $rewardOne = $rewardCfgArr[$stage];
        if(empty($rewardOne)){
            var_dump("sendErr,uid:{$uid},stage:{$stage}");
            oo::logs()->commlog("uid:{$uid},stage:{$stage}","sendErr.log");
            return ;
        }
        $rewards = [];
        foreach ($rewardOne as $row){
            if($stage==10&&$row['type']==18||$row['type']==14){
                continue;
            }
            $rewards[] =   $this->getPropsShow($uid,$aid,$actType,$row);
        }
        // var_dump($rewards);
        $ret = $this->sendRewardEmail($uid,$rewards,$msgId);
        if(!$ret){
            var_dump("sendmsgErr:{$ret}");
        }

    }





}