-- 품질관리 — waceplm.esgrin.com 운영 DB(211.115.91.141:11133/waceplm) 스키마/데이터 동기화.
-- 2단계 마이그레이션(02_*.sql)에서 만든 5개 wace_plm 스타일 테이블의 PK 타입이 bigserial 이었으나,
-- 실제 운영 DB 는 varchar(50) 을 사용하므로 DROP 후 운영 스키마 1:1 로 재정합한다.
-- 추가로 운영 환경에만 있는 customer_cs, pms_quality_ecr 도 신규 생성.

DROP TABLE IF EXISTS public.incoming_inspection_defect CASCADE;
DROP TABLE IF EXISTS public.incoming_inspection_detail CASCADE;
DROP TABLE IF EXISTS public.process_inspection_detail CASCADE;
DROP TABLE IF EXISTS public.process_inspection_master CASCADE;
DROP TABLE IF EXISTS public.pms_quality_semi_product_inspection CASCADE;

-- 고객 CS (waceplm 운영판)
CREATE TABLE IF NOT EXISTS public.customer_cs (
  objid              varchar(50) PRIMARY KEY,
  receipt_no         varchar(50),
  receipt_date       varchar(10),
  qty                numeric,
  customer_objid     varchar(200),
  model_name         varchar(200),
  product_name       varchar(200),
  part_no            varchar(200),
  production_date    varchar(10),
  sales_date         varchar(10),
  serial_no          varchar(200),
  manufacturer       varchar(200),
  complaint_content  varchar(2000),
  action_content     varchar(2000),
  blame_decision     varchar(50),
  status             varchar(50),
  remark             varchar(2000),
  action_date        varchar(10),
  action_user_id     varchar(50),
  attach_file_objid  varchar(50),
  writer             varchar(50),
  reg_date           timestamp,
  mod_date           timestamp,
  part_name          varchar,
  product_no         varchar,
  action_type        varchar,
  receipt_user_id    varchar(50)
);

-- ECR (waceplm 운영 신규 테이블 — 기존 ecr_mng 와는 별도 보존)
CREATE TABLE IF NOT EXISTS public.pms_quality_ecr (
  objid              varchar(50) PRIMARY KEY,
  ecr_no             varchar(50),
  request_date       varchar(10),
  requester_id       varchar(50),
  part_no            varchar(100),
  part_name          varchar(200),
  issue_content      text,
  due_date           varchar(10),
  action_dept        varchar(50),
  action_manager_id  varchar(50),
  action_user_id     varchar(50),
  action_content     text,
  complete_date      varchar(10),
  attach_file_objid  varchar(50),
  remark             text,
  writer             varchar(50),
  reg_date           timestamp,
  modifier           varchar(50),
  mod_date           timestamp,
  part_objid         varchar,
  ecr_doc_summary    text,
  ecr_doc_reason     text,
  ecr_rev_no         varchar(50),
  ecr_rev_date       varchar(20),
  ecr_doc_form_no    varchar(50),
  ecr_doc_author     varchar(100),
  change_type        varchar
);

-- 수입검사 진행 상세
CREATE TABLE IF NOT EXISTS public.incoming_inspection_detail (
  objid                       varchar(50) PRIMARY KEY,
  inventory_in_objid          varchar(50),
  purchase_order_master_objid varchar,
  request_date                varchar(10),
  request_user_id             varchar(50),
  inspection_date             varchar(10),
  inspector_id                varchar(50),
  inspection_type             varchar(50),
  inspection_yn               varchar(10),
  defect_type                 varchar(50),
  defect_reason               varchar(200),
  action_status               varchar(50),
  inspection_qty              numeric,
  defect_qty                  numeric,
  inspection_result           varchar(50),
  attach_file_objid           varchar(50),
  remark                      varchar(2000),
  writer                      varchar(50),
  reg_date                    timestamp
);

-- 수입검사 불량 상세
CREATE TABLE IF NOT EXISTS public.incoming_inspection_defect (
  objid                    varchar(50) PRIMARY KEY,
  inspection_detail_objid  varchar(50),
  inspection_type          varchar(50),
  inspection_date          varchar(10),
  inspector_id             varchar(50),
  defect_type              varchar(50),
  defect_reason            varchar(200),
  action_status            varchar(50),
  action_result            varchar(200),
  inspection_qty           numeric,
  defect_qty               numeric,
  inspection_result        varchar(50),
  remark                   varchar(2000),
  writer                   varchar(50),
  reg_date                 timestamp
);

-- 공정검사 마스터/디테일
CREATE TABLE IF NOT EXISTS public.process_inspection_master (
  objid           varchar(50) PRIMARY KEY,
  inspection_date varchar(10),
  inspector_id    varchar(50),
  remark          varchar(2000),
  writer          varchar(50),
  reg_date        timestamp,
  mod_date        timestamp
);

CREATE TABLE IF NOT EXISTS public.process_inspection_detail (
  objid              varchar(50) PRIMARY KEY,
  master_objid       varchar(50),
  process_cd         varchar(50),
  project_objid      varchar(50),
  part_objid         varchar(50),
  part_no            varchar(200),
  part_name          varchar(200),
  inspection_qty     numeric,
  defect_qty         numeric,
  work_env_status    varchar(10),
  measuring_device   varchar(10),
  dept_cd            varchar(50),
  user_id            varchar(50),
  inspection_date    varchar(10),
  inspector_id       varchar(50),
  remark             varchar(2000),
  action_status      varchar(50),
  inspection_result  varchar(10),
  writer             varchar(50),
  reg_date           timestamp,
  mod_date           timestamp
);

-- 반제품검사 (GOOD/DEFECT 통합)
CREATE TABLE IF NOT EXISTS public.pms_quality_semi_product_inspection (
  objid               varchar(50) PRIMARY KEY,
  project_no          varchar(200),
  work_order_no       varchar(100),
  part_no             varchar(200),
  part_name           varchar(200),
  receipt_qty         numeric,
  good_qty            numeric,
  defect_qty          numeric,
  disposition_type    varchar(50),
  remark              varchar(2000),
  writer              varchar(50),
  reg_date            timestamp,
  inspection_group_id varchar(50),
  data_type           varchar(20),
  defect_type         varchar(50),
  defect_cause        varchar(200),
  responsible_dept    varchar(50),
  worker              varchar(50),
  process_status      varchar(50),
  inspection_date     varchar(10),
  inspector           varchar(50),
  model_name          varchar(200),
  product_type        varchar(50),
  is_locked           varchar(1)
);

-- 데이터 복사 (운영 → 로컬, ON CONFLICT DO NOTHING). 실제 실행은 node script 로 진행.
--  customer_cs                              24 rows
--  pms_quality_ecr                          1
--  process_inspection_master                3
--  process_inspection_detail                13
--  incoming_inspection_detail               1
--  pms_quality_semi_product_inspection      84
