package com.goomood.phm.service.impl;

import com.goomood.phm.domain.FaultMonitor;
import com.goomood.phm.mapper.FaultMonitorAMapper;
import com.goomood.phm.service.FaultMonitorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 故障监控服务实现
 */
@Service
public class FaultMonitorServiceAImpl implements FaultMonitorService {

    @Resource
    private FaultMonitorAMapper faultMonitorMapper;

    @Override
    public Map<String, Long> getTop5FaultyPartsNextMonthInThreeYears() {
        // 计算关键日期
        LocalDate currentDate = LocalDate.now();
        LocalDate nextMonth = currentDate.plusMonths(1);
        int targetMonth = nextMonth.getMonthValue();
        
        // 查询近三年数据
        List<FaultMonitor> faultMonitors = faultMonitorMapper.findByIncidentDateBetween(
            toDate(currentDate.minusYears(3)),
            toDate(currentDate)
        );
        
        return faultMonitors.stream()
            .filter(fault -> isTargetMonth(fault.getIncidentDate(), targetMonth))
            .filter(this::isValidFaultPart)
            .collect(Collectors.groupingBy(
                FaultMonitor::getFaultyPartName,
                Collectors.counting()
            ))
            .entrySet().stream()
            .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
            .limit(5)
            .collect(Collectors.toMap(
                Map.Entry::getKey,
                Map.Entry::getValue,
                (e1, e2) -> e1,
                LinkedHashMap::new
            ));
    }

    @Override
    public Map<String, Long> getTop5FaultyPartsInSixMonths() {
        LocalDate currentDate = LocalDate.now();
        
        List<FaultMonitor> faultMonitors = faultMonitorMapper.findByIncidentDateBetween(
            toDate(currentDate.minusMonths(6)),
            toDate(currentDate)
        );
        
        return faultMonitors.stream()
            .filter(this::isValidFaultPart)
            .collect(Collectors.groupingBy(
                FaultMonitor::getFaultyPartName,
                Collectors.counting()
            ))
            .entrySet().stream()
            .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
            .limit(5)
            .collect(Collectors.toMap(
                Map.Entry::getKey,
                Map.Entry::getValue,
                (e1, e2) -> e1,
                LinkedHashMap::new
            ));
    }
    
    // 辅助方法：Date转LocalDate
    private LocalDate toLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
    
    // 辅助方法：LocalDate转Date
    private Date toDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
    
    // 检查是否为目标月份的故障
    private boolean isTargetMonth(Date date, int targetMonth) {
        if (date == null) return false;
        return toLocalDate(date).getMonthValue() == targetMonth;
    }
    
    // 验证故障部件有效性
    private boolean isValidFaultPart(FaultMonitor fault) {
        return fault != null 
            && fault.getFaultyPartName() != null 
            && !fault.getFaultyPartName().trim().isEmpty();
    }
}    