-- ========================================
-- 用药任务定时任务配置
-- 在 sys_job 表中插入定时任务
-- ========================================

-- 1. 每日生成服药任务（每日 00:05 执行）
INSERT INTO sys_job (job_id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status, create_by, create_time, remark)
VALUES (
    nextval('seq_sys_job'),
    '每日生成服药任务',
    'DEFAULT',
    'medicationTaskJob.generateDailyTasks',
    '0 5 0 * * ?',
    '2',
    '1',
    '0',
    'admin',
    NOW(),
    '每日凌晨00:05自动生成当天的服药任务'
);

-- 2. 扫描并发送服药提醒（每分钟执行）
INSERT INTO sys_job (job_id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status, create_by, create_time, remark)
VALUES (
    nextval('seq_sys_job'),
    '扫描服药提醒',
    'DEFAULT',
    'medicationRemindJob.scanAndSendReminders',
    '0 * * * * ?',
    '2',
    '1',
    '0',
    'admin',
    NOW(),
    '每分钟扫描待提醒的服药任务并发送提醒'
);

-- 3. 扫描超时未服药记录（每5分钟执行）
INSERT INTO sys_job (job_id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status, create_by, create_time, remark)
VALUES (
    nextval('seq_sys_job'),
    '扫描超时未服药',
    'DEFAULT',
    'medicationRemindJob.scanOverdueRecords',
    '0 */5 * * * ?',
    '2',
    '1',
    '0',
    'admin',
    NOW(),
    '每5分钟扫描超时未服药的记录并发送提醒'
);

-- 4. 标记过期服药记录（每小时执行）
INSERT INTO sys_job (job_id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status, create_by, create_time, remark)
VALUES (
    nextval('seq_sys_job'),
    '标记过期服药记录',
    'DEFAULT',
    'medicationTaskJob.markExpiredRecords',
    '0 0 * * * ?',
    '2',
    '1',
    '0',
    'admin',
    NOW(),
    '每小时检查并标记过期的服药记录'
);

-- ========================================
-- 提醒日志表（可选，用于记录提醒历史）
-- ========================================
DROP TABLE IF EXISTS health_medication_remind_log;
CREATE TABLE health_medication_remind_log (
    id              BIGINT          NOT NULL,
    record_id       BIGINT          NOT NULL,
    plan_id         BIGINT          NOT NULL,
    person_id       BIGINT          NOT NULL,
    remind_type     CHAR(1)         DEFAULT '1' COMMENT '提醒类型：1-按时提醒 2-超时提醒 3-漏服通知家属',
    remind_time     DATETIME        NOT NULL,
    remind_channel  VARCHAR(20)     DEFAULT 'app' COMMENT '提醒渠道：app/sms/wechat',
    remind_result   CHAR(1)         DEFAULT '1' COMMENT '结果：1-成功 2-失败',
    error_msg       VARCHAR(500)    NULL,
    create_time     DATETIME        DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
) ENGINE=InnoDB COMMENT='用药提醒日志表';

CREATE INDEX idx_rmrl_record_id ON health_medication_remind_log(record_id);
CREATE INDEX idx_rmrl_person_id ON health_medication_remind_log(person_id);
CREATE INDEX idx_rmrl_remind_time ON health_medication_remind_log(remind_time);