package com.zcxk.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zcxk.dao.ReportWaterPumpStateMapper;
import com.zcxk.dto.DeviceDto;
import com.zcxk.dto.watePump.ReprotWaterPumpDto;
import com.zcxk.dto.watePump.ReprotWaterPumpQueryDto;
import com.zcxk.service.DeviceService;
import com.zcxk.service.ReportWaterPumpStateService;
import com.zcxk.util.DatesUtil;
import com.zcxk.util.UserUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Log4j2
public class ReportWaterPumpStateServiceImpl implements ReportWaterPumpStateService {

    @Autowired
    private ReportWaterPumpStateMapper reportWaterPumpStateMapper;

    @Autowired
    private DeviceService deviceService;

    @Override
    public List<ReprotWaterPumpDto> findReportWaterPumpStateList(Long sceneId, List<Long> sceneIds, Integer days) {
        // 查询场景下的所有设备信息
        List<DeviceDto> devices = new ArrayList<>();
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setSceneIds(sceneIds);
        if (deviceDto.getSceneIds().size() > 0){
            devices.addAll(deviceService.selectList(deviceDto));
        }
        ReprotWaterPumpQueryDto queryDto = new ReprotWaterPumpQueryDto();
        queryDto.setDeviceIds(devices);
        queryDto.setTenantId(UserUtil.getCurrentUser().getTenantId());
        // queryDto.setDay(days);
        queryDto.setBeginTime(DatesUtil.formatDate(DatesUtil.addDayOfDate(new Date(), -(days-1)),"yyyy-MM-dd") + " 00:00:00");
        queryDto.setSceneId(sceneId);
        List<ReprotWaterPumpDto>  result = reportWaterPumpStateMapper.findReportWaterPumpStateList(queryDto);
        if (CollectionUtil.isEmpty(result)) {
            return result;
        }
        Comparator<ReprotWaterPumpDto> byYear  = Comparator.comparing(ReprotWaterPumpDto::getYear);
        Comparator<ReprotWaterPumpDto> byMonth = Comparator.comparing(ReprotWaterPumpDto::getMonth);
        Comparator<ReprotWaterPumpDto> byDay   = Comparator.comparing(ReprotWaterPumpDto::getDay);
        Collections.sort(result, byYear.thenComparing(byMonth.thenComparing(byDay)));
        return result;
    }
}
