<?php
/**
 * User: MikeMei
 */
defined('IN_WEB') or die('Include Error!');
class Config{
    /**
     * 消息中心
     * @return string
     * Created by: Owen
     * Created on: 2019/11/29 10:19
     */
    public function newsCenter($param){
        $uid  = oo::functions()->uint($param['uid']);
        $news = [];
        $sortEnd   = 100;
        $sortStart = 1;
		
		$version = oo::commonOprModel('user')->getUserVersion($uid);
		
        /**
         * 七日簽到
         */
        $isreward = intval(oo::commonOprRedis('User')->get(okeys::SignInToday($uid)));
        $spinMonthly = oo::commonOprModel('payment')->getSpinMonthly($uid,1001);
        $spinMonthly = empty($spinMonthly)?0:$spinMonthly['endTime'];
        $spinMonthlyPro = oo::commonOprModel('payment')->getSpinMonthly($uid,1002);
        $spinMonthlyPro = empty($spinMonthlyPro)?0:$spinMonthlyPro['endTime'];
        $signInSort = $isreward?$sortEnd--:$sortStart++;
        $progress  = oo::commonOprModel('member')->getUserSignInProgress($uid,true);
        $news[] = [
            'id'   => 1,
            'sort' => $signInSort,
            'type' => self::isShow(1),
            'data' =>[
                'days'       => intval(oo::commonOprRedis('User')->get(okeys::SignInTimes($uid))) + 1 - $isreward,
                'isreward'   => $isreward,
                'spinMonthly'=> [$spinMonthly,$spinMonthlyPro],
                'progress'   => [
                    'rewardDay' => $progress['rewardDay'],
                    'signInDay' => $progress['signInDay'],
                ],
            ]
        ];
        /**
         * 系統补偿,旧接口,不用了
         */
        //$config = oo::commonOprModel('config')->getSysCompensateConfig($uid);
        $config = [];
        if(!empty($config) && $config['endTime'] > time()){
            $reward =  $config['reward'];
            $isreward = intval(oo::commonOprRedis('User')->get(okeys::sysCompensate($config['id'],$uid)));
            if(!$isreward){
                $news[] = [
                    'id'   => 2,
                    'sort' => $isreward?$sortEnd--:$sortStart++,
                    'type' => self::isShow(2),
                    'data' =>[
                        'reward'  => json_decode($reward,true),
                        'isreward'=> $isreward,
                    ]
                ];
            }
        }

        /**
         * 更新礼包
         */
        $giftVersion = oo::commonOprModel('config')->getVersionGiftStatus($uid);
        $config  = oo::commonOprModel('config')->getVersionConfig();
        if($config['gu_isReward'] == 1 && !oo::compareVersion($version,$config['gu_version'],false)){
            $news[] = [
                'id'   => 4,
                'sort' => intval($giftVersion)?$sortEnd--:$sortStart++,
                'type' => self::isShow(4),
                'data' =>[
                    'reward'  => json_decode($config['gu_reward'],true),
                    'isreward'=> intval($giftVersion),
                    'version' => $config['gu_version']
                ]
            ];
        }

        /**
         * 收集活动
         */
        if(oo::commonOprModel('activity')->activityLock($uid,2,true)){
            $news[] = [
                'id'   => 5,
                'sort' => 5,
                'type' => self::isShow(5),
                'data' => [
                    'userData'     => oo::commonOprModel('activity')->getSymbol($uid),
                    'activityData' => oo::commonOprModel('activity')->getActivity(2),
                ]
            ];
        }

        /**
         * 气球活动
         */
        if(oo::commonOprModel('activity')->activityLock($uid,3,true)){
            $temp = oo::commonOprModel('activity')->getActivity(3);
            $temp['waves'] = intval(oo::commonOprRedis('common')->hGet(okeys::BalloonCount(),$uid));
            if($temp['activityType'] == 2){
                $id = 7;
            }else{
                $id = 6;
            }
            $news[] = [
                'id'   => $id,
                'sort' => $id,
                'type' => self::isShow($id),
                'data' => $temp
            ];
        }

        /**
         * 偷打
         */
        if(oo::commonOprModel('activity')->activityLock($uid,6,true)){
            $activity6 = oo::commonOprModel('activity')->getActivity(6);
            $news[] = [
                'id'   => 8,
                'sort' => self::isShow(8),
                'type' => 1,
                'data' => [
                    'userData'    =>oo::commonOprModel('activity')->updateStealAttack($uid),
                    'activityData'=>$activity6,
                ]
            ];
        }

        /**
         * 建筑大师
         */
        $activity8 = oo::commonOprModel('activity')->activityLock($uid,8,true);
        if($activity8){
            $userData = oo::commonOprRedis('common')->hGet(okeys::BuildMaster(),$uid);
            $news[] = [
                'id'   => 12,
                'sort' => 12,
                'type' => self::isShow(12),
                'data' =>[
                    'userData'    =>empty($userData)?[]:json_decode($userData,true),
                    'activityData'=>$activity8,
                ],
            ];
        }

        /**
         * 倍率活动
         */
        $activity9 = oo::commonOprModel('activity')->activityLock($uid,9,true);
        if($activity9){
            $news[] = [
                'id'   => 13,
                'sort' => 12,
                'type' => self::isShow(13),
                'data' => $activity9,
            ];
        }

        /**
         * 卡片活动
         */
        $activity10 = oo::commonOprModel('activity')->activityLock($uid,10,true);
        if($activity10){
            $news[] = [
                'id'   => 14,
                'sort' => 12,
                'type' => self::isShow(14),
                'data' => $activity10,
            ];
        }

        /**
         * jackPot
         */
        $activity11 = oo::commonOprModel('activity')->activityLock($uid,11,true);
        if($activity11){
            $activity11['protectTime'] = 300;
            $activity11['activityName']= "jackPot";
            $activity11['curNum'] = oo::commonOprModel('activity')->getJackPotPool();
            $news[] = [
                'id'   => 15,
                'sort' => 13,
                'type' => self::isShow(15),
                'data' => $activity11,
            ];
        }

        $activity12 = oo::commonOprModel('activity')->activityLock($uid,12,true);
        if($activity12){
            /**
             * 卡片交换
             */
            $news[] = [
                'id'   => 16,
                'sort' => $signInSort+1,
                'type' => self::isShow(16),
                'data' => $activity12
            ];
        }
        $activity13 = oo::commonOprModel('activity')->activityLock($uid,13,true);
        if($activity13){
            $activity13['model'] = oo::commonOprModel('readconfig')->getSysCon('activtity13',"model");
            $activity13['rewardTime'] = $activity13['reward_time'];
            /**
             * 排行榜活动
             */
            $activity13['curNum']   = intval(oo::commonOprRedis('common')->hGet(okeys::ActivityRankProgress("NUM"),$uid));
            $activity13['startNum'] = 5;
            $activity13['selfData'] = oo::commonOprModel('activity')->getUserRankData($uid);
            $activity13['rankData'] = oo::commonOprModel('activity')->getAllUserRankByUid($uid);
            $activity13['limitReward'] = [];
            $news[] = [
                'id'   => 17,
                'sort' => 17,
                'type' => self::isShow(17),
                'data' => $activity13
            ];
        }

        /**
         * 体力加速活动
         */
        $activity14 = oo::commonOprModel('activity')->activityLock($uid,14,true);
        if($activity14){
            $news[] = [
                'id'   => 18,
                'sort' => $signInSort+1,
                'type' => self::isShow(18),
                'data' => [
                    'userData' =>[
                        'bet'      => intval(oo::commonOprRedis('common')->get(okeys::SpeedUpSpins($uid))),
                        'lastTime' => max(0,oo::commonOprRedis('common')->ttl(okeys::SpeedUpSpins($uid))),
                    ],
                    'activityData'=>$activity14,
                ]
            ];
        }
        return oo::response(1,$news,'',true);
    }

    public function isShow($id){
        $show = oo::commonOprRedis('common')->hGet(okeys::RewardCenterShow(),$id);
        return ($show == false)?1:$show;
    }

    public function shareConfig($param){
        $uid = oo::functions()->uint($param['uid']);
        $config = oo::commonOprModel('config')->getShareConfig();
        $configTmp = [];
        foreach ($config as $row){
            $configTmp[$row['type']] = [
                'maxTimes'   => $row['maxTimes'],
                'selfReward' => $row['selfReward'],
                'otherReward'=> $row['otherReward'],
            ];
        }

        $reward = oo::commonOprRedis('common')->get(okeys::ShareRewardId($uid));
        if(empty($reward)){
            $reward = [];
            foreach ($config as $row){
                $rewardId = $row['type'].date('Ymd').$uid;
                $reward[$row['type']] = $rewardId;
                oo::commonOprRedis('common')->setex(okeys::ShareRewardIdData($rewardId),json_encode([
                    'uid'     =>$uid,
                    'type'    =>$row['type'],
                ]),oo::todayDeadline());
            }
            oo::commonOprRedis('common')->setex(okeys::ShareRewardId($uid),json_encode($reward),oo::todayDeadline());
        }else{
            $reward = json_decode($reward,true);
        }

        $userShareData = [];
        foreach ($config as $row){
            $userShareData[$row['type']] = [
                'times'   => intval(oo::commonOprRedis('common')->get(okeys::userShareTimes($uid,$row['type']))),
                'rewardId'=> $reward[$row['type']],
            ];
        }
        return oo::response(1,$userShareData,'',true);
    }

    /**
     * 宝箱
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/20 10:21
     */
    public function props($param){
        $uid = oo::functions()->uint($param['uid']);
        $props = oo::commonOprModel('props')->getUserProps($uid);
        return oo::response(1,$props,'',true);
    }

    public function luckyDraw($param){
        $uid = oo::functions()->uint($param['uid']);
        $progressConfig = oo::commonOprModel('luckydraw')->getProgressConfig();
        foreach ($progressConfig as $key=>$row){
            $temp['isreward'][] = oo::commonOprModel('luckydraw')->getRewardProgress($uid,$key);
        }
        $data= [
            'luckyValue'     =>oo::commonOprModel('luckydraw')->getUserLuckyValue($uid),
            'curProgress'    =>oo::commonOprModel('luckydraw')->getUserProgress($uid),
            'refreshTime'    =>time() + oo::weekDeadline(),
            'progressReward' =>$temp,
        ];
        return oo::response(1,$data,'',true);
    }

    /**
     * 宠物配置
     * @return string
     * created by owen
     * created at 21:24
     */
    public function petConfig(){
        $data =  oo::commonOprModel('pet')->getPetConfig();
        return oo::response(1,$data,'',true);
    }


}
