package com.hdkj.lt.bf.service.impl;

import com.hdkj.lt.bf.entity.dto.TodayOutageData;
import com.hdkj.lt.bf.entity.vo.IndicatorDashboardVO;
import com.hdkj.lt.bf.service.IndicatorDashboardService;
import com.hdkj.lt.bf.service.SjztPwwyyqxtPrmPdrOutageMvDsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 运行态指标仪表盘 Service 实现
 * <p>
 * 当前实现：
 * - ✅ 供电可靠性：停电时户数（今日 + 全年）、平均停电时长（今日 + 全年）
 * - ⏳ 电压质量、供电能力：预留 null（待后续接入数据源）
 *
 * @author lsl
 * @since 2026-07-21
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IndicatorDashboardServiceImpl implements IndicatorDashboardService {

    private final SjztPwwyyqxtPrmPdrOutageMvDsService outageService;

    @Override
    public IndicatorDashboardVO queryDashboard(String id, String type) {
        if (StringUtils.isBlank(id) || StringUtils.isBlank(type)) {
            log.warn("参数缺失: id={}, type={}", id, type);
            return null;
        }

        int nodeType;
        try {
            nodeType = Integer.parseInt(type);
        } catch (NumberFormatException e) {
            log.warn("type 参数非法: {}", type);
            return null;
        }

        // ========== 供电可靠性 ==========

        // 今日 — 实时表 grid_pd_outage_event_base
        TodayOutageData today = outageService.getTodayOutageData(id, nodeType);

        // 全年（1月1日 → 昨天）— T-2 表 sjzt_pwwyyqxt_prm_pdr_outage_mv_ds
        LocalDate todayDate = LocalDate.now();
        int year = todayDate.getYear();
        LocalDateTime yearStart = LocalDate.of(year, 1, 1).atStartOfDay();
        LocalDateTime yesterdayEnd = todayDate.minusDays(1).atTime(23, 59, 59);

        TodayOutageData yearStats = outageService.getOutageDurationHoursHouseholds(id, nodeType, yearStart, yesterdayEnd);

        // ========== 组装 VO ==========

        return IndicatorDashboardVO.builder()
                // 基本信息（预留）
                .indicatorResultId(null)
                .batchTime(null)
                .countyName(null)
                // 一、供电可靠性
                .outageHoursHouseholdsToday(today.getTotalHouseholds())
                .avgOutageDurationToday(today.getAvgDurationHours())
                .outageHoursHouseholdsYear(yearStats.getTotalHouseholds())
                .avgOutageDurationYear(yearStats.getAvgDurationHours())
                // 二、电压质量（全部预留）
                .iecUserVoltageIndex(null)
                .overUpperLimitAreaSevere(null)
                .overUpperLimitAreaGeneral(null)
                .overUpperLimitUserSevere(null)
                .overUpperLimitUserGeneral(null)
                .belowLowerLimitAreaSevere(null)
                .belowLowerLimitAreaGeneral(null)
                .belowLowerLimitUserSevere(null)
                .belowLowerLimitUserGeneral(null)
                // 三、供电能力（全部预留）
                .maxLoadRate(null)
                .avgLoadRate(null)
                .overloadEventTotal(null)
                .heavyLoadEventCount(null)
                .heavyLoadRate(null)
                .overloadEventCount(null)
                .overloadRate(null)
                .publicLineLossRate(null)
                .lineLossRate(null)
                .areaLineLossComplianceRate(null)
                .build();
    }
}
