package com.zcxk.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.common.StringUtils;
import com.zcxk.dao.DeviceSceneMapper;
import com.zcxk.dto.*;
import com.zcxk.dto.scene.SceneIdDto;
import com.zcxk.exception.ServiceException;
import com.zcxk.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ReportService {
    @Autowired
    private DayReportService dayReportService;
    @Autowired
    private MonthReportService monthReportService;
    @Autowired
    private YearReportService yearReportService;

    @Autowired
    private DeviceParmService deviceParmService;

    @Autowired
    private DeviceSceneMapper deviceSceneMapper;



    /**
     * 查询日报月报及年报表
     *
     * @param ids
     * @param year
     * @param month
     * @param day
     * @param reportType
     */
    public IPage<ReportDto> getReportBySceneIds(IPage<ReportDto> page, Long[] ids, Integer year, Integer month, Integer day, Integer reportType) {
        ReportQueryDto queryDto = new ReportQueryDto();
        queryDto.setIds(ids);
        Page<ReportDto> reportPage = new Page<>();
        // 日报
        if (reportType.equals(0)) {
            queryDto.setMonth(month);
            queryDto.setYear(year);
            queryDto.setDay(day);
            reportPage = dayReportService.findPage(page, ids, year, month, day);
            //reportPage = dayReportService.findPageReport(page, queryDto);
        }
        //月报
        if (reportType.equals(1)) {
            queryDto.setMonth(month);
            queryDto.setYear(year);
            reportPage = monthReportService.findPage(page, ids, year, month, day);
            //reportPage = monthReportService.findPageReport(page, queryDto);
        }
        //年报
        if (reportType.equals(2)) {
            queryDto.setYear(year);
            reportPage = yearReportService.findPage(page, ids, year, month, day);
            //reportPage = yearReportService.findPageReport(page, queryDto);
        }
        return reportPage;
    }

    /**
    * @Author lihui
    * @Description 查询日报月报及年报表
    * @Date 11:31 2021/4/15
    * @Param [page, queryDto]
    * @return com.baomidou.mybatisplus.core.metadata.IPage<com.zcxk.dto.ReportDto>
    **/
    public IPage<ReportDto> getReportBySceneIdsNew(IPage<ReportDto> page, ReportQueryDto queryDto) {
        Page<ReportDto> reportPage = new Page<>();
        // 日报
        if (queryDto.getReportType() == 0) {
            reportPage = dayReportService.findPageReport(page, queryDto);
        }
        // 月报或自定义时间
        if (queryDto.getReportType() == 1 || queryDto.getReportType() == 3) {
            formatTime(queryDto);
            queryDto.setDay(null);
            reportPage = monthReportService.findPageReport(page, queryDto);
        }
        // 年报
        if (queryDto.getReportType() == 2) {
            queryDto.setMonth(null);
            queryDto.setDay(null);
            reportPage = yearReportService.findPageReport(page, queryDto);
        }
        return reportPage;
    }

    /**
     * @Author wyy
     * @Description 查询历史数据
     * @Date 11:31 2021/4/15
     * @Param [page, queryDto]
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.zcxk.dto.ReportDto>
     **/
    public IPage<ReportDto> getHistoryDataReportBySceneIdsNews(IPage<ReportDto> page, ReportQueryDto queryDto) {
        Page<ReportDto> reportPage = new Page<>();
        formatTime(queryDto);
        reportPage = monthReportService.findHistoryDataPageReport(page, queryDto);
        return reportPage;
    }



    public IPage<DevicePressReportAttributeDto> getDevicePressReportByDeviceIds(IPage<DevicePressReportAttributeDto> page, Long[] ids, Integer year, Integer month, Integer day, Integer reportType, Integer type) {
        Page<DevicePressReportAttributeDto> reportPage = new Page<>();
        // 日报
        if (reportType.equals(0)) {
           reportPage = dayReportService.findDevicePressPage(page, ids, year, month, day,type);
        }
        if (reportType.equals(1))//月报
        {
            reportPage = monthReportService.findDevicePressPage(page, ids, year, month, day,type);
        }
        if (reportType.equals(2))//年报
        {
            reportPage = yearReportService.findDevicePressPage(page, ids, year, month, day,type);
        }
        return reportPage;
    }

    public IPage<DeviceWaterReportAttributeDto> getDevicWaterReportByDeviceIds(IPage<DeviceWaterReportAttributeDto> page, Long[] ids, Integer year, Integer month, Integer day, Integer reportType) {
        Page<DeviceWaterReportAttributeDto> reportPage = new Page<>();
        if (reportType.equals(0))//日报
        {
            reportPage = dayReportService.findDeviceWaterPage(page, ids, year, month, day);
        }
        if (reportType.equals(1))//月报
        {
            reportPage = monthReportService.findDeviceWaterPage(page, ids, year, month, day);
        }
        if (reportType.equals(2))//年报
        {
            reportPage = yearReportService.findDeviceWaterPage(page, ids, year, month, day);
        }
        return reportPage;
    }


    public IPage<ReportDto> getPipeDevicePressReportByDeviceIds(IPage<ReportDto> page, ReportQueryDto queryDto) {

        Page<ReportDto> reportPage = new Page<>();
        // 日报
        if (queryDto.getReportType() == 0){
            reportPage = dayReportService.findDevicePressPageReport(page, queryDto);
        }
        // 月报或自定义时间
        if (queryDto.getReportType() == 1 || queryDto.getReportType() == 3){
            formatTime(queryDto);
            queryDto.setDay(null);
            reportPage = monthReportService.findDevicePressPageReport(page, queryDto);
        }
        // 年报
        if (queryDto.getReportType() == 2) {
            queryDto.setDay(null);
            queryDto.setMonth(null);
            reportPage = yearReportService.findDevicePressPageReport(page, queryDto);
        }
        return reportPage;
    }

    public IPage<ReportDto> getPipeHistoryDataDevicePressReportByDeviceIds(IPage<ReportDto> page, ReportQueryDto queryDto) {

        Page<ReportDto> reportPage = new Page<>();
        formatTime(queryDto);
        reportPage = monthReportService.findHistoryDataDevicePressPageReport(page, queryDto);
        return reportPage;
    }


    /**
     * 查询场景下的统计维度
     * @param deviceId
     * @return
     */
    public List<ReportAttributeDto> findPipeAttributeNameList(Long deviceId, Integer type,Integer reportType) {
        ReportDto reportDto = new ReportDto();
        String name = type == 1 ? "压力" : ((type == 2) ? "流量" : "水质");
        SceneIdDto sceneIdDto = deviceSceneMapper.findSceneId("管网", name, deviceId);
        if (sceneIdDto != null) {
            reportDto.setSceneId(sceneIdDto.getSceneId());
        }
        List<DeviceDto> deviceDtos = new ArrayList<>();
        DeviceDto dto = new DeviceDto();
        dto.setId(deviceId);
        deviceDtos.add(dto);
        reportDto.setDeviceIds(deviceDtos);
        reportDto.setUserId(UserUtil.getCurrentUser().getId());
        if (reportType == 0) {
            return deviceParmService.findAttributeNameList(reportDto);
        } else {
            return deviceParmService.findHistoryDataAttributeNameList(reportDto);
        }
    }

    /**
     * 查询场景下的统计维度
     * @param ids
     * @return
     */
    public List<ReportAttributeDto> findAttributeNameList(Long[] ids, Long[] deviceIds,Integer type) {
        ReportDto reportDto = new ReportDto();
        reportDto.setParentSceneIds(ids);
        if (deviceIds != null && deviceIds.length > 0) {
            List<DeviceDto> deviceDtos = new ArrayList<>();
            for (Long deviceId : deviceIds) {
                DeviceDto dto = new DeviceDto();
                dto.setId(deviceId);
                deviceDtos.add(dto);
            }
            reportDto.setDeviceIds(deviceDtos);
        }
        reportDto.setUserId(UserUtil.getCurrentUser().getId());
        if (type == 0) {
            return deviceParmService.findAttributeNameList(reportDto);
        } else {
            return deviceParmService.findHistoryDataAttributeNameList(reportDto);
        }
    }

    /**
    * @Author lihui 
    * @Description 格式化时间
    * @Date 18:01 2021/4/15
    * @Param [queryDto] 
    * @return void 
    **/
    private void formatTime(ReportQueryDto queryDto){
        if (queryDto.getReportType() == 1){
            return;
        }
        if (StringUtils.isEmpty(queryDto.getStartTime()) || StringUtils.isEmpty(queryDto.getEndTime())) {
            throw new ServiceException(500, "开始时间或结束时间为空");
        }
        queryDto.setStartTime(queryDto.getStartTime() );
        queryDto.setEndTime(queryDto.getEndTime());
        queryDto.setYear(null);
        queryDto.setMonth(null);
    }

}
