-- ============================================
-- STORY #104: 增加报卡管理界面
-- 创建时间: 2026-03-05
-- 功能说明: 在疾病报告管理目录下增加报卡管理界面菜单
-- ============================================

-- 1. 创建疾病报告管理目录（如果不存在）
-- 注意：需要根据实际情况调整parent_id，这里假设是一级菜单
INSERT INTO "sys_menu" (
  "menu_name",
  "parent_id",
  "order_num",
  "path",
  "component",
  "query",
  "route_name",
  "is_frame",
  "is_cache",
  "menu_type",
  "visible",
  "status",
  "perms",
  "icon",
  "create_by",
  "create_time",
  "update_by",
  "update_time",
  "remark"
)
SELECT
  '疾病报告管理',
  0,  -- 一级菜单，根据实际情况调整
  100,
  'diseaseReport',
  NULL,
  NULL,
  NULL,
  '1',
  '0',
  'M',  -- M=目录
  '0',
  '0',
  NULL,
  'documentation',
  'admin',
  NOW(),
  NULL,
  NULL,
  '疾病报告管理目录'
WHERE NOT EXISTS (
  SELECT 1 FROM "sys_menu" WHERE "menu_name" = '疾病报告管理'
);

-- 2. 创建报卡管理菜单
-- 获取疾病报告管理目录的ID
INSERT INTO "sys_menu" (
  "menu_name",
  "parent_id",
  "order_num",
  "path",
  "component",
  "query",
  "route_name",
  "is_frame",
  "is_cache",
  "menu_type",
  "visible",
  "status",
  "perms",
  "icon",
  "create_by",
  "create_time",
  "update_by",
  "update_time",
  "remark"
)
SELECT
  '报卡管理',
  (SELECT "menu_id" FROM "sys_menu" WHERE "menu_name" = '疾病报告管理' LIMIT 1),
  1,
  'cardManagement',
  'cardmanagement/index',
  NULL,
  'CardManagement',
  '1',
  '0',
  'C',  -- C=菜单
  '0',
  '0',
  'card:management:list',
  'form',
  'admin',
  NOW(),
  NULL,
  NULL,
  '报卡管理界面'
WHERE NOT EXISTS (
  SELECT 1 FROM "sys_menu" WHERE "menu_name" = '报卡管理'
);

-- 3. 创建报卡管理相关按钮权限
-- 获取报卡管理菜单的ID
INSERT INTO "sys_menu" (
  "menu_name",
  "parent_id",
  "order_num",
  "path",
  "component",
  "query",
  "route_name",
  "is_frame",
  "is_cache",
  "menu_type",
  "visible",
  "status",
  "perms",
  "icon",
  "create_by",
  "create_time",
  "update_by",
  "update_time",
  "remark"
)
SELECT
  '报卡查询',
  (SELECT "menu_id" FROM "sys_menu" WHERE "menu_name" = '报卡管理' LIMIT 1),
  1,
  '',
  NULL,
  NULL,
  NULL,
  '1',
  '0',
  'F',  -- F=按钮
  '0',
  '0',
  'card:management:query',
  '#',
  'admin',
  NOW(),
  NULL,
  NULL,
  ''
WHERE NOT EXISTS (
  SELECT 1 FROM "sys_menu" WHERE "perms" = 'card:management:query'
);

INSERT INTO "sys_menu" (
  "menu_name",
  "parent_id",
  "order_num",
  "path",
  "component",
  "query",
  "route_name",
  "is_frame",
  "is_cache",
  "menu_type",
  "visible",
  "status",
  "perms",
  "icon",
  "create_by",
  "create_time",
  "update_by",
  "update_time",
  "remark"
)
SELECT
  '报卡审核',
  (SELECT "menu_id" FROM "sys_menu" WHERE "menu_name" = '报卡管理' LIMIT 1),
  2,
  '',
  NULL,
  NULL,
  NULL,
  '1',
  '0',
  'F',
  '0',
  '0',
  'card:management:audit',
  '#',
  'admin',
  NOW(),
  NULL,
  NULL,
  ''
WHERE NOT EXISTS (
  SELECT 1 FROM "sys_menu" WHERE "perms" = 'card:management:audit'
);

INSERT INTO "sys_menu" (
  "menu_name",
  "parent_id",
  "order_num",
  "path",
  "component",
  "query",
  "route_name",
  "is_frame",
  "is_cache",
  "menu_type",
  "visible",
  "status",
  "perms",
  "icon",
  "create_by",
  "create_time",
  "update_by",
  "update_time",
  "remark"
)
SELECT
  '报卡导出',
  (SELECT "menu_id" FROM "sys_menu" WHERE "menu_name" = '报卡管理' LIMIT 1),
  3,
  '',
  NULL,
  NULL,
  NULL,
  '1',
  '0',
  'F',
  '0',
  '0',
  'card:management:export',
  '#',
  'admin',
  NOW(),
  NULL,
  NULL,
  ''
WHERE NOT EXISTS (
  SELECT 1 FROM "sys_menu" WHERE "perms" = 'card:management:export'
);

-- 4. 创建数据库表（如果不存在）
-- 传染病报卡表
CREATE TABLE IF NOT EXISTS "infectious_card" (
  "id" BIGINT PRIMARY KEY,
  "card_no" VARCHAR(20) UNIQUE,
  "visit_id" BIGINT,
  "diag_id" BIGINT,
  "pat_id" BIGINT,
  "id_type" INTEGER,
  "id_no" VARCHAR(30),
  "pat_name" VARCHAR(50),
  "parent_name" VARCHAR(50),
  "sex" CHAR(1),
  "birthday" DATE,
  "age" INTEGER,
  "age_unit" CHAR(1),
  "workplace" VARCHAR(100),
  "phone" VARCHAR(20),
  "contact_phone" VARCHAR(20),
  "address_prov" VARCHAR(50),
  "address_city" VARCHAR(50),
  "address_county" VARCHAR(50),
  "address_town" VARCHAR(50),
  "address_village" VARCHAR(80),
  "address_house" VARCHAR(40),
  "patientbelong" VARCHAR(20),
  "occupation" VARCHAR(20),
  "disease_code" VARCHAR(10),
  "disease_name" VARCHAR(100),
  "disease_subtype" VARCHAR(50),
  "other_disease" VARCHAR(100),
  "disease_type" CHAR(1),
  "onset_date" DATE,
  "diag_date" TIMESTAMP,
  "death_date" DATE,
  "revised_disease_name" VARCHAR(100),
  "return_reason" VARCHAR(200),
  "report_org" VARCHAR(100),
  "report_org_phone" VARCHAR(20),
  "report_doc" VARCHAR(50),
  "report_date" DATE,
  "status" CHAR(1) DEFAULT '0',
  "fail_msg" VARCHAR(500),
  "xml_content" TEXT,
  "card_name_code" INTEGER,
  "registration_source" INTEGER,
  "dept_id" BIGINT,
  "dept_name" VARCHAR(100),
  "doctor_id" BIGINT,
  "create_by" VARCHAR(64),
  "create_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  "update_by" VARCHAR(64),
  "update_time" TIMESTAMP,
  "remark" VARCHAR(500)
);

-- 审核记录表
CREATE TABLE IF NOT EXISTS "infectious_audit" (
  "audit_id" BIGINT PRIMARY KEY,
  "card_id" BIGINT NOT NULL,
  "audit_seq" INTEGER NOT NULL,
  "audit_type" CHAR(1) NOT NULL,
  "audit_status_from" CHAR(1) NOT NULL,
  "audit_status_to" CHAR(1) NOT NULL,
  "audit_time" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "auditor_id" VARCHAR(20) NOT NULL,
  "auditor_name" VARCHAR(50) NOT NULL,
  "audit_opinion" TEXT,
  "reason_for_return" TEXT,
  "fail_reason_code" VARCHAR(20),
  "fail_reason_desc" TEXT,
  "is_batch" BOOLEAN DEFAULT FALSE,
  "batch_size" INTEGER DEFAULT 1,
  "create_by" VARCHAR(64),
  "create_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  "update_by" VARCHAR(64),
  "update_time" TIMESTAMP,
  "remark" VARCHAR(500)
);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_infectious_card_no ON "infectious_card"("card_no");
CREATE INDEX IF NOT EXISTS idx_infectious_card_status ON "infectious_card"("status");
CREATE INDEX IF NOT EXISTS idx_infectious_card_create_time ON "infectious_card"("create_time");
CREATE INDEX IF NOT EXISTS idx_infectious_audit_card_id ON "infectious_audit"("card_id");

-- 添加迁移记录
INSERT INTO __MigrationsHistory (MigrationId, ProductVersion)
VALUES ('202603051000_card_management_menu', '1.0.0')
ON CONFLICT DO NOTHING;
