-- =============================================
-- 注册物流异常检测定时任务到 scheduled_task 表
-- 物流服务启动时会由 @PostConstruct 或 Flyway 自动执行
-- =============================================

INSERT INTO scheduled_task (
    task_name,
    description,
    task_group,
    cron_expression,
    task_class,
    method_name,
    task_params,
    status,
    concurrent,
    sync,
    task_type,
    max_retries,
    retry_interval,
    timeout,
    alert_enabled,
    misfire_policy,
    owner,
    created_at,
    updated_at
) VALUES (
    'logistics_exception_detect',
    '【P0】物流异常检测：检测EXCEPTION状态运单、同步失败（重试>5次）、停滞运单（>48h无更新）、派送超时（>24h），每小时第5分钟执行',
    'LOGISTICS',
    '0 5 * * * ?',
    'com.erp.logistics.job.LogisticsExceptionDetectJob',
    'detectExceptions',
    '{"staleHours":48,"maxRetry":5,"alertDedupHours":24}',
    'RUNNING',
    TRUE,
    FALSE,
    'BEAN',
    3,
    60,
    300,
    TRUE,
    'DO_NOTHING',
    'system',
    NOW(),
    NOW()
) ON DUPLICATE KEY UPDATE
    description = VALUES(description),
    cron_expression = VALUES(cron_expression),
    task_params = VALUES(task_params),
    status = CASE WHEN status = 'DRAFT' THEN 'RUNNING' ELSE status END;

-- =============================================
-- 轨迹批量同步任务（补充DB记录，与代码中的@Scheduled配合）
-- 注意：代码中已有 @Scheduled(fixedDelay)，DB记录用于可视化管理和告警
-- =============================================

INSERT INTO scheduled_task (
    task_name,
    description,
    task_group,
    cron_expression,
    task_class,
    method_name,
    task_params,
    status,
    concurrent,
    sync,
    task_type,
    max_retries,
    retry_interval,
    timeout,
    alert_enabled,
    misfire_policy,
    owner,
    created_at,
    updated_at
) VALUES (
    'logistics_trace_sync',
    '【P1】物流轨迹批量同步：每30分钟同步一次待同步运单轨迹，批量处理最多100条，支持失败重试3次',
    'LOGISTICS',
    '0 */30 * * * ?',
    'com.erp.logistics.service.TraceSyncService',
    'syncPending',
    '{"batchSize":100,"maxRetry":3,"intervalMinutes":30}',
    'RUNNING',
    TRUE,
    FALSE,
    'BEAN',
    3,
    60,
    600,
    TRUE,
    'DO_NOTHING',
    'system',
    NOW(),
    NOW()
) ON DUPLICATE KEY UPDATE
    description = VALUES(description),
    cron_expression = VALUES(cron_expression),
    task_params = VALUES(task_params);

-- =============================================
-- 运单签收状态确认任务（每日检查长期未签收运单）
-- =============================================

INSERT INTO scheduled_task (
    task_name,
    description,
    task_group,
    cron_expression,
    task_class,
    method_name,
    task_params,
    status,
    concurrent,
    sync,
    task_type,
    max_retries,
    retry_interval,
    timeout,
    alert_enabled,
    misfire_policy,
    owner,
    created_at,
    updated_at
) VALUES (
    'logistics_signed_check',
    '【P2】每日签收确认：每日9:00检查3天前仍非签收状态的长周期运单，发送客服跟催提醒',
    'LOGISTICS',
    '0 0 9 * * ?',
    'com.erp.logistics.job.LogisticsExceptionDetectJob',
    'detectLongPending',
    '{"pendingDays":3}',
    'DRAFT',
    TRUE,
    FALSE,
    'BEAN',
    2,
    30,
    300,
    FALSE,
    'DO_NOTHING',
    'system',
    NOW(),
    NOW()
) ON DUPLICATE KEY UPDATE
    description = VALUES(description);
