package com.ruoyi.jarvis.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.jarvis.domain.TencentDocBatchPushRecord;
import com.ruoyi.jarvis.domain.TencentDocOperationLog;
import com.ruoyi.jarvis.mapper.TencentDocBatchPushRecordMapper;
import com.ruoyi.jarvis.mapper.TencentDocOperationLogMapper;
import com.ruoyi.jarvis.service.ITencentDocBatchPushService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 腾讯文档批量推送记录服务实现
 */
@Service
public class TencentDocBatchPushServiceImpl implements ITencentDocBatchPushService {

    @Resource
    private TencentDocBatchPushRecordMapper batchPushRecordMapper;

    @Resource
    private TencentDocOperationLogMapper operationLogMapper;

    @Resource
    private RedisCache redisCache;

    private static final String DELAYED_PUSH_TASK_KEY = "tendoc:delayed_push:task_scheduled";
    private static final String DELAYED_PUSH_SCHEDULE_TIME_KEY = "tendoc:delayed_push:next_time";

    @Override
    public String createBatchPushRecord(String fileId, String sheetId, String pushType,
                                         String triggerSource, Integer startRow, Integer endRow) {
        String batchId = UUID.randomUUID().toString().replace("-", "");
        
        TencentDocBatchPushRecord record = new TencentDocBatchPushRecord();
        record.setBatchId(batchId);
        record.setFileId(fileId);
        record.setSheetId(sheetId);
        record.setPushType(pushType);
        record.setTriggerSource(triggerSource);
        record.setStartTime(new Date());
        record.setStartRow(startRow);
        record.setEndRow(endRow);
        record.setTotalRows(endRow - startRow + 1);
        record.setSuccessCount(0);
        record.setSkipCount(0);
        record.setErrorCount(0);
        record.setStatus("RUNNING");
        
        batchPushRecordMapper.insertBatchPushRecord(record);
        
        return batchId;
    }

    @Override
    public void updateBatchPushRecord(String batchId, String status, Integer successCount,
                                       Integer skipCount, Integer errorCount, String resultMessage, String errorMessage) {
        TencentDocBatchPushRecord record = new TencentDocBatchPushRecord();
        record.setBatchId(batchId);
        record.setEndTime(new Date());
        record.setStatus(status);
        record.setSuccessCount(successCount);
        record.setSkipCount(skipCount);
        record.setErrorCount(errorCount);
        record.setResultMessage(resultMessage);
        record.setErrorMessage(errorMessage);
        
        // 计算耗时
        TencentDocBatchPushRecord existingRecord = batchPushRecordMapper.selectByBatchId(batchId);
        if (existingRecord != null && existingRecord.getStartTime() != null) {
            long durationMs = new Date().getTime() - existingRecord.getStartTime().getTime();
            record.setDurationMs(durationMs);
        }
        
        batchPushRecordMapper.updateBatchPushRecord(record);
    }

    @Override
    public TencentDocBatchPushRecord getBatchPushRecord(String batchId) {
        TencentDocBatchPushRecord record = batchPushRecordMapper.selectByBatchId(batchId);
        if (record != null) {
            // 加载关联的操作日志
            List<TencentDocOperationLog> logs = operationLogMapper.selectLogsByBatchId(batchId);
            record.setOperationLogs(logs);
        }
        return record;
    }

    @Override
    public List<TencentDocBatchPushRecord> getBatchPushRecordListWithLogs(String fileId, String sheetId, Integer limit) {
        TencentDocBatchPushRecord query = new TencentDocBatchPushRecord();
        query.setFileId(fileId);
        query.setSheetId(sheetId);
        
        List<TencentDocBatchPushRecord> records = limit != null && limit > 0 
            ? batchPushRecordMapper.selectRecentRecords(fileId, limit)
            : batchPushRecordMapper.selectBatchPushRecordList(query);
        
        // 为每条记录加载操作日志
        for (TencentDocBatchPushRecord record : records) {
            List<TencentDocOperationLog> logs = operationLogMapper.selectLogsByBatchId(record.getBatchId());
            record.setOperationLogs(logs);
        }
        
        return records;
    }

    @Override
    public TencentDocBatchPushRecord getLastSuccessRecord(String fileId, String sheetId) {
        TencentDocBatchPushRecord record = batchPushRecordMapper.selectLastSuccessRecord(fileId, sheetId);
        if (record != null) {
            List<TencentDocOperationLog> logs = operationLogMapper.selectLogsByBatchId(record.getBatchId());
            record.setOperationLogs(logs);
        }
        return record;
    }

    @Override
    public Map<String, Object> getPushStatusAndCountdown() {
        Map<String, Object> result = new HashMap<>();
        
        // 检查是否有定时任务（通过时间戳是否存在来判断）
        Long scheduleTime = redisCache.getCacheObject(DELAYED_PUSH_SCHEDULE_TIME_KEY);
        
        // 如果有推送时间戳，就认为有定时任务
        boolean isScheduled = scheduleTime != null;
        result.put("isScheduled", isScheduled);
        
        if (isScheduled && scheduleTime != null) {
            long now = System.currentTimeMillis();
            long remainingMs = scheduleTime - now;
            
            if (remainingMs > 0) {
                result.put("scheduledTime", new Date(scheduleTime));
                result.put("remainingSeconds", remainingMs / 1000);
                result.put("remainingMs", remainingMs);
                
                // 格式化倒计时显示
                long minutes = remainingMs / 60000;
                long seconds = (remainingMs % 60000) / 1000;
                result.put("countdownText", String.format("%d分%d秒", minutes, seconds));
            } else {
                result.put("remainingSeconds", 0);
                result.put("remainingMs", 0);
                result.put("countdownText", "即将执行");
            }
        } else {
            result.put("scheduledTime", null);
            result.put("remainingSeconds", 0);
            result.put("remainingMs", 0);
            result.put("countdownText", "无定时任务");
        }
        
        return result;
    }
}

