<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class iCredits
{
    private $_uid = null;

    private $_userName = null;

    private $_db = null;

    private $_moneyField = 'extcredits1';

    private $_consumeField = 'extcredits7';
    
    private function  __construct($uid, $userName)
    {
        global $db;
        
        $this->setUid($uid);
        $this->setUserName($userName);
        $this->setDb($db);
    }

    public function single($uid, $userName)
    {
        static $_self = null;
        if (is_null($_self))
        {
            $_self = new self($uid, $userName);
        }
        
        return $_self;
    }

    public function todayCredit($bookId)
    {
        $date = date('Y-m-d');
        $sql = "SELECT SUM(credit) FROM ".BookCreditsLog::table()." WHERE uid={$this->getUid()} AND `date` = '{$date}' AND bid={$bookId}";

        return $this->getDb()->getValue($sql);
    }

    public function consume($credit)
    {
        $mTable = Members::table();
        $this->log($this->getUid(), $this->getUserName(), $credit);
        $sql = "UPDATE {$mTable} 
                SET
                    {$this->getMoneyField()} = {$this->getMoneyField()} - $credit,
                    {$this->getConsumeField()} = {$this->getConsumeField()} + $credit
                WHERE
                    uid = {$this->getUid()}";
        $this->getDb()->query($sql);
    }

    public function addMoney($credit, $multiple = true, $log = true)
    {
        if ($multiple && '2010-09-15' == date('Y-m-d'))
        {
            $option = $this->getDb()->getValue("SELECT `option` FROM property_member WHERE uid={$this->getUid()} AND property_id=14");
            if (!empty($option))
            {
                $option = unserialize($option);
                if (!empty($option['expire']) && is_numeric($option['expire']) && $_SERVER['REQUEST_TIME'] < $option['expire'])
                {
                    $credit = $credit * $option['multiple'];
                }
            }
        }
        $log && $this->log($this->getUid(), $this->getUserName(), $credit);
        $this->getDb()->update(Members::table())->incrField(array($this->getMoneyField()=>$credit))->where('uid='.$this->getUid())->q();
    }

    public function getUid()
    {
        return $this->_uid;
    }

    public function getUserName()
    {
        return $this->_userName;
    }

    public function setUid($uid)
    {
        $this->_uid = $uid;
    }

    public function setUserName($userName)
    {
        $this->_userName = $userName;
    }

    public function getDb()
    {
        return $this->_db;
    }

    public function setDb($db)
    {
        $this->_db = $db;
    }

    public function getMoneyField()
    {
        return $this->_moneyField;
    }

    public function getConsumeField()
    {
        return $this->_consumeField;
    }

    public function log($uid,$username,$credit,$source = null)
    {
        $member = $this->getDb()->select(array($this->getMoneyField()))->from(Members::table())->where('uid='.$uid)->find(true);
        if (!empty($member) && is_null($source))
        {
            $self = $_SERVER['PHP_SELF'];
            $self{0} = '';
            $actionString = '';
            $params = $_GET;
            
            if (!empty($self))
            {
                if (1 < $count = (count($array = explode('/', $self))))
                {
                    $phpFile = $array[$count - 1];
                    $action = explode('.', $phpFile);
                    $action = $action[0];
                    $module = $array[$count - 2];
                    $actionString = $module;

                    switch (trim($module))
                    {
                        case 'bookforum':
                            $actionString .= '_' . (empty($params['module']) ? 'index' : trim($params['module']));
                            if (!empty($params['action']))
                            {
                                $actionString .= '_' . $params['action'];
                            }
                            break;
                        case 'property':
                            $actionString .= '_' . (empty($params['module']) ? 'index' : trim($params['module']));
                            break;
                    }
                }
                else
                {
                    $action = explode('.', $self);
                    $actionString = $action[0];

                    if (!empty($params['s']))
                    {
                        $actionString .= '_' . $params['s'];
                    }
                    if (!empty($params['c']))
                    {
                        $actionString .= '_' . $params['c'];
                    }
                    if (!empty($params['a']))
                    {
                        $actionString .= '_' . $params['a'];
                    }
                }
            }

            !is_null($source) && $actionString = $source;
            $denyActionString = array('read','bookshow','booktype','index','member','new','chapter','over','bookinfo','bookforum_index','property_index');
            if (!empty($actionString) && !in_array(trim($actionString), $denyActionString))
            {
                $data = array(
                    'uid' => $uid,
                    'username' => addslashes($username),
                    'createdate' => date('Y-m-d H:i:s'),
                    'credit1' => $member[$this->getMoneyField()],
                    'credit2' => $credit,
                    'action' => trim($actionString),
                    'date' => date('Y-m-d'),
                );
                $this->getDb()->insert(CreditLog::table())->value($data)->q();
            }
        }
    }
}