<?php
/**
 * 流失相关
 */

defined('IN_WEB') or die('Include Error!');
class ModelLose
{
    private   $moneyLevel = [
             [0, 1000],[1001, 40000], [40001,150000],[150001, 600000],[600001, 2000000],[2000001, 10000000],[10000001, 100000000]
    ];
    /**
     * 获取昨天流失玩家统计
     * @return bool
     */
    public function getYesterdayLoseUser()
    {
        set_time_limit(0);
        $key = okeys::robotUidList();
        $lastDay = date('Ymd', strtotime('-1 day'));
        $last2Day = date('Ymd', strtotime('-2 days'));
        $stime = strtotime("-2 days 00:00:00");
        $etime = strtotime("-2 days 23:59:59");
        $lastDayPlayer = $this->getAllPlayer($lastDay, 2);
        $last2DayPlayer = $this->getAllPlayer($last2Day, 2);
        $lostPlayer = array_diff($last2DayPlayer, $lastDayPlayer);//前天在玩，昨天不在。则为流失玩家
        $lostList = [];//昨天的玩家
        if(!empty($lostPlayer)) {
            foreach ($lostPlayer as $uid) {
                //判断是否是机器人
                if ($isRobot = oo::commonOprRedis('robot')->sContains($key, $uid)) {
                    continue;
                }
                $info = oo::commonOprModel('member')->getUserInfo($uid);
                if (empty($info)) {
                    continue;
                }
                $isVisitor = oo::commonOprModel('member')->isVistor($uid);
                $account = ($isVisitor == 1) ? 'V' : 'F';
                $divice = in_array($info['login_plat'], array(1, 11)) ? 'A' : 'I';
                $userInfo = oo::commonOprModel('member')->getUserLevel($uid);
                $level = $userInfo['levelId'];
                $checkBought = oo::commonOprModel('payment')->checkBought($uid);
                $bought = $checkBought ? 'pay' : 'notpay';//付费  非付费
                $lostList['level'][$level] += 1;
                $lostList['bought'][$bought] += 1;
                $lostList['deviceAccount'][$divice.$account] += 1;
                $lostList['total'] += 1;
            }

            //统计两天前总人数、付费人数、LIAFB、LIAV、LIIFB、LIIV
            $loginPlayer = otable::sta_lp();
            $sql = "SELECT * FROM {$loginPlayer} WHERE ordertime >= {$stime} AND ordertime <= {$etime} AND sid=999 LIMIT 1";
            $ret = oo::commonOprDb('common')->getOne($sql, 1);
            $lostList['dau'] = $ret['total'] ?? 0;
            $lostList['login_af'] = $ret['login_af'] ?? 0;
            $lostList['login_av'] = $ret['login_av'] ?? 0;
            $lostList['login_if'] = $ret['login_if'] ?? 0;
            $lostList['login_iv'] = $ret['login_iv'] ?? 0;

            $slTable = otable::summarylist();
            $sql2 = "SELECT DISTINCT sl_uid FROM {$slTable} WHERE sl_status = 2 ";
            $ret2 = oo::commonOprDb('common')->getAll($sql2, 1);
            $allPayUid = [];
            foreach ($ret2 as $value) {
                array_push($allPayUid, $value['sl_uid']);
            }
            $payNum = array_intersect($last2DayPlayer, $allPayUid);//前天登录的充值玩家
            $lostList['payNum'] = count($payNum);

        }
        $key = okeys::losestatistics($lastDay);
        oo::commonOprRedis('statistics')->set($key, serialize($lostList));
        oo::commonOprRedis('statistics')->expireAt($key, strtotime('+7 days 23:59:59'));
        return true;
    }
 /**
     * 获取7天流失玩家统计
     * @return bool
     */
    public function getSevendayLoseUser()
    {
        set_time_limit(0);
    //机器人id
        $key = okeys::robotUidList();
        $lastDay = date('Ymd', strtotime('-1 day'));
        $last2Day = date('Ymd', strtotime('-7 days'));
        $stime = strtotime("-7 days 00:00:00");
        $etime = strtotime("-7 days 23:59:59");
        $moneyLevel = $this->moneyLevel;

        $lastDayPlayer = $this->getAllPlayer($lastDay, 2);
        $last2DayPlayer = $this->getAllPlayer($last2Day, 2);
        $lostPlayer = array_diff($last2DayPlayer, $lastDayPlayer);//前天在玩，昨天不在。则为流失玩家
        $lostList = [];//昨天的玩家
        if(!empty($lostPlayer)) {
            foreach ($lostPlayer as $uid) {
                //判断是否是机器人
                if ($isRobot = oo::commonOprRedis('robot')->sContains($key, $uid)) {
                    continue;
                }

                $props = oo::commonOprModel('member')->getUserAssetsInfo($uid);
                $money = $props['money'];

                if ($money == 0) {//记录到机器人中
                    continue;
                }

                $info = oo::commonOprModel('member')->getUserInfo($uid);
                if (empty($info)) {
                    continue;
                }

                //记录
//                $version = $info['version'];

                $isVisitor = oo::commonOprModel('member')->isVistor($uid);
                $account = ($isVisitor == 1) ? 'V' : 'F';
                $divice = in_array($info['login_plat'], array(1, 11)) ? 'A' : 'I';
                $userInfo = oo::commonOprModel('member')->getUserLevel($uid);
                $level = $userInfo['levelId'];
                //        $total = $wintimes + $losetimes;

                $checkBought = oo::commonOprModel('payment')->checkBought($uid);
                $bought = $checkBought ? 'pay' : 'notpay';//付费  非付费
                $lostList['level'][$level] += 1;
//                $lostList['version'][$version] += 1;
                $lostList['bought'][$bought] += 1;
                $lostList['deviceAccount'][$divice.$account] += 1;
//                $lostList['device'][$divice] += 1;
                $lostList['total'] += 1;
                //判断筹码的区间
                $maxLength = count($moneyLevel);
                foreach ($moneyLevel as $k => $moneyBetween) {
                    $moneyBegin = $moneyBetween[0];
                    $moneyEnd = $moneyBetween[1];
                    if ($money >= $moneyBegin && $money <= $moneyEnd) {
                        $lostList['money'][$moneyBegin] += 1;
                        break;
                    } elseif ($k == $maxLength - 1) {
                        $lostList['money'][$moneyEnd] += 1;
                    }
                }
//                oo::logs()->debug3("uid:".$uid, 'Lose.php');
            }

            //统计七天前总人数、付费人数、LIAFB、LIAV、LIIFB、LIIV
            $loginPlayer = otable::sta_lp();
            $sql = "SELECT * FROM {$loginPlayer} WHERE ordertime >= {$stime} AND ordertime <= {$etime} AND sid=999 LIMIT 1";
            $ret = oo::commonOprDb('common')->getOne($sql, 1);
            $lostList['dau'] = $ret['total'] ?? 0;
            $lostList['login_af'] = $ret['login_af'] ?? 0;
            $lostList['login_av'] = $ret['login_av'] ?? 0;
            $lostList['login_if'] = $ret['login_if'] ?? 0;
            $lostList['login_iv'] = $ret['login_iv'] ?? 0;

            $slTable = otable::summarylist();
            $sql2 = "SELECT DISTINCT sl_uid FROM {$slTable} WHERE sl_status = 2 ";
            $ret2 = oo::commonOprDb('common')->getAll($sql2, 1);
            $allPayUid = [];
            foreach ($ret2 as $value) {
                array_push($allPayUid, $value['sl_uid']);
            }
            $payNum = array_intersect($last2DayPlayer, $allPayUid);//前天登录的充值玩家
            $lostList['payNum'] = count($payNum);

        }
        $key = okeys::losestatisticsseven($lastDay);
        oo::commonOprRedis('statistics')->set($key, serialize($lostList));
        oo::commonOprRedis('statistics')->expireAt($key, strtotime('+7 days 23:59:59'));
        return true;
    }


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

}