-- ========================================
-- 慢性疾病档案表
-- ========================================
DROP TABLE IF EXISTS chronic_disease_record;
CREATE TABLE chronic_disease_record (
    id                  BIGSERIAL       PRIMARY KEY,
    member_id           BIGINT          NOT NULL,
    disease_id          BIGINT,
    disease_name        VARCHAR(100)    NOT NULL,
    disease_code        VARCHAR(20),
    diagnosis_date      DATE,
    disease_status      SMALLINT        NOT NULL DEFAULT 1,
    main_doctor         VARCHAR(50),
    hospital            VARCHAR(100),
    department          VARCHAR(50),
    notes               TEXT,
    is_active           SMALLINT        DEFAULT 1,
    -- 若依标准字段
    create_by           VARCHAR(64)     DEFAULT '',
    create_time         TIMESTAMP       DEFAULT CURRENT_TIMESTAMP,
    update_by           VARCHAR(64)     DEFAULT '',
    update_time         TIMESTAMP       DEFAULT CURRENT_TIMESTAMP,
    remark              VARCHAR(500)    DEFAULT '',
    del_flag            SMALLINT        DEFAULT 0
);

COMMENT ON TABLE chronic_disease_record IS '慢性疾病档案表';
COMMENT ON COLUMN chronic_disease_record.id IS '主键ID';
COMMENT ON COLUMN chronic_disease_record.member_id IS '关联成员ID';
COMMENT ON COLUMN chronic_disease_record.disease_id IS '关联疾病ID（关联health_diseases表）';
COMMENT ON COLUMN chronic_disease_record.disease_name IS '疾病名称（高血压/糖尿病/冠心病等）';
COMMENT ON COLUMN chronic_disease_record.disease_code IS '疾病编码（ICD-10标准）';
COMMENT ON COLUMN chronic_disease_record.diagnosis_date IS '确诊日期';
COMMENT ON COLUMN chronic_disease_record.disease_status IS '状态：1-稳定 2-需关注 3-急性期';
COMMENT ON COLUMN chronic_disease_record.main_doctor IS '主治医生';
COMMENT ON COLUMN chronic_disease_record.hospital IS '就诊医院';
COMMENT ON COLUMN chronic_disease_record.department IS '科室';
COMMENT ON COLUMN chronic_disease_record.notes IS '备注说明';
COMMENT ON COLUMN chronic_disease_record.is_active IS '是否启用：1-是 0-否';
COMMENT ON COLUMN chronic_disease_record.create_by IS '创建者';
COMMENT ON COLUMN chronic_disease_record.create_time IS '创建时间';
COMMENT ON COLUMN chronic_disease_record.update_by IS '更新者';
COMMENT ON COLUMN chronic_disease_record.update_time IS '更新时间';
COMMENT ON COLUMN chronic_disease_record.remark IS '备注';
COMMENT ON COLUMN chronic_disease_record.del_flag IS '删除标记：0-正常 1-删除';

CREATE INDEX idx_cdr_member_id ON chronic_disease_record(member_id);
CREATE INDEX idx_cdr_disease_id ON chronic_disease_record(disease_id);
CREATE INDEX idx_cdr_disease_status ON chronic_disease_record(disease_status);
CREATE INDEX idx_cdr_is_active ON chronic_disease_record(is_active);

-- ========================================
-- 菜单 SQL（若依框架标准格式）
-- ========================================
-- 慢性疾病档案菜单
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES ('慢性疾病档案', 0, 5, 'chronicDisease', 'health/chronicDisease/index', 1, 0, 'C', '0', '0', 'health:chronicDisease:list', '#', 'admin', NOW(), '慢性疾病档案菜单');

-- 获取刚插入的菜单ID
SELECT @parentId := LAST_INSERT_ID();

-- 按钮权限
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES ('慢性疾病档案查询', @parentId, 1, '#', '', 1, 0, 'F', '0', '0', 'health:chronicDisease:query', '#', 'admin', NOW(), '');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES ('慢性疾病档案权增', @parentId, 2, '#', '', 1, 0, 'F', '0', '0', 'health:chronicDisease:add', '#', 'admin', NOW(), '');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES ('慢性疾病档案修改', @parentId, 3, '#', '', 1, 0, 'F', '0', '0', 'health:chronicDisease:edit', '#', 'admin', NOW(), '');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES ('慢性疾病档案删除', @parentId, 4, '#', '', 1, 0, 'F', '0', '0', 'health:chronicDisease:remove', '#', 'admin', NOW(), '');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES ('慢性疾病档案导出', @parentId, 5, '#', '', 1, 0, 'F', '0', '0', 'health:chronicDisease:export', '#', 'admin', NOW(), '');

-- ========================================
-- 字典数据
-- ========================================
-- 疾病状态字典
INSERT INTO sys_dict_type (dict_name, dict_type, status, create_by, create_time, remark)
VALUES ('疾病状态', 'disease_status', '0', 'admin', NOW(), '慢性疾病状态');

INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark)
VALUES (1, '稳定', '1', 'disease_status', '', 'success', 'Y', '0', 'admin', NOW(), '疾病状态-稳定');

INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark)
VALUES (2, '需关注', '2', 'disease_status', '', 'warning', 'N', '0', 'admin', NOW(), '疾病状态-需关注');

INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark)
VALUES (3, '急性期', '3', 'disease_status', '', 'danger', 'N', '0', 'admin', NOW(), '疾病状态-急性期');

-- 是否启用字典（如果不存在）
INSERT INTO sys_dict_type (dict_name, dict_type, status, create_by, create_time, remark)
VALUES ('是否启用', 'is_active', '0', 'admin', NOW(), '是否启用状态')
ON CONFLICT (dict_type) DO NOTHING;

INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark)
VALUES (1, '是', '1', 'is_active', '', 'success', 'Y', '0', 'admin', NOW(), '启用')
ON CONFLICT DO NOTHING;

INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark)
VALUES (2, '否', '0', 'is_active', '', 'danger', 'N', '0', 'admin', NOW(), '停用')
ON CONFLICT DO NOTHING;