-- ----------------------------
-- 用药计划表
-- ----------------------------
DROP TABLE IF EXISTS health_medication_plan;
CREATE TABLE health_medication_plan (
    id              BIGINT          NOT NULL,
    person_id       BIGINT          NOT NULL,
    medicine_id     BIGINT          NOT NULL,
    stock_in_id     BIGINT          NULL,
    plan_name       VARCHAR(200)    NULL,
    dosage          DECIMAL(10,2)   NOT NULL,
    dosage_unit     VARCHAR(20)     NULL,
    frequency       INT             NOT NULL,
    frequency_type  CHAR(1)         DEFAULT '1',
    time_points     VARCHAR(500)    NULL,
    week_days       VARCHAR(50)     NULL,
    start_date      DATE            NOT NULL,
    end_date        DATE            NULL,
    reminder_enabled CHAR(1)        DEFAULT '0',
    reminder_minutes INT            DEFAULT 15,
    status          CHAR(1)         DEFAULT '1',
    del_flag        CHAR(1)         DEFAULT '0',
    create_by       VARCHAR(64)     DEFAULT '',
    create_time     DATETIME        NULL,
    update_by       VARCHAR(64)     DEFAULT '',
    update_time     DATETIME        NULL,
    remark          VARCHAR(500)    NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB COMMENT='用药计划表';

CREATE INDEX idx_person_id ON health_medication_plan(person_id);
CREATE INDEX idx_medicine_id ON health_medication_plan(medicine_id);
CREATE INDEX idx_status ON health_medication_plan(status);
CREATE INDEX idx_start_date ON health_medication_plan(start_date);

-- ----------------------------
-- 用药记录表
-- ----------------------------
DROP TABLE IF EXISTS health_medication_record;
CREATE TABLE health_medication_record (
    id              BIGINT          NOT NULL,
    plan_id         BIGINT          NOT NULL,
    person_id       BIGINT          NOT NULL,
    medicine_id     BIGINT          NOT NULL,
    scheduled_time  DATETIME        NOT NULL,
    actual_time     DATETIME        NULL,
    dosage          DECIMAL(10,2)   NULL,
    dosage_unit     VARCHAR(20)     NULL,
    status          CHAR(1)         DEFAULT '1',
    del_flag        CHAR(1)         DEFAULT '0',
    create_by       VARCHAR(64)     DEFAULT '',
    create_time     DATETIME        NULL,
    update_by       VARCHAR(64)     DEFAULT '',
    update_time     DATETIME        NULL,
    remark          VARCHAR(500)    NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB COMMENT='用药记录表';

CREATE INDEX idx_plan_id ON health_medication_record(plan_id);
CREATE INDEX idx_person_id ON health_medication_record(person_id);
CREATE INDEX idx_scheduled_time ON health_medication_record(scheduled_time);
CREATE INDEX idx_status ON health_medication_record(status);

-- ----------------------------
-- 字典数据
-- ----------------------------
INSERT INTO sys_dict_type (dict_id, dict_name, dict_type, status, create_by, create_time, remark) 
VALUES 
(nextval('seq_sys_dict_type'), '用药频率类型', 'medication_frequency_type', '0', 'admin', NOW(), '用药频率类型字典'),
(nextval('seq_sys_dict_type'), '用药计划状态', 'medication_plan_status', '0', 'admin', NOW(), '用药计划状态字典'),
(nextval('seq_sys_dict_type'), '用药记录状态', 'medication_record_status', '0', 'admin', NOW(), '用药记录状态字典');

INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark) 
VALUES 
(nextval('seq_sys_dict_data'), 1, '每日', '1', 'medication_frequency_type', '', 'primary', 'Y', '0', 'admin', NOW(), NULL),
(nextval('seq_sys_dict_data'), 2, '隔日', '2', 'medication_frequency_type', '', 'success', 'N', '0', 'admin', NOW(), NULL),
(nextval('seq_sys_dict_data'), 3, '每周', '3', 'medication_frequency_type', '', 'info', 'N', '0', 'admin', NOW(), NULL),
(nextval('seq_sys_dict_data'), 4, '自定义', '4', 'medication_frequency_type', '', 'warning', 'N', '0', 'admin', NOW(), NULL);

INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark) 
VALUES 
(nextval('seq_sys_dict_data'), 1, '进行中', '1', 'medication_plan_status', '', 'primary', 'Y', '0', 'admin', NOW(), NULL),
(nextval('seq_sys_dict_data'), 2, '已结束', '2', 'medication_plan_status', '', 'info', 'N', '0', 'admin', NOW(), NULL),
(nextval('seq_sys_dict_data'), 3, '已暂停', '3', 'medication_plan_status', '', 'warning', 'N', '0', 'admin', NOW(), NULL);

INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark) 
VALUES 
(nextval('seq_sys_dict_data'), 1, '待服用', '1', 'medication_record_status', '', 'warning', 'Y', '0', 'admin', NOW(), NULL),
(nextval('seq_sys_dict_data'), 2, '已服用', '2', 'medication_record_status', '', 'success', 'N', '0', 'admin', NOW(), NULL),
(nextval('seq_sys_dict_data'), 3, '已跳过', '3', 'medication_record_status', '', 'info', 'N', '0', 'admin', NOW(), NULL),
(nextval('seq_sys_dict_data'), 4, '已过期', '4', 'medication_record_status', '', 'danger', 'N', '0', 'admin', NOW(), NULL);

-- ----------------------------
-- 菜单权限
-- ----------------------------
-- 用药计划菜单
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) 
VALUES 
(nextval('seq_sys_menu'), '用药计划', (SELECT menu_id FROM sys_menu WHERE menu_name = '健康管理' LIMIT 1), 10, 'medicationPlan', 'health/medicationPlan/index', NULL, 1, 0, 'C', '0', '0', 'health:medicationPlan:list', 'calendar', 'admin', NOW(), '用药计划菜单');

-- 用药计划按钮权限
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) 
SELECT nextval('seq_sys_menu'), '用药计划查询', m.menu_id, 1, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationPlan:query', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationPlan:list'
UNION ALL
SELECT nextval('seq_sys_menu'), '用药计划新增', m.menu_id, 2, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationPlan:add', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationPlan:list'
UNION ALL
SELECT nextval('seq_sys_menu'), '用药计划修改', m.menu_id, 3, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationPlan:edit', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationPlan:list'
UNION ALL
SELECT nextval('seq_sys_menu'), '用药计划删除', m.menu_id, 4, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationPlan:remove', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationPlan:list'
UNION ALL
SELECT nextval('seq_sys_menu'), '用药计划导出', m.menu_id, 5, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationPlan:export', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationPlan:list';

-- 用药记录菜单
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) 
VALUES 
(nextval('seq_sys_menu'), '用药记录', (SELECT menu_id FROM sys_menu WHERE menu_name = '健康管理' LIMIT 1), 11, 'medicationRecord', 'health/medicationRecord/index', NULL, 1, 0, 'C', '0', '0', 'health:medicationRecord:list', 'log', 'admin', NOW(), '用药记录菜单');

-- 用药记录按钮权限
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) 
SELECT nextval('seq_sys_menu'), '用药记录查询', m.menu_id, 1, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationRecord:query', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationRecord:list'
UNION ALL
SELECT nextval('seq_sys_menu'), '用药记录新增', m.menu_id, 2, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationRecord:add', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationRecord:list'
UNION ALL
SELECT nextval('seq_sys_menu'), '用药记录修改', m.menu_id, 3, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationRecord:edit', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationRecord:list'
UNION ALL
SELECT nextval('seq_sys_menu'), '用药记录删除', m.menu_id, 4, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationRecord:remove', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationRecord:list'
UNION ALL
SELECT nextval('seq_sys_menu'), '用药记录导出', m.menu_id, 5, '', NULL, NULL, 1, 0, 'F', '0', '0', 'health:medicationRecord:export', '#', 'admin', NOW(), '' FROM sys_menu m WHERE m.perms = 'health:medicationRecord:list';