package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.redis.service.RedisService;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.service.HomePageMapService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.PcFactoryVo;
import cn.cecep.talroad.vo.homepage.*;
import cn.cecep.talroad.vo.query.PcFactoryQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "首页-地图信息接口")
@Slf4j
@RestController
@RequestMapping("/homePageMapController")
public class HomePageMapController {

    @Autowired
    private HomePageMapService homePageMapService;

    @Autowired
    private RedisService redisService;

    @ApiOperation(value = "企业总览")
    @GetMapping("/companyOverview")
    public AjaxResults<HpmCompanyOverviewVo> companyOverview() {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        return AjaxResults.success(homePageMapService.selectCompanyOverview(null,regionCode));
    }


    @ApiOperation(value = "绩效评级总览")
    @GetMapping("/meritRatingOverview")
    public AjaxResults<HpmMeriRatingOverviewVo> meritRatingOverview() {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        HpmMeriRatingOverviewVo hrov = homePageMapService.selectMeritRatingOverview(null,regionCode);
        return AjaxResults.success(hrov);
    }

    @ApiOperation(value = "生产环境信用总览")
    @GetMapping("/environmentOverview")
    public AjaxResults<HpmEnvironmentOverviewVo> environmentOverview() {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        return AjaxResults.success(homePageMapService.selectOnvironmentOverview(null,regionCode));
    }

    @ApiOperation(value = "排放跟踪")
    @GetMapping("/emissionTrackingOverview")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emissionType", value = "系统: 0;执行报告:1;", dataType = "String"),
    })
    public AjaxResults<HpmEmissionTrackingOverviewVo> emissionTrackingOverview(String emissionType) {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        return AjaxResults.success(homePageMapService.selectEmissionTrackingOverview(emissionType,regionCode));
    }

    @ApiOperation(value = "污染物自行监测比例")
    @GetMapping("/emissionRatioOverview")
    public AjaxResults<HpmEmissionRatioOverviewVo> emissionRatioOverview() {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        return AjaxResults.success(homePageMapService.selectEmissionRatioOverview(regionCode));
    }

    @ApiOperation(value = "地图-企业点位弹出层")
    @GetMapping("/enterpriseMapPop")
    @ApiImplicitParam(name = "factoryId", value = "企业id", dataType = "String")
    public AjaxResults<HpmEnterprisePopVo> enterpriseMapPop(@RequestParam(value = "factoryId") String factoryId) {
        return AjaxResults.success(homePageMapService.selectEnterpriseMapPop(factoryId));
    }

    @ApiOperation(value = "地图-企业点位弹出层-总排放趋势图")
    @GetMapping("/enterpriseMapPopEmissionTrends")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "factoryId", value = "企业id", dataType = "String"),
            @ApiImplicitParam(name = "dataType", value = "时间类型 Y M D", dataType = "String"),
            @ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
    })
    public AjaxResults<EChartsVO> enterpriseMapPopEmissionTrends(@RequestParam(value = "factoryId") String factoryId,
                                                                 String dataType, String monitoringStartTime, String monitoringEndTime) {
        return AjaxResults.success(homePageMapService.enterpriseMapPopEmissionTrends(factoryId, dataType, monitoringStartTime, monitoringEndTime));
    }

    @ApiOperation(value = "地图-企业点位弹出层-产品产量图")
    @GetMapping("/enterpriseMapPopYield")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "factoryId", value = "企业id", dataType = "String"),
            @ApiImplicitParam(name = "dataType", value = "数据时间类型", dataType = "String"),
            @ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
    })
    public AjaxResults<EChartsVO> enterpriseMapPopYield(@RequestParam(value = "factoryId") String factoryId,
                                                        String dataType, String monitoringStartTime, String monitoringEndTime) {
        return AjaxResults.success(homePageMapService.selectEnterpriseMapPopYield(factoryId, dataType, monitoringStartTime, monitoringEndTime));
    }

    @ApiOperation(value = "地图-企业点位弹出层-废气/废水排放趋势图")
    @GetMapping("/emissionTrends")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "factoryId", value = "企业id", dataType = "String"),
            @ApiImplicitParam(name = "selectType", value = "废气 1/废水 2", dataType = "String"),
            @ApiImplicitParam(name = "dataType", value = "时间类型 Y M D", dataType = "String"),
            @ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
    })
    public AjaxResults<EChartsVO> emissionTrends(@RequestParam(value = "factoryId") String factoryId,
                                                 @RequestParam(value = "selectType") String selectType,
                                                 String dataType, String monitoringStartTime, String monitoringEndTime) {
        return AjaxResults.success(homePageMapService.emissionTrends(factoryId, selectType, dataType, monitoringStartTime, monitoringEndTime));
    }


    public String[] cachKey = new String[]{"CompanyOverview","IndustryEmissionTrackingOverview","IndustryPollutantDischargeTrends","IndustryProductOutputTrends","Top10CompaniesWithPollutantEmissionsInTheIndustry","CompanyOverview","MeritRatingOverview","EmissionTrackingOverview","EnterpriseMapPop","RankingStat","regionChildList","getRegionCodeListChild"};

    @ApiOperation(value = "根据Redis组名称清理缓存")
    @GetMapping("/cleanRedis")
    public AjaxResults cleanRedisByCacheNames() {
        Boolean aBoolean = false;
        for (String redisKey:cachKey){
            if (redisService.hasKey(redisKey)) {
                aBoolean = redisService.deleteObject(redisKey);
            } else {
                aBoolean = homePageMapService.cleanRedisByCacheNames(redisKey);
            }
        }
        if (aBoolean) {
            return AjaxResults.success(aBoolean);
        } else {
            return AjaxResults.error("没有匹配的值,删除失败！", aBoolean);
        }
    }

    /**
     *
     * 数据类型 1：DCS监控数据:，2：分表计电监测数据，3：在线监测数据，4：无组织监控数据:5：网格化监控数据，6：废水国省控站点监控数据，7：废气国省控站点监控数据
     * 其中dcs已初始化完成
     * 5、6、7 不需要设置参数、1参数已设置完成
     */
    public String[] paramKeys = new String[]{"2","3","4"};
    @ApiOperation(value = "初始化bas_device_param")
    @GetMapping("/initializeBasDeviceParamAll")
    public AjaxResults initializeBasDeviceParamAll() {
        log.info("初始化bas_device_param开始："+paramKeys);
        Boolean aBoolean = homePageMapService.initializeBasDeviceParamAll(paramKeys);
        if (aBoolean) {
            return AjaxResults.success(aBoolean);
        } else {
            return AjaxResults.error("没有匹配的值,删除失败！", aBoolean);
        }
    }
    
    @ApiOperation(value = "月度排放跟踪")
    @GetMapping("/emissionTrackingMon")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "factoryId", value = "企业ID", dataType = "String"),
    	@ApiImplicitParam(name = "monitoringPointId", value = "统计监测点ID", dataType = "String"),
    	@ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
        @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
    })
    public AjaxResults<List<HpmEmissionTrackingMonVo>> emissionTrackingMon(String factoryId, String monitoringPointId, String monitoringStartTime, String monitoringEndTime) {
    	List<HpmEmissionTrackingMonVo> datas= homePageMapService.getEmissionTrackingMon(factoryId, monitoringPointId, monitoringStartTime, monitoringEndTime) ; 
        return AjaxResults.success(datas);
    }

}
