-- 품질관리 — wace_plm 의 quality.xml MyBatis 매퍼가 참조하는 5개 테이블을
-- vexplor_rps 에 그대로 생성. 컬럼 정의는 INSERT/UPDATE/ResultMap 에서 추출.
--
-- ilshin DB 의 운영 환경에는 이 이름들이 존재하지 않고, ilshin은 inspection_mgmt
-- 단일 테이블로 통합 보관함. 본 5개 테이블은 wace_plm 코드 호환을 위한 신규 추가.

CREATE TABLE IF NOT EXISTS public.incoming_inspection_detail (
  objid                       bigserial PRIMARY KEY,
  inventory_in_objid          bigint,
  purchase_order_master_objid varchar,
  request_date                date,
  request_user_id             varchar,
  inspection_date             date,
  inspector_id                varchar,
  inspection_type             varchar,
  inspection_yn               varchar(1),
  defect_type                 varchar,
  defect_reason               varchar,
  action_status               varchar,
  inspection_qty              numeric(18,2),
  defect_qty                  numeric(18,2),
  inspection_result           varchar,
  attach_file_objid           bigint,
  remark                      text,
  writer                      varchar NOT NULL,
  reg_date                    timestamp NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_incoming_inspection_detail_po
  ON public.incoming_inspection_detail (purchase_order_master_objid);

CREATE TABLE IF NOT EXISTS public.incoming_inspection_defect (
  objid                    bigserial PRIMARY KEY,
  inspection_detail_objid  bigint NOT NULL,
  inspection_type          varchar,
  inspection_date          date,
  inspector_id             varchar,
  defect_type              varchar,
  defect_reason            varchar,
  action_status            varchar,
  action_result            varchar,
  inspection_qty           numeric(18,2),
  defect_qty               numeric(18,2),
  inspection_result        varchar,
  remark                   text,
  writer                   varchar NOT NULL,
  reg_date                 timestamp NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_incoming_inspection_defect_detail
  ON public.incoming_inspection_defect (inspection_detail_objid);

CREATE TABLE IF NOT EXISTS public.process_inspection_master (
  objid          bigserial PRIMARY KEY,
  inspection_date date,
  inspector_id   varchar,
  remark         text,
  writer         varchar NOT NULL,
  reg_date       timestamp NOT NULL DEFAULT NOW(),
  mod_date       timestamp
);

CREATE TABLE IF NOT EXISTS public.process_inspection_detail (
  objid             bigserial PRIMARY KEY,
  master_objid      bigint NOT NULL,
  process_cd        varchar,
  project_objid     bigint,
  part_objid        bigint,
  part_no           varchar,
  part_name         varchar,
  inspection_qty    numeric(18,2),
  defect_qty        numeric(18,2),
  work_env_status   varchar,
  measuring_device  varchar,
  dept_cd           varchar,
  user_id           varchar,
  inspection_date   date,
  inspector_id      varchar,
  remark            text,
  action_status     varchar,
  inspection_result varchar,
  writer            varchar NOT NULL,
  reg_date          timestamp NOT NULL DEFAULT NOW(),
  mod_date          timestamp
);
CREATE INDEX IF NOT EXISTS idx_process_inspection_detail_master
  ON public.process_inspection_detail (master_objid);

CREATE TABLE IF NOT EXISTS public.pms_quality_semi_product_inspection (
  objid               bigserial PRIMARY KEY,
  project_no          varchar,
  work_order_no       varchar,
  part_no             varchar,
  part_name           varchar,
  receipt_qty         numeric(18,2),
  good_qty            numeric(18,2),
  defect_qty          numeric(18,2),
  disposition_type    varchar,
  remark              text,
  writer              varchar NOT NULL,
  reg_date            timestamp NOT NULL DEFAULT NOW(),
  inspection_group_id bigint,
  data_type           varchar(20),
  defect_type         varchar,
  defect_cause        varchar,
  responsible_dept    varchar,
  worker              varchar,
  process_status      varchar,
  inspection_date     date,
  inspector           varchar,
  model_name          varchar,
  product_type        varchar,
  is_locked           varchar(1) DEFAULT 'N'
);
CREATE INDEX IF NOT EXISTS idx_semi_product_inspection_group
  ON public.pms_quality_semi_product_inspection (inspection_group_id);
CREATE INDEX IF NOT EXISTS idx_semi_product_inspection_date
  ON public.pms_quality_semi_product_inspection (inspection_date);
