--
-- PostgreSQL database dump
--


-- Dumped from database version 18.1
-- Dumped by pg_dump version 18.0

-- Started on 2026-02-02 14:06:03

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- TOC entry 6 (class 2615 OID 16413)
-- Name: kngil; Type: SCHEMA; Schema: -; Owner: postgres
--

CREATE SCHEMA kngil;


ALTER SCHEMA kngil OWNER TO postgres;

--
-- TOC entry 262 (class 1255 OID 16574)
-- Name: fn_base_cd(character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.fn_base_cd(p_main_cd character varying) RETURNS TABLE(id character varying, text character varying)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
	SELECT 
        -- CASE 문을 사용하여 조건에 따라 반환할 컬럼을 선택합니다.
        (CASE 
            WHEN b.use_bc = 'BS200100' THEN a.base_cd::VARCHAR 
			WHEN b.use_bc = 'BS200200' THEN a.sub_cd::VARCHAR
            END) AS id, 				-- 코드1이면 기초코드 아니면 서브코드 표시.
        a.sub_nm::VARCHAR AS text		-- 코드명
    FROM kngil.code_detail a							--공통코드 상세
    JOIN kngil.code_master b ON a.main_cd = b.main_cd	--공통코드 마스터
    WHERE a.main_cd = p_main_cd
      AND a.use_yn = 'Y' 				-- 사용여부
    ORDER BY 
		(CASE WHEN b.sort_bc = 'BS110100' THEN a.sub_nm END) ASC,
    	(CASE WHEN b.sort_bc = 'BS110200' THEN a.sort_sq END) ASC;
END;
$$;


ALTER FUNCTION kngil.fn_base_cd(p_main_cd character varying) OWNER TO postgres;

--
-- TOC entry 251 (class 1255 OID 16571)
-- Name: fn_base_nm(character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.fn_base_nm(p_base_cd character varying) RETURNS TABLE(name character varying)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
	SELECT 
        a.sub_nm::VARCHAR AS name		-- 코드명
    FROM kngil.code_detail a							--공통코드 상세
    JOIN kngil.code_master b ON a.main_cd = b.main_cd	--공통코드 마스터
    WHERE a.base_cd = p_base_cd;

END;
$$;


ALTER FUNCTION kngil.fn_base_nm(p_base_cd character varying) OWNER TO postgres;

--
-- TOC entry 276 (class 1255 OID 16703)
-- Name: fn_update_buy_area(); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.fn_update_buy_area() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
    v_member_id character varying;
BEGIN
    -- 1. 이벤트 종류(INSERT, UPDATE, DELETE)에 따라 member_id 추출
    IF (TG_OP = 'DELETE') THEN
        v_member_id := OLD.member_id;
    ELSE
        v_member_id := NEW.member_id;
    END IF;

    -- 2. 해당 회원의 당해 연도(end_dt 기준) 합계 적용면적 업데이트
    -- ok_yn = 'Y'인 데이터만 합산
    UPDATE kngil.members
    SET buy_area = (
        SELECT COALESCE(SUM(sum_area), 0)
        FROM kngil.buy_item
        WHERE member_id = v_member_id
          AND ok_yn = 'Y'
          AND EXTRACT(YEAR FROM end_dt) = EXTRACT(YEAR FROM CURRENT_DATE)
    )
    WHERE member_id = v_member_id;

    -- UPDATE 이벤트에서 member_id가 변경된 경우, 이전 member_id의 데이터도 갱신 필요
    IF (TG_OP = 'UPDATE' AND OLD.member_id <> NEW.member_id) THEN
        UPDATE kngil.members
        SET buy_area = (
            SELECT COALESCE(SUM(sum_area), 0)
            FROM kngil.buy_item
            WHERE member_id = OLD.member_id
              AND ok_yn = 'Y'
              AND EXTRACT(YEAR FROM end_dt) = EXTRACT(YEAR FROM CURRENT_DATE)
        )
        WHERE member_id = OLD.member_id;
    END IF;

    RETURN NULL; -- AFTER 트리거이므로 결과 반환은 필요 없음
END;
$$;


ALTER FUNCTION kngil.fn_update_buy_area() OWNER TO postgres;

--
-- TOC entry 250 (class 1255 OID 16541)
-- Name: fn_user_auth(); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.fn_user_auth() RETURNS TABLE(code character varying, name character varying)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
    SELECT 
        -- 마스터의 use_bc 설정에 따라 base_cd 또는 sub_cd 반환
        (CASE 
            WHEN b.use_bc = 'BS200100' THEN a.base_cd::VARCHAR 
            WHEN b.use_bc = 'BS200200' THEN a.sub_cd::VARCHAR
         END) AS code, 
        a.sub_nm::VARCHAR AS name
    FROM kngil.code_detail a
    JOIN kngil.code_master b ON a.main_cd = b.main_cd
    WHERE a.main_cd = 'BS100'   -- 권한관리코드 고정
      AND a.use_yn = 'Y'        -- 사용여부 'Y'
      AND a.m1 = '1'            -- 특정 필터 조건
    ORDER BY 
        (CASE WHEN b.sort_bc = 'BS110100' THEN a.sub_nm END) ASC,
        (CASE WHEN b.sort_bc = 'BS110200' THEN a.sort_sq END) ASC;
END;
$$;


ALTER FUNCTION kngil.fn_user_auth() OWNER TO postgres;

--
-- TOC entry 255 (class 1255 OID 16583)
-- Name: fn_user_id_check(character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.fn_user_id_check(p_user_id character varying) RETURNS character varying
    LANGUAGE plpgsql
    AS $$
DECLARE
    v_user_exists INTEGER;
/*
유저 중복 체크 함수 회원가입 시 중복체크
*/
BEGIN
	-- [1] 중복 아이디 체크 (대소문자 무시)
	SELECT COUNT(*) INTO v_user_exists 
	FROM kngil.users 
	WHERE LOWER(user_id) = LOWER(p_user_id); -- 양쪽 모두 소문자로 변환하여 비교

    IF v_user_exists > 0 THEN
        RETURN 'ERROR: 이미 존재하는 아이디입니다.';
    ELSE
        RETURN 'SUCCESS: 사용 가능한 아이디입니다.';
    END IF;
END;
$$;


ALTER FUNCTION kngil.fn_user_id_check(p_user_id character varying) OWNER TO postgres;

--
-- TOC entry 257 (class 1255 OID 16550)
-- Name: sp_buy_item_d(character varying, integer); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_buy_item_d(p_member_id character varying, p_sq_no integer) RETURNS text
    LANGUAGE plpgsql
    AS $$
DECLARE
/*
 설  명 	: buy_item 테이블 삭제 프로시져
 작성자 	: 권오재
 작성일	: 2026-01-14
 비	고 	: 최초작성
*/
    v_ok_yn CHAR(1);
BEGIN
    -- 1. 해당 구매 건의 승인 여부 확인
    SELECT ok_yn INTO v_ok_yn
    FROM kngil.buy_item
    WHERE member_id = p_member_id AND sq_no = p_sq_no;

    IF NOT FOUND THEN
        RETURN 'ERROR: 삭제할 구매 정보를 찾을 수 없습니다.';
    END IF;

    -- 2. 승인여부(ok_yn)가 'Y'이면 삭제 불가
    IF v_ok_yn = 'Y' THEN
        RETURN 'ERROR: 승인 완료(Y)된 구매 내역은 삭제할 수 없습니다.';
    END IF;

    -- 3. 삭제 실행
    DELETE FROM kngil.buy_item
    WHERE member_id = p_member_id AND sq_no = p_sq_no;

    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_buy_item_d(p_member_id character varying, p_sq_no integer) OWNER TO postgres;

--
-- TOC entry 273 (class 1255 OID 16563)
-- Name: sp_buy_item_history_r(character varying, character varying, date, date); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_buy_item_history_r(p_member_id character varying DEFAULT ''::character varying, p_member_nm character varying DEFAULT NULL::character varying, p_fbuy_dt date DEFAULT NULL::date, p_tbuy_dt date DEFAULT NULL::date) RETURNS TABLE(member_id character varying, sq_no integer, user_nm character varying, co_nm character varying, bs_no character varying, buy_dt date, itm_cd character varying, itm_nm character varying, area numeric, itm_qty numeric, itm_area numeric, add_area numeric, sum_area numeric, itm_amt numeric, dis_rt numeric, buy_amt numeric, vat_amt numeric, sum_amt numeric, end_dt date, ok_yn character, rmks character varying)
    LANGUAGE plpgsql
    AS $$
/*
 설  명 	: 상품등록화면에서 buy_item 테이블 내용 조회 member_id , buy_dt 변수 필수
 작성자 	: 권오재
 작성일	: 2026-01-14
 비	고 	: 최초작성
*/
BEGIN
    RETURN QUERY
    SELECT 
        a.member_id,      	-- 회원ID
        a.sq_no,          	-- 순번
		b.member_nm::character varying  as user_nm,		-- 구매자
		b.co_nm,			-- 회사명
		b.bs_no,			-- 사업자번호
        a.buy_dt,         	-- 구매일자
		a.itm_cd::character varying,         	-- 상품코드
        c.itm_nm::character varying as itm_nm,	-- 상품명
		c.area::NUMERIC as area,			 	-- 상품면적
		a.itm_qty::NUMERIC,  -- 수량
        a.itm_area::NUMERIC, -- 면적
        a.add_area::NUMERIC, -- 추가면적
        a.sum_area::NUMERIC, -- 합계면적
        a.itm_amt::NUMERIC,  -- 단가
        a.dis_rt::NUMERIC,   -- 할인율
        a.buy_amt::NUMERIC,  -- 공급금액
        a.vat_amt::NUMERIC,  -- 부가세
        a.sum_amt::NUMERIC,  -- 합계금액
        a.end_dt,         -- 만료일
        a.ok_yn,          -- 승인여부
        a.rmks            -- 비고
    FROM kngil.buy_item a									-- 구매정보
	left join kngil.members b on a.member_id = b.member_id 	-- 회원정보
	left join kngil.item c on a.itm_cd = c.itm_cd 				-- 상품정보
    WHERE 1=1
      -- 회원ID 검색 
    AND (a.member_id = p_member_id OR p_member_id = '')
      -- 구매일자 기간 검색
	AND (a.buy_dt >= p_fbuy_dt OR p_fbuy_dt IS NULL)
	AND (a.buy_dt <= p_tbuy_dt OR p_tbuy_dt IS NULL)
	AND (
    p_member_nm IS NULL
 OR p_member_nm = ''
 OR b.co_nm ILIKE '%' || p_member_nm || '%'
 OR b.member_nm ILIKE '%' || p_member_nm || '%'
)
    ORDER BY a.member_id,a.sq_no DESC;
END;
$$;


ALTER FUNCTION kngil.sp_buy_item_history_r(p_member_id character varying, p_member_nm character varying, p_fbuy_dt date, p_tbuy_dt date) OWNER TO postgres;

--
-- TOC entry 275 (class 1255 OID 16575)
-- Name: sp_buy_item_i(character varying, date, character, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, date, character, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_buy_item_i(p_member_id character varying, p_buy_dt date, p_itm_cd character, p_itm_qty numeric, p_itm_area numeric, p_add_area numeric, p_sum_area numeric, p_itm_amt numeric, p_dis_rt numeric, p_buy_amt numeric, p_vat_amt numeric, p_sum_amt numeric, p_end_dt date, p_ok_yn character, p_rmks character varying, p_cid character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$
DECLARE
/*
 설  명 	: 서비스 구매정보  isnert 프로시져 sq_no 계산하여 적용. 
 작성자 	: 권오재
 작성일	: 2026-01-14
 비	고 	: 최초작성
 	p_itm_cd character,		-- 상품코드
	p_itm_qty numeric,		-- 수량
	p_itm_area numeric,		-- 적용면적
	p_add_area numeric,		-- 추가면적
	p_sum_area numeric,		-- 합계면적
	p_itm_amt numeric,		-- 상품단가
	p_dis_rt numeric,		-- 할인율
	p_buy_amt numeric,		-- 공금금액
	p_vat_amt numeric,		-- 부가세
	p_sum_amt numeric,		-- 합계금액
	p_end_dt date,			-- 만료일자
	p_ok_yn character,		-- 승인여부
	p_rmks character varying,-- 비고
*/
    v_next_sq_no INTEGER;
BEGIN
    -- 해당 회원의 다음 순번(sq_no) 계산
    SELECT COALESCE(MAX(sq_no), 0) + 1 INTO v_next_sq_no 
    FROM kngil.buy_item 
    WHERE member_id = p_member_id;

    -- 데이터 삽입
    INSERT INTO kngil.buy_item (
        member_id, sq_no, buy_dt, itm_cd, itm_qty, itm_area, 
        add_area, sum_area, itm_amt, dis_rt, buy_amt, 
        vat_amt, sum_amt, end_dt, ok_yn, rmks, 
        cid, cdt, mid, mdt
    ) VALUES (
        p_member_id, v_next_sq_no, p_buy_dt, p_itm_cd, p_itm_qty, p_itm_area,
        p_add_area, p_sum_area, p_itm_amt, p_dis_rt, p_buy_amt,
        p_vat_amt, p_sum_amt, p_end_dt, p_ok_yn, p_rmks,
        p_cid, CURRENT_TIMESTAMP, p_cid, CURRENT_TIMESTAMP
    );

    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_buy_item_i(p_member_id character varying, p_buy_dt date, p_itm_cd character, p_itm_qty numeric, p_itm_area numeric, p_add_area numeric, p_sum_area numeric, p_itm_amt numeric, p_dis_rt numeric, p_buy_amt numeric, p_vat_amt numeric, p_sum_amt numeric, p_end_dt date, p_ok_yn character, p_rmks character varying, p_cid character varying) OWNER TO postgres;

--
-- TOC entry 274 (class 1255 OID 16702)
-- Name: sp_buy_item_r(character varying, date); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_buy_item_r(p_member_id character varying DEFAULT ''::character varying, p_buy_dt date DEFAULT NULL::date) RETURNS TABLE(member_id character varying, sq_no integer, buy_dt date, itm_cd character, itm_nm character varying, itm_qty numeric, itm_area numeric, add_area numeric, sum_area numeric, itm_amt numeric, dis_rt numeric, buy_amt numeric, vat_amt numeric, sum_amt numeric, end_dt date, ok_yn character, rmks character varying)
    LANGUAGE plpgsql
    AS $$
/*
 설  명 	: 상품등록화면에서 buy_item 테이블 내용 조회 member_id , buy_dt 변수 필수
 작성자 	: 권오재
 작성일	: 2026-01-14
 비	고 	: 최초작성
*/
BEGIN
    RETURN QUERY
    SELECT 
        a.member_id,      -- 회원ID
        a.sq_no,          -- 순번
        a.buy_dt,         -- 구매일자
        a.itm_cd,         -- 상품코드
		b.itm_nm::character varying,		  -- 상품명
        a.itm_qty::NUMERIC,  -- 수량
        a.itm_area::NUMERIC, -- 면적
        a.add_area::NUMERIC, -- 추가면적
        a.sum_area::NUMERIC, -- 합계면적
        a.itm_amt::NUMERIC,  -- 단가
        a.dis_rt::NUMERIC,   -- 할인율
        a.buy_amt::NUMERIC,  -- 공급금액
        a.vat_amt::NUMERIC,  -- 부가세
        a.sum_amt::NUMERIC,  -- 합계금액
        a.end_dt,         -- 만료일
        a.ok_yn,          -- 승인여부
        a.rmks            -- 비고
    FROM kngil.buy_item a
	left join kngil.item b on a.itm_cd = b.itm_cd	-- 상품코드
    WHERE 1=1
      -- 회원ID 검색 (회원ID 필수)
      AND a.member_id = p_member_id
      -- 구매일자 검색(필수)
      AND a.buy_dt = p_buy_dt
    ORDER BY a.sq_no DESC;
END;
$$;


ALTER FUNCTION kngil.sp_buy_item_r(p_member_id character varying, p_buy_dt date) OWNER TO postgres;

--
-- TOC entry 256 (class 1255 OID 16549)
-- Name: sp_buy_item_u(character varying, integer, date, character, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, numeric, date, character, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_buy_item_u(p_member_id character varying, p_sq_no integer, p_buy_dt date, p_itm_cd character, p_itm_qty numeric, p_itm_area numeric, p_add_area numeric, p_sum_area numeric, p_itm_amt numeric, p_dis_rt numeric, p_buy_amt numeric, p_vat_amt numeric, p_sum_amt numeric, p_end_dt date, p_ok_yn character, p_rmks character varying, p_mid character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$

BEGIN
    UPDATE kngil.buy_item
    SET 
        buy_dt = p_buy_dt,		-- 구매일자
        itm_cd = p_itm_cd,		-- 상품코드
        itm_qty = p_itm_qty,	-- 수량
        itm_area = p_itm_area,	-- 기본면적
        add_area = p_add_area,	-- 추가면적
        sum_area = p_sum_area,	-- 합계면적
        itm_amt = p_itm_amt,	-- 단가
        dis_rt = p_dis_rt,		-- 할인율
        buy_amt = p_buy_amt,	-- 공급금액
        vat_amt = p_vat_amt,	-- 부가세
        sum_amt = p_sum_amt,	-- 구매금액
        end_dt = p_end_dt,		-- 만료일자
        ok_yn = p_ok_yn,		-- 확정	[Y업데이트 시 members 테이블의 구매면적에 합산 N 업데이트시 차감 트리거 적용]
        rmks = p_rmks,			-- 비고
        mid = p_mid,           -- 수정자 ID 반영
        mdt = CURRENT_TIMESTAMP -- 수정일시 자동 기록
    WHERE member_id = p_member_id 
      AND sq_no = p_sq_no;

    IF NOT FOUND THEN
        RETURN 'ERROR: 수정할 구매 정보를 찾을 수 없습니다.';
    END IF;

    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_buy_item_u(p_member_id character varying, p_sq_no integer, p_buy_dt date, p_itm_cd character, p_itm_qty numeric, p_itm_area numeric, p_add_area numeric, p_sum_area numeric, p_itm_amt numeric, p_dis_rt numeric, p_buy_amt numeric, p_vat_amt numeric, p_sum_amt numeric, p_end_dt date, p_ok_yn character, p_rmks character varying, p_mid character varying) OWNER TO postgres;

--
-- TOC entry 238 (class 1255 OID 16685)
-- Name: sp_fa_comments_d(integer); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_fa_comments_d(p_id integer) RETURNS boolean
    LANGUAGE plpgsql
    AS $$
BEGIN
    -- fa_id를 조건으로 데이터를 삭제합니다.
    DELETE FROM kngil.fa_comments 
    WHERE fa_id = p_id;

    -- 삭제된 행이 있으면 true, 없으면 false를 반환합니다.
    RETURN FOUND;
END;
$$;


ALTER FUNCTION kngil.sp_fa_comments_d(p_id integer) OWNER TO postgres;

--
-- TOC entry 271 (class 1255 OID 16684)
-- Name: sp_fa_comments_i(text, text, integer, character, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_fa_comments_i(p_subject text, p_content text, p_sq_no integer, p_use_yn character, p_cid character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$
DECLARE
    v_new_id integer;
BEGIN
    -- fa_id는 GENERATED ALWAYS이므로 INSERT 문에서 제외하여 DB가 자동 생성하게 함
    INSERT INTO kngil.fa_comments (
        fa_subject, 
        fa_content, 
        sq_no, 
        use_yn,
		cid,
		cdt
    ) VALUES (
        p_subject, 
        p_content, 
        p_sq_no, 
        p_use_yn,
		p_cid,
		CURRENT_TIMESTAMP
    )
    RETURNING fa_id INTO v_new_id;

    RETURN v_new_id;
END;
$$;


ALTER FUNCTION kngil.sp_fa_comments_i(p_subject text, p_content text, p_sq_no integer, p_use_yn character, p_cid character varying) OWNER TO postgres;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- TOC entry 235 (class 1259 OID 16671)
-- Name: fa_comments; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.fa_comments (
    fa_id integer NOT NULL,
    fa_subject text,
    fa_content text,
    sq_no integer,
    use_yn character(1),
    cid character varying(20),
    cdt timestamp with time zone,
    mid character varying(20),
    mdt timestamp with time zone
);


ALTER TABLE kngil.fa_comments OWNER TO postgres;

--
-- TOC entry 268 (class 1255 OID 16686)
-- Name: sp_fa_comments_r(); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_fa_comments_r() RETURNS SETOF kngil.fa_comments
    LANGUAGE plpgsql
    AS $$
BEGIN
    -- 사용 여부(use_yn)가 'Y'인 데이터를 순번(sq_no) 오름차순으로 조회합니다.
    RETURN QUERY 
    SELECT * FROM kngil.fa_comments 
    WHERE use_yn = 'Y'
    ORDER BY sq_no ASC, fa_id DESC;
END;
$$;


ALTER FUNCTION kngil.sp_fa_comments_r() OWNER TO postgres;

--
-- TOC entry 272 (class 1255 OID 16683)
-- Name: sp_fa_comments_u(integer, text, text, integer, character, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_fa_comments_u(p_id integer, p_subject text, p_content text, p_sq_no integer, p_use_yn character, p_mid character varying) RETURNS boolean
    LANGUAGE plpgsql
    AS $$
BEGIN
    -- 고유 ID(fa_id)를 조건으로 데이터를 업데이트합니다.
    UPDATE kngil.fa_comments 
    SET 
        fa_subject = p_subject,
        fa_content = p_content,
        sq_no      = p_sq_no,
        use_yn     = p_use_yn,
		mid		   = p_mid,
		mdt		   = CURRENT_TIMESTAMP
    WHERE 
        fa_id = p_id;

    -- 업데이트된 행이 있는지 확인하여 반환합니다.
    RETURN FOUND;
END;
$$;


ALTER FUNCTION kngil.sp_fa_comments_u(p_id integer, p_subject text, p_content text, p_sq_no integer, p_use_yn character, p_mid character varying) OWNER TO postgres;

--
-- TOC entry 261 (class 1255 OID 16568)
-- Name: sp_item_d(character); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_item_d(p_itm_cd character) RETURNS text
    LANGUAGE plpgsql
    AS $$
DECLARE
    v_buy_count INTEGER;
BEGIN
    -- 1. 무결성 체크: 이 상품을 구매한 내역(buy_item)이 있는지 확인
    SELECT COUNT(*) INTO v_buy_count 
    FROM kngil.buy_item 
    WHERE itm_cd = p_itm_cd;

    IF v_buy_count > 0 THEN
        RETURN 'ERROR: 구매 내역이 존재하는 상품은 삭제할 수 없습니다. (사용여부를 N으로 변경하세요)';
    END IF;

    -- 2. 상품 존재 여부 확인
    IF NOT EXISTS (SELECT 1 FROM kngil.item WHERE itm_cd = p_itm_cd) THEN
        RETURN 'ERROR: 삭제할 상품 정보를 찾을 수 없습니다.';
    END IF;

    -- 3. 삭제 실행
    DELETE FROM kngil.item
    WHERE itm_cd = p_itm_cd;

    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    -- 오류 발생 시 자동 롤백됩니다.
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_item_d(p_itm_cd character) OWNER TO postgres;

--
-- TOC entry 263 (class 1255 OID 16576)
-- Name: sp_item_i(character, character varying, numeric, numeric, character, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_item_i(p_itm_cd character, p_itm_nm character varying, p_area numeric, p_itm_amt numeric, p_use_yn character, p_rmks character varying, p_cid character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$
DECLARE
    v_count INTEGER;
BEGIN
    -- 1. 상품코드(itm_cd) 중복 체크
    SELECT COUNT(*) INTO v_count FROM kngil.item WHERE itm_cd = p_itm_cd;
    
    IF v_count > 0 THEN
        RETURN 'ERROR: 이미 존재하는 상품코드입니다.';
    END IF;

    -- 2. 데이터 삽입 
    INSERT INTO kngil.item (
        itm_cd, itm_nm, area, itm_amt, 
        use_yn, rmks, 
        cid, cdt, mid, mdt
    ) VALUES (
        p_itm_cd, p_itm_nm, p_area, p_itm_amt,
        p_use_yn, p_rmks,
        p_cid, CURRENT_TIMESTAMP, p_cid, CURRENT_TIMESTAMP
    );

    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    -- 트랜잭션 원자성에 의해 오류 발생 시 자동 롤백됩니다.
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_item_i(p_itm_cd character, p_itm_nm character varying, p_area numeric, p_itm_amt numeric, p_use_yn character, p_rmks character varying, p_cid character varying) OWNER TO postgres;

--
-- TOC entry 252 (class 1255 OID 16572)
-- Name: sp_item_r(); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_item_r() RETURNS TABLE(itm_cd character varying, itm_nm character varying, area numeric, itm_amt numeric, use_yn character varying, rmks character varying, cid character varying, cdt timestamp without time zone, mid character varying, mdt timestamp without time zone)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
    SELECT 
        a.itm_cd::VARCHAR,    -- bpchar 에러 방지를 위한 명시적 변환
        a.itm_nm::VARCHAR,
        a.area::NUMERIC,      -- DECIMAL을 NUMERIC으로 맞춤
        a.itm_amt::NUMERIC,
        a.use_yn::VARCHAR,
        a.rmks::VARCHAR,
        a.cid::VARCHAR,
        a.cdt,
        a.mid::VARCHAR,
        a.mdt
    FROM kngil.item a
    ORDER BY a.itm_cd ASC;
END;
$$;


ALTER FUNCTION kngil.sp_item_r() OWNER TO postgres;

--
-- TOC entry 260 (class 1255 OID 16567)
-- Name: sp_item_u(character, character varying, numeric, numeric, character, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_item_u(p_itm_cd character, p_itm_nm character varying, p_area numeric, p_itm_amt numeric, p_use_yn character, p_rmks character varying, p_mid character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$
BEGIN
    -- 1. 데이터 업데이트 수행
    UPDATE kngil.item
    SET 
        itm_nm = p_itm_nm,		-- 상품명
        area = p_area,			-- 면적
        itm_amt = p_itm_amt,	-- 상품금액
        use_yn = p_use_yn,		-- 사용여부
        rmks = p_rmks,			-- 비고
        mid = p_mid,           -- 수정자 기록
        mdt = CURRENT_TIMESTAMP -- 수정일시 기록
    WHERE itm_cd = p_itm_cd;

    -- 2. 업데이트된 행이 있는지 확인
    IF NOT FOUND THEN
        RETURN 'ERROR: 수정할 상품코드를 찾을 수 없습니다.';
    END IF;

    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_item_u(p_itm_cd character, p_itm_nm character varying, p_area numeric, p_itm_amt numeric, p_use_yn character, p_rmks character varying, p_mid character varying) OWNER TO postgres;

--
-- TOC entry 266 (class 1255 OID 16580)
-- Name: sp_member_i(character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_member_i(p_co_bc character varying, p_member_id character varying, p_user_pw character varying, p_member_nm character varying, p_email character varying, p_tel_no character varying, p_co_nm character varying, p_dept_nm character varying, p_cid character varying) RETURNS character varying
    LANGUAGE plpgsql
    AS $$
  -- 값을 반환하기 위해 VARCHAR로 수정
	DECLARE v_user_exists INTEGER; 
			v_itm_area numeric; -- 무료 제공 면적
			v_end_dt timestamp;
/*
회원가입시 사용되는 함수
*/
BEGIN

	v_end_dt := (date_trunc('year', CURRENT_TIMESTAMP) + INTERVAL '1 year - 1 day')::timestamp;
	
	-- [0] 무료제공면적
	SELECT a.area INTO v_itm_area 
	FROM kngil.item a
	WHERE a.itm_cd = 'A0000'; -- 회원가입제공 면적
	

	-- [1] 중복 아이디 체크 (대소문자 무시)
	SELECT COUNT(*) INTO v_user_exists 
	FROM kngil.users 
	WHERE LOWER(user_id) = LOWER(p_member_id); -- 양쪽 모두 소문자로 변환하여 비교
	
	IF v_user_exists > 0 THEN
	    RETURN 'ERROR: 이미 존재하는 아이디입니다. (대소문자 포함)';
	END IF;
		
	-- 회원 테이블 저장
    INSERT INTO kngil.members (
        member_id, member_nm, co_bc, bs_no, co_nm, 
        co_tel, tel_no, email, join_dt, buy_area, 
        use_area, stat_bc, memo, cid, cdt
    ) VALUES (
          p_member_id			-- 회원ID
		, p_member_nm			-- 성명
		, p_co_bc				-- 법인구분 case 구문 사용해야 할 수 있음. 변수가 라디오 버튼
		, null					-- 사업자번호
		, p_co_nm				-- 회사명
		, null					-- 회사번호
		, p_tel_no				-- 휴대폰
		, p_email				-- 이메일	
		, CURRENT_DATE			-- 가입일자
		, null					-- 구매면적
		, null					-- 사용면적
		, 'SA100100'			-- 회원상태 : 사용중
		, null					-- 메모
		, p_cid					-- 생성자
		, CURRENT_TIMESTAMP		-- 생성일
    );

	-- 회원가입 시 사용자정보 권한 "메인" 생성
	INSERT INTO kngil.users (
        member_id, user_id, user_pw, user_nm, dept_nm,
        posit_nm, tel_no, email, auth_bc, use_yn,
        rmks, cid, cdt
    ) VALUES (
          p_member_id		-- 회원ID
		, p_member_id		-- 유저ID : 최초 회원의 ID와 동일
		, p_user_pw			-- 로그인 PW
		, p_member_nm		-- 성명
		, p_dept_nm			-- 부서
		, null				-- 직위
		, p_tel_no			-- 전화번호
		, p_email			-- 이메일
		, 'BS100300'		-- 권한관리 : 메인
		, 'Y'				-- 사용여부 Y 고정
		, null				-- 비고
		, p_cid					-- 생성자
		, CURRENT_TIMESTAMP		-- 생성일
  		  );	



	-- 사용면적 제공	(구매이력 테이블에 초기제공상품 정보 등록 금액 0원)
			PERFORM kngil.sp_buy_item_i(
			        p_member_id::character varying,          -- 1. p_member_id
			        CURRENT_DATE::date,						-- 2.가입일
			        'A0000'::character varying,              -- 3. p_itm_cd
			        1::integer,                              -- 4. p_itm_qty
			        v_itm_area::numeric,                     -- 5. p_itm_area
			        0::numeric,                              -- 6. p_add_area
			        v_itm_area::numeric,                     -- 7. p_sum_area
			        0::numeric,                              -- 8. p_itm_amt
			        0::numeric,                              -- 9. p_dis_rt
			        0::numeric,                              -- 10. p_buy_amt
			        0::numeric,                              -- 11. p_vat_amt
			        0::numeric,                              -- 12. p_sum_amt
			        v_end_dt::date,   						-- 13. p_end_dt
			        'Y'::character varying,                  -- 14. p_ok_yn
			        '최초 가입 제공'::character varying,      -- 15. p_rmks
			        p_cid::character varying                 -- 16. p_cid
			    );
	
    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_member_i(p_co_bc character varying, p_member_id character varying, p_user_pw character varying, p_member_nm character varying, p_email character varying, p_tel_no character varying, p_co_nm character varying, p_dept_nm character varying, p_cid character varying) OWNER TO postgres;

--
-- TOC entry 259 (class 1255 OID 16565)
-- Name: sp_member_sys_r(character varying, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_member_sys_r(p_co_nm character varying DEFAULT NULL::character varying, p_rem_area character varying DEFAULT NULL::character varying, p_stat_bc character varying DEFAULT NULL::character varying) RETURNS TABLE(member_id character varying, user_id character varying, user_nm character varying, tel_no character varying, email character varying, co_nm character varying, bs_no character varying, join_dt date, user_y numeric, buy_area numeric, use_area numeric, rem_area numeric, stat_bc character varying, memo text)
    LANGUAGE plpgsql
    AS $$
/*
 설  명 	: 큰길회원 list 관리자 화면 조회, 사용자의 권한구분이 "메인" 인 인원 
 작성자 	: 권오재
 작성일	: 2026-01-14
 비	고 	: 최초작성
*/
BEGIN
    RETURN QUERY
    SELECT 
	    a.member_id,	-- 회원ID
		b.user_id,		-- 사용자ID
		b.user_nm, 		-- 사용자이름
		b.tel_no,		-- 사용자연락처
		b.email, 		-- 사용자이메일
		a.co_nm,		-- 회사명
		a.bs_no,		-- 사업자번호
		a.join_dt, 		-- 가입일자
		(SELECT COUNT(*) 
		FROM kngil.users x
		left join kngil.code_detail y on x.auth_bc = y.base_cd 
		WHERE x.member_id = a.member_id 
		and y.m1 = '1'
		and x.use_yn='Y')::numeric as user_y,	-- 사용자수
		a.buy_area,		-- 구매면적
		a.use_area,		-- 사용면적
		a.buy_area - a.use_area  as rem_area,	-- 잔여면적
		a.stat_bc,		-- 회원상태
		a.memo 
    FROM kngil.members a									-- 회원정보
	left join kngil.users b on a.member_id = b.member_id and b.auth_bc = 'BS100300' -- 권한구분이 메인
    WHERE 1=1
	AND (a.co_nm LIKE '%' || p_co_nm || '%' OR p_co_nm = '')
	AND (p_rem_area = '' OR p_rem_area::NUMERIC >= (a.buy_area - a.use_area))
	and (p_stat_bc ='' OR a.stat_bc = p_stat_bc)
    ORDER BY a.join_dt DESC;
END;
$$;


ALTER FUNCTION kngil.sp_member_sys_r(p_co_nm character varying, p_rem_area character varying, p_stat_bc character varying) OWNER TO postgres;

--
-- TOC entry 264 (class 1255 OID 16578)
-- Name: sp_member_sys_u(character varying, character varying, character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_member_sys_u(p_member_id character varying, p_tel_no character varying, p_email character varying, p_bs_no character varying, p_co_nm character varying, p_mid character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$
BEGIN
    -- 1. 회원정보 업데이트
    UPDATE kngil.members
    SET tel_no = p_tel_no,
        email  = p_email,
        bs_no  = p_bs_no,
        co_nm  = p_co_nm,
        mid    = p_mid,
        mdt    = CURRENT_TIMESTAMP
    WHERE member_id = p_member_id;

    -- 2. 유저정보 업데이트 
    UPDATE kngil.users
    SET tel_no = p_tel_no,
        email  = p_email,
        mid    = p_mid,
        mdt    = CURRENT_TIMESTAMP
    WHERE member_id = p_member_id
      AND auth_bc = 'BS100300'; -- b.auth_bc 대신 컬럼명 직접 사용

    -- 결과 확인 (하나라도 수정되었다면 성공)
    IF NOT FOUND THEN
        RETURN 'ERROR: 수정할 회원 또는 유저 정보를 찾을 수 없습니다.';
    else
        RETURN 'SUCCESS';
    END IF;

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_member_sys_u(p_member_id character varying, p_tel_no character varying, p_email character varying, p_bs_no character varying, p_co_nm character varying, p_mid character varying) OWNER TO postgres;

--
-- TOC entry 277 (class 1255 OID 16707)
-- Name: sp_use_history(character varying, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_use_history(p_member_id character varying, p_user_nm character varying DEFAULT NULL::character varying, p_dept_nm character varying DEFAULT NULL::character varying) RETURNS TABLE(member_id character varying, use_dt date, user_id character varying, sq_no integer, user_nm character varying, dept_nm character varying, posit_nm character varying, use_yn character varying, use_area numeric, ser_bc character varying, cdt timestamp without time zone)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
    SELECT 
        b.member_id::character varying,
        a.use_dt,                   -- DATE 타입
        a.user_id::character varying,
        a.sq_no,
        b.user_nm::character varying,
        b.dept_nm::character varying,
        b.posit_nm::character varying,
        b.use_yn::character varying,
        a.use_area,
        kngil.fn_base_nm(a.ser_bc)::character varying as ser_bc,
        a.cdt
    FROM kngil.use_history a
    INNER JOIN kngil.users b ON a.user_id = b.user_id
    WHERE b.member_id = p_member_id
      -- 이름 검색 (값이 있을 때만 LIKE)
      AND (NULLIF(p_user_nm, '') IS NULL OR b.user_nm LIKE '%' || p_user_nm || '%')
      -- 부서 검색 (값이 있을 때만 LIKE)
      AND (NULLIF(p_dept_nm, '') IS NULL OR b.dept_nm LIKE '%' || p_dept_nm || '%')
    ORDER BY a.use_dt DESC, a.sq_no DESC;
END;
$$;


ALTER FUNCTION kngil.sp_use_history(p_member_id character varying, p_user_nm character varying, p_dept_nm character varying) OWNER TO postgres;

--
-- TOC entry 270 (class 1255 OID 16637)
-- Name: sp_user_auth_tran(character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_user_auth_tran(p_tuser_id character varying, p_mid character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$
DECLARE
	v_fuser_id character varying; 	-- 위임 주는 사람 (From, 기존 메인)
    v_member_id character varying;	-- 위임 받는 사람의 회원ID
BEGIN
	-- 1. 위임 받을 사용자 정보 조회 (한 번에 조회)
    SELECT member_id INTO v_member_id
    FROM kngil.users 
    WHERE user_id = p_tuser_id;

	-- 사용자가 없는 경우 처리
    IF v_member_id IS NULL THEN
        RETURN 'ERROR: 수정할 사용자를 찾을 수 없습니다.';
    END IF;
	
	-- 2. 해당 멤버의 기존 메인 사용자(BS100300) 찾기
    SELECT user_id INTO v_fuser_id
    FROM kngil.users 
    WHERE member_id = v_member_id
    AND auth_bc = 'BS100300' 
    AND use_yn = 'Y';

	-- 메인 사용자가 없는 경우 (위임해 줄 대상이 없음)
    IF v_fuser_id IS NULL THEN
        RETURN 'ERROR: 위임할 메인 사용자를 찾을 수 없습니다.';
    END IF;

	-- 본인에게 위임하는 경우 방지 (필요 시)
    IF v_fuser_id = p_tuser_id THEN
        RETURN 'ERROR: 본인에게는 권한을 위임할 수 없습니다.';
    END IF;
	
	-- 3. 권한 위임받는 사람 업데이트 (서브 -> 메인)
    UPDATE kngil.users
    SET 
        auth_bc = 'BS100300',
        rmks = '권한위임[' || v_fuser_id || ']', -- + 대신 || 사용
        mid = p_mid,
        mdt = CURRENT_TIMESTAMP
    WHERE user_id = p_tuser_id;

	-- 4. 권한 위임주는 사람 업데이트 (메인 -> 서브)
    UPDATE kngil.users
    SET 
        auth_bc = 'BS100400',
        rmks = '권한위임[' || p_tuser_id || ']', -- + 대신 || 사용
        mid = p_mid,
        mdt = CURRENT_TIMESTAMP
    WHERE user_id = v_fuser_id;
	  
    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_user_auth_tran(p_tuser_id character varying, p_mid character varying) OWNER TO postgres;

--
-- TOC entry 278 (class 1255 OID 16711)
-- Name: sp_user_end(character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_user_end(p_user_id character varying) RETURNS void
    LANGUAGE plpgsql
    AS $$
DECLARE 
    v_member_id character varying;
	v_auth_bc	character varying;
BEGIN
	-- 1. 유저 ID로 소속 멤버 ID와 권한 코드를 한 번에 조회
    SELECT a.member_id, a.auth_bc 
    INTO v_member_id, v_auth_bc 
    FROM kngil.users a 
    WHERE a.user_id = p_user_id;
	
    -- 2. 권한 코드에 따른 조건 분기
    IF v_auth_bc = 'BS100300' THEN 
        -- [메인 관리자 권한] 해당 멤버 전체 탈퇴 및 소속 유저 전원 중지
        
        -- 멤버 테이블 업데이트 (탈퇴 처리)
        UPDATE kngil.members
        SET stat_bc = 'SA100900',
            mid     = p_user_id,
            mdt     = CURRENT_TIMESTAMP
        WHERE member_id = v_member_id;

        -- 해당 멤버에 속한 모든 유저 사용 안함 처리
        UPDATE kngil.users
        SET use_yn  = 'N',
            mid     = p_user_id,
            mdt     = CURRENT_TIMESTAMP
        WHERE member_id = v_member_id;

    ELSE 
        -- [일반 권한] 해당 유저 개인만 사용 안함 처리
        
        UPDATE kngil.users
        SET use_yn  = 'N',
            mid     = p_user_id,
            mdt     = CURRENT_TIMESTAMP
        WHERE user_id = p_user_id;

    END IF;

END;
$$;


ALTER FUNCTION kngil.sp_user_end(p_user_id character varying) OWNER TO postgres;

--
-- TOC entry 254 (class 1255 OID 16546)
-- Name: sp_users_d(character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_users_d(p_member_id character varying, p_user_id character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$
DECLARE
/*
 설  명 	: users 테이블 삭제 프로시져 
 작성자 	: 권오재
 작성일	: 2026-01-14
 비	고 	: 최초작성
*/
    v_exists INTEGER;
BEGIN
    -- 1. 삭제 대상이 존재하는지 먼저 확인
    SELECT COUNT(*) INTO v_exists 
    FROM kngil.users 
    WHERE member_id = p_member_id AND user_id = p_user_id;

    IF v_exists = 0 THEN
        RETURN 'ERROR: 삭제할 사용자를 찾을 수 없습니다.';
    END IF;

    -- 2. 데이터 삭제 실행
    DELETE FROM kngil.users
    WHERE member_id = p_member_id 
    AND user_id = p_user_id;

    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    -- 트랜잭션 원자성에 의해 에러 발생 시 자동 롤백됩니다.
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_users_d(p_member_id character varying, p_user_id character varying) OWNER TO postgres;

--
-- TOC entry 265 (class 1255 OID 16577)
-- Name: sp_users_i(character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_users_i(p_member_id character varying, p_user_id character varying, p_user_pw character varying, p_user_nm character varying, p_dept_nm character varying, p_posit_nm character varying, p_tel_no character varying, p_email character varying, p_auth_bc character varying, p_use_yn character varying, p_rmks character varying, p_cid character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$
/*
 설  명 	:	users table 데이터 생성 프로시져  "회원 관리자 페이지" 사용자 추가
 작성자 	: 권오재
 작성일	: 2026-01-14
 비	고 	: 최초작성
*/
DECLARE
    v_count INTEGER;
  -- 값을 반환하기 위해 VARCHAR로 수정
	DECLARE v_user_exists INTEGER; 
BEGIN

	-- [1] 중복 아이디 체크 (대소문자 무시)
		SELECT COUNT(*) INTO v_user_exists 
		FROM kngil.users 
		WHERE LOWER(user_id) = LOWER(p_user_id); -- 양쪽 모두 소문자로 변환하여 비교

	IF v_user_exists > 0 THEN
		RETURN 'ERROR: 이미 존재하는 아이디입니다.';
	END IF;
	
    -- 1. 부모 테이블(members)에 member_id가 존재하는지 먼저 체크
    SELECT COUNT(*) INTO v_count FROM kngil.members WHERE member_id = p_member_id;
    
    IF v_count = 0 THEN
        RETURN 'ERROR: 존재하지 않는 회원ID(member_id)입니다.';
    END IF;

    -- 2. 사용자ID(user_id) 중복 체크
    SELECT COUNT(*) INTO v_count FROM kngil.users WHERE user_id = p_user_id;
    
    IF v_count > 0 THEN
        RETURN 'ERROR: 이미 존재하는 사용자ID입니다.';
    END IF;

    -- 3. 데이터 삽입 
    INSERT INTO kngil.users (
        member_id, user_id, user_pw, user_nm, 
        dept_nm, posit_nm, tel_no, email, 
        auth_bc, use_yn, rmks, 
        cid, cdt, mid, mdt
    ) VALUES (
        p_member_id, p_user_id, p_user_pw, p_user_nm,
        p_dept_nm, p_posit_nm, p_tel_no, p_email,
        p_auth_bc, p_use_yn, p_rmks,
        p_cid, CURRENT_TIMESTAMP, p_cid, CURRENT_TIMESTAMP
    );

    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_users_i(p_member_id character varying, p_user_id character varying, p_user_pw character varying, p_user_nm character varying, p_dept_nm character varying, p_posit_nm character varying, p_tel_no character varying, p_email character varying, p_auth_bc character varying, p_use_yn character varying, p_rmks character varying, p_cid character varying) OWNER TO postgres;

--
-- TOC entry 258 (class 1255 OID 16590)
-- Name: sp_users_my_history(character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_users_my_history(p_user_id character varying) RETURNS TABLE(use_dt date, user_nm character varying, use_area numeric, ser_bc character varying, cdt timestamp without time zone)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
    SELECT 
        h.use_dt,
        u.user_nm,
        h.use_area,
        kngil.fn_base_nm(h.ser_bc) as ser_bc,
        h.cdt
    FROM kngil.use_history h
    JOIN kngil.users u ON h.user_id = u.user_id
    WHERE h.user_id = p_user_id
    ORDER BY h.use_dt DESC, h.sq_no DESC;
END;
$$;


ALTER FUNCTION kngil.sp_users_my_history(p_user_id character varying) OWNER TO postgres;

--
-- TOC entry 269 (class 1255 OID 16633)
-- Name: sp_users_my_r(character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_users_my_r(p_user_id character varying) RETURNS TABLE(co_bc character varying, user_id character varying, user_pw character varying, user_nm character varying, email character varying, tel_no character varying, co_nm character varying, dept_nm character varying, tot_use numeric, year_use numeric)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
    SELECT 
         b.co_bc		-- 법인구분
	   , a.user_id		-- ID
	   , a.user_pw		-- 비밀번호
	   , a.user_nm		-- 성명
	   , a.email		-- 이메일
	   , a.tel_no		-- 연락처
	   , b.co_nm		-- 회사명
	   , a.dept_nm 		-- 부서
	   , (select sum(x.use_area) from kngil.use_history x where a.user_id = x.user_id)::numeric as tot_use	-- 누적 사용량
	   , (select sum(x.use_area) from kngil.use_history x where a.user_id = x.user_id and EXTRACT(YEAR FROM use_dt) = EXTRACT(YEAR FROM CURRENT_DATE))::numeric as year_use	-- 당년 사용량
    FROM kngil.users a
    left JOIN kngil.members b ON a.member_id = b.member_id 
    WHERE a.user_id = p_user_id;
END;
$$;


ALTER FUNCTION kngil.sp_users_my_r(p_user_id character varying) OWNER TO postgres;

--
-- TOC entry 267 (class 1255 OID 16584)
-- Name: sp_users_my_u(character varying, character varying, character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_users_my_u(p_user_id character varying, p_user_pw character varying, p_email character varying, p_tel_no character varying, p_dept_nm character varying, p_mid character varying) RETURNS character varying
    LANGUAGE plpgsql
    AS $$
  -- 값을 반환하기 위해 VARCHAR로 수정
	DECLARE v_user_exists INTEGER; 
/*
내정보 수정 화면에서 사용. 
*/
	
BEGIN

	-- 데이터 업데이트 
    UPDATE kngil.users
    SET 
        user_pw = [REDACTED] ''), user_pw), -- 비밀번호가 빈값이면 기존 유지
        dept_nm = p_dept_nm,	-- 부서명
        tel_no = p_tel_no,		-- 전화번호
        email = p_email,		-- 메일주소
        mid = p_mid,           	-- 수정자 ID 기록
        mdt = CURRENT_TIMESTAMP -- 수정일시 기록
    WHERE 1=1 
    AND user_id = p_user_id;
	
    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_users_my_u(p_user_id character varying, p_user_pw character varying, p_email character varying, p_tel_no character varying, p_dept_nm character varying, p_mid character varying) OWNER TO postgres;

--
-- TOC entry 279 (class 1255 OID 16718)
-- Name: sp_users_r(character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_users_r(p_member_id character varying DEFAULT ''::character varying, p_user_nm character varying DEFAULT ''::character varying, p_dept_nm character varying DEFAULT ''::character varying, p_use_yn character varying DEFAULT ''::character varying) RETURNS TABLE(member_id character varying, users_tot bigint, users_y bigint, term text, tuse_area numeric, tbuy_area numeric, remain_area numeric, user_id character varying, user_pw character varying, user_nm character varying, tel_no character varying, email character varying, dept_nm character varying, use_area numeric, cdt date, use_yn character varying, auth_bc character varying, rmks character varying, itm_nm character varying)
    LANGUAGE plpgsql
    AS $$
/*
 설  명 	: 조회 프로시져 members 기반 users 테이블 조인하여 사용. "회원 관리자 페이지" 조회용
 작성자 	: 권오재
 작성일	: 2026-01-14
 비	고 	: 최초작성
*/
DECLARE 
    v_to_dt character varying; 	-- 유효일자
	v_remain_days integer; 		-- 잔여일 저장 변수
	v_itm_nm character varying;	-- 가입 상품명
BEGIN

    SELECT 
        MAX(end_dt)::text 
		
    INTO 
        v_to_dt
    FROM kngil.buy_item a
    WHERE a.member_id = p_member_id; -- 유효일자 종료일자만 표시 END_DT 가 가장 늦은 날 기준.		


	SELECT  b.itm_nm
	INTO	v_itm_nm
	FROM kngil.buy_item a
	LEFT JOIN kngil.item b ON a.itm_cd = b.itm_cd
	WHERE a.member_id = p_member_id
	AND EXTRACT(YEAR FROM a.end_dt) = EXTRACT(YEAR FROM CURRENT_DATE)
	-- 금액이 큰 순서대로 정렬
	ORDER BY a.itm_amt DESC, a.end_dt DESC
	LIMIT 1;

	
	v_remain_days := COALESCE(v_to_dt::DATE - CURRENT_DATE, 0);


	
    RETURN QUERY
    SELECT 
        -- [회원정보 기반]
        a.member_id,
        (SELECT COUNT(*) FROM kngil.users u WHERE u.member_id = a.member_id) AS users_tot, -- 발급사용자 수
        (SELECT COUNT(*) FROM kngil.users u WHERE u.member_id = a.member_id and u.use_yn='Y') AS users_y, -- 실사용자 수
        --v_fr_dt||' ~ '||v_to_dt AS term, -- 유효일자
		' : '||v_to_dt||' ('||v_remain_days||'일)' AS term, -- 유효일자 종료일자만 표시 END_DT 가 가장 늦은 날 기준.
        a.use_area AS tuse_area,  -- 총 사용면적
        a.buy_area AS tbuy_area,  -- 총 구입면적
        (a.buy_area - a.use_area) AS remain_area, -- 잔여면적 (계산필드)

        -- [사용자정보 기반]
        b.user_id,
        b.user_pw,
        b.user_nm,
        b.tel_no,
        b.email,
        b.dept_nm,
        -- 사용자별 개별 사용량 (use_history 테이블 참조) -- 당해년도기준으로 할지 전체 누적으로 할지 고민 필요.
        COALESCE((SELECT SUM(uh.use_area) FROM kngil.use_history uh WHERE uh.user_id = b.user_id), 0) AS use_area,
        b.cdt::DATE as cdt,
		b.use_yn,	-- 사용여부
        b.auth_bc,	-- 권한구분
        b.rmks,		-- 유저비고
		v_itm_nm::character varying as itm_nm
    FROM kngil.members a
    LEFT JOIN kngil.users b ON a.member_id = b.member_id
    WHERE 1=1
	  AND a.member_id = p_member_id
      AND (b.user_nm LIKE '%' || p_user_nm || '%' OR p_user_nm = '')
      AND (b.dept_nm LIKE '%' || p_dept_nm || '%' OR p_dept_nm = '')
      AND (p_use_yn = '' OR b.use_yn = p_use_yn)
    ORDER BY a.member_id DESC, b.user_id ASC;

END;
$$;


ALTER FUNCTION kngil.sp_users_r(p_member_id character varying, p_user_nm character varying, p_dept_nm character varying, p_use_yn character varying) OWNER TO postgres;

--
-- TOC entry 253 (class 1255 OID 16545)
-- Name: sp_users_u(character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: kngil; Owner: postgres
--

CREATE FUNCTION kngil.sp_users_u(p_member_id character varying, p_user_id character varying, p_user_pw character varying, p_user_nm character varying, p_dept_nm character varying, p_posit_nm character varying, p_tel_no character varying, p_email character varying, p_auth_bc character varying, p_use_yn character varying, p_rmks character varying, p_mid character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$
DECLARE
    v_exists INTEGER;
BEGIN
    -- 1. 해당 사용자가 존재하는지 확인
    SELECT COUNT(*) INTO v_exists 
    FROM kngil.users 
    WHERE member_id = p_member_id AND user_id = p_user_id;

    IF v_exists = 0 THEN
        RETURN 'ERROR: 수정할 사용자를 찾을 수 없습니다.';
    END IF;

    -- 2. 데이터 업데이트 
    UPDATE kngil.users
    SET 
        user_pw = [REDACTED] ''), user_pw), -- 비밀번호가 빈값이면 기존 유지
        user_nm = p_user_nm,	-- 이름
        dept_nm = p_dept_nm,	-- 부서명
        posit_nm = p_posit_nm,	-- 직위
        tel_no = p_tel_no,		-- 전화번호
        email = p_email,		-- 메일주소
        auth_bc = p_auth_bc,	-- 권한구분
        use_yn = p_use_yn,		-- 사용여부
        rmks = p_rmks,			-- 비고
        mid = p_mid,           	-- 수정자 ID 기록
        mdt = CURRENT_TIMESTAMP -- 수정일시 기록
    WHERE member_id = p_member_id 
      AND user_id = p_user_id;

    RETURN 'SUCCESS';

EXCEPTION WHEN OTHERS THEN
    RETURN 'ERROR: ' || SQLERRM;
END;
$$;


ALTER FUNCTION kngil.sp_users_u(p_member_id character varying, p_user_id character varying, p_user_pw character varying, p_user_nm character varying, p_dept_nm character varying, p_posit_nm character varying, p_tel_no character varying, p_email character varying, p_auth_bc character varying, p_use_yn character varying, p_rmks character varying, p_mid character varying) OWNER TO postgres;

--
-- TOC entry 223 (class 1259 OID 16449)
-- Name: buy_item; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.buy_item (
    member_id character varying(20) NOT NULL,
    sq_no integer NOT NULL,
    buy_dt date NOT NULL,
    itm_cd character(5) NOT NULL,
    itm_qty numeric(18,0) NOT NULL,
    itm_area numeric(18,0) NOT NULL,
    add_area numeric(18,0),
    sum_area numeric(18,0),
    itm_amt numeric(18,2) NOT NULL,
    dis_rt numeric(5,2),
    buy_amt numeric(18,2),
    vat_amt numeric(18,2),
    sum_amt numeric(18,2),
    end_dt date,
    ok_yn character(1),
    rmks character varying(100),
    cid character varying(20),
    cdt timestamp without time zone,
    mid character varying(20),
    mdt timestamp without time zone
);


ALTER TABLE kngil.buy_item OWNER TO postgres;

--
-- TOC entry 227 (class 1259 OID 16522)
-- Name: code_detail; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.code_detail (
    main_cd character varying(5) NOT NULL,
    sub_cd character varying(5) NOT NULL,
    sub_nm character varying(100),
    base_cd character varying(10) NOT NULL,
    use_yn character(1) NOT NULL,
    sort_sq integer,
    rmks character varying(100),
    m1 character varying(50),
    m2 character varying(50),
    m3 character varying(50),
    m4 character varying(50),
    m5 character varying(50),
    m6 character varying(50),
    m7 character varying(50),
    m8 character varying(50),
    m9 character varying(50),
    m10 character varying(50)
);


ALTER TABLE kngil.code_detail OWNER TO postgres;

--
-- TOC entry 5044 (class 0 OID 0)
-- Dependencies: 227
-- Name: TABLE code_detail; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON TABLE kngil.code_detail IS '공통코드상세';


--
-- TOC entry 5045 (class 0 OID 0)
-- Dependencies: 227
-- Name: COLUMN code_detail.sub_nm; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON COLUMN kngil.code_detail.sub_nm IS '서브코드명';


--
-- TOC entry 226 (class 1259 OID 16497)
-- Name: code_master; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.code_master (
    main_cd character varying(5) NOT NULL,
    nain_nm character varying(100),
    use_yn character(1),
    use_bc character varying(10),
    sort_bc character varying(10),
    rmks character varying(100),
    t1 character varying(50),
    t2 character varying(50),
    t3 character varying(50),
    t4 character varying(50),
    t5 character varying(50),
    t6 character varying(50),
    t7 character varying(50),
    t8 character varying(50),
    t9 character varying(50),
    t10 character varying(50)
);


ALTER TABLE kngil.code_master OWNER TO postgres;

--
-- TOC entry 5046 (class 0 OID 0)
-- Dependencies: 226
-- Name: TABLE code_master; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON TABLE kngil.code_master IS '공통코드마스터';


--
-- TOC entry 5047 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN code_master.main_cd; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON COLUMN kngil.code_master.main_cd IS '메인코드';


--
-- TOC entry 5048 (class 0 OID 0)
-- Dependencies: 226
-- Name: COLUMN code_master.nain_nm; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON COLUMN kngil.code_master.nain_nm IS '코드명';


--
-- TOC entry 236 (class 1259 OID 16679)
-- Name: fa_comments_fa_id_seq; Type: SEQUENCE; Schema: kngil; Owner: postgres
--

ALTER TABLE kngil.fa_comments ALTER COLUMN fa_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME kngil.fa_comments_fa_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- TOC entry 221 (class 1259 OID 16427)
-- Name: item; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.item (
    itm_cd character varying(5) NOT NULL,
    itm_nm character varying(50),
    area numeric(18,0),
    itm_amt numeric(18,2),
    use_yn character(1),
    rmks character varying(100),
    cid character varying(20),
    cdt timestamp without time zone,
    mid character varying(20),
    mdt timestamp without time zone
);


ALTER TABLE kngil.item OWNER TO postgres;

--
-- TOC entry 5049 (class 0 OID 0)
-- Dependencies: 221
-- Name: TABLE item; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON TABLE kngil.item IS '서비스 상품 관리 테이블';


--
-- TOC entry 225 (class 1259 OID 16484)
-- Name: login_history; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.login_history (
    user_id character varying(20) NOT NULL,
    sq_no integer NOT NULL,
    public_ip character varying(45),
    local_ip character varying(45),
    login_tm timestamp without time zone
);


ALTER TABLE kngil.login_history OWNER TO postgres;

--
-- TOC entry 220 (class 1259 OID 16414)
-- Name: members; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.members (
    member_id character varying(20) CONSTRAINT member_member_id_not_null NOT NULL,
    member_nm character varying(50) CONSTRAINT member_member_nm_not_null NOT NULL,
    co_bc character varying(10) CONSTRAINT member_co_bc_not_null NOT NULL,
    bs_no character varying(15),
    co_nm character varying(50) CONSTRAINT member_co_nm_not_null NOT NULL,
    co_tel character varying(13),
    tel_no character varying(13),
    email character varying(50),
    join_dt date CONSTRAINT member_join_dt_not_null NOT NULL,
    end_dt date,
    buy_area numeric(18,0),
    use_area numeric(18,0),
    stat_bc character varying(10) CONSTRAINT member_stat_bc_not_null NOT NULL,
    memo text,
    cid character varying(20),
    cdt timestamp without time zone,
    mid character varying(20),
    mdt timestamp without time zone
);


ALTER TABLE kngil.members OWNER TO postgres;

--
-- TOC entry 5050 (class 0 OID 0)
-- Dependencies: 220
-- Name: TABLE members; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON TABLE kngil.members IS '회원 관리 테이블';


--
-- TOC entry 5051 (class 0 OID 0)
-- Dependencies: 220
-- Name: COLUMN members.member_id; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON COLUMN kngil.members.member_id IS '회원ID';


--
-- TOC entry 228 (class 1259 OID 16594)
-- Name: qa_attachments; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.qa_attachments (
    post_id integer,
    ori_name character varying,
    save_path character varying,
    file_size integer,
    uploaded_at timestamp with time zone,
    id bigint NOT NULL
);


ALTER TABLE kngil.qa_attachments OWNER TO postgres;

--
-- TOC entry 5052 (class 0 OID 0)
-- Dependencies: 228
-- Name: TABLE qa_attachments; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON TABLE kngil.qa_attachments IS 'Q&A 첨부파일 테이블';


--
-- TOC entry 237 (class 1259 OID 16687)
-- Name: qa_attachments_id_seq; Type: SEQUENCE; Schema: kngil; Owner: postgres
--

ALTER TABLE kngil.qa_attachments ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
    SEQUENCE NAME kngil.qa_attachments_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- TOC entry 234 (class 1259 OID 16627)
-- Name: qa_comment_images; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.qa_comment_images (
    id integer NOT NULL,
    comment_id integer,
    file_name character varying(255),
    file_path character varying(255),
    thumb_path character varying(255),
    file_size integer,
    uploaded_at timestamp with time zone
);


ALTER TABLE kngil.qa_comment_images OWNER TO postgres;

--
-- TOC entry 5053 (class 0 OID 0)
-- Dependencies: 234
-- Name: TABLE qa_comment_images; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON TABLE kngil.qa_comment_images IS '뎃글 이미지';


--
-- TOC entry 233 (class 1259 OID 16626)
-- Name: qa_comment_images_id_seq; Type: SEQUENCE; Schema: kngil; Owner: postgres
--

ALTER TABLE kngil.qa_comment_images ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
    SEQUENCE NAME kngil.qa_comment_images_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- TOC entry 232 (class 1259 OID 16618)
-- Name: qa_comments; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.qa_comments (
    comment_id integer NOT NULL,
    post_id integer,
    commenter character varying(255),
    content text,
    cdt_dt timestamp with time zone,
    user_nm character varying(100),
    mdt_dt timestamp with time zone
);


ALTER TABLE kngil.qa_comments OWNER TO postgres;

--
-- TOC entry 5054 (class 0 OID 0)
-- Dependencies: 232
-- Name: TABLE qa_comments; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON TABLE kngil.qa_comments IS '뎃글';


--
-- TOC entry 231 (class 1259 OID 16617)
-- Name: qa_comments_comment_id_seq; Type: SEQUENCE; Schema: kngil; Owner: postgres
--

ALTER TABLE kngil.qa_comments ALTER COLUMN comment_id ADD GENERATED ALWAYS AS IDENTITY (
    SEQUENCE NAME kngil.qa_comments_comment_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- TOC entry 230 (class 1259 OID 16603)
-- Name: qa_posts; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.qa_posts (
    post_id integer NOT NULL,
    tel_no character varying(20),
    user_id character varying(64),
    user_nm character varying(100),
    category character varying(20),
    co_nm character varying(100),
    dept_nm character varying(100),
    title character varying(255),
    content text,
    attachment character varying(500),
    stat_bc character varying(50),
    complete_form character(1) DEFAULT 0,
    cdt_dt timestamp with time zone,
    mid_dt timestamp with time zone,
    is_secret character(1) DEFAULT 'N'::bpchar,
    is_read_admin character(1) DEFAULT 0
);


ALTER TABLE kngil.qa_posts OWNER TO postgres;

--
-- TOC entry 5055 (class 0 OID 0)
-- Dependencies: 230
-- Name: TABLE qa_posts; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON TABLE kngil.qa_posts IS 'Q&A 내용등록';


--
-- TOC entry 5056 (class 0 OID 0)
-- Dependencies: 230
-- Name: COLUMN qa_posts.co_nm; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON COLUMN kngil.qa_posts.co_nm IS '회사명
';


--
-- TOC entry 229 (class 1259 OID 16602)
-- Name: qa_posts_post_id_seq; Type: SEQUENCE; Schema: kngil; Owner: postgres
--

ALTER TABLE kngil.qa_posts ALTER COLUMN post_id ADD GENERATED ALWAYS AS IDENTITY (
    SEQUENCE NAME kngil.qa_posts_post_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- TOC entry 224 (class 1259 OID 16471)
-- Name: use_history; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.use_history (
    user_id character varying(20) NOT NULL,
    sq_no integer NOT NULL,
    use_dt date,
    use_area numeric(18,0),
    ser_bc character varying(10),
    cid character varying(20),
    cdt timestamp without time zone,
    mid character varying(20),
    mdt timestamp without time zone
);


ALTER TABLE kngil.use_history OWNER TO postgres;

--
-- TOC entry 222 (class 1259 OID 16433)
-- Name: users; Type: TABLE; Schema: kngil; Owner: postgres
--

CREATE TABLE kngil.users (
    member_id character varying(20) CONSTRAINT user_member_id_not_null NOT NULL,
    user_id character varying(20) CONSTRAINT user_user_id_not_null NOT NULL,
    user_pw character varying(255),
    user_nm character varying(50) CONSTRAINT user_user_nm_not_null NOT NULL,
    dept_nm character varying(50),
    posit_nm character varying(50),
    tel_no character varying(13) CONSTRAINT user_tel_no_not_null NOT NULL,
    email character varying(50),
    auth_bc character varying(10) CONSTRAINT user_auth_bc_not_null NOT NULL,
    use_yn character varying(10) CONSTRAINT user_use_yn_not_null NOT NULL,
    rmks character varying(100),
    cid character varying(20),
    cdt timestamp without time zone,
    mid character varying(20),
    mdt timestamp without time zone,
    oidc_sub character varying(255)
);


ALTER TABLE kngil.users OWNER TO postgres;

--
-- TOC entry 5057 (class 0 OID 0)
-- Dependencies: 222
-- Name: TABLE users; Type: COMMENT; Schema: kngil; Owner: postgres
--

COMMENT ON TABLE kngil.users IS '사용자관리 테이블';


--
-- TOC entry 5024 (class 0 OID 16449)
-- Dependencies: 223
-- Data for Name: buy_item; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.buy_item (member_id, sq_no, buy_dt, itm_cd, itm_qty, itm_area, add_area, sum_area, itm_amt, dis_rt, buy_amt, vat_amt, sum_amt, end_dt, ok_yn, rmks, cid, cdt, mid, mdt) FROM stdin;
B260001	1	2026-01-14	BSV01	1	10000	500	10500	5000000.00	0.00	5000000.00	500000.00	5500000.00	2026-12-31	N	test	m24031	2026-01-14 18:48:27.938147	m24031	2026-01-14 18:48:27.938147
sdisdi	2	2026-01-28	BSV01	3	0	100	100	5000000.00	10.00	13500000.00	1350000.00	14850000.00	\N	N		ADMIN	2026-01-29 14:02:04.478554	ADMIN	2026-01-29 16:57:37.297659
sdisdi	1	2026-01-28	실버   	2	0	0	0	5000000.00	10.00	9000000.00	900000.00	9900000.00	\N	N		ADMIN	2026-01-29 13:20:06.016857	ADMIN	2026-01-29 16:57:37.297659
sdisdi	4	2026-01-28	BSV01	1	10000	0	10000	5000000.00	5.00	5000000.00	500000.00	5500000.00	2026-12-31	N		ADMIN	2026-01-29 16:57:37.297659	ADMIN	2026-01-29 16:57:37.297659
B260001	4	2026-01-16	DDA01	2	10000	0	10000	10000000.00	20.00	8000000.00	800000.00	8800000.00	2026-01-29	Y	test1	ADMIN	2026-01-19 09:29:15.302285	ADMIN	2026-01-19 09:29:26.957085
B260001	6	2026-01-19	DDA01	2	100000	0	100000	10000000.00	25.00	15000000.00	1500000.00	16500000.00	2026-01-29	Y		ADMIN	2026-01-19 09:53:04.703453	ADMIN	2026-01-19 11:18:22.708297
B260001	5	2026-01-19	DDA01	4	10000	0	10000	10000000.00	10.00	36000000.00	3600000.00	39600000.00	2026-01-30	Y		ADMIN	2026-01-19 09:33:44.805899	ADMIN	2026-01-19 11:18:22.708297
B260001	3	2026-01-16	DDA01	1	10000	0	10000	10000000.00	50.00	5000000.00	500000.00	5500000.00	2026-01-26	Y	test	ADMIN	2026-01-16 17:43:34.138282	ADMIN	2026-01-19 09:29:26.957085
B260001	2	2026-01-16	DDA01	1	10000	0	10000	10000000.00	10.00	9000000.00	900000.00	9900000.00	2026-01-29	Y	test	ADMIN	2026-01-16 17:29:57.797917	ADMIN	2026-01-19 09:29:26.957085
B260001	7	2026-01-19	BSV01	1	10000	0	10000	5000000.00	20.00	5000000.00	500000.00	5500000.00	2026-02-06	N		ADMIN	2026-01-19 11:18:09.714842	ADMIN	2026-01-19 11:18:22.708297
sdisdi	5	2026-01-29	CGD01	2	20000	500	20500	10000000.00	25.00	15000000.00	1500000.00	16500000.00	2026-12-30	N		ADMIN	2026-01-29 17:10:39.39493	ADMIN	2026-01-29 17:12:56.807569
sdisdi	3	2026-01-29	BSV01	1	10000	0	10000	5000000.00	5.00	4750000.00	475000.00	5225000.00	2026-12-31	N		ADMIN	2026-01-29 15:31:42.295678	ADMIN	2026-01-29 17:12:56.807569
tester001	1	2025-04-12	BSV01	1	10000	\N	10000	5000000.00	0.00	5000000.00	500000.00	5500000.00	2025-12-31	Y	\N	\N	\N	\N	\N
tester001	2	2026-01-01	BSV01	1	10000	300	10300	5000000.00	0.00	5000000.00	500000.00	5500000.00	2026-12-31	Y	\N	\N	\N	\N	\N
tester001	3	2026-01-01	BSV01	1	10000	\N	10000	5000000.00	0.00	5000000.00	500000.00	5500000.00	2026-12-31	Y	\N	\N	\N	\N	\N
tester001	4	2026-01-01	BSV01	1	10000	\N	10000	5000000.00	0.00	5000000.00	500000.00	5500000.00	2026-12-31	Y	\N	\N	\N	\N	\N
sdi9429	1	2026-01-29	골드   	1	20000	0	20000	10000000.00	0.00	10000000.00	1000000.00	11000000.00	\N	Y		ADMIN	2026-01-29 13:05:15.730538	ADMIN	2026-01-29 13:14:38.877759
sdisdi	6	2026-01-29	ZET01	3000	1	0	1	0.00	0.00	0.00	0.00	0.00	2026-12-31	N		ADMIN	2026-01-29 17:12:56.807569	ADMIN	2026-01-29 17:12:56.807569
am24031	1	2026-01-30	A0000	1	3000	0	3000	0.00	0.00	0.00	0.00	0.00	2026-12-31	Y	최초 가입 제공	am24031	2026-01-30 13:25:57.005379	am24031	2026-01-30 13:25:57.005379


--
-- TOC entry 5028 (class 0 OID 16522)
-- Dependencies: 227
-- Data for Name: code_detail; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.code_detail (main_cd, sub_cd, sub_nm, base_cd, use_yn, sort_sq, rmks, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10) FROM stdin;
BS100	300	메인	BS100300	Y	3	\N	1	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS110	100	오름차순	BS110100	Y	1	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS110	200	정의	BS110200	Y	2	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS200	100	코드1	BS200100	Y	1	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS200	200	코드2	BS200200	Y	2	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS210	Y	Y	BS210Y	Y	1	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS210	N	N	BS210N	Y	2	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS220	1	사용	BS2201	Y	1	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS220	0	미사용	BS2200	Y	2	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
SA100	100	사용	SA100100	Y	1	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
SA100	200	미사용	SA100200	Y	2	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS100	100	개발자	BS100100	Y	1	\N	\N	1	\N	\N	\N	\N	\N	\N	\N	\N
BS100	200	관리자	BS100200	Y	2	\N	\N	1	\N	\N	\N	\N	\N	\N	\N	\N
BS100	400	서브	BS100400	Y	4	\N	1	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS100	500	일반	BS100500	Y	5	\N	1	\N	\N	\N	\N	\N	\N	\N	\N	\N
SA150	200	개인	SA150200	Y	2	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
SA150	100	기업	SA150100	Y	1	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
SA200	100	상하수도 기초현황 보고서	SA200100	Y	1	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
SA200	200	도시계획 기초현황 보고서	SA200200	Y	2	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
SA200	300	기초현황 DATA 파일	SA200300	Y	3	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N


--
-- TOC entry 5027 (class 0 OID 16497)
-- Dependencies: 226
-- Data for Name: code_master; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.code_master (main_cd, nain_nm, use_yn, use_bc, sort_bc, rmks, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) FROM stdin;
BS110	정렬기준	Y	BS200100	BS110100	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS200	사용구분\n	Y	BS200100	BS110100	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS210	Y/N 구분	Y	BS200200	BS110100	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS220	사용여부	Y	BS200200	BS110100	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
SA100	회원상태	Y	BS200100	BS110100	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
SA200	서비스구분	Y	BS200100	BS110100	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N
BS100	권한관리	Y	BS200100	BS110100	\N	사용자	관리자	\N	\N	\N	\N	\N	\N	\N	\N
SA150	법인구분	Y	BS200100	BS110200	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N	\N


--
-- TOC entry 5036 (class 0 OID 16671)
-- Dependencies: 235
-- Data for Name: fa_comments; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.fa_comments (fa_id, fa_subject, fa_content, sq_no, use_yn, cid, cdt, mid, mdt) FROM stdin;
3	계정은 어떻게 생성하나요?	회원가입 메뉴를 통해 계정을 생성할 수 있습니다.	2	Y	\N	\N	\N	\N
1	KNGIL은 어떤 서비스인가요?	KNGIL은 교량 BIM 기반 설계·관리 플랫폼입니다. hahaha	1	Y	\N	\N	\N	\N
13	이것은질문	이것은<br>답글	0	Y	m24031	2026-01-29 14:12:20.625501+09	m24031	2026-01-29 14:26:04.837518+09
14	ㅇㅇㅇㅇ343	ㅇㅁㅇㄹㅇㅇㄹㅇㄹㅇㄹ	0	Y	m24031	2026-02-02 11:22:02.315133+09	m24031	2026-02-02 11:22:07.740956+09


--
-- TOC entry 5022 (class 0 OID 16427)
-- Dependencies: 221
-- Data for Name: item; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.item (itm_cd, itm_nm, area, itm_amt, use_yn, rmks, cid, cdt, mid, mdt) FROM stdin;
CGD01	골드	20000	10000000.00	Y	test	m24031	2026-01-14 17:55:27.062562	m24031	2026-01-14 17:55:27.062562
ZET01	서비스	1	0.00	Y	test	m24031	2026-01-14 17:56:47.427489	m24031	2026-01-14 17:56:47.427489
BSV01	실버	10000	5000000.00	Y	test	m24031	2026-01-14 17:54:53.937187	m24031	2026-01-14 17:58:17.831158
BBBB	TEST23	4	2.00	Y		m24031	2026-01-23 14:28:12.793976	m24031	2026-01-23 15:23:19.231161
DDA01	다이아	30000	30000000.00	Y	testㅇㅇㅇ	m24031	2026-01-14 17:56:22.996253	m24031	2026-01-29 13:50:16.978103
A0000	회원가입 제공	3000	0.00	Y		m24031	2026-01-30 10:01:26.15998	m24031	2026-01-30 10:01:26.15998


--
-- TOC entry 5026 (class 0 OID 16484)
-- Dependencies: 225
-- Data for Name: login_history; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.login_history (user_id, sq_no, public_ip, local_ip, login_tm) FROM stdin;


--
-- TOC entry 5021 (class 0 OID 16414)
-- Dependencies: 220
-- Data for Name: members; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.members (member_id, member_nm, co_bc, bs_no, co_nm, co_tel, tel_no, email, join_dt, end_dt, buy_area, use_area, stat_bc, memo, cid, cdt, mid, mdt) FROM stdin;
b25027	김수현	CB100100	223-33-44445	한맥기술	\N	010-5645-5153	b25027@hanmaceng.co.kr	2026-01-29	\N	\N	\N	SA100100	\N	b25027	2026-01-29 11:25:14.184682	b24014	2026-01-29 16:21:12.842875
B260001	바론 관리자	SA150100	222222222	바론 컨설턴트	\N	010-1111-1111	sdi@sdi.com	2026-01-01	\N	140000	1200	SA100100	\N	\N	\N	SYSTEM	2026-01-21 15:45:40.131991
tester001	테스터10	SA150100	\N	기업1	\N	010-1111-1111	111@gmail.com	2026-01-20	\N	3000	\N	SA100100	\N	m24031	2026-01-20 19:50:23.029203	\N	\N
ctest004	c테스터33	SA150200	\N	기업3	\N	010-3333-3333	333@gmail.com	2026-01-20	\N	1000	\N	SA100100	\N	m24031	2026-01-20 19:57:42.806965	\N	\N
sdi9429	송대일	CB100100	\N	바론	\N	010-8627-0921	sdi9429@naver.com	2026-01-22	\N	1000	\N	SA100100	\N	sdi9429	2026-01-22 12:56:43.651431	\N	\N
tester003	테스터33	SA150200	222222222	기업3	\N	010-3333-3333	333@gmail.com	2026-01-20	\N	2000	\N	SA100100	\N	m24031	2026-01-20 19:52:19.322838	m24031	2026-01-23 16:50:12.264163
tester002	테스터10	SA150100	222222	기업1	\N	010-1111-1111	111@gmail.com	2026-01-20	\N	2000	\N	SA100100	\N	m24031	2026-01-20 19:51:18.809202	m24031	2026-01-27 10:44:31.258757
sdisdi	송대일	CB100100	\N	바론11	\N	010-8627-0923	sdi9429@naver.com	2026-01-22	\N	0	\N	SA100100	\N	sdisdi	2026-01-22 13:12:29.376769	m24031	2026-01-27 10:44:47.776389
Km24031	 권오재	CB100100	\N	한맥기술	\N	010-9114-3944	koj111@naver.com	2026-01-30	\N	\N	\N	SA100100	\N	Km24031	2026-01-30 09:02:15.195496	\N	\N
am24031	권오재A	CB100100	\N	한맥	\N	010-2222-2222	ddd@gmail.com	2026-01-30	\N	3000	\N	SA100100	\N	am24031	2026-01-30 13:25:57.005379	\N	\N


--
-- TOC entry 5029 (class 0 OID 16594)
-- Dependencies: 228
-- Data for Name: qa_attachments; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.qa_attachments (post_id, ori_name, save_path, file_size, uploaded_at, id) FROM stdin;
7	traffic_image.jpg	/kngil/uploads/qa/1769509499_417330b6f49e.jpg	123753	2026-01-27 19:24:59.134487+09	1
8	traffic_image.jpg	/kngil/uploads/qa/1769513369_959a050cb972.jpg	123753	2026-01-27 20:29:29.973998+09	2


--
-- TOC entry 5035 (class 0 OID 16627)
-- Dependencies: 234
-- Data for Name: qa_comment_images; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.qa_comment_images (id, comment_id, file_name, file_path, thumb_path, file_size, uploaded_at) FROM stdin;
1	2	og-main-thumb.JPG	/kngil/uploads/comment/1769511961_dda23bd3.jpg	/kngil/uploads/comment/1769511961_dda23bd3.jpg	112930	2026-01-27 20:06:01.050825+09


--
-- TOC entry 5033 (class 0 OID 16618)
-- Dependencies: 232
-- Data for Name: qa_comments; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.qa_comments (comment_id, post_id, commenter, content, cdt_dt, user_nm, mdt_dt) FROM stdin;
2	7	b24014	test	2026-01-27 20:06:01.050825+09	송대일1	\N


--
-- TOC entry 5031 (class 0 OID 16603)
-- Dependencies: 230
-- Data for Name: qa_posts; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.qa_posts (post_id, tel_no, user_id, user_nm, category, co_nm, dept_nm, title, content, attachment, stat_bc, complete_form, cdt_dt, mid_dt, is_secret, is_read_admin) FROM stdin;
7	\N	b24014	송대일1	오류문의	\N	\N	테스트	<p>1234</p>	\N	review	0	2026-01-27 19:24:59.131617+09	2026-01-27 20:14:21.993651+09	0	Y
8	\N	b24014	송대일1	오류문의	\N	\N	첨부파일 업로드	<p>123</p>	\N	deep	0	2026-01-27 20:29:29.970856+09	2026-01-27 20:32:21.107049+09	0	Y
10	010-8627-0921	b24014	송대일1	오류문의	바론 컨설턴트	총괄기획실	test	<p>1qasdweqwe</p>	\N	review	0	2026-01-28 10:43:24.655249+09	2026-01-28 11:00:56.361487+09	Y	Y
1				오류문의			1111	<p>3333</p>	\N	wait	0	2026-01-27 14:09:56.639903+09	\N	Y	0
2	\N	b24014	송대일1	오류문의	\N	\N	1234	<p>3333</p>	\N	wait	0	2026-01-27 15:56:13.097696+09	\N	0	N
5	\N	b24014	송대일1	오류문의	\N	\N	123	<p>456</p>	\N	wait	0	2026-01-27 16:40:55.238147+09	\N	0	N
9	\N	b24014	송대일1	오류문의	\N	\N	최종테스트	<p>11111333</p>	\N	wait	0	2026-01-28 10:09:07.943555+09	\N	0	Y
6	\N	b24014	송대일1	오류문의	\N	\N	첨부파일 업로드	<p>123</p>	\N	wait	0	2026-01-27 19:17:08.118218+09	\N	0	Y
4	\N	b24014	송대일1	개선문의	\N	\N	tests	<p>tests</p>	\N	wait	0	2026-01-27 16:00:43.415892+09	\N	0	Y
12	010-8627-0921	b24014	송대일1	일반문의	바론 컨설턴트	총괄기획실	테스트 작성글	<p>ㅅㅅㅅㅅㅅㅅㅅ</p>	\N	wait	0	2026-01-30 16:10:49.845849+09	2026-02-02 09:32:40.063076+09	N	Y
11	010-8627-0921	b24014	송대일1	오류문의	바론 컨설턴트	총괄기획실	1111	<p>3123123</p>	\N	wait	0	2026-01-30 16:10:30.821517+09	2026-02-02 09:32:54.632593+09	N	Y
13	010-8627-0921	b24014	송대일1	오류문의	바론 컨설턴트	총괄기획실	테스트 중입니다	<p>3333444</p>	\N	wait	0	2026-02-02 09:33:22.636799+09	\N	N	Y
14	010-8627-0921	b24014	송대일1	공지사항	바론 컨설턴트	총괄기획실	공지 테스트	<p>1234</p>	\N	wait	0	2026-02-02 09:40:50.464071+09	\N	N	Y


--
-- TOC entry 5025 (class 0 OID 16471)
-- Dependencies: 224
-- Data for Name: use_history; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.use_history (user_id, sq_no, use_dt, use_area, ser_bc, cid, cdt, mid, mdt) FROM stdin;
m24031	1	2026-01-18	100	SA200100	\N	\N	\N	\N
test001	1	2026-01-18	200	SA200100	\N	\N	\N	\N
test001	2	2026-01-19	200	SA200100	\N	\N	\N	\N
m24031	2	2026-01-19	200	SA200300	\N	\N	\N	\N
m24031	3	2026-01-19	300	SA200200	\N	\N	\N	\N
m24031	4	2026-01-19	100	SA200300	\N	\N	\N	\N
m24031	5	2026-01-19	20000	SA200300	\N	\N	\N	\N
m24031	6	2026-01-19	1000	SA200200	\N	\N	\N	\N
m24031	7	2025-01-19	20000	SA200100	\N	\N	\N	\N


--
-- TOC entry 5023 (class 0 OID 16433)
-- Dependencies: 222
-- Data for Name: users; Type: TABLE DATA; Schema: kngil; Owner: postgres
--

COPY kngil.users (member_id, user_id, user_pw, user_nm, dept_nm, posit_nm, tel_no, email, auth_bc, use_yn, rmks, cid, cdt, mid, mdt, oidc_sub) FROM stdin;
B260001	test003	test003	테스터3	기술개발센터	수석연구원	010-1111-3333	test@test.com	BS100400	N	test 입니다.	m24031	2026-01-14 18:39:43.030024	m24031	2026-01-14 18:42:22.488198	\N
sdisdi	song12124	test1234!!@	테스터4	erp		1012346770	test@test.co.kr	BS100500	Y		song12124	2026-01-30 11:12:14.172743	song12124	2026-01-30 11:12:14.172743	\N
tester002	tester002	tester002	류호성	전산실	\N	010-3371-5649	111@gmail.com	BS100100	Y	\N	m24031	2026-01-20 19:51:18.809202	m24031	2026-01-27 10:44:31.258757	\N
sdisdi	song12125	test1234!!@	테스터5	erp		1012346771	test@test.co.kr	BS100500	Y		song12125	2026-01-30 11:12:14.184522	song12125	2026-01-30 11:12:14.184522	\N
ctest004	ctest004	ctest004	권오재2	전산실	\N	010-9114-3943	333@gmail.com	BS100300	N	\N	m24031	2026-01-20 19:57:42.806965	\N	\N	\N
B260001	sdi1108	sdi1108	어드민	ERP팀ㅁ		010-1111-1112	test@test1.com	BS100400	Y		\N	2026-01-15 20:08:48.731648	m24031	2026-01-29 13:29:52.388359	\N
sdisdi	song12126	test1234!!@	테스터6	erp		1012346772	test@test.co.kr	BS100500	Y		song12126	2026-01-30 11:18:04.916446	song12126	2026-01-30 11:18:04.916446	\N
sdisdi	song12127	test1234!!@	테스터7	erp		1012346773	test@test.co.kr	BS100500	Y		song12127	2026-01-30 11:18:04.921195	song12127	2026-01-30 11:18:04.921195	\N
sdisdi	song12128	test1234!!@	테스터8	erp		1012346774	test@test.co.kr	BS100500	Y		song12128	2026-01-30 12:53:06.276828	song12128	2026-01-30 12:53:06.276828	\N
sdisdi	song12129	test1234!!@	테스터9	erp		1012346775	test@test.co.kr	BS100500	Y		song12129	2026-01-30 12:53:06.284774	song12129	2026-01-30 12:53:06.284774	\N
sdisdi	song12130	test1234!!@	테스터10	erp		1012346776	test@test.co.kr	BS100500	Y		song12130	2026-01-30 13:01:11.566978	song12130	2026-01-30 13:01:11.566978	\N
sdisdi	song12131	test1234!!@	테스터11	erp		1012346777	test@test.co.kr	BS100500	Y		song12131	2026-01-30 13:01:11.57227	song12131	2026-01-30 13:01:11.57227	\N
am24031	am24031	!rnjsdhwo729	권오재A	ERP 기획	\N	010-2222-2222	ddd@gmail.com	BS100300	Y	\N	am24031	2026-01-30 13:25:57.005379	\N	\N	\N
b25027	b25027	a1357125!@23	김수현	디자인기획팀	\N	010-5645-5153	b25027@hanmaceng.co.kr	BS100300	Y	\N	b25027	2026-01-29 11:25:14.184682	b24014	2026-01-29 16:21:12.842875	\N
B260001	test005	b23008	염승호	총괄기획실	수석 연구원	010-8835-0501	df	BS100100	Y		m24031	2026-01-29 13:30:30.456963	m24031	2026-01-29 13:30:30.456963	\N
B260001	test002	test002	테스터2	기술개발센터		010-1111-2222	test@test.com	BS100400	Y		m24031	2026-01-14 18:39:03.432801	m24031	2026-01-29 20:31:27.681975	\N
tester003	btest001	btest001	테스터33	도로부				BS100500	Y	test	m24031	2026-01-20 19:56:32.467743	m24031	2026-01-20 19:56:32.467743	\N
tester003	btest002	btest002	b테스터22	도로부				BS100500	Y	test	m24031	2026-01-20 19:56:48.750462	m24031	2026-01-20 19:56:48.750462	\N
B260001	m24031	m24031	권오재	총괄기획실	선임 연구원	010-9114-3943	m24031@hanmaceng.co.kr	BS100100	Y	\N	\N	\N	\N	\N	\N
Km24031	Km24031	!rnjsdhwo729	 권오재	ERP기획팀	\N	010-9114-3944	koj111@naver.com	BS100300	Y	\N	Km24031	2026-01-30 09:02:15.195496	\N	\N	\N
sdi9429	sdi9429	test1111	송대일	총괄기획실	\N	010-8627-0922	sdi9429@naver.com	BS100300	Y	\N	sdi9429	2026-01-22 12:56:43.651431	\N	\N	\N
B260001	b24014	test001	송대일1	총괄기획실		010-8627-0921	b24014@hanmaceng.co.kr\n	BS100100	Y	123	\N	\N	SYSTEM	2026-01-19 16:44:25.814475	\N
tester003	tester003	test001	테스터33	전산실	\N	010-3333-3333	333@gmail.com	BS100300	Y	\N	m24031	2026-01-20 19:52:19.322838	m24031	2026-01-23 16:50:12.264163	\N
B260001	test001	test001	회사관리자	총괄기획실1		010-3189-1514	sdi1111@sdi.com	BS100300	Y	권한위임[sdi1108]	\N	\N	m24031	2026-01-22 17:11:58.275998	\N
B260001	test0005	test0005	테스트55	총괄기획실1		010-4158-5840	b24000@hanmaceng.co.kr	BS100400	Y	비고 입력 테스트	SYSTEM	2026-01-19 19:39:10.476205	SYSTEM	2026-01-19 19:39:10.476205	\N
tester001	tester001	test001	테스터10	전산실	\N	010-9523-0055	111@gmail.com	BS100300	Y	\N	m24031	2026-01-20 19:50:23.029203	\N	\N	\N
sdisdi	sdisdi	song1108!	송대일	총괄기획실	\N	010-8627-0923	sdi9429@naver.com	BS100300	Y	\N	sdisdi	2026-01-22 13:12:29.376769	m24031	2026-01-27 10:44:47.776389	\N
sdisdi	song1212	test1234!!@	테스터	erp		01012345689	test@test.co.kr	BS100500	N		song1212	2026-01-30 09:58:45.274688	sdisdi	2026-01-30 10:02:41.963234	\N
sdisdi	song12121	test1234!!@	테스터1	erp		1012346767	test@test.co.kr	BS100400	N		song12121	2026-01-30 10:05:59.726345	song12121	2026-01-30 10:05:59.726345	\N
sdisdi	song12122	test1234!!@	테스터2	erp		1012346768	test@test.co.kr	BS100500	Y		song12122	2026-01-30 10:46:04.719717	song12122	2026-01-30 10:46:04.719717	\N
sdisdi	song12123	test1234!!@	테스터3	erp		1012346769	test@test.co.kr	BS100500	Y		song12123	2026-01-30 10:46:04.729306	song12123	2026-01-30 10:46:04.729306	\N
B260001	sdi111	000000	test123	ert33		010-8623-6564	test@test.com	BS100500	Y		b24014	2026-01-30 13:02:45.327993	b24014	2026-01-30 14:08:03.710466	\N
B260001	sdisssss	1234asd!!	test1232	erpp		010-8686-2323	test@test.com	BS100500	Y		b24014	2026-01-30 13:40:50.09753	b24014	2026-01-30 14:08:14.578146	\N
B260001	test1010	test1010	test12	erp		010-8888-2222	test@test1.co.kr	BS100500	Y		b24014	2026-01-30 13:56:43.807999	b24014	2026-01-30 14:18:12.484107	\N
am24031	kwon001	kwon001	권1	dd		010-1111-2111	kwon001	BS100500	Y		kwon001	2026-01-30 16:05:35.819863	kwon001	2026-01-30 16:05:35.819863	\N
am24031	kwon002	kwon002	권2	aa		010-1111-2112	kwon002	BS100500	Y		kwon002	2026-01-30 16:05:35.846448	kwon002	2026-01-30 16:05:35.846448	\N


--
-- TOC entry 5058 (class 0 OID 0)
-- Dependencies: 236
-- Name: fa_comments_fa_id_seq; Type: SEQUENCE SET; Schema: kngil; Owner: postgres
--

SELECT pg_catalog.setval('kngil.fa_comments_fa_id_seq', 14, true);


--
-- TOC entry 5059 (class 0 OID 0)
-- Dependencies: 237
-- Name: qa_attachments_id_seq; Type: SEQUENCE SET; Schema: kngil; Owner: postgres
--

SELECT pg_catalog.setval('kngil.qa_attachments_id_seq', 2, true);


--
-- TOC entry 5060 (class 0 OID 0)
-- Dependencies: 233
-- Name: qa_comment_images_id_seq; Type: SEQUENCE SET; Schema: kngil; Owner: postgres
--

SELECT pg_catalog.setval('kngil.qa_comment_images_id_seq', 1, true);


--
-- TOC entry 5061 (class 0 OID 0)
-- Dependencies: 231
-- Name: qa_comments_comment_id_seq; Type: SEQUENCE SET; Schema: kngil; Owner: postgres
--

SELECT pg_catalog.setval('kngil.qa_comments_comment_id_seq', 2, true);


--
-- TOC entry 5062 (class 0 OID 0)
-- Dependencies: 229
-- Name: qa_posts_post_id_seq; Type: SEQUENCE SET; Schema: kngil; Owner: postgres
--

SELECT pg_catalog.setval('kngil.qa_posts_post_id_seq', 14, true);


--
-- TOC entry 4867 (class 2606 OID 16678)
-- Name: fa_comments fa_comments_pkey; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.fa_comments
    ADD CONSTRAINT fa_comments_pkey PRIMARY KEY (fa_id);


--
-- TOC entry 4855 (class 2606 OID 16643)
-- Name: login_history login_history_pkey; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.login_history
    ADD CONSTRAINT login_history_pkey PRIMARY KEY (user_id, sq_no);


--
-- TOC entry 4851 (class 2606 OID 16460)
-- Name: buy_item pk_buy_item; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.buy_item
    ADD CONSTRAINT pk_buy_item PRIMARY KEY (member_id, sq_no);


--
-- TOC entry 4859 (class 2606 OID 16532)
-- Name: code_detail pk_code_detail; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.code_detail
    ADD CONSTRAINT pk_code_detail PRIMARY KEY (main_cd, sub_cd);


--
-- TOC entry 4857 (class 2606 OID 16504)
-- Name: code_master pk_code_master; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.code_master
    ADD CONSTRAINT pk_code_master PRIMARY KEY (main_cd);


--
-- TOC entry 4845 (class 2606 OID 16555)
-- Name: item pk_item; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.item
    ADD CONSTRAINT pk_item PRIMARY KEY (itm_cd);


--
-- TOC entry 4843 (class 2606 OID 16426)
-- Name: members pk_members; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.members
    ADD CONSTRAINT pk_members PRIMARY KEY (member_id);


--
-- TOC entry 4861 (class 2606 OID 16690)
-- Name: qa_attachments qa_attachments_pkey; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.qa_attachments
    ADD CONSTRAINT qa_attachments_pkey PRIMARY KEY (id);


--
-- TOC entry 4865 (class 2606 OID 16625)
-- Name: qa_comments qa_comments_pkey; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.qa_comments
    ADD CONSTRAINT qa_comments_pkey PRIMARY KEY (comment_id);


--
-- TOC entry 4863 (class 2606 OID 16616)
-- Name: qa_posts qa_posts_pkey; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.qa_posts
    ADD CONSTRAINT qa_posts_pkey PRIMARY KEY (post_id);


--
-- TOC entry 4853 (class 2606 OID 16641)
-- Name: use_history use_history_pkey; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.use_history
    ADD CONSTRAINT use_history_pkey PRIMARY KEY (user_id, sq_no);


--
-- TOC entry 4847 (class 2606 OID 16721)
-- Name: users users_oidc_sub_key; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.users
    ADD CONSTRAINT users_oidc_sub_key UNIQUE (oidc_sub);


--
-- TOC entry 4849 (class 2606 OID 16645)
-- Name: users users_pkey; Type: CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (user_id);


--
-- TOC entry 4873 (class 2620 OID 16705)
-- Name: buy_item trg_buy_item_changed; Type: TRIGGER; Schema: kngil; Owner: postgres
--

CREATE TRIGGER trg_buy_item_changed AFTER INSERT OR DELETE OR UPDATE ON kngil.buy_item FOR EACH ROW EXECUTE FUNCTION kngil.fn_update_buy_area();


--
-- TOC entry 4872 (class 2606 OID 16533)
-- Name: code_detail fk_code_master_to_code_detail; Type: FK CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.code_detail
    ADD CONSTRAINT fk_code_master_to_code_detail FOREIGN KEY (main_cd) REFERENCES kngil.code_master(main_cd) ON DELETE CASCADE;


--
-- TOC entry 4868 (class 2606 OID 16656)
-- Name: users fk_member_to_user; Type: FK CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.users
    ADD CONSTRAINT fk_member_to_user FOREIGN KEY (member_id) REFERENCES kngil.members(member_id) NOT VALID;


--
-- TOC entry 4869 (class 2606 OID 16461)
-- Name: buy_item fk_members_to_buy_item; Type: FK CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.buy_item
    ADD CONSTRAINT fk_members_to_buy_item FOREIGN KEY (member_id) REFERENCES kngil.members(member_id);


--
-- TOC entry 4871 (class 2606 OID 16651)
-- Name: login_history fk_user_to_login_history; Type: FK CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.login_history
    ADD CONSTRAINT fk_user_to_login_history FOREIGN KEY (user_id) REFERENCES kngil.users(user_id) NOT VALID;


--
-- TOC entry 4870 (class 2606 OID 16646)
-- Name: use_history fk_user_to_use_history; Type: FK CONSTRAINT; Schema: kngil; Owner: postgres
--

ALTER TABLE ONLY kngil.use_history
    ADD CONSTRAINT fk_user_to_use_history FOREIGN KEY (user_id) REFERENCES kngil.users(user_id) NOT VALID;


-- Completed on 2026-02-02 14:06:04

--
-- PostgreSQL database dump complete
--


