<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class BuyAction extends DownloadAction
{
    public function run()
    {
        isLogin();
        $bookId = S::gp('bid', 'G', 2);
        $book = fetchBookInfo($bookId);
        if (!$book)
        {
            throw new iSystemException('不存在的小说');
        }

        try
        {
            Members::single()->hasMoney($this->uid(), setting('bookdownload'));
        }
        catch (NotEnoughMoneyException $exce)
        {
            throw new iSystemException($exce->getMessage());
        }

        iCredits::single($this->uid(), $this->userName())->consume(setting('bookdownload'));
        $this->db()->insert(Order::table())->value(array('memberid'=>$this->uid(),'id'=>$bookId,'type'=>'book','price'=>setting('bookdownload'),'createdate'=>CDATETIME))->q();

        iforward(url('system', array('s'=>'download','bid'=>$bookId)));
    }
}