<?php
/* 
 * 更新推广积分
 * 运行时间：1 3 * * *
 */
require_once dirname(dirname(__FILE__)) . '/init.php';

ini_set('Memory', '750M');

$rewards = $db->select(array('rewardid','uid','ruid','reward','status'))->from(Reward::table())->where(array('status'=>0))->find();
if (!empty($rewards))
{
    foreach ($rewards as $reward)
    {
        $rewardid = $reward['rewardid'];
        $ruid = $reward['ruid'];
        $uid = $reward['uid'];

        if ($ruid != 1)
        {
            $member = $db->select()->from(Members::table())->where(array('uid'=>$uid, 'verify'=>1))->find(true);
            if (!empty($member) && $member['extcredits1'] >= 100)
            {
                $db->update(Reward::table())->value(array('status'=>1, 'updatedate'=>date("Y-m-d H:i:s")))->where(array('rewardid'=>$rewardid))->q();

                $subject = '[系统消息]获得推广积分';
                $content = '您推广的UID=' . $uid . '用户已经达到100分，获得' . $cfg_reward . '分奖励';
                $did = MessageData::single()->create($subject,$content);
                Message::single()->send($did, 1, 'admin', $ruid);
                
                $username = Members::single()->getNameByUid($ruid);
                $iCredits = iCredits::single($ruid,$username);
                $iCredits->addMoney((0.8 * $cfg_reward),false,false);
                $iCredits->log($ruid,$username,'Reward');
            }
        }
    }
}
