package com.ho.business.controller;

import com.ho.business.entity.Station;
import com.ho.business.feignclient.FileCenterFeignClient;
import com.ho.business.feignclient.UserFeignClient;
import com.ho.business.service.IargeScreenShowService;
import com.ho.business.service.StationService;
import com.ho.business.vo.req.cockpit.CockpitReqVO;
import com.ho.business.vo.resp.iargeScreen.*;
import com.ho.common.tools.annotation.CommonLargeScreenToken;
import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.entity.WeatherRespVo;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.BigDecimalUtil;
import com.ho.flow.vo.resp.event.EventDayNum;
import com.ho.user.api.entity.SysDept;
import com.ho.user.api.vo.req.QueryDeptReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gyan
 * @desc: 大屏展示
 * @DateTime: 2023/12/13 10:37
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "IargeScreenShow")
@Api(tags = "新大屏")
public class IargeScreenShowController {
    @Autowired
    IargeScreenShowService iargeScreenShowService;

    @Autowired
    BigDecimalUtil bigDecimalUtil;

    @Autowired
    FileCenterFeignClient fileCenterFeignClient;

    @Autowired
    RedisService redisService;

    @Autowired
    StationService stationService;

    @Autowired
    UserFeignClient userFeignClient;

    @Value("${largeScreen.weatherCode}")
    String adCode;

    private final static Integer maxSize = 30;


    @PostMapping("overviewData")
    @ApiOperation(value = "储能年度概览//年度收益概览接口返回")
    @TokenIgnore
    public DataResult<AnnualOverviewResp> getOverviewData(HttpServletRequest request) {
        AnnualOverviewResp annualOverviewResp = iargeScreenShowService.getOverviewData();
        bigDecimalUtil.keepTwoDecimalPlaces(annualOverviewResp);
        bigDecimalUtil.ifIsNUll(annualOverviewResp);
        return DataResult.success(annualOverviewResp);
    }


    @PostMapping("efficiencyDate")
    @ApiOperation(value = "系统转换率")
    @TokenIgnore
    public DataResult<List<Subdata>> getEfficiencyDate() {
        List<Subdata> efficiencyList = iargeScreenShowService.getEfficiencyDate();
        return DataResult.success(efficiencyList);
    }

    // 电站收益率排名
    @PostMapping("incomeDate")
    @ApiOperation(value = "电站收益排名")
    @TokenIgnore
    public DataResult<List<Subdata>> getIncomeList() {
        List<Subdata> regionList = new ArrayList<>();
        regionList = iargeScreenShowService.getProfit(null, null);
        if (regionList.size() > maxSize) {
            regionList = regionList.subList(0, maxSize);
        }
        for (Subdata subdata : regionList) {
            bigDecimalUtil.keepTwoDecimalPlaces(subdata);
            bigDecimalUtil.ifIsNUll(subdata);
        }
        return DataResult.success(regionList);
    }


    // 节能减排
    @PostMapping("getEnergySaving")
    @ApiOperation(value = "节能减排")
    @TokenIgnore
    public DataResult<EnergySavingRespVo> getEnergySaving() {
        EnergySavingRespVo energySaving = iargeScreenShowService.getEnergySaving();
        bigDecimalUtil.keepTwoDecimalPlaces(energySaving);
        bigDecimalUtil.ifIsNUll(energySaving);
        return DataResult.success(energySaving);
    }


    // 电站区域分布
    @PostMapping("regionalDistribution")
    @ApiOperation(value = "电站区域分布")
    @TokenIgnore
    public DataResult<List<Subdata>> regionalDistribution() {
        List<Subdata> regionList = iargeScreenShowService.getRegionList();
        regionList.sort(Comparator.comparing((Subdata::getRegionValue)).reversed());
        return DataResult.success(regionList);
    }

    // 电站区域分布
    @PostMapping("getCapacity")
    @ApiOperation(value = "装机容量倒排")
    @TokenIgnore
    public DataResult<List<Subdata>> getCapacity() {
        List<Subdata> regionList = iargeScreenShowService.getCapacity();
        return DataResult.success(regionList);
    }

    //获取电站信息
    @PostMapping("stationInfo")
    @ApiOperation(value = "获取电站信息")
    @TokenIgnore
    public DataResult<List<Station>> getStationInfo() {
        List<Station> stations = iargeScreenShowService.getStationInfo();
        return DataResult.success(stations);
    }

    //    日充日放对比
    @PostMapping("getIncomeCurve")
    @ApiOperation(value = "收益曲线")
    @TokenIgnore
    public DataResult<List<AnnualChartValue>> getIncomeCurve(@RequestBody CockpitReqVO vo, HttpServletRequest request) {
        // List<AnnualChartValue> list = iargeScreenShowService.getIncomeCurve(vo);
        List<AnnualChartValue> list = iargeScreenShowService.getProfitCurve(vo);
        for (AnnualChartValue annualChartValue : list) {
            bigDecimalUtil.keepTwoDecimalPlaces(annualChartValue);
            bigDecimalUtil.ifIsNUll(annualChartValue);
        }
        return DataResult.success(list);
    }

    //    日充日放对比
    @PostMapping("chargeDailyChart")
    @ApiOperation(value = "日充日放对比图")
    @TokenIgnore
    public DataResult<List<AnnualChartValue>> getChargeDailyChart(HttpServletRequest request) {
        List<AnnualChartValue> list = iargeScreenShowService.getChargeDailyChart();
        return DataResult.success(list);
    }

    @PostMapping("/newWeather")
    @ApiOperation(value = "新大屏天气接口")
    @TokenIgnore
    public DataResult<WeatherRespVo> newWeather() {
        String key = RedisKeyConstant.WEATHER_PROVINCE_CITY + adCode;
        WeatherRespVo info = (WeatherRespVo) redisService.get(key);
        return DataResult.success(info);
    }


    /**************************以下为通用大屏接口********************************/

    @PostMapping("getCommonOverviewData")
    @ApiOperation(value = "通用储能年度概览//年度收益概览接口返回")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<AnnualOverviewResp> getCommonOverviewData(HttpServletRequest request) {
        AnnualOverviewResp annualOverviewResp = iargeScreenShowService.getOverviewData(getSimpleUser(request));
        bigDecimalUtil.keepTwoDecimalPlaces(annualOverviewResp);
        bigDecimalUtil.ifIsNUll(annualOverviewResp);
        return DataResult.success(annualOverviewResp);
    }

    @PostMapping("getCommonRegionalDistribution")
    @ApiOperation(value = "通用电站区域分布")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<List<Subdata>> getCommonRegionalDistribution(HttpServletRequest request) {
        List<Subdata> regionList = iargeScreenShowService.getCommonRegionList(getSimpleUser(request));
        regionList.sort(Comparator.comparing((Subdata::getRegionValue)).reversed());
        return DataResult.success(regionList);
    }

    @PostMapping("getCommonEnergySaving")
    @ApiOperation(value = "通用节能减排")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<EnergySavingRespVo> getEnergySaving(HttpServletRequest request) {
        EnergySavingRespVo energySaving = iargeScreenShowService.getEnergySaving(getSimpleUser(request));
        bigDecimalUtil.keepTwoDecimalPlaces(energySaving);
        bigDecimalUtil.ifIsNUll(energySaving);
        return DataResult.success(energySaving);
    }

    @PostMapping("getCommonIncomeCurve")
    @ApiOperation(value = "通用收益曲线")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<List<AnnualChartValue>> getCommonIncomeCurve(@RequestBody CockpitReqVO vo, HttpServletRequest request) {
        List<AnnualChartValue> list = iargeScreenShowService.getCommonProfitCurve(vo, getSimpleUser(request));
        for (AnnualChartValue annualChartValue : list) {
            bigDecimalUtil.keepTwoDecimalPlaces(annualChartValue);
            bigDecimalUtil.ifIsNUll(annualChartValue);
        }
        return DataResult.success(list);
    }

    @PostMapping("getCommonChargeDailyChart")
    @ApiOperation(value = "通用日充日放对比图")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<List<AnnualChartValue>> getCommonChargeDailyChart(HttpServletRequest request) {
        List<AnnualChartValue> list = iargeScreenShowService.getCommonChargeDailyChart(getSimpleUser(request));
        return DataResult.success(list);
    }

    // 电站收益率排名
    @PostMapping("getCommonIncomeList")
    @ApiOperation(value = "通用电站收益排名")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<List<Subdata>> getCommonIncomeList(HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        List<Subdata> regionList = iargeScreenShowService.getCommonProfit(null, null, getSimpleUser(request),lang);
        if (regionList.size() > maxSize) {
            regionList = regionList.subList(0, maxSize);
        }
        for (Subdata subdata : regionList) {
            bigDecimalUtil.keepTwoDecimalPlaces(subdata);
            bigDecimalUtil.ifIsNUll(subdata);
        }
        return DataResult.success(regionList);
    }

    @PostMapping("getCommonEfficiencyDate")
    @ApiOperation(value = "通用系统转换率")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<List<Subdata>> getCommonEfficiencyDate(HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        List<Subdata> efficiencyList = iargeScreenShowService.getCommonEfficiencyDate(getSimpleUser(request),lang);
        return DataResult.success(efficiencyList);
    }

    @PostMapping("getCommonStationInfo")
    @ApiOperation(value = "通用获取电站信息")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<List<Station>> getCommonStationInfo(HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        List<Integer> ids = getSimpleUser(request);
        List<Station> stations = new ArrayList<>();
        if (ids != null) {
            stations = iargeScreenShowService.getCountStations(ids,lang);
        }
        return DataResult.success(stations);
    }

    @PostMapping("getCommonCapacity")
    @ApiOperation(value = "通用装机容量倒排")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<List<Subdata>> getCommonCapacity(HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        List<Subdata> regionList = iargeScreenShowService.getCommonCapacity(getSimpleUser(request),lang);
        return DataResult.success(regionList);
    }

    @PostMapping("getCommonEventCount")
    @ApiOperation(value = "通用获取告警数目曲线")
    @TokenIgnore
    @CommonLargeScreenToken
    public DataResult<List<EventDayNum>> getCommonEventCount(HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByCommonToken(token);
        List<EventDayNum> commonEventCount = iargeScreenShowService.getCommonEventCount(user);
        return DataResult.success(commonEventCount);
    }

    private List<Integer> getSimpleUser(HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByCommonToken(token);
        List<Integer> stationIds = user.getStationIds();
        return stationIds;
    }
    /************************************************************************/

    /**************************以下为中自大屏接口********************************/
    private List<Integer> getByDeptId(Integer deptId) {
        List<SysDept> sysDeptList = userFeignClient.getSysDeptByDeptId(deptId);
        List<Integer> idList = sysDeptList.stream().map(SysDept::getId).collect(Collectors.toList());
        List<Station> stationList = stationService.selectByDeptIds(idList);
        List<Integer> stationIds = stationList.stream().map(Station::getId).collect(Collectors.toList());
        return stationIds;
    }

    @PostMapping("getByStationId")
    @ApiOperation(value = "中自根据电站Id查询所属组织机构返回")
    @TokenIgnore
    public DataResult<Station> getByStationId(@RequestBody CockpitReqVO vo) {
        Station station = new Station();
        if (null == vo.getStationId()) {
            station.setDeptId(155);
        } else {
            station = stationService.selectById(vo.getStationId());
        }
        return DataResult.success(station);
    }

    @PostMapping("getZhongZiOverviewData")
    @ApiOperation(value = "中自储能年度概览//年度收益概览接口返回")
    @TokenIgnore
    public DataResult<AnnualOverviewResp> getZhongZiOverviewData(@RequestBody CockpitReqVO vo) {
        AnnualOverviewResp annualOverviewResp = iargeScreenShowService.getOverviewData(getByDeptId(vo.getDeptId()));
        bigDecimalUtil.keepTwoDecimalPlaces(annualOverviewResp);
        bigDecimalUtil.ifIsNUll(annualOverviewResp);
        return DataResult.success(annualOverviewResp);
    }

    @PostMapping("getZhongZiDistribution")
    @ApiOperation(value = "中自电站区域分布")
    @TokenIgnore
    public DataResult<List<Subdata>> getZhongZiDistribution(@RequestBody CockpitReqVO vo) {
        List<Subdata> regionList = iargeScreenShowService.getCommonRegionList((getByDeptId(vo.getDeptId())));
        regionList.sort(Comparator.comparing((Subdata::getRegionValue)).reversed());
        return DataResult.success(regionList);
    }

    @PostMapping("getZhongZiEnergySaving")
    @ApiOperation(value = "中自节能减排")
    @TokenIgnore
    public DataResult<EnergySavingRespVo> getZhongZiEnergySaving(@RequestBody CockpitReqVO vo) {
        EnergySavingRespVo energySaving = iargeScreenShowService.getEnergySaving(getByDeptId(vo.getDeptId()));
        bigDecimalUtil.keepTwoDecimalPlaces(energySaving);
        bigDecimalUtil.ifIsNUll(energySaving);
        return DataResult.success(energySaving);
    }

    @PostMapping("getZhongZiIncomeCurve")
    @ApiOperation(value = "通用收益曲线")
    @TokenIgnore
    public DataResult<List<AnnualChartValue>> getZhongZiIncomeCurve(@RequestBody CockpitReqVO vo) {
        List<AnnualChartValue> list = iargeScreenShowService.getCommonProfitCurve(vo, getByDeptId(vo.getDeptId()));
        for (AnnualChartValue annualChartValue : list) {
            bigDecimalUtil.keepTwoDecimalPlaces(annualChartValue);
            bigDecimalUtil.ifIsNUll(annualChartValue);
        }
        return DataResult.success(list);
    }

    @PostMapping("getZhongZiChargeDailyChart")
    @ApiOperation(value = "中自日充日放对比图")
    @TokenIgnore
    public DataResult<List<AnnualChartValue>> getZhongZiChargeDailyChart(@RequestBody CockpitReqVO vo) {
        List<AnnualChartValue> list = iargeScreenShowService.getCommonChargeDailyChart(getByDeptId(vo.getDeptId()));
        return DataResult.success(list);
    }

    // 电站收益率排名
    @PostMapping("getZhongZiIncomeList")
    @ApiOperation(value = "中自电站收益排名")
    @TokenIgnore
    public DataResult<List<Subdata>> getZhongZiIncomeList(@RequestBody CockpitReqVO vo,HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        List<Subdata> regionList = iargeScreenShowService.getCommonProfit(null, null, getByDeptId(vo.getDeptId()),lang);
        if (regionList.size() > maxSize) {
            regionList = regionList.subList(0, maxSize);
        }
        for (Subdata subdata : regionList) {
            bigDecimalUtil.keepTwoDecimalPlaces(subdata);
            bigDecimalUtil.ifIsNUll(subdata);
        }
        return DataResult.success(regionList);
    }

    @PostMapping("getZhongZiEfficiencyDate")
    @ApiOperation(value = "中自系统转换率")
    @TokenIgnore
    public DataResult<List<Subdata>> getZhongZiEfficiencyDate(@RequestBody CockpitReqVO vo,HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        List<Subdata> efficiencyList = iargeScreenShowService.getCommonEfficiencyDate(getByDeptId(vo.getDeptId()),lang);
        return DataResult.success(efficiencyList);
    }

    @PostMapping("getZhongZiStationInfo")
    @ApiOperation(value = "获取中自电站信息")
    @TokenIgnore
    public DataResult<List<Station>> getZhongZiStationInfo(@RequestBody CockpitReqVO vo,HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        List<Integer> ids = getByDeptId(vo.getDeptId());
        List<Station> stations = new ArrayList<>();
        if (ids != null) {
            stations = iargeScreenShowService.getCountStations(ids,lang);
        }
        return DataResult.success(stations);
    }

    @PostMapping("getZhongZiCapacity")
    @ApiOperation(value = "中自装机容量倒排")
    @TokenIgnore
    public DataResult<List<Subdata>> getZhongZiCapacity(@RequestBody CockpitReqVO vo,HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        List<Subdata> regionList = iargeScreenShowService.getCommonCapacity(getByDeptId(vo.getDeptId()),lang);
        return DataResult.success(regionList);
    }

    /*********************************以下为中自-光伏大屏接口***************************************/

    @PostMapping("getZhongZiPvDistribution")
    @ApiOperation(value = "中自-光伏电站区域分布")
    @TokenIgnore
    public DataResult<List<Subdata>> getZhongZiPvDistribution(@RequestBody CockpitReqVO vo) {
        List<Subdata> regionList = iargeScreenShowService.getCommonPvRegionList((getByDeptId(vo.getDeptId())));
        regionList.sort(Comparator.comparing((Subdata::getRegionValue)).reversed());
        return DataResult.success(regionList);
    }

    @PostMapping("getZhongZiPvOverviewData")
    @ApiOperation(value = "中自储能-光伏年度概览/年度收益概览接口返回")
    @TokenIgnore
    public DataResult<AnnualPvOverviewResp> getZhongZiPvOverviewData(@RequestBody CockpitReqVO vo) {
        AnnualPvOverviewResp annualOverviewResp = iargeScreenShowService.getPvOverviewData(getByDeptId(vo.getDeptId()));
        bigDecimalUtil.keepTwoDecimalPlaces(annualOverviewResp);
        bigDecimalUtil.ifIsNUll(annualOverviewResp);
        return DataResult.success(annualOverviewResp);
    }

    @PostMapping("getZhongZiPvEnergySaving")
    @ApiOperation(value = "中自-光伏节能减排")
    @TokenIgnore
    public DataResult<EnergySavingRespVo> getZhongZiPvEnergySaving(@RequestBody CockpitReqVO vo) {
        EnergySavingRespVo energySaving = iargeScreenShowService.getPvEnergySaving(getByDeptId(vo.getDeptId()));
        bigDecimalUtil.keepTwoDecimalPlaces(energySaving);
        bigDecimalUtil.ifIsNUll(energySaving);
        return DataResult.success(energySaving);
    }

    @PostMapping("getZhongZiPvIncomeCurve")
    @ApiOperation(value = "中自光伏收益曲线")
    @TokenIgnore
    public DataResult<List<AnnualChartValue>> getZhongZiPvIncomeCurve(@RequestBody CockpitReqVO vo) {
        List<AnnualChartValue> list = iargeScreenShowService.getPVCommonProfitCurve(vo, getByDeptId(vo.getDeptId()));
        for (AnnualChartValue annualChartValue : list) {
            bigDecimalUtil.keepTwoDecimalPlaces(annualChartValue);
            bigDecimalUtil.ifIsNUll(annualChartValue);
        }
        return DataResult.success(list);
    }

    @PostMapping("getZhongZiPvIncomeList")
    @ApiOperation(value = "中自-光伏电站收益排名")
    @TokenIgnore
    public DataResult<List<Subdata>> getZhongZiPvIncomeList(@RequestBody CockpitReqVO vo) {
        List<Subdata> regionList = iargeScreenShowService.getPvCommonProfit(null,null,getByDeptId(vo.getDeptId()));
        if (regionList.size() > maxSize) {
            regionList = regionList.subList(0, maxSize);
        }
        for (Subdata subdata : regionList) {
            bigDecimalUtil.keepTwoDecimalPlaces(subdata);
            bigDecimalUtil.ifIsNUll(subdata);
        }
        return DataResult.success(regionList);
    }

    @PostMapping("getZhongZiPvElecList")
    @ApiOperation(value = "中自-光伏电站发电量排名")
    @TokenIgnore
    public DataResult<List<Subdata>> getZhongZiPvElecList(@RequestBody CockpitReqVO vo) {
        List<Subdata> regionList = iargeScreenShowService.getPvCommonElec(null,null,getByDeptId(vo.getDeptId()));
        if (regionList.size() > maxSize) {
            regionList = regionList.subList(0, maxSize);
        }
        for (Subdata subdata : regionList) {
            bigDecimalUtil.keepTwoDecimalPlaces(subdata);
            bigDecimalUtil.ifIsNUll(subdata);
        }
        return DataResult.success(regionList);
    }

    @PostMapping("getZhongZiPvRelease")
    @ApiOperation(value = "中自pv日发电量")
    @TokenIgnore
    public DataResult<List<AnnualChartValue>> getZhongZiPvRelease(@RequestBody CockpitReqVO vo) {
        List<AnnualChartValue> list = iargeScreenShowService.getCommonPvRelease(getByDeptId(vo.getDeptId()));
        for (AnnualChartValue subdata : list) {
            bigDecimalUtil.keepTwoDecimalPlaces(subdata);
            bigDecimalUtil.ifIsNUll(subdata);
        }
        return DataResult.success(list);
    }
}
