--DROP FUNCTION f_import_main(IN _shopdiv integer, IN _itemcd integer, IN _itemnm varchar, IN _itemnmkana varchar, IN _makercd integer, IN _makernm varchar, IN _labelcd integer, IN _labelnm varchar, IN _seriescd integer, IN _seriesnm varchar, IN _itemdivcd integer, IN _examgroupcd integer, IN _examno varchar, IN _iteminfo text, IN _eirinrating integer, IN _subtitle varchar, IN _OriginalTitle varchar, IN _ProductYear varchar, IN _ProductCountry varchar, IN _rankingrate integer, IN _comment varchar, IN _mediacd integer, IN _makeritemcd varchar, IN _jancd varchar, IN _isbncd varchar, IN _salestartdate date, IN _orderclosedate date, IN _saleformcd integer, IN _rectime integer, IN _makerprice integer, IN _exhibitcount integer, IN _condition integer, IN _sellingprice integer, IN _souryouprc integer, IN _stockstate integer,IN _genrecd integer[], IN _genrenm varchar[], IN _castcd integer[], IN _castnm varchar[], IN directorcode integer[], IN directorname varchar[], IN release integer, IN usr varchar, OUT errcd integer, OUT errmsg varchar, OUT code integer);


CREATE OR REPLACE FUNCTION f_import_item_main(
    IN _itemcd integer,     -- 品目C
    
    -- 基本
    IN _itemnm varchar,     -- 品名
    IN _itemnmkana varchar, -- 品名(かな)
    IN _itemexp text,       -- 作品案内
    
    -- web
    IN _release date,         -- 公開日
    IN _web_comment text,      -- コメント
    
    -- 構成
    IN _variationnm varchar,    -- バリエーション
    IN _makeritemcd varchar,    -- 品番
    IN _jancd varchar,          -- JANコード
    IN _salestartdate date,     -- 発売日
    IN _orderclosedate date,    -- 予約終了日
    IN _struct_comment text,    -- コメント
    
    IN _genrecd integer[],      -- ジャンルC
    IN _genrenm varchar[],      -- ジャンル名
    
    IN usr varchar,             -- 更新ユーザ

    OUT errcd integer,
    OUT errmsg varchar,
    OUT code integer) AS
$BODY$

DECLARE
    sql varchar;
    rec_item RECORD;
    rec_genre RECORD;
    i integer := 1;
    itemc  integer;
BEGIN
    
    IF _itemnm LIKE '' THEN
        errcd := -20;
        errmsg := 'タイトルは必須です。';
        RETURN;
    END IF;
    
    -- 重複チェック
    SELECT itemcd INTO itemc FROM t_item_basic_attr WHERE delflg = false AND s_get_nocase_text(itemnm) LIKE s_get_nocase_text(_itemnm);
    IF FOUND THEN
        errcd := -20;
        errmsg := '同じタイトルが既に登録されています。';
        RETURN;
    END IF;

    ---- 更新処理
    
    -- 基本
    INSERT INTO t_item_basic_attr(itemnm, itemnmkana, itemexp, updusrnm) 
        VALUES(_itemnm, _itemnmkana, _itemexp, usr);
    
    itemc := lastval();
    
    -- 構成
    INSERT INTO t_item_struct(itemcd, variationnm, makeritemcd, jancd, salestartdate, orderclosedate, comment, updusrnm) 
        VALUES(itemc, _variationnm, _makeritemcd, _jancd, _salestartdate, _orderclosedate, _struct_comment, usr);
    
    -- WEB
    INSERT INTO t_item_web_attr(itemcd, releasedate, comment, updusrnm) 
        VALUES(itemc, _release, _web_comment,usr);

    -- ジャンル
    IF _genrecd IS NOT NULL OR _genrenm IS NOT NULL THEN
        SELECT * FROM f_import_genre_item(
            itemc,
            _genrecd,
            _genrenm,
            usr
        ) INTO rec_genre ;
        IF rec_genre.errcd <> 1 THEN
            errcd := rec_genre.errcd;
            errmsg := rec_genre.errmsg;
            RETURN;
        END IF;
    END IF;

    errcd := 1;
    errmsg := '完了';
    code := itemc;
END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE;
