-- システム情報テーブル 変更 HTMLテンプレート用
-- Nullのものは更新しない
CREATE OR REPLACE FUNCTION f_systeminfo_html_update(
    IN integer,

    IN text,
    IN text,
    IN text,
    IN text,
    IN text,
    IN text,
    
    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_company_overview ALIAS FOR $2;      -- 会社概要
    i_privacy_policy ALIAS FOR $3;        -- プライベートポリシー
    i_transaction_law ALIAS FOR $4;       -- 特定商取引法
    i_permit_rule ALIAS FOR $5;           -- 許可・届出利用規約
    i_method_payment ALIAS FOR $6;        -- お支払方法
    i_use_rule ALIAS FOR $7;              -- 利用規約
    
    i_updusrnm ALIAS FOR $8;              -- 更新ユーザー名
    i_updpcnm ALIAS FOR $9;               -- 更新マシン名
    i_updcnt ALIAS FOR $10;               -- UpdCnt
    

    
    
    --cntcheck integer;
    rec RECORD;
    nowdate timestamp;
    newupdcnt integer;
    _company_overview text;      -- 会社概要
    _privacy_policy text;        -- プライベートポリシー
    _transaction_law text;       -- 特定商取引法
    _permit_rule text;           -- 許可・届出利用規約
    _method_payment text;        -- お支払方法
    _use_rule text;              -- 利用規約

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 IN (2) THEN
        SELECT * INTO rec FROM t_systeminfo WHERE validflg=TRUE;
          IF NOT FOUND OR rec.updcnt <> i_updcnt THEN
            RAISE EXCEPTION '更新カウンタが不正です';
          END IF;
    END IF;
    
    --- 更新処理 ---
    IF kubun = 2 THEN
        IF i_company_overview IS NULL THEN
            _company_overview := rec.company_overview;
        ELSE
            _company_overview := i_company_overview;
        END IF;
        
        IF i_privacy_policy IS NULL THEN
            _privacy_policy := rec.privacy_policy;
        ELSE
            _privacy_policy := i_privacy_policy;
        END IF;
        
        IF i_transaction_law IS NULL THEN
            _transaction_law := rec.transaction_law;
        ELSE
            _transaction_law := i_transaction_law;
        END IF;
        
        IF i_permit_rule IS NULL THEN
            _permit_rule := rec.permit_rule;
        ELSE
            _permit_rule := i_permit_rule;
        END IF;
        
        IF i_method_payment IS NULL THEN
            _method_payment := rec.method_payment;
        ELSE
            _method_payment := i_method_payment;
        END IF;
        
        IF i_use_rule IS NULL THEN
            _use_rule := rec.use_rule;
        ELSE
            _use_rule := i_use_rule;
        END IF;
        
        UPDATE t_systeminfo SET
            company_overview     = _company_overview,
            privacy_policy     = _privacy_policy,
            transaction_law     = _transaction_law,
            permit_rule = _permit_rule,
            method_payment = _method_payment,
            use_rule = _use_rule,
            
            upddate      = nowdate,
            updusrnm     = i_updusrnm,
            updpcnm      = i_updpcnm
            WHERE validflg=TRUE;
    END IF;

    code := 1;

END;
$$
LANGUAGE 'plpgsql' VOLATILE;
