-- ============================================================
-- 견적요청서 운영 sample 데이터 → RPS 이관
-- 운영: 211.115.91.141:11133/waceplm
--   quotation_request_master 4건 / quotation_request_detail 4건
-- 대상: 211.115.91.141:11134/vexplor_rps
--
-- 함정:
--   1) objid / sales_request_master_objid / project_mgmt_objid : numeric → varchar
--   2) detail.part_objid : numeric → bigint (RPS part_mng.objid bigint 호환)
--   3) FK 미매칭 sales_request_part_objid 는 NULL 처리
--
-- 멱등성: ON CONFLICT DO NOTHING.
-- ============================================================

-- ── master ────────────────────────────────────────────────────
INSERT INTO quotation_request_master
  (objid, quotation_request_no, sales_request_master_objid, project_mgmt_objid,
   vendor_objid, vendor_type, status, mail_send_date, mail_send_yn, due_date,
   remark, writer, reg_date, edit_date)
VALUES
  ('-1554146727','Q20260401-115','-722096187','-1752090174','0000000007','SUPPLY','received','2026-04-03 04:36:11.666917','Y','2026-04-06',NULL,'ady1225','2026-04-01 07:11:30.812611','2026-04-01 07:16:21.513931'),
  ('-1629785580','Q20260401-116','-722096187','-1752090174','0000000012','PROCESSING','create',NULL,'N','2026-04-06',NULL,'ady1225','2026-04-01 07:11:30.814099',NULL),
  ('185180465','Q20260401-118','-722096187','-1752090174','0000008379','PROCESSING','create',NULL,'N','2026-04-06',NULL,'ady1225','2026-04-01 07:11:30.836506',NULL),
  ('211976545','Q20260401-119','-722096187','-1752090174','0000012062','PROCESSING','create',NULL,'N','2026-04-06',NULL,'ady1225','2026-04-01 07:11:30.841764',NULL)
ON CONFLICT (objid) DO NOTHING;

-- ── detail ────────────────────────────────────────────────────
INSERT INTO quotation_request_detail
  (objid, quotation_request_master_objid, sales_request_part_objid, part_objid,
   part_no, part_name, raw_material, size, qty, unit_price, total_price,
   remark, delivery_request_date, reg_date, edit_date)
VALUES
  ('-1266428262','-1554146727','-1279349416',1868255637,'C3P50L22','Ti(GR5)','Ti(GR5)','Ø50*22',0,10000,0,NULL,'2026-04-03','2026-04-01 07:11:30.812611','2026-04-01 07:16:21.513931'),
  ('-2130546975','-1629785580','1187291883',1868255516,'10024-0066','SHEET',NULL,NULL,4,0,0,NULL,NULL,'2026-04-01 07:11:30.814099',NULL),
  ('-392083183','185180465','-1279349416',1868255637,'10026-0031','HOLDER',NULL,NULL,4,0,0,NULL,NULL,'2026-04-01 07:11:30.836506',NULL),
  ('-563828077','211976545','-1291084031',1868257572,'30004-0098','NUT',NULL,NULL,4,0,0,NULL,NULL,'2026-04-01 07:11:30.841764',NULL)
ON CONFLICT (objid) DO NOTHING;

-- FK 미매칭 sales_request_part_objid 는 NULL 처리 (현재 RPS sales_request_part 0건)
UPDATE quotation_request_detail
   SET sales_request_part_objid = NULL
 WHERE sales_request_part_objid IS NOT NULL
   AND NOT EXISTS (
     SELECT 1 FROM sales_request_part WHERE objid = quotation_request_detail.sales_request_part_objid
   );

-- FK 미매칭 part_objid 는 NULL 처리 (RPS part_mng 와 매칭 안 되면)
UPDATE quotation_request_detail
   SET part_objid = NULL
 WHERE part_objid IS NOT NULL
   AND NOT EXISTS (SELECT 1 FROM part_mng WHERE objid = quotation_request_detail.part_objid);
