package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.domain.R;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.analysis.SRaMultiSource;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.HomePageMapService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.SysBasRegion;
import cn.cecep.talroad.vo.homepage.*;
import cn.cecep.talroad.vo.query.EffectiveMonitoringRateQuery;
import cn.cecep.talroad.vo.query.analysis.MonitoringPointWarningDetailsQuery;
import cn.cecep.talroad.vo.query.analysis.SRaMultiSourceQuery;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "大屏--在线监控分析")
@RestController
@RequestMapping("/onlineMonitoringAnalysis")
public class OnlineMonitoringAnalysisController {
    @Autowired
    private HomePageMapService homePageMapService;
    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;

    @ApiOperation(value = "在线监测数量总览")
    @GetMapping("/overviewOnlineMonitors")
    public AjaxResults<OverviewOnlineMonitorsVo> overviewOnlineMonitors() {
        return AjaxResults.success(homePageMapService.overviewOnlineMonitors());
    }
    @ApiOperation(value = "监测点位预警报警情况")
    @GetMapping("/monitoringPointWarning")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1废气2废水", dataType = "String"),
    })
    public AjaxResults<EChartsVO> monitoringPointWarning(@RequestParam String type) {
        return AjaxResults.success(homePageMapService.monitoringPointWarning(type));
    }
    @ApiOperation(value = "监测点位预警报警情况详情")
    @GetMapping("/monitoringPointWarningDetails")
    public AjaxResults<IPage<MonitoringPointWarningDetailsVo>> monitoringPointWarningDetails(MonitoringPointWarningDetailsQuery query) {
        return AjaxResults.success(homePageMapService.monitoringPointWarningDetails(query));
    }
    @ApiOperation(value = "导出监测点位预警报警情况详情")
    @Log(title = "监测点位预警报警情况详情", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMonitoringPointWarningDetails")
    public void exportMonitoringPointWarningDetails(HttpServletResponse response, @RequestBody MonitoringPointWarningDetailsQuery query)
    {
            List<MonitoringPointWarningDetailsVo> warningDetailsVoList=homePageMapService.exportMonitoringPointWarningDetails(query);
            ExcelUtil<MonitoringPointWarningDetailsVo> util = new ExcelUtil<MonitoringPointWarningDetailsVo>(MonitoringPointWarningDetailsVo.class);
            util.exportExcel(response, warningDetailsVoList, "监测点位预警报警情况详情");
    }
    @ApiOperation(value = "企业问题TOP10")
    @GetMapping("/enterpriseIssuesTop")
    public AjaxResults<List<EnterpriseIssuesTopVo>> enterpriseIssuesTop() {
        return AjaxResults.success(homePageMapService.enterpriseIssuesTop());
    }

    @ApiOperation(value = "在线监测排放量变化趋势")
    @GetMapping("/effectiveMonitoringRate")
    public AjaxResults<EChartsVO> effectiveMonitoringRate(EffectiveMonitoringRateQuery query) {
        return AjaxResults.success(homePageMapService.effectiveMonitoringRate(query));
    }
    /*定时更新在线监测排放量变化趋势缓存数据*/
    @Scheduled(cron = "0 2 * * * ?")
    public void scheduledUpdate(){
        List<SysBasRegion> regionArr=new ArrayList<>();
        R<List<SysBasRegion>> r = baseRegionService.getRegionList("130200");
        if(r.getCode()==200){
            regionArr=r.getData();
        }
        String end = DateUtil.now();
        Date newDate = DateUtil.offset(DateUtil.parse(end), DateField.DAY_OF_MONTH, -1);
        String now = DateUtil.format(newDate, DatePattern.NORM_DATETIME_FORMAT);
        regionArr.forEach(item->{
            EffectiveMonitoringRateQuery effectiveMonitoringRateQuery=new EffectiveMonitoringRateQuery();
            effectiveMonitoringRateQuery.setDataType("H");
            effectiveMonitoringRateQuery.setMonitoringStartTime(now);
            effectiveMonitoringRateQuery.setMonitoringEndTime(end);
            effectiveMonitoringRateQuery.setType("1");
            effectiveMonitoringRateQuery.setRegionCode(item.getRegionCode());
            homePageMapService.effectiveMonitoringRate(effectiveMonitoringRateQuery);
        });
        regionArr.forEach(item->{
            EffectiveMonitoringRateQuery effectiveMonitoringRateQuery=new EffectiveMonitoringRateQuery();
            effectiveMonitoringRateQuery.setDataType("H");
            effectiveMonitoringRateQuery.setMonitoringStartTime(now);
            effectiveMonitoringRateQuery.setMonitoringEndTime(end);
            effectiveMonitoringRateQuery.setType("2");
            effectiveMonitoringRateQuery.setRegionCode(item.getRegionCode());
            homePageMapService.effectiveMonitoringRate(effectiveMonitoringRateQuery);
        });
    }
    @ApiOperation(value = "区域在线监测有效监测率及超标率")
    @GetMapping("/regionalOnlineMonitoring")
    public AjaxResults<List<RegionalOnlineMonitoringVo>> regionalOnlineMonitoring(EffectiveMonitoringRateQuery query) {
        return AjaxResults.success(homePageMapService.regionalOnlineMonitoring(query));
    }
}
