<?php

define('RECALL_SECRET', 'ab*&()[cae!@+?>#5981~.,-zm');

class ModelRecall {

    /**
     * 老玩家召回应用外推送
     * @param int $uid
     */
    public function firebasePush($uid = 0){
	    if($uid){
            $reward = $this->getRecallReward($uid);
            if(empty($reward)){
                return;
            }
            oo::commonOprModel('push')->pushNews($uid, 'push.title.recall', 'push.content.recall', $reward);
        }else{
            $config = oo::commonOprModel('config')->recallConfig();
            $tb = otable::userinfo();
            $stime = time() - 16 * 24 * 3600;
            $etime = time() - 3 * 24 * 3600;
            $sql = "SELECT uid, lasttime FROM {$tb} WHERE lasttime >= {$stime} AND lasttime < $etime";
            $arr = oo::commonOprDb('userinfo')->getAll($sql, 1);
            foreach($arr as $val){
                $reward = $this->getRecallReward($val['uid'], $val['lasttime'], $config);
                if(empty($reward)){
                    continue;
                }
                oo::commonOprModel('push')->pushNews($val['uid'], 'push.title.recall', 'push.content.recall', $reward);
            }
        }
    }

    public function getRecallReward($uid, $lasttime = 0, array $config = [], $level = 0) {
        if(empty($config)){
            $config = oo::commonOprModel('config')->recallConfig();
        }
        if(empty($lasttime)){
            $info = oo::commonOprModel('member')->getUserInfo($uid);
            $lasttime = empty($info['lasttime']) ? time() : $info['lasttime'];
        }
        $money = $spins = 0;
        $time = time();
        if(empty($level)){
            $level = oo::commonOprModel('member')->getUserLevelId($uid);
        }
        foreach ($config as $row){
            if(($lasttime + $row['day']*24*60*60) > $time){
                continue;
            }
            if(!empty($row['reward']['coins'])&&!empty($row['reward']['coins']['coins'])){
                $coins = oo::commonOprModel('goods')->getGoodCoinsByLevelId($level,$row['reward']['coins']['coins']);
                $bet = empty($row['reward']['coins']['bet']) ? 1 : $row['reward']['coins']['bet'];
                $money += $coins * $bet;
            }
            if(!empty($row['reward']['spins'])){
                $spins += $row['reward']['spins'];
            }
        }
        if(empty($money) && empty($spins)){
            return [];
        }
        return [intval($money), $spins];
    }
}
