package com.zsElectric.boot.charging.quartz;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zsElectric.boot.charging.entity.ThirdPartyConnectorInfo;
import com.zsElectric.boot.charging.mapper.ThirdPartyConnectorInfoMapper;
import com.zsElectric.boot.charging.service.ChargingBusinessService;
import com.zsElectric.boot.charging.vo.ChargingPricePolicyVO;
import com.zsElectric.boot.charging.vo.QueryStationsInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 充电站信息同步定时任务
 *
 * @author system
 * @since 2025-12-11
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ChargingJob {

    private final ChargingBusinessService chargingBusinessService;
    private final ThirdPartyConnectorInfoMapper connectorInfoMapper;

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 定时同步充电站信息
     * 每半小时执行一次，从第三方接口获取充电站信息并存储到数据库
     * cron表达式: 0 0/30 * * * ? 表示每小时的0分和30分执行
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void syncStationsInfo() {
        log.info("开始执行充电站信息同步定时任务");
        
        try {
            // 获取当前时间作为查询时间
            String lastQueryTime = LocalDateTime.now().minusMinutes(30).format(FORMATTER);
            
            // 分页查询，每次查询100条
            Integer pageNo = 1;
            Integer pageSize = 100;
            
            log.info("查询参数 - LastQueryTime: {}, PageNo: {}, PageSize: {}", lastQueryTime, pageNo, pageSize);
            
            // 调用业务服务查询充电站信息（会自动保存到数据库）
            QueryStationsInfoVO result = chargingBusinessService.queryStationsInfo(lastQueryTime, pageNo, pageSize);
            
            if (result != null && result.getStationInfos() != null) {
                log.info("充电站信息同步完成，共同步 {} 个充电站", result.getStationInfos().size());
            } else {
                log.warn("充电站信息同步结果为空");
            }
            
        } catch (JsonProcessingException e) {
            log.error("充电站信息同步失败 - JSON处理异常", e);
        } catch (Exception e) {
            log.error("充电站信息同步失败 - 系统异常", e);
        }
        
        log.info("充电站信息同步定时任务执行结束");
    }

    /**
     * 定时同步设备价格策略信息
     * 每天凌晨5:30执行，查询所有充电桩的价格策略并存储到数据库
     * cron表达式: 0 30 5 * * ? 表示每天凌晨5点30分执行
     */
    @Scheduled(cron = "0 */3 * * * ?")
    public void syncEquipmentPricePolicy() {
        log.info("开始执行设备价格策略同步定时任务");
        
        try {
            // 查询所有充电桩接口信息
            List<ThirdPartyConnectorInfo> connectorList = connectorInfoMapper.selectList(null);
            
            if (CollectionUtils.isEmpty(connectorList)) {
                log.warn("未查询到充电桩接口信息，跳过价格策略同步");
                return;
            }
            
            log.info("开始同步价格策略，总共 {} 个充电桩接口", connectorList.size());
            
            int successCount = 0;
            int failCount = 0;
            
            // 遍历每个充电桩接口，查询价格策略
            for (ThirdPartyConnectorInfo connector : connectorList) {
                try {
                    String connectorId = connector.getConnectorId();
                    String equipmentId = connector.getEquipmentId();
                    
                    log.info("查询价格策略 - connectorId: {}, equipmentId: {}", connectorId, equipmentId);
                    
                    // 调用业务服务查询价格策略（会自动保存到数据库）
                    ChargingPricePolicyVO result = chargingBusinessService.queryEquipBusinessPolicy(equipmentId, connectorId);
                    
                    if (result != null && result.getSuccStat() != null && result.getSuccStat() == 0) {
                        successCount++;
                        log.info("价格策略同步成功 - connectorId: {}, 时段数: {}", connectorId, result.getSumPeriod());
                    } else {
                        failCount++;
                        log.warn("价格策略同步失败 - connectorId: {}, 失败原因: {}", connectorId, 
                                result != null ? result.getFailReason() : "返回结果为空");
                    }
                    
                    // 适当延迟，避免频繁调用第三方接口
                    Thread.sleep(500);
                    
                } catch (Exception e) {
                    failCount++;
                    log.error("同步价格策略失败 - connectorId: {}", connector.getConnectorId(), e);
                }
            }
            
            log.info("设备价格策略同步完成 - 成功: {}, 失败: {}, 总数: {}", successCount, failCount, connectorList.size());
            
        } catch (Exception e) {
            log.error("设备价格策略同步定时任务执行异常", e);
        }
        
        log.info("设备价格策略同步定时任务执行结束");
    }
}
