<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
require_once 'init.php';

$mTable = Members::table();
$mfTable = Memberfields::table();
$bmTable = BookManagers::table();
$beTable = BookMembers::table();
$bTable = Book::table();
//if ('06' == date("d"))
//{
    $month = date('Y-m');
    $options = array();
    
    $sql = "UPDATE {$mfTable} AS mf
            INNER JOIN {$mTable} AS m ON m.uid = mf.uid
            SET mf.bookvote = if (extcredits1 > 200, 2, 1)
            WHERE m.verify = 1";
    $db->query($sql);

    $sql = "SELECT uid,username,bookid FROM applybm WHERE FROM_UNIXTIME(createdate,'%Y-%m') = '$month' AND issuper = 0";
    $applys = $db->setQueryString($sql)->find();
    if (!empty($applys))
    {
        foreach ($applys as $apply)
        {
            $data[$apply['bookid']][] = mysql_real_escape_string($apply['username']) . '[UID:'  .$apply['uid'] . ']';
        }
        if (!empty($data))
        {
            $existBookIds = array_keys($data);

            $existBookIds[] = 13089;
            $sql = 'SELECT bookid FROM '.$bTable.' WHERE `status` = 0 AND `type` = 1 AND bookid NOT IN ('.implode(',', $existBookIds).')';
            $bookIds = $db->setQueryString($sql)->find();
            foreach ($bookIds as $id)
            {
                $sql = 'SELECT COUNT(*) AS c FROM '.$bmTable.' WHERE bookid = '.$id['bookid'].' AND status = 1';
                $count = $db->getValue($sql);
                if (0 < $count)
                {
                    $data[$id['bookid']][] = '';
                }
            }
        }

        $voteObj = BookVote::single();
        foreach ($data as $key => &$value)
        {
            $book = getBookInfo($key);
            if (empty($book))
            {
                continue;
            }
            $bookId = $book['bookid'];
            $bookName = filter_content($book['bookname']);
            $description = date("Y年m月") . '《'.$bookName.'》书会书管选举投票';
            $expire = strtotime(date('Y-m-t') . " 00:00:00") - 86400;
            $create = array(
                'subject' => $description,
                'uid' => 1,
                'username' => 'admin',
                'description' => $description,
                'createdate' => time(),
                'expire' => $expire,
                'maxchoices' => 1,
                'bookid' => $key,
                'award' => 5,
            );

            $db->insert(BookVote::table())->value($create)->q();
            $voteId = $db->getInsertId();
            if ($voteId)
            {
                $string = array();
                $oldBookManagers = $db->select(array('uid','username'))->from($bmTable)->where(array('status'=>1,'bookid'=>$bookId))->find();
                if (!empty($oldBookManagers))
                {
                    foreach ($oldBookManagers as $old)
                    {
                        $string[] = mysql_real_escape_string($old['username']) . '[UID:'.$old['uid'] . ']';
                    }
                    $value = array_merge($string, $value);
                }

                foreach ($value as $v)
                {
                    if (!empty($v))
                    {
                        $db->insert(BookVote::optionTable())->value(array('voteid'=>$voteId,'option'=>$v))->q();
                    }
                }
            }
        }
    }
//}