<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/7
 * Time: 18:42
 */

class ModelRemain {
    public function recordData()
    {
        $day = date('Ymd', strtotime("-1 day"));
        $now = time();
        $table = otable::remainStatistics();
        $sidList = [1,2];//1安卓 2IOS
        foreach($sidList as $sid){
        	$data = $this->getRemainData($day,[$sid]);
            $sql = "INSERT INTO {$table} (date, day1Remain, day3Remain, day7Remain, day30Remain, day60Remain, day90Remain, day120Remain, day1ActRemain, day3ActRemain, day7ActRemain, day30ActRemain, day60ActRemain, day90ActRemain, day120ActRemain, createtime, sid) VALUES({$day}, {$data['day1Remain']}, {$data['day3Remain']}, {$data['day7Remain']}, {$data['day30Remain']}, {$data['day60Remain']}, {$data['day90Remain']}, {$data['day120Remain']}, {$data['day1ActRemain']}, {$data['day3ActRemain']}, {$data['day7ActRemain']}, {$data['day30ActRemain']}, {$data['day60ActRemain']}, {$data['day90ActRemain']}, {$data['day120ActRemain']}, {$now}, {$sid})";
	        $ret = oo::commonOprDb('common')->query($sql);
        }
        return true;
    }

    public function getRemainData($day, $sidList=[]){
        $days1ago = $day;
        $days2ago = date('Ymd', strtotime($day.'-1 day'));
        $days3age = date('Ymd', strtotime($day.'-3 days'));
        $days8age = date('Ymd', strtotime($day.'-7 days'));
        $days30age = date('Ymd', strtotime($day.'-30 days'));
        $days60age = date('Ymd', strtotime($day.'-60 days'));
        $days90age = date('Ymd', strtotime($day.'-90 days'));
        $days120age = date('Ymd', strtotime($day.'-120 days'));

        ////次日新增留存 比如今天29号 就计算28号的次日留存 那么新增玩家从27号算,
        $yesterdayPlayer = $this->getAllPlayer($days1ago, 2, $sidList);//登陆玩家
        $last2daysPlayer = $this->getAllPlayer($days2ago, 1, $sidList);//新增玩家
        $intersect1dayPlay = array_intersect($yesterdayPlayer, $last2daysPlayer);//交集玩家
        $tempDay1Remain = empty(count($last2daysPlayer)) ? 0 : count($intersect1dayPlay) / count($last2daysPlayer);//次日留存率
        $data['day1Remain'] = round($tempDay1Remain, 4)*100;//次日留存率
        //$data['day1Remain'][] = count($intersect1dayPlay) / count($last2daysPlayer);//次日留存率

        //3日新增留存
        $last3daysPlayer = $this->getAllPlayer($days3age, 1, $sidList);//新增玩家
        $intersect3dayPlay = array_intersect($yesterdayPlayer, $last3daysPlayer);//交集玩家
        //$data['day3Remain'][] = count($intersect3dayPlay) / count($last3daysPlayer);//3日留存率
        $tday3Remain = empty(count($last3daysPlayer)) ? 0 :  count($intersect3dayPlay) / count($last3daysPlayer);//3日留存率
        $data['day3Remain'] = round($tday3Remain, 4)*100;//3日留存率

        //7日新增留存
        $last8daysPlayer = $this->getAllPlayer($days8age, 1, $sidList);//新增玩家
        $intersect8dayPlay = array_intersect($yesterdayPlayer, $last8daysPlayer);//交集玩家
        $data['day7Remain'] = empty(count($last8daysPlayer)) ? 0 : round( count($intersect8dayPlay) / count($last8daysPlayer), 4 )*100;//3日留存率

        //30日新增留存
        $last30daysPlay = $this->getAllPlayer($days30age, 1, $sidList);//30天新增玩家
        $intersect30dayPlay = array_intersect($yesterdayPlayer, $last30daysPlay); //交集玩家
//            oo::logs()->debug3(array('$last30daysPlay' => $last30daysPlay, '$intersect30dayPlay' => $intersect30dayPlay), 'Keydata.php');
        $data['day30Remain'] = empty(count($last30daysPlay)) ? 0 : round(count($intersect30dayPlay) / count($last30daysPlay), 4) * 100;//30日留存率

        //60日新增留存
        $last60daysPlayer = $this->getAllPlayer($days60age, 1, $sidList);//新增玩家
        $intersect60dayPlay = array_intersect($yesterdayPlayer, $last60daysPlayer);//交集玩家
        $data['day60Remain'] = empty(count($last60daysPlayer)) ? 0 : round( count($intersect60dayPlay) / count($last60daysPlayer), 4 )*100;//3日留存率

        //90日新增留存
        $last90daysPlayer = $this->getAllPlayer($days90age, 1, $sidList);//新增玩家
        $intersect90dayPlay = array_intersect($yesterdayPlayer, $last90daysPlayer);//交集玩家
        $data['day90Remain'] = empty(count($last90daysPlayer)) ? 0 : round( count($intersect90dayPlay) / count($last90daysPlayer), 4 )*100;//3日留存率

        //120日新增留存
        $last120daysPlayer = $this->getAllPlayer($days120age, 1, $sidList);//新增玩家
        $intersect120dayPlay = array_intersect($yesterdayPlayer, $last120daysPlayer);//交集玩家
        $data['day120Remain'] = empty(count($last120daysPlayer)) ? 0 : round( count($intersect120dayPlay) / count($last120daysPlayer), 4 )*100;//3日留存率

        //次日活跃留存
        $yesterdayLoginPlayer = $this->getAllPlayer($days1ago, 2, $sidList);//登陆玩家
        $last2daysLoginPlayer = $this->getAllPlayer($days2ago, 2, $sidList);//登陆玩家
        $intersect1dayLoginPlay = array_intersect($yesterdayLoginPlayer, $last2daysLoginPlayer);//交集玩家
        $data['day1ActRemain'] = empty(count($last2daysLoginPlayer)) ? 0 : round( count($intersect1dayLoginPlay) / count($last2daysLoginPlayer), 4 )*100;//次日留存率

        //3日活跃留存
        $last3daysLoginPlayer = $this->getAllPlayer($days3age, 2, $sidList);//登陆玩家
        $intersect3dayLoginPlay = array_intersect($yesterdayLoginPlayer, $last3daysLoginPlayer);//交集玩家
        $data['day3ActRemain'] = empty(count($last3daysLoginPlayer)) ? 0 : round(count( $intersect3dayLoginPlay) / count($last3daysLoginPlayer), 4 )*100;//3日留存率

        //7日活跃留存
        $last8daysLoginPlayer = $this->getAllPlayer($days8age, 2, $sidList);//登陆玩家
        $intersect8dayLoginPlay = array_intersect($yesterdayLoginPlayer, $last8daysLoginPlayer);//交集玩家
        $data['day7ActRemain'] = empty(count($last8daysLoginPlayer)) ? 0 : round( count($intersect8dayLoginPlay) / count($last8daysLoginPlayer), 4 )*100;//3日留存率

        //30日活跃留存
        $last30daysLoginPlayer = $this->getAllPlayer($days30age, 2, $sidList);//近30天登录的玩家
        $intersect30daysLoginPlayer = array_intersect($yesterdayLoginPlayer, $last30daysLoginPlayer);//30天玩家与昨天玩家交集
        $data['day30ActRemain'] = empty(count($last30daysLoginPlayer)) ? 0 :  round( count($intersect30daysLoginPlayer) / count($last30daysLoginPlayer), 4) * 100; //30天留存率

        //60日活跃留存
        $last60daysLoginPlayer = $this->getAllPlayer($days60age, 2, $sidList);//近60天登录的玩家
        $intersect60daysLoginPlayer = array_intersect($yesterdayLoginPlayer, $last60daysLoginPlayer);//60天玩家与昨天玩家交集
        $data['day60ActRemain'] = empty(count($last60daysLoginPlayer)) ? 0 :  round( count($intersect60daysLoginPlayer) / count($last60daysLoginPlayer), 4) * 100; //60天留存率

        //90日活跃留存
        $last90daysLoginPlayer = $this->getAllPlayer($days90age, 2, $sidList);//近90天登录的玩家
        $intersect90daysLoginPlayer = array_intersect($yesterdayLoginPlayer, $last90daysLoginPlayer);//90天玩家与昨天玩家交集
        $data['day90ActRemain'] = empty(count($last90daysLoginPlayer)) ? 0 :  round( count($intersect90daysLoginPlayer) / count($last90daysLoginPlayer), 4) * 100; //90天留存率

        //120日活跃留存
        $last120daysLoginPlayer = $this->getAllPlayer($days120age, 2, $sidList);//近120天登录的玩家
        $intersect120daysLoginPlayer = array_intersect($yesterdayLoginPlayer, $last120daysLoginPlayer);//120天玩家与昨天玩家交集
        $data['day120ActRemain'] = empty(count($last120daysLoginPlayer)) ? 0 :  round( count($intersect120daysLoginPlayer) / count($last120daysLoginPlayer), 4) * 100; //120天留存率

        return $data;
    }


    /**
     *	计算所有玩家
     *	type 1 新注册 其他登陆
     */
    protected function getAllPlayer($date, $type, $sidList=[])
    {
        $total = oo::commonOprModel('statistics')->getAllPlayer($date, $type, $sidList);
        return $total;
    }
}