package com.gyee.frame.controller.leaderboard;

import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.auto.Curvefittingmain;
import com.gyee.frame.model.auto.Curvefittingmonthmain;
import com.gyee.frame.model.auto.Windturbineinfoday3;
import com.gyee.frame.model.custom.StatisticalAnalysisTopVo;
import com.gyee.frame.model.custom.StatisticalAnalysisWarningTopVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.service.leaderboard.LeaderboardService;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/leaderboard")
public class LeaderboardController {
    @Autowired
    private LeaderboardService leaderboardService;

    /**查询拟合优度列表**/
    @GetMapping("/fittingtoplist")
    @ResponseBody
    public AjaxResult fittingtoplist(Tablepar tablepar, String wpIps, String beginDate, String endDate) {

        Date beginDate_d=null;
        Date endDate_d=null;
        if (StringUtils.isNotEmpty(beginDate))
        {
            beginDate_d= DateUtils.parseDate(beginDate);
        }
        if (StringUtils.isNotEmpty(endDate))
        {
            endDate_d= DateUtils.parseDate(endDate);
        }
        List<StatisticalAnalysisTopVo> vos= leaderboardService.getRnewTopDate(wpIps, beginDate_d, endDate_d);

        for (StatisticalAnalysisTopVo vo : vos) {
            if (vo.getPower() > 1) {
                vo.setPower(1.0);
            }
        }

        if (tablepar.getOrderByColumn().equals("power")) {
            if (tablepar.getIsAsc().equals("asc")) {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg0.getPower().compareTo(arg1.getPower());
                    }

                });

            } else {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg1.getPower().compareTo(arg0.getPower());
                    }

                });
            }
        } else if (tablepar.getOrderByColumn().equals("speed")) {
            if (tablepar.getIsAsc().equals("asc")) {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                    }

                });

            } else {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg1.getSpeed().compareTo(arg0.getSpeed());
                    }

                });
            }
        }

        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }

    }


    /**查询拟合优度图表**/
    @GetMapping("/fittingtopchart")
    @ResponseBody
    public AjaxResult fittingtopchart(String wtIds, String beginDate, String endDate) throws Exception {
        Date beginDate_d=null;
        Date endDate_d=null;
        if (StringUtils.isNotEmpty(beginDate))
        {
            beginDate_d= DateUtils.parseDate(beginDate);
        }
        if (StringUtils.isNotEmpty(endDate))
        {
            endDate_d= DateUtils.parseDate(endDate);
        }
        Map<String, List> map =new HashMap<>();
        if (StringUtils.notEmp(wtIds) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {
            map = leaderboardService.fittingtopchart(wtIds, beginDate_d, endDate_d);
        }

        if (map != null && !map.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, map);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, map);
        }
    }

    /**查询总发电量排行列表**/
    @GetMapping("/totalPowerCapacityTopList")
    @ResponseBody
    public AjaxResult totalPowerCapacityTopList(Tablepar tablepar, String wpIps, String beginDate, String endDate) {

        Date beginDate_d=null;
        Date endDate_d=null;
        if (StringUtils.isNotEmpty(beginDate))
        {
            beginDate_d= DateUtils.parseDate(beginDate);
        }
        if (StringUtils.isNotEmpty(endDate))
        {
            endDate_d= DateUtils.parseDate(endDate);
        }
        List<StatisticalAnalysisTopVo> vos= leaderboardService.totalPowerCapacityTopList(wpIps, beginDate_d, endDate_d);


        if (tablepar.getOrderByColumn().equals("power")) {
            if (tablepar.getIsAsc().equals("asc")) {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg0.getPower().compareTo(arg1.getPower());
                    }

                });

            } else {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg1.getPower().compareTo(arg0.getPower());
                    }

                });
            }
        } else if (tablepar.getOrderByColumn().equals("speed")) {
            if (tablepar.getIsAsc().equals("asc")) {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                    }

                });

            } else {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg1.getSpeed().compareTo(arg0.getSpeed());
                    }

                });
            }
        } else if (tablepar.getOrderByColumn().equals("generatingcapacity")) {
            if (tablepar.getIsAsc().equals("asc")) {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                    }

                });

            } else {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg1.getSpeed().compareTo(arg0.getSpeed());
                    }

                });
            }
        }

        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }

    }


    /**查询利用率排行榜列表**/
    @GetMapping("/utilizationList")
    @ResponseBody
    public AjaxResult utilizationList(Tablepar tablepar, String wpId, String beginDate, String endDate,String type) {

        Date beginDate_d=null;
        Date endDate_d=null;
        if (StringUtils.isNotEmpty(beginDate))
        {
            beginDate_d= DateUtils.parseDate(beginDate);
        }
        if (StringUtils.isNotEmpty(endDate))
        {
            endDate_d= DateUtils.parseDate(endDate);
        }

        List<Windturbineinfoday3> vos= leaderboardService.utilizationList(tablepar, wpId, beginDate_d, endDate_d, type);


        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }

    }

    /**查询损失率排行榜列表**/
    @GetMapping("/lossList")
    @ResponseBody
    public AjaxResult lossList(Tablepar tablepar, String wpId, String beginDate, String endDate,String type) {

        Date beginDate_d=null;
        Date endDate_d=null;
        if (StringUtils.isNotEmpty(beginDate))
        {
            beginDate_d= DateUtils.parseDate(beginDate);
        }
        if (StringUtils.isNotEmpty(endDate))
        {
            endDate_d= DateUtils.parseDate(endDate);
        }
        List<Windturbineinfoday3> vos= leaderboardService.lossList(tablepar, wpId, beginDate_d, endDate_d, type);


        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }

    }

    /**查询负荷率排行榜列表**/
    @GetMapping("/loadfactortoplist")
    @ResponseBody
    public AjaxResult loadfactortoplist(Tablepar tablepar, String wpId, String beginDate, String endDate) {


        Date beginDate_d=null;
        Date endDate_d=null;
        if (StringUtils.isNotEmpty(beginDate))
        {
            beginDate_d= DateUtils.parseDate(beginDate);
        }
        if (StringUtils.isNotEmpty(endDate))
        {
            endDate_d= DateUtils.parseDate(endDate);
        }
        List<StatisticalAnalysisTopVo> vos= leaderboardService.loadfactortoplist( wpId, beginDate_d, endDate_d);

        if (tablepar.getOrderByColumn().equals("power")) {
            if (tablepar.getIsAsc().equals("asc")) {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg0.getPower().compareTo(arg1.getPower());
                    }

                });

            } else {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg1.getPower().compareTo(arg0.getPower());
                    }

                });
            }
        } else if (tablepar.getOrderByColumn().equals("speed")) {
            if (tablepar.getIsAsc().equals("asc")) {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                    }

                });

            } else {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg1.getSpeed().compareTo(arg0.getSpeed());
                    }

                });
            }
        } else if (tablepar.getOrderByColumn().equals("generatingcapacity")) {
            if (tablepar.getIsAsc().equals("asc")) {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                    }

                });

            } else {
                Collections.sort(vos, new Comparator<StatisticalAnalysisTopVo>() {
                    public int compare(StatisticalAnalysisTopVo arg0, StatisticalAnalysisTopVo arg1) {
                        return arg1.getSpeed().compareTo(arg0.getSpeed());
                    }

                });
            }
        }


        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }

    }

    /**查询报警排行榜列表**/
    @GetMapping("/querywarningStatistical")
    @ResponseBody
    public AjaxResult querywarningStatistical(Tablepar tablepar, String type, String beginDate, String endDate) {

        Date beginDate_d=null;
        Date endDate_d=null;
        if (StringUtils.isNotEmpty(beginDate))
        {
            beginDate_d= DateUtils.parseDate(beginDate);
        }
        if (StringUtils.isNotEmpty(endDate))
        {
            endDate_d= DateUtils.parseDate(endDate);
        }
        List<StatisticalAnalysisWarningTopVo> vos= leaderboardService.getWarningTopDate( tablepar, type, beginDate_d, endDate_d);


        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }

    }

    /**查询报警排行榜图表**/
    @GetMapping("/querywarningStatisticalPie")
    @ResponseBody
    public AjaxResult querywarningStatisticalPie(String type,  String beginDate, String endDate) {

        Date beginDate_d=null;
        Date endDate_d=null;
        if (StringUtils.isNotEmpty(beginDate))
        {
            beginDate_d= DateUtils.parseDate(beginDate);
        }
        if (StringUtils.isNotEmpty(endDate))
        {
            endDate_d= DateUtils.parseDate(endDate);
        }
        List<StatisticalAnalysisWarningTopVo> vos=
                leaderboardService.getWarningTopDatePercentage( type, beginDate_d, endDate_d);


        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }

    }
/*******************************************************************************************************************************/
    /**查询日曲线偏差率列表**/
    @GetMapping("/curvefittingmainList")
    @ResponseBody
    public AjaxResult curvefittingmainList(Tablepar tablepar, String wpId, String recorddate) {
        List<Curvefittingmain> vos=new ArrayList<>();
        if (StringUtils.isNotEmpty(recorddate))
        {
            Date  date= DateUtils.parseDate(recorddate);
            vos= leaderboardService.curvefittingmainList( tablepar, wpId, date);

        }

        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }

    /**查询日曲线（实际/保证 实际/最优 最优/保证）图表**/
    @GetMapping("/getCurvechatAjax")
    @ResponseBody
    public AjaxResult getCurvechatAjax(String type,String wtId,  String recorddate) {
        Map<String, Object> vos=new HashMap<>();
        if (StringUtils.isNotEmpty(recorddate))
        {
            Date  date= DateUtils.parseDate(recorddate);
            vos= leaderboardService.getCurvechatAjax( type,wtId, date);
        }


        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }

    /**查询日曲线同比图表**/
    @GetMapping("/curvechatAjaxtb")
    @ResponseBody
    public AjaxResult curvechatAjaxtb(String type,String wtId,  String recorddate) {

        Map<String, Object> vos=new HashMap<>();
        if (StringUtils.isNotEmpty(recorddate))
        {
            Date  date= DateUtils.parseDate(recorddate);
           vos= leaderboardService.curvechatAjaxtb( type,wtId, date);
        }


        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }
    /**查询日曲线环比图表**/
    @GetMapping("/curvechatAjaxhb")
    @ResponseBody
    public AjaxResult curvechatAjaxhb(String type,String wtId,  String recorddate) {
        Map<String, Object> vos=new HashMap<>();
        if (StringUtils.isNotEmpty(recorddate))
        {
            Date  date= DateUtils.parseDate(recorddate);
            vos= leaderboardService.curvechatAjaxhb( type,wtId, date);
        }


        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }
    /**查询日曲线标杆图表**/
    @GetMapping("/curvechatAjaxbg")
    @ResponseBody
    public AjaxResult curvechatAjaxbg(String type,String wtId,  String recorddate) {
        Map<String, Object> vos=new HashMap<>();
        if (StringUtils.isNotEmpty(recorddate))
        {
            Date  date= DateUtils.parseDate(recorddate);
            vos= leaderboardService.curvechatAjaxbg( type,wtId, date);
        }


        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }

    /*******************************************************************************************************************************/
    /**查询月曲线偏差率列表**/
    @GetMapping("/curvefittingmonthmainList")
    @ResponseBody
    public AjaxResult curvefittingmonthmainList(Tablepar tablepar, String wpId, String year,String month) {

        List<Curvefittingmonthmain> vos= leaderboardService.curvefittingmonthmainList( tablepar, wpId, year, month);

        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }

    /**查询月曲线（实际/保证 实际/最优 最优/保证）图表**/
    @GetMapping("/curveMonthchatAjax")
    @ResponseBody
    public AjaxResult curveMonthchatAjax(String type,String wtId,  String year,String month) {

        Map<String, Object> vos= leaderboardService.curveMonthchatAjax( type,wtId, year, month);

        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }

    /**查询月曲线同比图表**/
    @GetMapping("/curveMonthchatAjaxtb")
    @ResponseBody
    public AjaxResult curveMonthchatAjaxtb(String type,String wtId, String year,String month) {

        Map<String, Object> vos= leaderboardService.curveMonthchatAjaxtb( type,wtId, year, month);

        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }
    /**查询月曲线环比图表**/
    @GetMapping("/curveMonthchatAjaxhb")
    @ResponseBody
    public AjaxResult curveMonthchatAjaxhb(String type,String wtId,  String year,String month) {

        Map<String, Object> vos= leaderboardService.curveMonthchatAjaxhb( type,wtId, year, month);

        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }
    /**查询月曲线标杆图表**/
    @GetMapping("/curveMonthchatAjaxbg")
    @ResponseBody
    public AjaxResult curveMonthchatAjaxbg(String type,String wtId,  String year,String month) {

        Map<String, Object> vos= leaderboardService.curveMonthchatAjaxbg( type,wtId, year, month);

        if (vos != null && !vos.isEmpty()) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }
    }
}
