<?php
/**
 * 扫描移动ftp上的数据文件，获取移动阅读基地的书籍ID
 */
require_once dirname(dirname(__FILE__)) . '/init.php';

$fileName = $argv[1];
if (empty($fileName))
{
    die("请输入文件名\n");
}

$fopen = fopen($fileName, 'r');
while(!feof($fopen))
{
    $line = fgets($fopen);
    if (!empty($line))
    {
        $array = explode(chr(128), $line);
        $bookName = trim(iconv('gb18030', 'utf-8', $array[2]));
        $bookId = $array[1];
        echo $bookName . "\n";

        $sql = 'SELECT bookid FROM ' . Book::table().' WHERE bookname = '.S::sqlEscape($bookName);
        $books = $db->setQueryString($sql)->find();
        if (!empty($books))
        {
            foreach ($books as $book)
            {
                $db->update(Book::table())->value(array('cmbid'=>$bookId))->where(array('bookid'=>$book['bookid']))->q();
            }
        }
    }
}