package com.zcxk.charge.web.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.charge.api.dto.*;
import com.zcxk.charge.api.dto.query.ReportQueryPageDto;
import com.zcxk.charge.api.vo.*;
import com.zcxk.charge.core.dao.DeviceSceneMapper;
import com.zcxk.charge.api.dto.scene.SceneIdDto;
import com.zcxk.core.common.exception.BusinessException;
import com.zcxk.core.oauth2.util.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author  andy
 */
@Service
public class ReportService {

    @Autowired
    private DayReportService dayReportService;

    @Autowired
    private MonthReportService monthReportService;

    @Autowired
    private YearReportService yearReportService;

    @Autowired
    private DeviceParmService deviceParmService;

    @Autowired
    private DeviceSceneMapper deviceSceneMapper;


    /**
    * @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<ReportVo> getReportBySceneIdsNew(IPage<ReportDto> page, ReportQueryDto queryDto) {
        Page<ReportVo> 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<ReportVo> getHistoryDataReportBySceneIdsNews(IPage<ReportDto> page, ReportQueryDto queryDto) {
        Page<ReportVo> reportPage = new Page<>();
        formatTime(queryDto);
        reportPage = monthReportService.findHistoryDataPageReport(page, queryDto);
        return reportPage;
    }



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

        Page<ReportVo> 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<ReportVo> getPipeHistoryDataDevicePressReportByDeviceIds(IPage<ReportDto> page, ReportQueryDto queryDto) {

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


    /**
     * 查询场景下的统计维度
     * @param deviceId
     * @return
     */
    public List<ReportAttributeVo> findPipeAttributeNameList(Long deviceId, Integer type, Integer reportType) {
        ReportDto reportDto = new ReportDto();
        String name = type == 1 ? "压力" : ((type == 2) ? "流量" : "水质");
        SceneIdVo sceneIdDto = deviceSceneMapper.findSceneId("管网", name, deviceId);
        if (sceneIdDto != null) {
            reportDto.setSceneId(sceneIdDto.getSceneId());
        }
        List<DeviceVo> deviceDtos = new ArrayList<>();
        DeviceVo dto = new DeviceVo();
        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<ReportAttributeVo> findAttributeNameList(Long[] ids, Long[] deviceIds,Integer type) {
        ReportDto reportDto = new ReportDto();
        reportDto.setParentSceneIds(ids);
        if (deviceIds != null && deviceIds.length > 0) {
            List<DeviceVo> deviceDtos = new ArrayList<>();
            for (Long deviceId : deviceIds) {
                DeviceVo dto = new DeviceVo();
                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 BusinessException.builder(500, "开始时间或结束时间为空");
        }
        queryDto.setStartTime(queryDto.getStartTime() );
        queryDto.setEndTime(queryDto.getEndTime());
        queryDto.setYear(null);
        queryDto.setMonth(null);
    }

}
