-- ============================================================
-- M-BOM 의존 테이블 운영 → RPS 데이터 이관 (PR-A0)
-- 운영: 211.115.91.141:11133/waceplm
-- 대상: 211.115.91.141:11134/vexplor_rps
--
-- 함정:
--   1) mbom_history.mbom_header_objid FK → 매칭 없는 행은 import 제외 (CASCADE 와 일치)
--   2) sales_request_master.mbom_header_objid 는 FK 없음 (NULL 허용)
--   3) client_mng 는 운영 8,946건 전량 이관 (그리드 CUSTOMER_NAME 매칭용)
--
-- 실행 전 export (운영에서):
--   PGPASSWORD='waceplm0909!!' psql -h 211.115.91.141 -p 11133 -U postgres -d waceplm \
--     -c "\copy (SELECT * FROM mbom_history) TO '/tmp/mbom_history.csv' WITH CSV HEADER"
--   PGPASSWORD='waceplm0909!!' psql -h 211.115.91.141 -p 11133 -U postgres -d waceplm \
--     -c "\copy (SELECT * FROM sales_request_master) TO '/tmp/sales_request_master.csv' WITH CSV HEADER"
--   PGPASSWORD='waceplm0909!!' psql -h 211.115.91.141 -p 11133 -U postgres -d waceplm \
--     -c "\copy (SELECT * FROM client_mng) TO '/tmp/client_mng.csv' WITH CSV HEADER"
-- ============================================================

-- ── 1. mbom_history ────────────────────────────────────────────
DROP TABLE IF EXISTS mbom_history_stage;
CREATE TABLE mbom_history_stage (LIKE mbom_history INCLUDING DEFAULTS);
ALTER TABLE mbom_history_stage DROP CONSTRAINT IF EXISTS mbom_history_pkey;
ALTER TABLE mbom_history_stage DROP CONSTRAINT IF EXISTS fk_mbom_history_header;

\copy mbom_history_stage FROM '/tmp/mbom_history.csv' WITH CSV HEADER

-- RPS mbom_header 에 없는 mbom_header_objid 행은 import 제외 (FK CASCADE 호환)
DELETE FROM mbom_history_stage
 WHERE NOT EXISTS (SELECT 1 FROM mbom_header WHERE objid = mbom_history_stage.mbom_header_objid);

INSERT INTO mbom_history SELECT * FROM mbom_history_stage;
DROP TABLE mbom_history_stage;


-- ── 2. sales_request_master ────────────────────────────────────
DROP TABLE IF EXISTS sales_request_master_stage;
CREATE TABLE sales_request_master_stage (LIKE sales_request_master INCLUDING DEFAULTS);
ALTER TABLE sales_request_master_stage DROP CONSTRAINT IF EXISTS sales_request_master_pkey;

\copy sales_request_master_stage FROM '/tmp/sales_request_master.csv' WITH CSV HEADER

-- mbom_header_objid 매칭 안 되는 경우 NULL (FK 는 없지만 그리드 매칭 시 NULL fallback)
UPDATE sales_request_master_stage SET mbom_header_objid = NULL
 WHERE mbom_header_objid IS NOT NULL
   AND NOT EXISTS (SELECT 1 FROM mbom_header WHERE objid = sales_request_master_stage.mbom_header_objid);

INSERT INTO sales_request_master SELECT * FROM sales_request_master_stage;
DROP TABLE sales_request_master_stage;


-- ── 3. client_mng ──────────────────────────────────────────────
DROP TABLE IF EXISTS client_mng_stage;
CREATE TABLE client_mng_stage (LIKE client_mng INCLUDING DEFAULTS);
ALTER TABLE client_mng_stage DROP CONSTRAINT IF EXISTS uk_client_mng_client_cd;

\copy client_mng_stage FROM '/tmp/client_mng.csv' WITH CSV HEADER

INSERT INTO client_mng SELECT * FROM client_mng_stage;
DROP TABLE client_mng_stage;


-- ── 결과 ──────────────────────────────────────────────────────
SELECT 'mbom_history'         AS t, COUNT(*) FROM mbom_history
UNION ALL SELECT 'sales_request_master', COUNT(*) FROM sales_request_master
UNION ALL SELECT 'client_mng',           COUNT(*) FROM client_mng;
