<?php

/**
 * 流失统计相关
 */
defined('IN_WEB') or die('Include Error!');
class Lose
{
    function __construct()
    {

    }

    /**
     * 返回昨日流失玩家数据
     * @param $param
     * @return string
     */
    public function index($param)
    {
        $data = array();
        $stime = oo::functions()->uint($param['stime']);
        $etime = oo::functions()->uint($param['etime']);

        $dayList = $this->getDays($stime, $etime);
        $loseList = [];
        foreach ($dayList as $day) {
            $day = date('Ymd', strtotime($day));
            $key = okeys::losestatistics($day);
            $loseList[$day] = oo::commonOprRedis('statistics')->get($key);
        }
        $data['dayList'] = $dayList;
        $data['loseList'] = $loseList;

        return json_encode($data);
    }

 /**
     * 返回7日流失玩家数据
     * @param $param
     * @return string
     */
    public function sevenDaysLose($param)
    {
        $data = array();
        $stime = oo::functions()->uint($param['stime']);
        $etime = oo::functions()->uint($param['etime']);

        $dayList = $this->getDays($stime, $etime);
        $loseList = [];
        foreach ($dayList as $day) {
            $day = date('Ymd', strtotime($day));
            $key = okeys::losestatisticsseven($day);
            $loseList[$day] = oo::commonOprRedis('statistics')->get($key);
        }
        $data['dayList'] = $dayList;
        $data['loseList'] = $loseList;

        return json_encode($data);
    }


    /**
     *	计算天数差
     */
    private function getDays($day1, $day2)
    {
        $second1 = $day1;
        $second2 = $day2;

        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        $days = ($second1 - $second2) / 86400;
        $dayList = array();
        for ($i=0; $i <= $days ; $i++) {
            $dayList[] = date('Y-m-d', $second2 + 86400*$i);
        }
        return $dayList;
    }
}