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

[ 프로파일 ] sys_profile_api 패키지를 생성합니다.

by 태백성 2024. 4. 12.
create or replace package bsys.sys_profile_api
is
    -- 고정변수 정의
    c_package_name                     constant varchar2(0200) default 'sys_profile_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                                     -- 법인id
        ,pi_lang_cd                    in     varchar2                                   -- 언어코드
        ,pi_profile_id                 in     number   default null                      -- 프로파일id
        ,pi_profile_cd                 in     varchar2                                   -- 프로파일코드
        ,pi_profile_name               in     varchar2                                   -- 프로파일명
        ,pi_profile_short_name         in     varchar2                                   -- 프로파일명(약칭)
        ,pi_profile_value              in     varchar2                                   -- 프로파일값
        ,pi_start_date                 in     date                                       -- 시작일자
        ,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_profile%rowtype                        -- 프로파일(레코드)
        );

    procedure update_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_rec                        in     sys_profile%rowtype                        -- 프로파일(레코드)
        );

    procedure delete_data_p
        (po_result_cd                  in out number                                     -- 결과코드(0: 정상, -1: 오류)
        ,po_result_descr               in out varchar2                                   -- 결과내용
        ,pi_profile_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_profile%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                                   -- 작업사용자cd
        ,pi_transaction_type           in     varchar2                                   -- 처리유형(I:신규, U:업데이트, D:삭제)
        --
        ,pi_corp_id                    in     number                                     -- 법인id
        ,pi_lang_cd                    in     varchar2                                   -- 언어코드
        ,pi_profile_id                 in     number   default null                      -- 프로파일id
        ,pi_profile_cd                 in     varchar2                                   -- 프로파일코드
        ,pi_profile_name               in     varchar2                                   -- 프로파일명
        ,pi_profile_short_name         in     varchar2                                   -- 프로파일명(약칭)
        ,pi_profile_value              in     varchar2                                   -- 프로파일값
        ,pi_start_date                 in     date                                       -- 시작일자
        ,pi_end_date                   in     date                                       -- 종료일자
        ,pi_remark                     in     varchar2                                   -- 비고
        );

end sys_profile_api;

 

create or replace package body bsys.sys_profile_api
    /* ********************************************************************************************
       * 업 무  단 위 : 시스템관리
       * 패   키   지 : bsys.sys_profile_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:삭제)
        --
        ,pi_corp_id                    in     number                                     -- 법인id
        ,pi_lang_cd                    in     varchar2                                   -- 언어코드
        ,pi_profile_id                 in     number   default null                      -- 프로파일id
        ,pi_profile_cd                 in     varchar2                                   -- 프로파일코드
        ,pi_profile_name               in     varchar2                                   -- 프로파일명
        ,pi_profile_short_name         in     varchar2                                   -- 프로파일명(약칭)
        ,pi_profile_value              in     varchar2                                   -- 프로파일값
        ,pi_start_date                 in     date                                       -- 시작일자
        ,pi_end_date                   in     date                                       -- 종료일자
        ,pi_remark                     in     varchar2                                   -- 비고
        )
    is
        l_rec                          sys_profile%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_lang_cd            => pi_lang_cd
            ,pi_profile_id         => pi_profile_id
            ,pi_profile_cd         => pi_profile_cd
            ,pi_profile_name       => pi_profile_name
            ,pi_profile_short_name => pi_profile_short_name
            ,pi_profile_value      => pi_profile_value
            ,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.profile_id := nvl(trim(pi_profile_id), sys_profile_s.nextval);
        else l_rec.profile_id := pi_profile_id;
        end if;
        
        l_rec.corp_id            := nvl(pi_corp_id, sys_env_f('corp_id'));
        l_rec.profile_cd         := upper(trim(pi_profile_cd));
        l_rec.profile_name       := trim(pi_profile_name);
        l_rec.profile_short_name := trim(pi_profile_short_name);        
        l_rec.profile_value      := upper(trim(pi_profile_value));
        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_profile_id   => l_rec.profile_id
                    );
                
            end if;
            
        end if;
        
        --po_result_cd := -1;
        --po_result_descr := '[프로파일명] ' || l_rec.profile_name || ' - 오류를 강제적으로 발생합니다.';
        -- 정상처리
        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 dbms_output.put_line('po_result_descr => ' || po_result_descr);
                 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_profile%rowtype                      -- 프로파일(레코드)
        )
    is
        l_user_exception               exception;
    begin
        -- 기본값 할당
        po_result_cd    := 0;
        po_result_descr := sys_init_api.c_success_descr;
       
        -- 프로파일 자료저장
        insert into sys_profile
            (corp_id
            ,profile_id
            ,profile_cd
            ,profile_name
            ,profile_short_name
            ,profile_value
            ,start_date
            ,end_date
            ,remark 
            --
            ,create_date
            ,create_by
            ,update_date
            ,update_by
            )
        values 
            (pi_rec.corp_id
            ,pi_rec.profile_id
            ,pi_rec.profile_cd
            ,pi_rec.profile_name
            ,pi_rec.profile_short_name
            ,pi_rec.profile_value
            ,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 := '[신규저장] ' || sqlerrm;
            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_profile%rowtype                      -- 프로파일(레코드)
        )
    is
        l_user_exception               exception;
    begin
        -- 기본값 할당
        po_result_cd    := 0;
        po_result_descr := sys_init_api.c_success_descr;
        
        -- 프로파일 자료수정
        update sys_profile
           set corp_id            = pi_rec.corp_id
              ,profile_cd         = pi_rec.profile_cd
              ,profile_name       = pi_rec.profile_name
              ,profile_short_name = pi_rec.profile_short_name
              ,profile_value      = pi_rec.profile_value
              ,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 profile_id         = pi_rec.profile_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 := '[수정저장] ' ||  sqlerrm;
            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_profile_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_profile
         where 1 = 1
           and profile_id = pi_profile_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 := '[자료삭제] ' || sqlerrm;
            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_profile%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 (sys_init_api.c_insert, sys_init_api.c_update) then

            if pi_valid_type = 'U' and trim(pi_rec.corp_id) is null then
                po_result_descr := '법인id 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.profile_id) is null then
                po_result_descr := '프로파일id 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.profile_cd) is null then
                po_result_descr := '프로파일코드 항목은 필수입니다.';
                raise l_user_exception;

            if trim(pi_rec.profile_name) is null then
                po_result_descr := '프로파일명 항목은 필수입니다.';
                raise l_user_exception;
            end if;

            if trim(pi_rec.profile_short_name) is null then
                po_result_descr := '프로파일명(약칭) 항목은 필수입니다.';
                raise l_user_exception;
            end if;
            end if;

            if trim(pi_rec.profile_value) 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 = sys_init_api.c_delete then
            if trim(pi_rec.profile_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 :=  '[자료검증] ' || sqlerrm;
            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                                   -- 작업사용자cd
        ,pi_transaction_type           in     varchar2                                   -- 처리유형(I:신규, U:업데이트, D:삭제)
        --
        ,pi_corp_id                    in     number                                     -- 법인id
        ,pi_lang_cd                    in     varchar2                                   -- 언어코드
        ,pi_profile_id                 in     number   default null                      -- 프로파일id
        ,pi_profile_cd                 in     varchar2                                   -- 프로파일코드
        ,pi_profile_name               in     varchar2                                   -- 프로파일명
        ,pi_profile_short_name         in     varchar2                                   -- 프로파일명(약칭)
        ,pi_profile_value              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_profile_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_corp_id            → ' || pi_corp_id            || chr(10) ||
                                                    'pi_lang_cd            → ' || pi_lang_cd            || chr(10) ||
                                                    'pi_profile_id         → ' || pi_profile_id         || chr(10) ||
                                                    'pi_profile_cd         → ' || pi_profile_cd         || chr(10) ||
                                                    'pi_profile_name       → ' || pi_profile_name       || chr(10) ||
                                                    'pi_profile_short_name → ' || pi_profile_short_name || chr(10) ||
                                                    'pi_profile_value      → ' || pi_profile_value      || 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 :=  '[로그저장] ' || sqlerrm;
            dbms_output.put_line('po_result_descr => ' || po_result_descr);
    end log_data_p;

end sys_profile_api;