<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */

/**
 * データインポート
 *
 * @package     dataimport
 * @author      netnee T.Ototgawa <tomokauzu.otogawa@netnee.jp>
 * @access      public
 */
class Onedepo_Logic_Dataimport {

    var $dataimport;
    var $dbaccess;
    var $db_name;

    function doImport($data, $filenm, $filearr, $release, $images)
    {
        $where = array();
        $params = array();

        ob_flush();
        flush();

        $log =& LogFactory::getLog();

        $logfile = IMPORTDIR_LOGS ."/". $filenm;
        $logfp = @fopen($logfile, "w");

        $savefile = IMPORTDIR_SAVED ."/". $filenm;
        $svfp = @fopen($savefile, "w");

        $updateusrnm = "-imp-". substr($filenm,0, -4);
        $row = 1;
        $i = 0;
        $resultmsg = "";
        $throughmsg = array();
        $okrow = 0;

        $db = $this->dbaccess->doConnect();
        $db->autoCommit(false);
        $db->query('BEGIN;');

        foreach($data as $key => $val) {
            $msg = "";
            extract($val, EXTR_OVERWRITE);

            if (empty($target) AND !empty($itemnm) ) {

                if (empty($itemcd)) {

                    $qry = $this->makeQuery($val, $db);
                    $qry["query"] .= ",?";
                    $qry["params"][] = $release;
                    $qry["query"] .= ",?";
                    $qry["params"][] = $updateusrnm;
                    $sql  = sprintf("select * from f_import_main(%s);", $qry["query"]);
                    $params = $qry["params"];
                    $error = false;
                    $check = false;
                    $checkmsg = "";

                    //型チェック
                    if(!is_numeric($params[2]) AND !empty($params[2]) ) {
                        $resultmsg[] = $msg = 'メーカコードが不正';
                        $error = true;

                    }
                    if(!is_numeric($params[4]) AND !empty($params[4]) ) {
                        $resultmsg[] = $msg = 'レーベルコードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[6]) AND !empty($params[6]) ) {
                        $resultmsg[] = $msg = 'レーベルコードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[8]) AND !empty($params[8]) ) {
                        $resultmsg[] = $msg = '作品区分コードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[9]) AND !empty($params[9]) ) {
                        $resultmsg[] = $msg = '受審団体コードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[12]) AND !empty($params[12])) {
                        $resultmsg[] = $msg = '映倫規定コードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[14]) AND !empty($params[14])) {
                        $resultmsg[] = $msg = 'ランクが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[16]) AND !empty($params[16]) ) {
                        $resultmsg[] = $msg = 'メディアコードが不正';
                        $error = true;
                    }
                    if(!ereg("^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} *", $params[20] ) AND !empty($params[20]) ) {
                        $resultmsg[] = $msg = '発売日が不正';
                        $error = true;
                    }
                    if(!ereg("^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} *", $params[21] ) AND !empty($params[21])) {
                        $resultmsg[] = $msg = '予約終了日が不正';
                        $error = true;
                    }
                    if(!is_numeric($params[22]) AND !empty($params[22]) ) {
                        $resultmsg[] = $msg = '販売形態コードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[23]) AND !empty($params[23])) {
                        $resultmsg[] = $msg = '収録時間が不正';
                        $error = true;
                    }

                    if(!is_numeric($params[24]) AND !empty($params[24])) {
                        $resultmsg[] = $msg = 'メーカー定価が不正';
                        $error = true;
                    }

                    if(!is_numeric($params[25]) AND !empty($params[25])) {
                        $resultmsg[] = $msg = '販売価格が不正';
                        $error = true;
                    }

                    if (!is_null($params[30]) ) {
                        $genre = ltrim($params[30], "{");
                        $genre = rtrim($genre, "}");
                        $genre = explode(",", $genre);
                        foreach ($genre as $value) {
                            if(!is_numeric($value)) {
                                $resultmsg[] = $msg = 'ジャンルコードが不正';
                                $error = true;
                                break;
                            }
                        }
                    }

                    if (!is_null($params[32]) ) {
                        $cast = ltrim($params[32], "{");
                        $cast = rtrim($cast, "}");
                        $cast = explode(",", $cast);
                        foreach ($cast as $value) {
                            if(!is_numeric($value)) {
                                $resultmsg[] = $msg = 'キャストコードが不正';
                                $error = true;
                                break;
                            }
                        }
                    }

                    if (!is_null($params[34]) ) {
                        $cast = ltrim($params[34], "{");
                        $cast = rtrim($cast, "}");
                        $cast = explode(",", $cast);
                        foreach ($cast as $value) {
                            if(!is_numeric($value)) {
                                $resultmsg[] = $msg = 'キャストコード(監督)が不正';
                                $error = true;
                                break;
                            }
                        }
                    }

                    //チェックエラーで
                    if(!$error) {

                        $res = $db->getRow($sql, $params, DB_FETCHMODE_ASSOC);
                        if (DB::isError($res)) {
                            $log->info("db sintax error [userinfo] = ". $res->userinfo, "Onedepo_Logic_Dataimport#doImport");

                            $errmsg = $msg = "DBエラー";
                            //if (LOG_LEVEL <= LEVEL_DEBUG)
                                $errmsg .= " ". $res->userinfo ;
                            $res = array("errcd"=>"", "errmsg"=>$errmsg);
                            $error = true;
                            $resultmsg[] = $res["errmsg"];

                        } else if($res["errcd"] == -20) {
                            //既に登録されているため登録しません
                            $check = true;
                            $throughmsg[] = $msg = $row. "行目 「".$params[0]."」は既に登録されているため登録されませんでした";
                            $log->info("import error title=".$params[0], "Onedepo_Logic_Dataimport#doImport");
                        } else if($res["errcd"] < 0) {
                            $log->info("import error code=". $res["errcd"] . "error msg=". $res["errmsg"], "Onedepo_Logic_Dataimport#doImport");
                            $error = true;
                            $resultmsg[] = $msg = $res["errmsg"];

                        } else { //成功
                            $okrow++;
                        }
                    }

                } else {
                    //品目Cでの更新はできません
                    $check = true;
                    $throughmsg[] = $msg = $row. "行目 「品目Cでの更新はできません」";
                    $log->info("import error itemcd is set cd=".$itemcd, "Onedepo_Logic_Dataimport#doImport");
                }

            } else {
                //1行目のカラム名のとき
                $check = true;
            }

            if (!$error AND !$check) {

                $newitemcd = $res["code"];

                // 画像
                $imglarge = $this->setValueAndEnc($imglarge);
                $imgmedium = $this->setValueAndEnc($imgmedium);
                $imgsmall = $this->setValueAndEnc($imgsmall);

                // サイズ大
                if (!empty($imglarge)) {
                    $oldfile_large  = IMPORTDIR_IMAGES ."/". $imglarge;
                    // 品目Cと紐付けたファイル名でrename
                    $newfile_large  = IMAGE_WORKDIR ."/". $newitemcd ."L.jpg";

                    clearstatcache();
                    if (is_file($oldfile_large)) {

                        $sql  = "select * from f_image_update(1,1,?,0,'L',0,?,NULL);";
                        $params = array($newitemcd, $updateusrnm);

                        $res = $db->getRow($sql, $params, DB_FETCHMODE_ASSOC);
                        if (DB::isError($res) || $res["errcd"] < 0) {
                            if (DB::isError($res)) {
                                $log->info("db sintax error [userinfo] = ". $res->userinfo, "Onedepo_Logic_Dataimport#doImport");
                                if (LOG_LEVEL <= LEVEL_DEBUG) $resultmsg[] = $msg = "DBエラー ". $res->userinfo;
                            } else {
                                $log->info("import error(imglarge) code=". $res["errcd"], "Onedepo_Logic_Dataimport#doImport");
                            }
                            $error = true;
                        }

                        if (!$error) {
                            @copy($oldfile_large,  $newfile_large);
                        }
                    } else {
                        $error = true;
                        $resultmsg[] = $msg = $row. "行目 画像大が見つかりません ". $oldfile_large;
                        $log->info("file not found ". $oldfile_large, "Onedepo_Logic_Dataimport#doImport");
                    }
                }

                // サイズ中
                if (!empty($imgmedium)) {
                    $oldfile_medium = IMPORTDIR_IMAGES ."/". $imgmedium;
                    // 品目Cと紐付けたファイル名でrename
                    $newfile_medium = IMAGE_WORKDIR ."/". $newitemcd ."M.jpg";

                    clearstatcache();
                    if (is_file($oldfile_medium)) {

                        $sql  = "select * from f_image_update(1,1,?,0,'M',0,?,NULL);";
                        $params = array($newitemcd, $updateusrnm);

                        $res = $db->getRow($sql, $params, DB_FETCHMODE_ASSOC);
                        if (DB::isError($res) || $res["errcd"] < 0) {
                            if (DB::isError($res)) {
                                $log->info("db sintax error [userinfo] = ". $res->userinfo, "Onedepo_Logic_Dataimport#doImport");
                                if (LOG_LEVEL <= LEVEL_DEBUG) $resultmsg[] = $msg = "DBエラー ". $res->userinfo;
                            } else {
                                $log->info("import error(imgmedium) code=". $res["errcd"], "Onedepo_Logic_Dataimport#doImport");
                            }
                            $error = true;
                        }

                        if (!$error) {
                            @copy($oldfile_medium,  $newfile_medium);
                        }
                    } else {
                        $error = true;
                        $resultmsg[] = $msg = $row. "行目 画像中が見つかりません ". $oldfile_medium;
                        $log->info("file not found ". $oldfile_medium, "Onedepo_Logic_Dataimport#doImport");
                    }
                }

                // サイズ小
                if (!empty($imgsmall)) {
                    $oldfile_small  = IMPORTDIR_IMAGES ."/". $imgsmall;
                    // 品目Cと紐付けたファイル名でrename
                    $newfile_small  = IMAGE_WORKDIR ."/". $newitemcd ."S.jpg";

                    clearstatcache();
                    if (is_file($oldfile_small)) {

                        $sql  = "select * from f_image_update(1,1,?,0,'S',0,?,NULL);";
                        $params = array($newitemcd, $updateusrnm);

                        $res = $db->getRow($sql, $params, DB_FETCHMODE_ASSOC);
                        if (DB::isError($res) || $res["errcd"] < 0) {
                            if (DB::isError($res)) {
                                $log->info("db sintax error [userinfo] = ". $res->userinfo, "Onedepo_Logic_Dataimport#doImport");
                                if (LOG_LEVEL <= LEVEL_DEBUG) $resultmsg[] = $msg = "DBエラー ". $res->userinfo;
                            } else {
                                $log->info("import error(imgsmall) code=". $res["errcd"], "Onedepo_Logic_Dataimport#doImport");
                            }
                            $error = true;
                        }

                        if (!$error) {
                            @copy($oldfile_small,  $newfile_small);
                        }
                    } else {
                        $error = true;
                        $resultmsg[] = $msg = $row. "行目 画像小が見つかりません ". $oldfile_small;
                        $log->info("file not found ". $oldfile_small, "Onedepo_Logic_Dataimport#doImport");
                    }
                }
                //--

            }

            // ログ
            $datastr =  $row .",". mb_convert_encoding($msg,"SJIS","UTF-8") .",". date('Y/m/d H:i:s') .",";
            $datastr .= trim($filearr[$i]) ."\n";
            @fwrite($logfp, $datastr);
            // 成功したレコードのみ保存
            if (!$error AND !$check) {
                $datastr = trim($filearr[$i]) ."\n";
                @fwrite($svfp, $datastr);
            }
            if ($error) break;

            $row++;
            $i++;
        }

        fclose($logfp);
        fclose($svfp);


        if ($error) {
            $log->debug("rollback", "Onedepo_Logic_Dataimport#doImport");
            $db->query('ROLLBACK;');
            $db->rollback();

            unlink($savefile);
            $this->removeImages();
        } else {
            $log->debug("commit", "Onedepo_Logic_Dataimport#doImport");
            $db->query('COMMIT;');
            $db->commit();

            $this->moveImages();
            switch ($images) {
            case 'move':
            $this->moveImagesAll($filenm);
            break;
            case 'remove':
            $this->removeImagesAll();
            break;
            }
        }
        $this->dbaccess->doClose($db);

        if ($error) { // NG
            return array("errcd"  => 0,
                         "errrow" => $row,
                         "errmsg" => $resultmsg,
                         "throughmsg" => $throughmsg);
        } else { // OK
            return array("errcd"  => 1,
                         "allrow" => $row-1,
                         "okrow"  => $okrow,
                         "throughmsg" => $throughmsg);
        }
    }

    /**
     * クエリ生成
     *
     * @access  public
     */
    function makeQuery ($arr, $db)
    {
        $ret = array("query"=>"", "params"=>"");
        extract($arr, EXTR_OVERWRITE);

        $ret["query"]  .= "1";
        $ret["query"]  .= ",NULL";
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($itemnm, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($itemnmkana, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($makercd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($makernm, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($labelcd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($labelnm, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($seriescd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($seriesnm, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($kubuncd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($jushincd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($jushinno, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($workguide, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($eirincd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($subtitle, "var");
        $ret["query"]  .= ",NULL";   // 原題
        $ret["query"]  .= ",NULL";   // 制作年
        $ret["query"]  .= ",NULL";   // 制作国
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($rank, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($webcomment, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($mediacd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($makerno, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($jancd, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($isbncd, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($saledate, "date");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($reserveenddate, "date");
        $ret["query"]  .= ",?";

        //販売形態Cが無い時販売形態名から取得
        $ret["params"][] = $this->setSaleFormCd($sellcd, $sellnm);
        $ret["query"]  .= ",?";

        $ret["params"][] = $this->setValueAndEnc($rectime, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($makerprice, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc(IMPORT_EXHIBITCOUNT, "int"); // 在庫数
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc(IMPORT_CONDITION, "int"); // コンディション
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($sellprice, "int"); // 販売価格
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc(IMPORT_SOURYOUPRC, "int"); // 送料
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc(IMPORT_STOCKSTATE, "int"); // ステータス

        $this->makeCodeGenreOrCastQuery($ret["query"], $ret["params"], $db, $genrecd);
        $this->makeNameGenreOrCastQuery($ret["query"], $ret["params"], $db, $genrenm);
        $this->makeCodeGenreOrCastQuery($ret["query"], $ret["params"], $db, $castcd);
        $this->makeNameGenreOrCastQuery($ret["query"], $ret["params"], $db, $castnm);
        $this->makeCodeGenreOrCastQuery($ret["query"], $ret["params"], $db, $directorcd);
        $this->makeNameGenreOrCastQuery($ret["query"], $ret["params"], $db, $directornm);

        return $ret;
    }

    /**
     * クエリ生成(ジャンルC、キャストC部)
     *
     * @access  public
     */
    function makeCodeGenreOrCastQuery (&$sql, &$params, $db, $arr)
    {
        $ret = '';
        foreach ($arr as $val) {
            if($val == '')   $val = 0;

            if (empty($ret)) {
                $ret .= " " . $val;
            } else {
                $ret .= "," . $val;
            }
        }

        $sql .= ",?";
        if (!empty($ret)) {
            $params[] = '{'. $this->setValueAndEnc($ret) .'}';
        } else {
            $params[] = NULL;
        }
    }

    /**
     * クエリ生成(ジャンルnm、キャストnm部)
     *
     * @access  public
     */
    function makeNameGenreOrCastQuery (&$sql, &$params, $db, $arr)
    {
        $ret = '';
        foreach ($arr as $val) {
            if($val == '')   $val = "NULL";

            if (empty($ret)) {
                $ret .= " " . $val;
            } else {
                $ret .= "," . $val;
            }
        }

        $sql .= ",?";
        if (!empty($ret)) {
            $params[] = '{'. $this->setValueAndEnc($ret) .'}';
        } else {
            $params[] = NULL;
        }
    }

    /**
     * 販売形態Cと販売形態から販売形態C取得
     *
     * @param   $sellcd  販売形態C
     *          $sellnm  販売形態
     * @access  public
     */
    function setSaleFormCd($salecd, $selenm)
    {
        if( !empty($salecd) ) {
            $value = $this->setValueAndEnc($salecd, "int");
        } else {
            $sell = $this->db_name->selectKeyOfName(5, $this->setValueAndEnc($salecd, "var"));
            if($sell != false) {
                $value = $this->setValueAndEnc($sell['namecd'], "int");
            } else {
                $value = $this->setValueAndEnc($salecd, "var");  // error
            }
        }
        return $value;
    }

    /**
     * デフォルトの値セット＆エンコード
     * int varchar その他のみ。必要があれば追加。
     *
     * @param   $value
     *          $type  変数の型
     * @access  public
     */
    function setValueAndEnc ($value, $type="")
    {
        if (empty($value))
            return NULL;
        switch ($type)
        {
            case 'date':
                $value = str_replace("/", "-", $value);
            break;

        }
        $value = mb_convert_encoding(trim($value), "UTF-8", "sjis-win,SJIS,EUC-JP,UTF-8,ASCII,JIS");
        return $value;
    }

    function moveImages()
    {
        foreach (glob(IMAGE_WORKDIR ."/*", GLOB_NOSORT) as $filename) {
            @rename($filename, IMAGE_DIR."/1/". basename($filename));
        }
    }

    function removeImages()
    {
        foreach (glob(IMAGE_WORKDIR ."/*", GLOB_NOSORT) as $filename) {
            @unlink($filename);
        }
    }

    function moveImagesAll($file)
    {
        $svdir = IMPORTDIR_SAVED ."/". substr($file, 0, -4);
        mkdir($svdir, 0777);
        chmod($svdir, 0777);
        foreach (glob(IMPORTDIR_IMAGES ."/*", GLOB_NOSORT) as $filename) {
            @rename($filename, $svdir."/". basename($filename));
        }
    }

    function removeImagesAll()
    {
        foreach (glob(IMPORTDIR_IMAGES ."/*", GLOB_NOSORT) as $filename) {
            unlink($filename);
        }
    }


    /**
     * 今までにインポートしたupdusrnm取得。
     *
     * @access  public
     */
    function getUser($to, $from)
    {
        $params = array();
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        //$params = array($to, $from);

        $basic_sql  = "SELECT updusrnm, count(updusrnm) AS count FROM t_item_basic_attr";

        $where  = " WHERE updusrnm LIKE '-imp-%' ";
        $where .= " GROUP BY updusrnm ";
        $where .= " ORDER BY updusrnm DESC";

        $cnt_sql = "SELECT count(sub) FROM ( SELECT updusrnm FROM t_item_basic_attr " . $where . " ) as sub";
        $basic_sql .= $where . " LIMIT ". $to  ." OFFSET ". $from;

        $data = $db->getAll($basic_sql, $params);
        if (DB::isError($data)) {
            $log->debug("error ".$data->userinfo, "Onedepo_Logic_Dataimport#getUser");
            die($data->getMessage());
        }

        $cnt = $db->getOne($cnt_sql);
        if (DB::isError($cnt)) {
            $log->debug("error ".$data->userinfo, "Onedepo_Logic_Dataimport#getUser");
            die($data->getMessage());
        }
        $data += array("recordcount" => $cnt);

        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * 今までにインポートした一般商品のupdusrnm取得。
     *
     * @access  public
     */
    function getUserNormal($to, $from)
    {
        $params = array();
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        //$params = array($to, $from);

        $basic_sql  = "SELECT updusrnm, count(updusrnm) AS count FROM t_item_basic_attr";

        $where  = " WHERE updusrnm LIKE '-imp-%' ";
        $where .= " AND shopdiv = 2 ";
        $where .= " GROUP BY updusrnm ";
        $where .= " ORDER BY updusrnm DESC";

        $cnt_sql = "SELECT count(sub) FROM ( SELECT updusrnm FROM t_item_basic_attr " . $where . " ) as sub";
        $basic_sql .= $where . " LIMIT ". $to  ." OFFSET ". $from;

        $data = $db->getAll($basic_sql, $params);
        if (DB::isError($data)) {
            $log->debug("error ".$data->userinfo, "Onedepo_Logic_Dataimport#getUser");
            die($data->getMessage());
        }

        $cnt = $db->getOne($cnt_sql);
        if (DB::isError($cnt)) {
            $log->debug("error ".$data->userinfo, "Onedepo_Logic_Dataimport#getUser");
            die($data->getMessage());
        }
        $data += array("recordcount" => $cnt);

        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * updusrnmより、削除対象件数取得
     *
     * @access  public
     */
    function getDeleteCount($updusrnm)
    {
        $params = array();
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $sql  = "SELECT count(*) as deletecount FROM t_item_basic_attr";
        $sql .= " WHERE updusrnm=?";

        $data = $db->getOne($sql, array($updusrnm));
        if (DB::isError($data)) {
            $log->debug("error ".$data->userinfo, "Onedepo_Logic_Dataimport#getDeleteCount");
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * updusrnmより、削除対象商品C取得
     *
     * @access  public
     */
    function getDeleteItemcd($updusrnm)
    {
        $params = array();
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $sql  = "SELECT itemcd FROM t_item_basic_attr";
        $sql .= " WHERE updusrnm=?";

        $data = $db->getAll($sql, array($updusrnm));
        if (DB::isError($data)) {
            $log->debug("error ".$data->userinfo, "Onedepo_Logic_Dataimport#getDeleteItemcd");
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * 今までにインポートしたデータ
     * itemcdで指定したものを削除。
     * @access  public
     */
    function delImport($itemcd)
    {
         $log =& LogFactory::getLog();
        $func_name = "Onedepo_Logic_Dataimport#delImport";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($itemcd);

        $sql  = "SELECT * FROM f_import_delete_main(?);";

        $res = $db->getRow($sql, $params);

        if (DB::isError($res) || $res[0] < 0) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);

        $db->commit();
        $this->dbaccess->doClose($db);
        return true;
    }


    /**
     * キャストデータインポート
     *
     * @access  public
     */
    function doCastImport($data, $filenm, $filearr, $shopdiv, $images)
    {
        $where = array();
        $params = array();

        ob_flush();
        flush();

        $log =& LogFactory::getLog();

        $logfile = IMPORTDIR_LOGS ."/". $filenm;
        $logfp = @fopen($logfile, "w");

        $savefile = IMPORTDIR_SAVED ."/". $filenm;
        $svfp = @fopen($savefile, "w");

        $updateusrnm = "-imp-". substr($filenm,0, -4);
        $row = 1;

        $resultmsg = "";
        $throughmsg = array();
        $okrow = 0;
        $error = false;
        $sql = "SELECT * FROM f_import_cast(NULL,?,?,?,?,?,?,?,?,?,?,?,?)";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit(false);
        $db->query('BEGIN;');

        //文字コード変更
        $data = $this->setCsvArrayEnc($data);

        //前処理 簡易チェック
        $check = $this->checkCastCsvArray($data);
        if($check['errcd']==0)  return $check;     // エラー情報返却

        //print_r($data);

        //データインポート
        foreach($data as $key => $rec) {
            // 追加
            if( !empty($rec['target']) ) {
                // カラム名の行

            } else if( !empty($rec['castcd']) ) {
                //キャストコード
                $error = true;
                $throughmsg[] = $row. "行目 にキャストコードがあるため登録されませんでした。";
            } else {

                if($rec['recommenddiv']==1 OR $rec['recommenddiv']=='はい') {
                    $rec['recommenddiv'] = 1;
                } else {
                    $rec['recommenddiv'] = 2;
                }


                $params = array($rec['castnm'],
                                $rec['castnmkana'],
                                $rec['castdevcd'],
                                $rec['castdevnm'],
                                $rec['recommenddiv'],
                                $rec['comment'],
                                $rec['debutdate'],
                                $rec['birthplacecd'],
                                $rec['birthplacenm'],
                                $rec['birthdate'],
                                $shopdiv, // 区分
                                $updateusrnm);

                $res = $db->getRow($sql, $params, DB_FETCHMODE_ASSOC);

                if (DB::isError($res)) {
                    $log->info("db sintax error [userinfo] = ". $res->userinfo, "Onedepo_Logic_Dataimport#doImport");

                    $errmsg = $msg = "DBエラー";
                    if (LOG_LEVEL <= LEVEL_DEBUG) $errmsg .= " ". $res->userinfo ;
                    $res = array("errcd"=>"", "errmsg"=>$errmsg);
                    $error = true;
                    $resultmsg[] = $res["errmsg"];

                } else if($res["errcd"] == -20) {
                    //既に登録されているため登録しません
                    $throughmsg[] = $msg = $row. "行目 「".$rec['castnm']."」は既に登録されているため登録されませんでした";
                    $log->info("import error title=".$rec['castnm'], "Onedepo_Logic_Dataimport#doImport");
                } else if($res["errcd"] < 0) {
                    // SPエラー
                    $log->info("import error code=". $res["errcd"] . "error msg=". $res["errmsg"], "Onedepo_Logic_Dataimport#doImport");
                    $error = true;
                    $resultmsg[] = $msg = $res["errmsg"];

                } else { //成功
                    $okrow++;
                    $log->debug("commit", "Onedepo_Logic_Dataimport#doImport");
                    $db->query('COMMIT;');
                    $db->commit();
                }

            }
            // ログ
            $datastr =  $row .",". mb_convert_encoding($msg,"SJIS","UTF-8") .",". date('Y/m/d H:i:s') .",";
            $datastr .= trim($filearr[$row-1]) ."\n";
            @fwrite($logfp, $datastr);
            // 成功したレコードのみ保存
            if (!$error AND !$check) {
                $datastr = trim($filearr[$row-1]) ."\n";
                @fwrite($svfp, $datastr);
            }

            if ($error) break;

            $row++;
        }

        fclose($logfp);
        fclose($svfp);

        if ($error) { // NG
            $log->debug("rollback", "Onedepo_Logic_Dataimport#doImport");
            $db->query('ROLLBACK;');
            $db->rollback();

            $this->dbaccess->doClose($db);
/*
            unlink($savefile);
            $this->removeImages();
*/
            return array("errcd"  => 0,
                         "errrow" => $row,
                         "errmsg" => $resultmsg,
                         "throughmsg" => $throughmsg);
        } else { // OK
            $log->debug("commit", "Onedepo_Logic_Dataimport#doImport");
            $db->query('COMMIT;');
            $db->commit();

            $this->dbaccess->doClose($db);

/* 画像削除
            $this->moveImages();
            switch ($images) {
                case 'move':
                    $this->moveImagesAll($filenm);
                    break;
                case 'remove':
                    $this->removeImagesAll();
                    break;
            }
*/
            return array("errcd"  => 1,
                         "allrow" => $row-1,
                         "okrow"  => $okrow,
                         "throughmsg" => $throughmsg);
        }
    }


// -





    // csv配列にの文字コードをUTF-8変更
    function setCsvArrayEnc($data)
    {
        foreach($data as &$val) {
            foreach($val as $key => $column) {
                if(is_array($column) ) {
                    foreach($column as $key2 => $col) {
                        $val[$key][$key2] = mb_convert_encoding(trim($col), "UTF-8", "sjis-win,SJIS,EUC-JP,UTF-8,ASCII,JIS");;
                        if( empty($col) ) $val[$key][$key2] = NULL;
                    }
                } else {
                    $val[$key] = mb_convert_encoding(trim($column), "UTF-8", "sjis-win,SJIS,EUC-JP,UTF-8,ASCII,JIS");
                    if( empty($column) ) $val[$key] = NULL;
                }
            }
        }
        return $data;
    }

    // キャストcsv配列に不正がないかチェック
    function checkCastCsvArray($data)
    {
        $errflg = 1;
        $row = 1; //実行行

        foreach($data as $rec) {
            //型チェック

            //説明行はチェックしないように
            if( empty($rec['target']) ) {
                if(!is_numeric($rec['castdevcd']) AND !empty($rec['castdevcd']) ) {
                    $resultmsg[] = 'キャスト種別Cが不正';
                    $errflg = 0;
                }

                if(!is_numeric($rec['birthplacecd']) AND !empty($rec['birthplacecd']) ) {
                    $resultmsg[] = '出身Cが不正';
                    $errflg = 0;
                }

                if(!is_numeric($rec['debutdate']) AND !empty($rec['debutdate']) ) {
                    $resultmsg[] = 'デビュー年月が不正';
                    $errflg = 0;
                }

                if(!ereg("^[0-9]{4}/[0-9]{1,2}/[0-9]{1,2} *", $rec['birthdate'] ) AND !empty($rec['birthdate'])) {
                    $resultmsg[] = '生年月日が不正';
                    $errflg = 0;
                }

                if($errflg == 0) {
                   return array("errcd"  => 0,
                             "errrow" => $row,
                             "errmsg" => $resultmsg,
                             "throughmsg" => '');
                }
            }
            $row++;
        }

        return array("errcd"  => 1);
    }

    function doNormalImport($data, $filenm, $filearr, $release, $images)
    {
        $where = array();
        $params = array();

        ob_flush();
        flush();

        $log =& LogFactory::getLog();

        $logfile = IMPORTDIR_LOGS ."/". $filenm;
        $logfp = @fopen($logfile, "w");

        $savefile = IMPORTDIR_SAVED ."/". $filenm;
        $svfp = @fopen($savefile, "w");

        $updateusrnm = "-imp-". substr($filenm,0, -4);
        $row = 1;
        $i = 0;
        $resultmsg = "";
        $throughmsg = array();
        $okrow = 0;

        $db = $this->dbaccess->doConnect();
        $db->autoCommit(false);
        $db->query('BEGIN;');

        foreach($data as $key => $val) {
            $msg = "";
            extract($val, EXTR_OVERWRITE);
            if (empty($target) AND !empty($itemnm) ) {

                if (empty($itemcd)) {

                    $qry = $this->makeNormalQuery($val, $db);
                    $qry["query"] .= ",?";
                    $qry["params"][] = $release;
                    $qry["query"] .= ",?";
                    $qry["params"][] = $updateusrnm;
                    $sql  = sprintf("select * from f_import_main(%s);", $qry["query"]);
                    $params = $qry["params"];
                    $error = false;
                    $check = false;
                    $checkmsg = "";

                    //型チェック
                    if(!is_numeric($params[2]) AND !empty($params[2]) ) {
                        $resultmsg[] = $msg = 'メーカコードが不正';
                        $error = true;
                    }

                    if(!is_numeric($params[4]) AND !empty($params[4]) ) {
                        $resultmsg[] = $msg = 'シリーズコードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[6]) AND !empty($params[6]) ) {
                        $resultmsg[] = $msg = '作品区分コードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[8]) AND !empty($params[8]) ) {
                        $resultmsg[] = $msg = '受審団体コードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[10]) AND !empty($params[10])) {
                        $resultmsg[] = $msg = '映倫規定コードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[15]) AND !empty($params[15])) {
                        $resultmsg[] = $msg = 'ランクが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[17]) AND !empty($params[17]) ) {
                        $resultmsg[] = $msg = 'メディアコードが不正';
                        $error = true;
                    }
                    if(!ereg("^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} *", $params[21] ) AND !empty($params[21]) ) {
                        $resultmsg[] = $msg = '発売日が不正';
                        $error = true;
                    }
                    if(!ereg("^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} *", $params[22] ) AND !empty($params[22])) {
                        $resultmsg[] = $msg = '予約終了日が不正';
                        $error = true;
                    }
                    if(!is_numeric($params[23]) AND !empty($params[23]) ) {
                        $resultmsg[] = $msg = '販売形態コードが不正';
                        $error = true;
                    }
                    if(!is_numeric($params[24]) AND !empty($params[24])) {
                        $resultmsg[] = $msg = '収録時間が不正';
                        $error = true;
                    }

                    if(!is_numeric($params[24]) AND !empty($params[24])) {
                        $resultmsg[] = $msg = 'メーカー定価が不正';
                        $error = true;
                    }

                    if(!is_numeric($params[25]) AND !empty($params[25])) {
                        $resultmsg[] = $msg = '販売価格が不正';
                        $error = true;
                    }

                    if (!is_null($params[31]) ) {
                        $genre = ltrim($params[31], "{");
                        $genre = rtrim($genre, "}");
                        $genre = explode(",", $genre);
                        foreach ($genre as $value) {
                            if(!is_numeric($value)) {
                                $resultmsg[] = $msg = 'ジャンルコードが不正';
                                $error = true;
                                break;
                            }
                        }
                    }

                    if (!is_null($params[33]) ) {
                        $cast = ltrim($params[33], "{");
                        $cast = rtrim($cast, "}");
                        $cast = explode(",", $cast);
                        foreach ($cast as $value) {
                            if(!is_numeric($value)) {
                                $resultmsg[] = $msg = 'キャストコードが不正';
                                $error = true;
                                break;
                            }
                        }
                    }

                    if (!is_null($params[35]) ) {
                        $cast = ltrim($params[35], "{");
                        $cast = rtrim($cast, "}");
                        $cast = explode(",", $cast);
                        foreach ($cast as $value) {
                            if(!is_numeric($value)) {
                                $resultmsg[] = $msg = 'キャストコード(監督)が不正';
                                $error = true;
                                break;
                            }
                        }
                    }

                    //チェックエラーで
                    if(!$error) {

                        $res = $db->getRow($sql, $params, DB_FETCHMODE_ASSOC);
                        if (DB::isError($res)) {
                            $log->info("db sintax error [userinfo] = ". $res->userinfo, "Onedepo_Logic_Dataimport#doImport");

                            $errmsg = $msg = "DBエラー";
                            if (LOG_LEVEL <= LEVEL_DEBUG) $errmsg .= " ". $res->userinfo ;
                            $res = array("errcd"=>"", "errmsg"=>$errmsg);
                            $error = true;
                            $resultmsg[] = $res["errmsg"];

                        } else if($res["errcd"] == -20) {
                            //既に登録されているため登録しません
                            $check = true;
                            $throughmsg[] = $msg = $row. "行目 「".$params[0]."」は既に登録されているため登録されませんでした";
                            $log->info("import error title=".$params[0], "Onedepo_Logic_Dataimport#doImport");
                        } else if($res["errcd"] < 0) {
                            $log->info("import error code=". $res["errcd"] . "error msg=". $res["errmsg"], "Onedepo_Logic_Dataimport#doImport");
                            $error = true;
                            $resultmsg[] = $msg = $res["errmsg"];

                        } else { //成功
                            $okrow++;
                        }
                    }

                } else {
                    //品目Cでの更新はできません
                    $check = true;
                    $throughmsg[] = $msg = $row. "行目 「品目Cでの更新はできません」";
                    $log->info("import error itemcd is set cd=".$itemcd, "Onedepo_Logic_Dataimport#doImport");
                }

            } else {
                //1行目のカラム名のとき
                $check = true;
            }

            if (!$error AND !$check) {

                $newitemcd = $res["code"];

                // 画像
                $imglarge = $this->setValueAndEnc($imglarge);
                $imgmedium = $this->setValueAndEnc($imgmedium);
                $imgsmall = $this->setValueAndEnc($imgsmall);

                // サイズ大
                if (!empty($imglarge)) {
                    $oldfile_large  = IMPORTDIR_IMAGES ."/". $imglarge;
                    // 品目Cと紐付けたファイル名でrename
                    $newfile_large  = IMAGE_WORKDIR ."/". $newitemcd ."L.jpg";

                    clearstatcache();
                    if (is_file($oldfile_large)) {

                        $sql  = "select * from f_image_update(1,1,?,0,'L',0,?,NULL);";
                        $params = array($newitemcd, $updateusrnm);

                        $res = $db->getRow($sql, $params, DB_FETCHMODE_ASSOC);
                        if (DB::isError($res) || $res["errcd"] < 0) {
                            if (DB::isError($res)) {
                                $log->info("db sintax error [userinfo] = ". $res->userinfo, "Onedepo_Logic_Dataimport#doImport");
                                if (LOG_LEVEL <= LEVEL_DEBUG) $resultmsg[] = $msg = "DBエラー ". $res->userinfo;
                            } else {
                                $log->info("import error(imglarge) code=". $res["errcd"], "Onedepo_Logic_Dataimport#doImport");
                            }
                            $error = true;
                        }

                        if (!$error) {
                            @copy($oldfile_large,  $newfile_large);
                        }
                    } else {
                        $error = true;
                        $resultmsg[] = $msg = $row. "行目 画像大が見つかりません ". $oldfile_large;
                        $log->info("file not found ". $oldfile_large, "Onedepo_Logic_Dataimport#doImport");
                    }
                }

                // サイズ中
                if (!empty($imgmedium)) {
                    $oldfile_medium = IMPORTDIR_IMAGES ."/". $imgmedium;
                    // 品目Cと紐付けたファイル名でrename
                    $newfile_medium = IMAGE_WORKDIR ."/". $newitemcd ."M.jpg";

                    clearstatcache();
                    if (is_file($oldfile_medium)) {

                        $sql  = "select * from f_image_update(1,1,?,0,'M',0,?,NULL);";
                        $params = array($newitemcd, $updateusrnm);

                        $res = $db->getRow($sql, $params, DB_FETCHMODE_ASSOC);
                        if (DB::isError($res) || $res["errcd"] < 0) {
                            if (DB::isError($res)) {
                                $log->info("db sintax error [userinfo] = ". $res->userinfo, "Onedepo_Logic_Dataimport#doImport");
                                if (LOG_LEVEL <= LEVEL_DEBUG) $resultmsg[] = $msg = "DBエラー ". $res->userinfo;
                            } else {
                                $log->info("import error(imgmedium) code=". $res["errcd"], "Onedepo_Logic_Dataimport#doImport");
                            }
                            $error = true;
                        }

                        if (!$error) {
                            @copy($oldfile_medium,  $newfile_medium);
                        }
                    } else {
                        $error = true;
                        $resultmsg[] = $msg = $row. "行目 画像中が見つかりません ". $oldfile_medium;
                        $log->info("file not found ". $oldfile_medium, "Onedepo_Logic_Dataimport#doImport");
                    }
                }

                // サイズ小
                if (!empty($imgsmall)) {
                    $oldfile_small  = IMPORTDIR_IMAGES ."/". $imgsmall;
                    // 品目Cと紐付けたファイル名でrename
                    $newfile_small  = IMAGE_WORKDIR ."/". $newitemcd ."S.jpg";

                    clearstatcache();
                    if (is_file($oldfile_small)) {

                        $sql  = "select * from f_image_update(1,1,?,0,'S',0,?,NULL);";
                        $params = array($newitemcd, $updateusrnm);

                        $res = $db->getRow($sql, $params, DB_FETCHMODE_ASSOC);
                        if (DB::isError($res) || $res["errcd"] < 0) {
                            if (DB::isError($res)) {
                                $log->info("db sintax error [userinfo] = ". $res->userinfo, "Onedepo_Logic_Dataimport#doImport");
                                if (LOG_LEVEL <= LEVEL_DEBUG) $resultmsg[] = $msg = "DBエラー ". $res->userinfo;
                            } else {
                                $log->info("import error(imgsmall) code=". $res["errcd"], "Onedepo_Logic_Dataimport#doImport");
                            }
                            $error = true;
                        }

                        if (!$error) {
                            @copy($oldfile_small,  $newfile_small);
                        }
                    } else {
                        $error = true;
                        $resultmsg[] = $msg = $row. "行目 画像小が見つかりません ". $oldfile_small;
                        $log->info("file not found ". $oldfile_small, "Onedepo_Logic_Dataimport#doImport");
                    }
                }
                //--

            }

            // ログ
            $datastr =  $row .",". mb_convert_encoding($msg,"SJIS","UTF-8") .",". date('Y/m/d H:i:s') .",";
            $datastr .= trim($filearr[$i]) ."\n";
            @fwrite($logfp, $datastr);
            // 成功したレコードのみ保存
            if (!$error AND !$check) {
                $datastr = trim($filearr[$i]) ."\n";
                @fwrite($svfp, $datastr);
            }
            if ($error) break;

            $row++;
            $i++;
        }

        fclose($logfp);
        fclose($svfp);


        if ($error) {
            $log->debug("rollback", "Onedepo_Logic_Dataimport#doImport");
            $db->query('ROLLBACK;');
            $db->rollback();

            unlink($savefile);
            $this->removeImages();
        } else {
            $log->debug("commit", "Onedepo_Logic_Dataimport#doImport");
            $db->query('COMMIT;');
            $db->commit();

            $this->moveImages();
            switch ($images) {
            case 'move':
            $this->moveImagesAll($filenm);
            break;
            case 'remove':
            $this->removeImagesAll();
            break;
            }
        }
        $this->dbaccess->doClose($db);

        if ($error) { // NG
            return array("errcd"  => 0,
                         "errrow" => $row,
                         "errmsg" => $resultmsg,
                         "throughmsg" => $throughmsg);
        } else { // OK
            return array("errcd"  => 1,
                         "allrow" => $row-1,
                         "okrow"  => $okrow,
                         "throughmsg" => $throughmsg);
        }
    }

        /**
     * クエリ生成
     *
     * @access  public
     */
    function makeNormalQuery ($arr, $db)
    {
        $ret = array("query"=>"", "params"=>"");
        extract($arr, EXTR_OVERWRITE);

        $ret["query"]  .= "2";
        $ret["query"]  .= ",NULL";
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($itemnm, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($itemnmkana, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($makercd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($makernm, "var");
        $ret["query"]  .= ",NULL";
        $ret["query"]  .= ",NULL";
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($seriescd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($seriesnm, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($kubuncd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($jushincd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($jushinno, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($workguide, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($eirincd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($subtitle, "var");
        $ret["query"]  .= ",?";   // 原題
        $ret["params"][] = $this->setValueAndEnc($originaltitle, "var");
        $ret["query"]  .= ",?";   // 制作年
        $ret["params"][] = $this->setValueAndEnc($productyear, "var");
        $ret["query"]  .= ",?";   // 制作国
        $ret["params"][] = $this->setValueAndEnc($productcountry, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($rank, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($webcomment, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($mediacd, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($makerno, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($jancd, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($isbncd, "var");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($saledate, "date");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($reserveenddate, "date");
        $ret["query"]  .= ",?";

        //販売形態Cが無い時販売形態名から取得
        $ret["params"][] = $this->setSaleFormCd($sellcd, $sellnm);
        $ret["query"]  .= ",?";

        $ret["params"][] = $this->setValueAndEnc($rectime, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($makerprice, "int");
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc(IMPORT_EXHIBITCOUNT, "int"); // 在庫数
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc(IMPORT_CONDITION, "int"); // コンディション
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc($sellprice, "int"); // 販売価格
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc(IMPORT_SOURYOUPRC, "int"); // 送料
        $ret["query"]  .= ",?";
        $ret["params"][] = $this->setValueAndEnc(IMPORT_STOCKSTATE, "int"); // ステータス

        $this->makeCodeGenreOrCastQuery($ret["query"], $ret["params"], $db, $genrecd);
        $this->makeNameGenreOrCastQuery($ret["query"], $ret["params"], $db, $genrenm);
        $this->makeCodeGenreOrCastQuery($ret["query"], $ret["params"], $db, $castcd);
        $this->makeNameGenreOrCastQuery($ret["query"], $ret["params"], $db, $castnm);
        $this->makeCodeGenreOrCastQuery($ret["query"], $ret["params"], $db, $directorcd);
        $this->makeNameGenreOrCastQuery($ret["query"], $ret["params"], $db, $directornm);

        return $ret;
    }
}
?>