CREATE OR REPLACE FUNCTION f_import_item_struct(
    IN hinmeicode integer,
    IN mediacode integer,
    IN makeritem character varying,
    IN jancode character varying,
    IN isbncode character varying,
    IN hatsubai timestamp without time zone,
    IN yoyaku timestamp without time zone,
    IN saleformcode integer,
    IN playtime integer,
    IN makerteika integer,
    IN usr character varying,

    OUT errcd integer, OUT errmsg character varying, OUT code integer) AS
$BODY$

DECLARE
    sql1 varchar;
    sql2 varchar;
    _time timestamp without time zone;
BEGIN
    IF usr IS NULL THEN
        errmsg := '更新ユーザが指定されていません。';
        errcd := -10;
        RETURN;
    END IF;

    -- 発注締切日が未入力のときは発売日をセットする
    IF yoyaku IS NULL THEN
        _time := hatsubai;
    ELSE
        _time := yoyaku;
    END IF;

    IF hinmeicode IS NOT NULL THEN
        INSERT INTO t_item_struct(itemcd, mediacd, makeritemcd, jancd, isbncd, salestartdate, orderclosedate, saleformcd, rectime, makerprice, updusrnm)
                    VALUES(hinmeicode, mediacode, makeritem, jancode, isbncode, hatsubai, _time, saleformcode, playtime, makerteika, usr);
        errcd := 1;
        errmsg := '成功';
        SELECT INTO code MAX(structcd) FROM t_item_struct WHERE itemcd = hinmeicode ;
        RETURN;

    ELSE
        errcd := -10;
        errmsg := '品目コードが指定されていません';
        code := 0;
        RETURN;
    END IF;

END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

