본문 바로가기
  • 노란색 세상은 어디에 있을까?
  • 봄이 오면 여기에 있겠지.
  • 잠시나마 유유자적 하겠네.
오라클/패키지(시스템관리)

[ 국가 ] sys_country_api 패키지를 생성합니다.

by 태백성 2024. 4. 12.
create or replace PACKAGE bsys.sys_country_api
is
    -- 고정변수 정의
    c_package_name                     constant varchar2(0200) default 'sys_country_api';

    -- 전역변수 정의
    g_error_display_type                        varchar2(0020) default 'W';             -- W: 웹페이지, P: PL/SQL

    -- 함수 정의
    procedure main_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_work_user_cd               in     varchar2                                   -- 작업사용자cd
        ,pi_transaction_type           in     varchar2                                   -- 처리유형(I:신규, U:업데이트, D:삭제)
        --
        ,pi_corp_id                    in     number   default null                      -- 법인id
        ,pi_country_id                 in     number   default null                      -- 국가id
        ,pi_lang_cd                    in     varchar2                                   -- 언어코드
        ,pi_country_cd                 in     varchar2                                   -- 국가코드
        ,pi_country_name               in     varchar2                                   -- 국가명
        ,pi_country3_cd                in     varchar2                                   -- 국가코드3
        ,pi_language_cd                in     varchar2                                   -- 언어코드
        ,pi_language_name              in     varchar2                                   -- 언어명
        ,pi_currency_cd                in     varchar2                                   -- 화폐코드
        ,pi_currency_name              in     varchar2                                   -- 화폐명
        ,pi_start_date                 in     date     default trunc(sysdate)            -- 시작일자
        ,pi_end_date                   in     date                                       -- 종료일자
        ,pi_remark                     in     varchar2                                   -- 비고
        );
        
    procedure insert_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_rec                        in     sys_country%rowtype                        -- 국가(레코드)
        );

    procedure update_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_rec                        in     sys_country%rowtype                        -- 국가(레코드)
        );

    procedure delete_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_country_id                 in     number                                     -- 국가id
        );

    procedure valid_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_valid_type                 in     varchar2                                   -- 검증유형
        ,pi_rec                        in     sys_country%rowtype                        -- 국가(레코드)
        );
        
    procedure log_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_work_user_cd               in     varchar2                                   -- 작업사용자코드
        ,pi_transaction_type           in     varchar2                                   -- 처리유형(I:신규, U:업데이트, D:삭제)
        --
        ,pi_corp_id                    in     number                                     -- 법인id
        ,pi_country_id                 in     number                                     -- 국가id
        ,pi_lang_cd                    in     varchar2                                   -- 언어코드
        ,pi_country_cd                 in     varchar2                                   -- 국가코드
        ,pi_country_name               in     varchar2                                   -- 국가명
        ,pi_country3_cd                in     varchar2                                   -- 국가코드3
        ,pi_language_cd                in     varchar2                                   -- 언어코드
        ,pi_language_name              in     varchar2                                   -- 언어명
        ,pi_currency_cd                in     varchar2                                   -- 화폐코드
        ,pi_currency_name              in     varchar2                                   -- 화폐명
        ,pi_start_date                 in     date                                       -- 시작일자
        ,pi_end_date                   in     date                                       -- 종료일자
        ,pi_remark                     in     varchar2                                   -- 비고
        );

end sys_country_api;

 

create or replace package body bsys.sys_country_api
    /* ********************************************************************************************
       * 업 무  단 위 : 시스템관리
       * 패   키   지 : bsys.sys_country_api
       * 사 용  목 적 : 국가 등록/수정/삭제 처리를 위한 패키지
       * 생 성  일 자 : 2024-02-01
       * 생   성   자 : Admin 
       --------------------------------------------------------------------------------------------
       * 수정일자       수정자      수정내역
       --------------------------------------------------------------------------------------------
       * 2024-02-01     Admin       최초 작성
    ******************************************************************************************** */
is
    /* ********************************************************************************************
       * 사 용  목 적 : 국가 등록/수정/삭제 메인 함수
       * 생 성  일 자 : 2024-02-01
       * 생   성   자 : Admin 
    ******************************************************************************************** */
    procedure main_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_work_user_cd               in     varchar2                                   -- 작업사용자cd
        ,pi_transaction_type           in     varchar2                                   -- 처리유형(I:신규, U:업데이트, D:삭제, V:검증)
        --
        ,pi_corp_id                    in     number   default null                      -- 법인id
        ,pi_country_id                 in     number   default null                      -- 국가id
        ,pi_lang_cd                    in     varchar2                                   -- 언어코드
        ,pi_country_cd                 in     varchar2                                   -- 국가코드
        ,pi_country_name               in     varchar2                                   -- 국가명
        ,pi_country3_cd                in     varchar2                                   -- 국가코드3
        ,pi_language_cd                in     varchar2                                   -- 언어코드
        ,pi_language_name              in     varchar2                                   -- 언어명
        ,pi_currency_cd                in     varchar2                                   -- 화폐코드
        ,pi_currency_name              in     varchar2                                   -- 화폐명
        ,pi_start_date                 in     date     default trunc(sysdate)            -- 시작일자
        ,pi_end_date                   in     date                                       -- 종료일자
        ,pi_remark                     in     varchar2                                   -- 비고
        )
    is
        l_rec                          sys_country%rowtype   default null;               -- 국가
        l_user_exception               exception;
    begin
        -- 기본값 할당
        po_result_cd    := 0;
        po_result_descr := sys_init_api.c_success_descr;
        l_rec           := null;
        
        -- 로그저장
        log_data_p
            (po_result_cd        => po_result_cd
            ,po_result_descr     => po_result_descr
            ,pi_work_user_cd     => pi_work_user_cd
            ,pi_transaction_type => pi_transaction_type
            --
            ,pi_corp_id          => pi_corp_id
            ,pi_country_id       => pi_country_id
            ,pi_lang_cd          => pi_lang_cd
            ,pi_country_cd       => pi_country_cd
            ,pi_country_name     => pi_country_name
            ,pi_country3_cd      => pi_country3_cd
            ,pi_language_cd      => pi_language_cd
            ,pi_language_name    => pi_language_name
            ,pi_currency_cd      => pi_currency_cd
            ,pi_currency_name    => pi_currency_name
            ,pi_start_date       => pi_start_date
            ,pi_end_date         => pi_end_date
            ,pi_remark           => pi_remark
            );
        if po_result_cd = -1 then raise l_user_exception; end if;
        
        -- 처리유형
        if pi_transaction_type is null or pi_transaction_type not in ('I', 'U', 'D', 'V') then
            po_result_descr := '데이터 처리유형을 선택하시기 바랍니다.';
            raise l_user_exception;
        end if;
           
        -- 국가 변수값 할당
        if pi_transaction_type = 'I' then
             l_rec.country_id := nvl(trim(pi_country_id), sys_country_s.nextval);
        else l_rec.country_id := trim(pi_country_id);
        end if;
        
        l_rec.country_cd      := upper(trim(pi_country_cd));
        l_rec.country_name    := trim(pi_country_name);        
        l_rec.country3_cd     := upper(trim(pi_country3_cd));
        l_rec.language_cd     := upper(trim(pi_language_cd));
        l_rec.language_name   := trim(pi_language_name);
        l_rec.currency_cd     := upper(trim(pi_currency_cd));
        l_rec.currency_name   := trim(pi_currency_name);
        l_rec.start_date      := trunc(pi_start_date);
        l_rec.end_date        := trunc(pi_end_date);
        l_rec.remark          := trim(pi_remark);
        
        -- Who 컬럼값 할당
        l_rec.create_date     := systimestamp;
        l_rec.create_by       := upper(nvl(pi_work_user_cd, nvl(wwv_flow.g_user, user)));
        l_rec.update_date     := systimestamp;
        l_rec.update_by       := upper(nvl(pi_work_user_cd, nvl(wwv_flow.g_user, user)));
        
        -- 자료검증
        valid_data_p
            (po_result_cd    => po_result_cd
            ,po_result_descr => po_result_descr
            ,pi_valid_type   => pi_transaction_type
            ,pi_rec          => l_rec
            );
        if po_result_cd = -1         then raise l_user_exception; end if;        
        if pi_transaction_type = 'V' then return; end if;
        
        -- 자료저장
        if po_result_cd = -1 then
            raise l_user_exception;

        else
            -- 신규저장 함수 호출
            if pi_transaction_type = 'I' then
                insert_data_p
                    (po_result_cd    => po_result_cd
                    ,po_result_descr => po_result_descr
                    ,pi_rec          => l_rec
                    );
                
            -- 수정저장 함수 호출
            elsif pi_transaction_type = 'U' then
                update_data_p
                    (po_result_cd    => po_result_cd
                    ,po_result_descr => po_result_descr
                    ,pi_rec          => l_rec
                    );
           
            -- 삭제 함수 호출
            elsif pi_transaction_type = 'D' then
                delete_data_p
                    (po_result_cd    => po_result_cd
                    ,po_result_descr => po_result_descr
                    ,pi_country_id   => l_rec.country_id
                    );
                
            end if;
            
        end if;
        
        -- 정상처리
        if po_result_cd = -1 then            
             raise l_user_exception;
        else po_result_descr := sys_init_api.c_success_descr;
        end if;

    exception
        when l_user_exception then
            po_result_cd    := -1;            
            if g_error_display_type = 'P' then
                 po_result_descr := sys_init_api.out_msg_f(sys_init_api.c_insert, c_package_name || '.main_data_p', po_result_descr);
            else dbms_output.put_line('po_result_descr → ' || po_result_descr);
                 raise_application_error(-20000, po_result_descr);
            end if;            
        
        when others then
            po_result_cd    := -1;
            if g_error_display_type = 'P' then
                 po_result_descr := sys_init_api.out_msg_f(sys_init_api.c_insert, c_package_name || '.main_data_p', sqlerrm);            
            else po_result_descr := substr(sqlerrm, 11);
                 raise_application_error(-20000, po_result_descr);                 
            end if;
    end main_data_p;
    
    /* ********************************************************************************************
       * 사 용  목 적 : 국가 신규 등록하는 함수
       * 생 성  일 자 : 2024-02-01
       * 생   성   자 : Admin 
    ******************************************************************************************** */
    procedure insert_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_rec                        in     sys_country%rowtype                      -- 국가(레코드)
        )
    is
        l_user_exception               exception;
    begin
        -- 기본값 할당
        po_result_cd    := 0;
        po_result_descr := sys_init_api.c_success_descr;
        
        -- 국가 자료저장
        insert into sys_country
            (country_id
            ,country_cd
            ,country_name 
            ,country3_cd
            ,language_cd
            ,language_name 
            ,currency_cd
            ,currency_name 
            ,start_date
            ,end_date
            ,remark 
            --
            ,create_date
            ,create_by
            ,update_date
            ,update_by
            )
        values
            (pi_rec.country_id
            ,pi_rec.country_cd
            ,pi_rec.country_name
            ,pi_rec.country3_cd
            ,pi_rec.language_cd
            ,pi_rec.language_name
            ,pi_rec.currency_cd
            ,pi_rec.currency_name
            ,pi_rec.start_date
            ,pi_rec.end_date
            ,pi_rec.remark
            --
            ,pi_rec.create_date
            ,pi_rec.create_by
            ,pi_rec.update_date
            ,pi_rec.update_by
            );

        -- 정상처리
        po_result_descr := sys_init_api.c_success_descr;

    exception
        when l_user_exception then
            po_result_cd    := -1;
            po_result_descr := '[신규저장] ' || po_result_descr;
            dbms_output.put_line('po_result_descr → ' || po_result_descr);

        when dup_val_on_index then
            po_result_cd    := -1;
            po_result_descr := '[신규저장] 국가코드가 중복되었습니다';
            dbms_output.put_line('po_result_descr → ' || po_result_descr);

        when others then
            po_result_cd    := -1;
            po_result_descr := '[신규저장] ' || substr(sqlerrm, 11);
            dbms_output.put_line('po_result_descr → ' || po_result_descr);
    end insert_data_p;

    /* ********************************************************************************************
       * 사 용  목 적 : 국가 자료를 수정하는 함수
       * 생 성  일 자 : 2024-02-01
       * 생   성   자 : Admin 
    ******************************************************************************************** */
    procedure update_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_rec                        in     sys_country%rowtype                      -- 국가(레코드)
        )
    is
        l_user_exception               exception;
    begin
        -- 기본값 할당
        po_result_cd    := 0;
        po_result_descr := sys_init_api.c_success_descr;
        
        -- 시스템언어코드
        -- l_sys_lang_cd := wwv_flow.g_flow_language;
        
        -- 국가 자료수정
        update sys_country
           set country_cd      = pi_rec.country_cd
              ,country_name    = pi_rec.country_name
              ,country3_cd     = pi_rec.country3_cd
              ,language_cd     = pi_rec.language_cd
              ,language_name   = pi_rec.language_name
              ,currency_cd     = pi_rec.currency_cd
              ,currency_name   = pi_rec.currency_name
              ,start_date      = pi_rec.start_date
              ,end_date        = pi_rec.end_date
              ,remark          = pi_rec.remark
              --
              ,update_date        = pi_rec.update_date
              ,update_by          = pi_rec.update_by
         where 1 = 1
           and country_id      = pi_rec.country_id;

        -- 정상처리
        po_result_descr := sys_init_api.c_success_descr;

    exception
        when l_user_exception then
            po_result_cd    := -1;
            po_result_descr := '[수정저장] ' || po_result_descr;
            dbms_output.put_line('po_result_descr → ' || po_result_descr);

        when dup_val_on_index then
            po_result_cd    := -1;
            po_result_descr := '[수정저장] 국가코드가 중복되었습니다.';
            dbms_output.put_line('po_result_descr → ' || po_result_descr);

        when others then
            po_result_cd    := -1;
            po_result_descr := '[수정저장] ' ||  substr(sqlerrm, 11);
            dbms_output.put_line('po_result_descr → ' || po_result_descr);
    end update_data_p;

    /* ********************************************************************************************
       * 사 용  목 적 : 국가 자료를 삭제하는 함수
       * 생 성  일 자 : 2024-02-01
       * 생   성   자 : Admin 
    ******************************************************************************************** */
    procedure delete_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_country_id                 in     number                                     -- 국가id
        )
    is
        l_user_exception               exception;
    begin
        -- 기본값 할당
        po_result_cd    := 0;
        po_result_descr := sys_init_api.c_success_descr;

        -- 국가 자료삭제
        delete sys_country
         where 1 = 1
           and country_id = pi_country_id;

        -- 정상처리
        po_result_descr := sys_init_api.c_success_descr;

    exception
        when l_user_exception then
            po_result_cd    := -1;
            po_result_descr := '[자료삭제] ' || po_result_descr;
            dbms_output.put_line('po_result_descr → ' || po_result_descr);

        when others then
            po_result_cd    := -1;
            po_result_descr := '[자료삭제] ' || substr(sqlerrm, 11);
            dbms_output.put_line('po_result_descr → ' || po_result_descr);
    end delete_data_p;

    /* ********************************************************************************************
       * 사 용  목 적 : 국가 자료를 검증하는 함수
       * 생 성  일 자 : 2024-02-01
       * 생   성   자 : Admin 
    ******************************************************************************************** */
    procedure valid_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_valid_type                 in     varchar2                                   -- 검증유형
        ,pi_rec                        in     sys_country%rowtype                      -- 국가(레코드)
        )
    is
        l_user_exception               exception;
    begin
        -- 기본값 할당
        po_result_cd    := 0;
        po_result_descr := sys_init_api.c_success_descr;

        -- 신규 및 수정 검증
        if pi_valid_type in ('I', 'U', 'V') then
            
            if pi_valid_type = 'U' and trim(pi_rec.country_id) is null then
                po_result_descr := '국가id 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.country_cd) is null then
                po_result_descr := '국가코드 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.country_name) is null then
                po_result_descr := '국가명 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.country3_cd) is null then
                po_result_descr := '국가코드3 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.language_cd) is null then
                po_result_descr := '언어코드 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.language_name) is null then
                po_result_descr := '언어명 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.currency_cd) is null then
                po_result_descr := '화폐코드 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.currency_name) is null then
                po_result_descr := '화폐명 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trunc(pi_rec.start_date) is null then
                po_result_descr := '시작일자 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            -- 시직일자 및 종료일자 검증
            sys_valid_api.date_range_p
                (pi_start_date   => pi_rec.start_date
                ,pi_end_date     => pi_rec.end_date
                ,po_result_cd    => po_result_cd
                ,po_result_descr => po_result_descr
                );
            if po_result_cd = -1 then raise l_user_exception; end if;

        end if;

        -- 삭제 검증
        if pi_valid_type = 'D' then
            if trim(pi_rec.country_id) is null then
                po_result_descr := '국가id 항목은 필수입니다.';
                raise l_user_exception;
            end if;

        end if;

    exception
        when l_user_exception then
            po_result_cd    := -1;
            po_result_descr :=  '[자료검증] ' || po_result_descr;
            dbms_output.put_line('po_result_descr → ' || po_result_descr);

        when others then
            po_result_cd    := -1;
            po_result_descr :=  '[자료검증] ' || substr(sqlerrm, 11);
            dbms_output.put_line('po_result_descr → ' || po_result_descr);
    end valid_data_p;

    /* ********************************************************************************************
       * 사 용  목 적 : 국가 로그정보를 생성하는 함수
       * 생 성  일 자 : 2024-02-01
       * 생   성   자 : Admin 
    ******************************************************************************************** */
    procedure log_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_work_user_cd               in     varchar2                                   -- 작업사용자코드
        ,pi_transaction_type           in     varchar2                                   -- 처리유형(I:신규, U:업데이트, D:삭제, V:검증)
        --
        ,pi_corp_id                    in     number                                     -- 법인id
        ,pi_country_id                 in     number                                     -- 국가id
        ,pi_lang_cd                    in     varchar2                                   -- 언어코드
        ,pi_country_cd                 in     varchar2                                   -- 국가코드
        ,pi_country_name               in     varchar2                                   -- 국가명
        ,pi_country3_cd                in     varchar2                                   -- 국가코드3
        ,pi_language_cd                in     varchar2                                   -- 언어코드
        ,pi_language_name              in     varchar2                                   -- 언어명
        ,pi_currency_cd                in     varchar2                                   -- 화폐코드
        ,pi_currency_name              in     varchar2                                   -- 화폐명
        ,pi_start_date                 in     date                                       -- 시작일자
        ,pi_end_date                   in     date                                       -- 종료일자
        ,pi_remark                     in     varchar2                                   -- 비고
        )
    is
        l_user_exception               exception;
    begin
        -- 기본값 할당
        po_result_cd    := 0;
        po_result_descr := sys_init_api.c_success_descr;
        
        -- 로그저장
        sys_log_api.g_log                        := null;
        sys_log_api.g_log.log_name               := '국가 데이터 저장';
        sys_log_api.g_log.param_package          := 'bsys.sys_country_api.main_data_p';
        sys_log_api.g_log.corp_id                := pi_corp_id;
        sys_log_api.g_log.param_corp_id          := pi_corp_id;
        sys_log_api.g_log.param_lang_cd          := upper(nvl(pi_lang_cd, wwv_flow.g_flow_language));
        sys_log_api.g_log.param_user_cd          := upper(nvl(pi_work_user_cd, nvl(wwv_flow.g_user, user)));
        sys_log_api.g_log.param_transaction_type := pi_transaction_type;
        sys_log_api.g_log.param_file_name        := null;
        sys_log_api.g_log.param_info             := '-- 국가 변수값 할당'                     || chr(10) ||
                                                    'pi_country_id    → ' || pi_country_id    || chr(10) ||
                                                    'pi_country_cd    → ' || pi_country_cd    || chr(10) ||
                                                    'pi_country_name  → ' || pi_country_name  || chr(10) ||
                                                    'pi_country3_cd   → ' || pi_country3_cd   || chr(10) ||
                                                    'pi_language_cd   → ' || pi_language_cd   || chr(10) ||
                                                    'pi_language_name → ' || pi_language_name || chr(10) ||
                                                    'pi_currency_cd   → ' || pi_currency_cd   || chr(10) ||
                                                    'pi_currency_name → ' || pi_currency_name || chr(10) ||
                                                    'pi_start_date    → ' || pi_start_date    || chr(10) ||
                                                    'pi_end_date      → ' || pi_end_date      || chr(10) ||
                                                    'pi_remark        → ' || pi_remark;
        sys_log_api.g_log.result_status_cd       := 'U';
        sys_log_api.g_log.result_descr           := null;
        sys_log_api.log_p(po_result_cd, po_result_descr);

    exception
        when others then
            po_result_cd    := -1;
            po_result_descr :=  '[로그저장] ' || substr(sqlerrm, 11);
            dbms_output.put_line('po_result_descr → ' || po_result_descr);
    end log_data_p;

end sys_country_api;