package com.gyee.frame.controller.powercompare;

import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.StopTypeTreeVo;
import com.gyee.frame.service.Windpowerinfoday3Service;
import com.gyee.frame.service.Windturbineinfoday3Service;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/powercompare")
@Api(value = "风机绩效榜功能",tags =  "风机绩效榜功能")
public class PowerCompareController {

    @Resource
    private Windpowerinfoday3Service windpowerinfoday3Service;
    @Resource
    private Windturbineinfoday3Service windturbineinfoday3Service;

    /**
     * 报警类型列表
     * @return
     */
    @GetMapping("/warningClassAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询报警类型列表", notes = "查询报警类型列表")
    public AjaxResult warningClassAjax() {

        List<WarningClassify> vos = new ArrayList<WarningClassify>();
        if(!InitialRunner.warningClassls.isEmpty())
        {
            vos=InitialRunner.warningClassls;
        }
        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }
    }

    /**
     * 风场下拉接口
     * @return
     */
    @GetMapping("/windfarmAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询风电场数据列表", notes = "查询风电场数据列表")
    public AjaxResult windfarmAjax() {
        List<StopTypeTreeVo> vos = new ArrayList<StopTypeTreeVo>();
        for (Windpowerstation station : InitialRunner.wpls) {
            if (station.getId().endsWith("FDC")) {
                StopTypeTreeVo vo = new StopTypeTreeVo();
                vo.setId(station.getId());
                vo.setName(station.getName());
                vo.setpId("0");
                vos.add(vo);
            }
        }
        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }
    }

    /**
     * 风电场下拉接口
     * @return
     */
    @GetMapping("/windfarmAllAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询风电场(光伏)数据列表", notes = "查询风电场(光伏)数据列表")
      public AjaxResult windfarmAllAjax() {
        List<StopTypeTreeVo> vos = new ArrayList<StopTypeTreeVo>();
        for (Windpowerstation station : InitialRunner.wpallls) {
            StopTypeTreeVo vo = new StopTypeTreeVo();
            vo.setId(station.getId());
            vo.setName(station.getName());
            vo.setpId("0");
            vos.add(vo);
        }
        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }
    }

    /**
     * 项目下拉接口
     * @return
     */
    @GetMapping("/projectAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "通过风场编号查询项目数据列表", notes = "通过风场编号查询项目数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wpIds", value = "风场编号", required = true, dataType = "string", paramType = "query")})

    public AjaxResult projectAjax(String wpIds) {
        List<StopTypeTreeVo> vos = new ArrayList<StopTypeTreeVo>();
        if (StringUtils.notEmp(wpIds)) {
            String[] ids = wpIds.split(",");
            // String pids = "";

            for (String id : ids) {
                for (Project project : InitialRunner.pjsmap.get(id)) {

                    StopTypeTreeVo vo = new StopTypeTreeVo();
                    vo.setId(project.getId());
                    vo.setName(project.getName());
                    vo.setpId("0");
                    vos.add(vo);
                }
            }
        }
        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }
    }


    /**
     * 所有风电场项目下拉接口
     * @return
     */
    @GetMapping("/projectAllAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询风电项目数据列表", notes = "查询风电项目数据列表")
    public AjaxResult projectAllAjax() {
        List<StopTypeTreeVo> vos = new ArrayList<StopTypeTreeVo>();
        for (Project project : InitialRunner.pjls) {

            StopTypeTreeVo vo = new StopTypeTreeVo();
            vo.setId(project.getId());
            vo.setName(project.getName());
            vo.setpId("0");
            vos.add(vo);
        }
        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }
    }

    /**
     * 线路下拉接口
     * @return
     */
    @GetMapping("/lineAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "通过项目编号查询线路数据列表", notes = "通过项目编号查询线路数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pjIds", value = "项目编号", required = true, dataType = "string", paramType = "query")})

    public AjaxResult lineAjax(String pjIds) {
        List<StopTypeTreeVo> vos = new ArrayList<StopTypeTreeVo>();
        if (StringUtils.notEmp(pjIds)) {
            String[] ids = pjIds.split(",");
            // String pids = "";

            for (String id : ids) {
                for (Line line : InitialRunner.lnsmap.get(id)) {

                    StopTypeTreeVo vo = new StopTypeTreeVo();
                    vo.setId(line.getId());
                    vo.setName(line.getName());
                    vo.setpId("0");
                    vos.add(vo);
                }
            }
        }
        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }
    }
    /**
     * 线路下拉接口
     * @return
     */
    @GetMapping("/lineWpIdAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "通过风场编号查询线路数据列表", notes = "通过项目编号查询线路数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query")})

    public AjaxResult lineWpIdAjax(String wpId) {
        List<StopTypeTreeVo> vos = new ArrayList<StopTypeTreeVo>();
        if (StringUtils.notEmp(wpId)) {

            if(InitialRunner.wpmap.containsKey(wpId))
            {
                List<String>  ids=new ArrayList<>();
                for (Project project : InitialRunner.pjsmap.get(wpId)) {
                    ids.add(project.getId());
                }

                for (String id : ids) {
                    for (Line line : InitialRunner.lnsmap.get(id)) {

                        StopTypeTreeVo vo = new StopTypeTreeVo();
                        vo.setId(line.getId());
                        vo.setName(line.getName());
                        vo.setpId("0");
                        vos.add(vo);
                    }
                }
            }


        }
        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }
    }
    /**
     * 线路下拉接口
     * @return
     */
    @GetMapping("/lineAllAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询风电线路数据列表", notes = "查询风电线路数据列表")

    public AjaxResult lineAllAjax() {
        List<StopTypeTreeVo> vos = new ArrayList<StopTypeTreeVo>();
        for (Line line : InitialRunner.linels) {

            StopTypeTreeVo vo = new StopTypeTreeVo();
            vo.setId(line.getId());
            vo.setName(line.getName());
            vo.setpId("0");
            vos.add(vo);
        }
        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }
    }
    /**
     * 通过风电场ID获取风机下拉接口
     * @return
     */
    @GetMapping("/windturbineAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "通过风场编号查询风机数据列表", notes = "通过风场编号查询风机数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query")})

    public AjaxResult windturbineAjax(String wpId) {

        List<StopTypeTreeVo> vos = new ArrayList<StopTypeTreeVo>();

        if (StringUtils.isNotEmpty(wpId) && InitialRunner.wp_wtmap.containsKey(wpId)){

            List<Windturbine> wtls=InitialRunner.wp_wtmap.get(wpId);

            for (Windturbine wt : wtls) {
                StopTypeTreeVo vo = new StopTypeTreeVo();
                vo.setId(wt.getId());
                vo.setName(wt.getName());
                vo.setpId("0");
                vos.add(vo);
            }
        }


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

    /**
     * 获得所有风电场风机下拉接口
     * @return
     */
    @GetMapping("/windturbineAllAjax")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询风机数据列表", notes = "查询风机数据列表")

    public AjaxResult windturbineAllAjax() {

        List<StopTypeTreeVo> vos = new ArrayList<StopTypeTreeVo>();

            for (Windturbine wt : InitialRunner.wtls) {
                StopTypeTreeVo vo = new StopTypeTreeVo();
                vo.setId(wt.getId());
                vo.setName(wt.getName());
                vo.setpId("0");
                vos.add(vo);
            }


        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }
    }
    /**查询风机图表数据结果**/
    @GetMapping("/queryPower")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询风机图表数据结果", notes = "查询风机图表数据结果")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wpIds", value = "风场编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pjIds", value = "项目编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "lineIds", value = "线路编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "开始日期", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult queryPower(String wpIds, String pjIds, String lineIds, 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);
        }
        Map<String, List<String>> resultmap = new HashMap<String, List<String>>();
        resultmap = windturbineinfoday3Service.getWindturbineinfoJson(wpIds, pjIds, lineIds, beginDate_d, endDate_d);
        if(resultmap !=null && !resultmap.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,resultmap);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,resultmap);
        }
    }

    /**查询风场、项目、线路图表数据结果**/
    @GetMapping("/queryPowerAll")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询风场、项目、线路图表数据结果", notes = "查询风场、项目、线路图表数据结果")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wpIds", value = "风场编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "开始日期", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型 fc:风场,pj:项目,xl:线路", required = true, dataType = "string", paramType = "query")})

    public AjaxResult queryPowerAll(String wpIds,  String beginDate,
                                 String endDate,String  queryType)  {
        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<String>> resultmap = new HashMap<String, List<String>>();
        if (StringUtils.notEmp(queryType)) {
            if (queryType.equals("fc")) {
                resultmap = windpowerinfoday3Service.getWindturbineinfoAllJson(wpIds, beginDate_d, endDate_d, 0);
            } else if (queryType.equals("pj")) {
                resultmap = windpowerinfoday3Service.getWindturbineinfoAllJson(wpIds, beginDate_d, endDate_d, 1);
            } else if (queryType.equals("xl")) {
                resultmap = windpowerinfoday3Service.getWindturbineinfoAllJson(wpIds, beginDate_d, endDate_d, 2);
            }
        }
        if(resultmap !=null && !resultmap.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,resultmap);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,resultmap);
        }
    }

    /**查询风机列表数据结果**/
    @GetMapping("/powerAjax")
    @ResponseBody
    @CrossOrigin

    @ApiOperation(value = "查询风机图表数据结果", notes = "查询风机图表数据结果")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wpIds", value = "风场编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pjIds", value = "项目编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "lineIds", value = "线路编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "开始日期", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortName", value = "排序字段", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortOrder", value = "排序顺序 asc,desc", required = true, dataType = "string", paramType = "query")})
    public AjaxResult powerAjax(String wpIds, String pjIds, String lineIds, String beginDate,
                                String endDate, String sortName, String sortOrder)  {
        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 = windturbineinfoday3Service.getWindturbineinfo(wpIds, pjIds, lineIds, beginDate_d, endDate_d, sortName,
                sortOrder);
//        if(vos !=null && !vos.isEmpty())
//        {
//            return	AjaxResult.successData(AjaxStatus.success.code,vos);
//        }else
//        {
//            return	AjaxResult.successData(AjaxStatus.success.code,vos);
//        }


        List<Map<String,Object>> lylls=new ArrayList<>();
        Map<String,Object> result=new LinkedHashMap<>();



        if(vos !=null  && !vos.isEmpty())
        {

            List<Windturbineinfoday3> taglist=vos.stream()
                    .filter(Windturbineinfoday3->(!Windturbineinfoday3.getWtName().equals("合计")))
                    .collect(Collectors.toList());
            Optional<Windturbineinfoday3> maxEmp= taglist.stream().filter(Objects::nonNull).max(Comparator.comparingDouble(Windturbineinfoday3 ::getTotal));
            Windturbineinfoday3 wi3 = maxEmp.orElse(new Windturbineinfoday3());
            double xAxis=wi3.getTotal();
            for (int i=0;i<vos.size();i++)
            {
                Windturbineinfoday3 wi=vos.get(i);
                if(!wi.getWtName().endsWith("合计"))
                {
                    Map<String,Object> data=new LinkedHashMap<>();
                    data.put("yAxis",i);
                    data.put("xAxis",xAxis);
                    data.put("symbolSize",1);
                    data.put("symbol","circle");

                    Map<String,Object> itemStyle=new LinkedHashMap<>();
                    Map<String,Object> normal=new LinkedHashMap<>();
                    normal.put("color","#87b754");
                    itemStyle.put("normal",normal);
                    data.put("itemStyle",itemStyle);

                    Map<String,Object> label=new LinkedHashMap<>();
                    Map<String,Object> normal2=new LinkedHashMap<>();

                    normal2.put("color","#000");
                    normal2.put("position","right");
                    normal2.put("distance",12);
                    normal2.put("show",true);
                    normal2.put("formatter","利用率"+wi.getLyl());

                    label.put("normal",normal2);
                    data.put("label",label);

                    lylls.add(data);
                }

            }

            //       System.out.println( JSONObject.toJSONString(lylls));

            result.put("list",vos);
            result.put("lyl",lylls);
            return	AjaxResult.successData(AjaxStatus.success.code,result);
        }else
        {
            result.put("list",vos);
            result.put("lyl",lylls);
            return	AjaxResult.successData(AjaxStatus.success.code,result);
        }
    }

    /**查询风场、项目、线路列表数据结果**/
    @GetMapping("/powerAjaxAll")
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询风场、项目、线路列表数据结果", notes = "查询风场、项目、线路列表数据结果")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wpIds", value = "风场编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "开始日期", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型 fc:风场,pj:项目,xl:线路", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortName", value = "排序字段", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortOrder", value = "排序顺序 asc,desc", required = true, dataType = "string", paramType = "query")})

    public AjaxResult powerAjaxAll(String wpIds,  String beginDate,
                                    String endDate,String  queryType, String sortName, String sortOrder)  {
        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<Windpowerinfoday3>  vos=null;

        if (queryType.equals("fc")) {
            vos = windpowerinfoday3Service.getWindturbineinfoAll(wpIds, beginDate_d, endDate_d, 0, sortName,
                    sortOrder);
        } else if (queryType.equals("pj")) {
            vos = windpowerinfoday3Service.getWindturbineinfoAll(wpIds, beginDate_d, endDate_d, 1, sortName,
                    sortOrder);
        } else if (queryType.equals("xl")) {
            vos = windpowerinfoday3Service.getWindturbineinfoAll(wpIds, beginDate_d, endDate_d, 2, sortName,
                    sortOrder);
        }

        List<Map<String,Object>> lylls=new ArrayList<>();
        Map<String,Object> result=new LinkedHashMap<>();



        if(vos !=null  && !vos.isEmpty())
        {


            List<Windpowerinfoday3> taglist=vos.stream()
                    .filter(Windpowerinfoday3->(!Windpowerinfoday3.getWtName().equals("合计")))
                    .collect(Collectors.toList());

            Optional<Windpowerinfoday3> maxEmp= taglist.stream().filter(Objects::nonNull).max(Comparator.comparingDouble(Windpowerinfoday3 ::getTotal));
            Windpowerinfoday3 wi3 = maxEmp.orElse(new Windpowerinfoday3());
            double xAxis=wi3.getTotal();
            for (int i=0;i<vos.size();i++)
            {
                Windpowerinfoday3 wi=vos.get(i);

                if(!wi.getWtName().endsWith("合计"))
                {
                    Map<String,Object> data=new LinkedHashMap<>();
                    data.put("yAxis",i);
                    data.put("xAxis",xAxis);
                    data.put("symbolSize",1);
                    data.put("symbol","circle");

                    Map<String,Object> itemStyle=new LinkedHashMap<>();
                    Map<String,Object> normal=new LinkedHashMap<>();
                    normal.put("color","#87b754");
                    itemStyle.put("normal",normal);
                    data.put("itemStyle",itemStyle);

                    Map<String,Object> label=new LinkedHashMap<>();
                    Map<String,Object> normal2=new LinkedHashMap<>();

                    normal2.put("color","#000");
                    normal2.put("position","right");
                    normal2.put("distance",12);
                    normal2.put("show",true);
                    normal2.put("formatter","利用率"+wi.getLyl());

                    label.put("normal",normal2);
                    data.put("label",label);

                    lylls.add(data);
                }

            }

     //       System.out.println( JSONObject.toJSONString(lylls));

            result.put("list",vos);
            result.put("lyl",lylls);
            return	AjaxResult.successData(AjaxStatus.success.code,result);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.success.code,result);
        }
    }
}
