-- システム情報テーブル 変更
CREATE OR REPLACE FUNCTION f_systeminfo_update(
    IN integer,

    IN character varying,
    IN character varying,
    IN character varying,

    IN character varying,
    IN character varying,
    IN integer,

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

DECLARE
    kubun ALIAS FOR $1;                  -- 区分

    i_shopnm ALIAS FOR $2;               -- ショップ名
    i_shopcontent ALIAS FOR $3;               -- ショップ説明
    i_buynote ALIAS FOR $4;               -- 購入時、備考

    i_updusrnm ALIAS FOR $5;             -- 更新ユーザー名
    i_updpcnm ALIAS FOR $6;              -- 更新マシン名
    i_updcnt ALIAS FOR $7;               -- UpdCnt

    cntcheck integer;
    nowdate timestamp;
    newupdcnt integer;

BEGIN
-- 必須チェック --

    IF kubun IS NULL OR kubun NOT IN (2) THEN
        RAISE EXCEPTION '処理区分が不正です';
    END IF;

    IF i_updusrnm IS NULL THEN
        RAISE EXCEPTION '更新ユーザー名は必須です';
    END IF;
    IF i_updpcnm IS NULL THEN
        RAISE EXCEPTION '更新PC名は必須です';
    END IF;
    IF i_updcnt IS NULL THEN
        RAISE EXCEPTION 'UpdCntは必須です';
    END IF;

    -- 更新カウンタチェック
    IF kubun IN (2) THEN
        SELECT updcnt INTO cntcheck FROM t_systeminfo WHERE validflg=TRUE;
          IF NOT FOUND OR cntcheck <> i_updcnt THEN
            RAISE EXCEPTION '更新カウンタが不正です';
          END IF;
    END IF;

    --- 更新処理 ---
     IF kubun = 2 THEN
        UPDATE t_systeminfo SET
            shopnm  = i_shopnm,
            shopcontent     = i_shopcontent,
            buynote     = i_buynote,
            upddate      = nowdate,
            updusrnm     = i_updusrnm,
            updpcnm      = i_updpcnm
            WHERE validflg=TRUE;
    END IF;

    code := 1;

END;
$$
LANGUAGE 'plpgsql' VOLATILE;
