-- 住院号为唯一定位条件：数据库强制“不能为空且唯一”。
-- 不校验 ZY 位数格式；空住院号记录会先删除，重复住院号保留 id 最大/最新的一条。

DELETE FROM "Patient_Lists"
WHERE inpatient_no IS NULL OR btrim(inpatient_no) = '';

DELETE FROM "Patient_Lists" p
USING (
  SELECT record_id
  FROM (
    SELECT record_id, row_number() OVER (PARTITION BY inpatient_no ORDER BY record_id DESC) AS rn
    FROM "Patient_Lists"
  ) ranked
  WHERE rn > 1
) duplicate_rows
WHERE p.record_id = duplicate_rows.record_id;

ALTER TABLE "Patient_Lists"
  DROP CONSTRAINT IF EXISTS ck_patient_lists_inpatient_no_format;

ALTER TABLE "Patient_Lists"
  DROP CONSTRAINT IF EXISTS uq_patient_lists_inpatient_no;

DROP INDEX IF EXISTS uq_patient_lists_inpatient_no;

ALTER TABLE "Patient_Lists"
  DROP CONSTRAINT IF EXISTS ck_patient_lists_inpatient_no_present;

ALTER TABLE "Patient_Lists"
  DROP CONSTRAINT IF EXISTS ck_patient_lists_inpatient_no_required;

ALTER TABLE "Patient_Lists"
  ALTER COLUMN inpatient_no SET NOT NULL;

ALTER TABLE "Patient_Lists"
  ADD CONSTRAINT ck_patient_lists_inpatient_no_present
  CHECK (btrim(inpatient_no) <> '');

ALTER TABLE "Patient_Lists"
  ADD CONSTRAINT uq_patient_lists_inpatient_no UNIQUE (inpatient_no);

COMMENT ON COLUMN "Patient_Lists".inpatient_no
  IS '住院号，不能为空且全库唯一；不强制校验格式';

COMMENT ON CONSTRAINT ck_patient_lists_inpatient_no_present ON "Patient_Lists"
  IS '住院号不能为空；格式不做强制校验';

COMMENT ON CONSTRAINT uq_patient_lists_inpatient_no ON "Patient_Lists"
  IS '保证同一个住院号只归档一条患者记录';

DELETE FROM "Patient_FrontPages"
WHERE inpatient_no IS NULL OR btrim(inpatient_no) = '';

DELETE FROM "Patient_FrontPages" p
USING (
  SELECT id
  FROM (
    SELECT id, row_number() OVER (PARTITION BY inpatient_no ORDER BY id DESC) AS rn
    FROM "Patient_FrontPages"
  ) ranked
  WHERE rn > 1
) duplicate_rows
WHERE p.id = duplicate_rows.id;

ALTER TABLE "Patient_FrontPages"
  DROP CONSTRAINT IF EXISTS "ck_Patient_FrontPages_inpatient_no_format";

ALTER TABLE "Patient_FrontPages"
  DROP CONSTRAINT IF EXISTS ck_patient_frontpages_inpatient_no_format;

ALTER TABLE "Patient_FrontPages"
  DROP CONSTRAINT IF EXISTS uq_patient_frontpages_inpatient_no;

DROP INDEX IF EXISTS uq_patient_frontpages_inpatient_no;

ALTER TABLE "Patient_FrontPages"
  DROP CONSTRAINT IF EXISTS ck_patient_frontpages_inpatient_no_present;

ALTER TABLE "Patient_FrontPages"
  DROP CONSTRAINT IF EXISTS "ck_Patient_FrontPages_inpatient_no_required";

ALTER TABLE "Patient_FrontPages"
  DROP CONSTRAINT IF EXISTS ck_patient_frontpages_inpatient_no_required;

ALTER TABLE "Patient_FrontPages"
  ALTER COLUMN inpatient_no SET NOT NULL;

ALTER TABLE "Patient_FrontPages"
  ADD CONSTRAINT ck_patient_frontpages_inpatient_no_present
  CHECK (btrim(inpatient_no) <> '');

ALTER TABLE "Patient_FrontPages"
  ADD CONSTRAINT uq_patient_frontpages_inpatient_no UNIQUE (inpatient_no);

COMMENT ON COLUMN "Patient_FrontPages".inpatient_no
  IS '住院号，不能为空且全库唯一；不强制校验格式';

COMMENT ON CONSTRAINT ck_patient_frontpages_inpatient_no_present ON "Patient_FrontPages"
  IS '住院号不能为空；格式不做强制校验';

COMMENT ON CONSTRAINT uq_patient_frontpages_inpatient_no ON "Patient_FrontPages"
  IS '保证同一个住院号只保留一条首页记录';
