<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class BscoreAction extends BcartAction
{
    public function run()
    {
        $id = S::gp('id', 'G', 2);
        $t = S::gp('t', 'G', 2);
        if (!key_exists($t, BcartSubject::$scoreMapArray))
        {
            throw new iSystemException('不存在的评分选项');
        }
        $subject = $this->db()->select()->from(BcartSubject::table())->where(array('id'=>$id))->find(true);
        if (empty($subject))
        {
            throw new iSystemException('不存在的书单内容');
        }

        $check = $this->db()->getValue('SELECT COUNT(*) FROM '.BcartScore::table().' WHERE uid='.S::sqlEscape($this->uid()).' AND bcid='.S::sqlEscape($subject['id']).' AND type = 2');
        if ($check)
        {
            throw new iSystemException('您已经给本书评过分!');
        }

        $this->db()->insert(BcartScore::table())->value(array('uid'=>$this->uid(),'type'=>1, 'bcid'=>$subject['id'],'ip'=>ONLINE_IP,'createdate'=>CDATETIME,'score'=>$t,'valid'=>u('verify') ? 1 : 0))->q();
        $this->db()->update(BcartSubject::table())->incrField(array('score'=>$t,'s'.$t=>1))->where(array('id'=>$id))->q();
        iforward(url('system', array('s'=>'bcart','a'=>'view','id'=>$subject['bcid'])));
    }
}
