package com.njcn.device.pq.service.impl;

import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.device.common.service.GeneralDeviceService;
import com.njcn.device.pq.mapper.LargeScreenMapper;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.param.LargeScreenParam;
import com.njcn.device.pq.pojo.param.MonitoringPointScaleParam;
import com.njcn.device.pq.pojo.vo.MonitoringPointScaleVO;
import com.njcn.device.pq.pojo.vo.OldTimeVO;
import com.njcn.device.pq.service.LargeScreenService;

import com.njcn.system.pojo.enums.StatisticsEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @version 1.0.0
 * @author: zbj
 * @date: 2023/03/21
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LargeScreenServiceImpl implements LargeScreenService {

    private final GeneralDeviceService generalDeviceService;

    private final LargeScreenMapper largeScreenMapper;

    /**
     * 监测点规模(区域)
     */
    @Override
    public MonitoringPointScaleVO getMonitoringPointScale(LargeScreenParam largeScreenParam) {
        //创建返回VO
        MonitoringPointScaleVO result = new MonitoringPointScaleVO();
        ArrayList<MonitoringPointScaleParam> list = new ArrayList<>();
        DeviceInfoParam.BusinessParam deviceInfoParam = new DeviceInfoParam.BusinessParam();
        //部门索引
        deviceInfoParam.setDeptIndex(largeScreenParam.getDeptIndex());
        //统计类型
        SimpleDTO simpleDTO = new SimpleDTO();
        simpleDTO.setCode(String.valueOf(StatisticsEnum.POWER_NETWORK));
        deviceInfoParam.setStatisticalType(simpleDTO);
        //添加时间
        deviceInfoParam.setSearchBeginTime(largeScreenParam.getSearchBeginTime());
        deviceInfoParam.setSearchEndTime(largeScreenParam.getSearchEndTime());
        // 获取所有数据
        List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceService.getDeviceInfo(deviceInfoParam, Stream.of(0).collect(Collectors.toList()), Stream.of(1).collect(Collectors.toList()));

        for (GeneralDeviceDTO generalDeviceDTO : generalDeviceDTOList) {
            MonitoringPointScaleParam param = new MonitoringPointScaleParam();
            param.setIndex(generalDeviceDTO.getIndex());
            param.setName(generalDeviceDTO.getName());
            param.setCount(generalDeviceDTO.getLineIndexes().size());
            list.add(param);
        }
        result.setParam(list);
        //所有监测点id
        List<String> lineIds = generalDeviceDTOList.stream().flatMap(dto -> dto.getLineIndexes().stream()).collect(Collectors.toList());
        Map<String, Object> count = largeScreenMapper.getMonitorAddCount(lineIds, largeScreenParam.getSearchBeginTime(), largeScreenParam.getSearchEndTime());
        result.setMonitorTotalCount(String.valueOf(lineIds.size()));
        result.setMonitorAddCount(count.get("count").toString());
        return result;
    }

    /**
     * 监测点规模(负荷类型)
     */
    @Override
    public MonitoringPointScaleVO getLoadType(LargeScreenParam largeScreenParam) {
        //创建返回VO
        MonitoringPointScaleVO result = new MonitoringPointScaleVO();
        ArrayList<MonitoringPointScaleParam> list = new ArrayList<>();
        DeviceInfoParam.BusinessParam deviceInfoParam = new DeviceInfoParam.BusinessParam();
        //部门索引
        deviceInfoParam.setDeptIndex(largeScreenParam.getDeptIndex());
        //统计类型
        SimpleDTO simpleDTO = new SimpleDTO();
        simpleDTO.setCode(String.valueOf(StatisticsEnum.LOAD_TYPE));
        deviceInfoParam.setStatisticalType(simpleDTO);
        //添加时间
        deviceInfoParam.setSearchBeginTime(largeScreenParam.getSearchBeginTime());
        deviceInfoParam.setSearchEndTime(largeScreenParam.getSearchEndTime());
        // 获取所有数据
        List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceService.getDeviceInfo(deviceInfoParam, Stream.of(0).collect(Collectors.toList()), Stream.of(1).collect(Collectors.toList()));

        for (GeneralDeviceDTO generalDeviceDTO : generalDeviceDTOList) {
            MonitoringPointScaleParam param = new MonitoringPointScaleParam();
            param.setIndex(generalDeviceDTO.getIndex());
            param.setName(generalDeviceDTO.getName());
            param.setCount(generalDeviceDTO.getLineIndexes().size());
            list.add(param);
        }
        result.setParam(list);
        return result;
    }

    /**
     * 运行时间
     */
    @Override
    public OldTimeVO getRunTime() {

        OldTimeVO oldTimeVO =  largeScreenMapper.getRunTime();
        LocalDateTime date2 = LocalDateTime.now();

        Instant instant1 = oldTimeVO.getOldDate().atZone(ZoneId.systemDefault()).toInstant();
        Instant instant2 = date2.atZone(ZoneId.systemDefault()).toInstant();

        long days = Duration.between(instant1, instant2).toDays();
        oldTimeVO.setDays(days);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDateTime = oldTimeVO.getOldDate().format(formatter);
        oldTimeVO.setOldDateNew(formattedDateTime);

        return oldTimeVO;
    }

}
