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 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.*;

@Controller
@RequestMapping("/powercompare")
public class PowerCompareController {

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


    /**
     * 风场下拉接口
     * @return
     */
    @GetMapping("/windfarmAjax")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,vos);
        }
    }

    /**
     * 风电场下拉接口
     * @return
     */
    @GetMapping("/windfarmAllAjax")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,vos);
        }
    }

    /**
     * 项目下拉接口
     * @return
     */
    @GetMapping("/projectAjax")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,vos);
        }
    }


    /**
     * 所有风电场项目下拉接口
     * @return
     */
    @GetMapping("/projectAllAjax")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,vos);
        }
    }
    /**
     * 线路下拉接口
     * @return
     */
    @GetMapping("/lineAjax")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,vos);
        }
    }
    /**
     * 风电场线路下拉接口
     * @return
     */
    @GetMapping("/lineAllAjax")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,vos);
        }
    }
    /**
     * 通过风电场ID获取风机下拉接口
     * @return
     */
    @GetMapping("/windturbineAjax")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,vos);
        }
    }

    /**
     * 获得所有风电场风机下拉接口
     * @return
     */
    @GetMapping("/windturbineAllAjax")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,vos);
        }
    }
    /**查询风机图表数据结果**/
    @GetMapping("/queryPower")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,resultmap);
        }
    }

    /**查询风场、项目、线路图表数据结果**/
    @GetMapping("/queryPowerAll")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,resultmap);
        }
    }

    /**查询风机列表数据结果**/
    @GetMapping("/powerAjax")
    @ResponseBody
    @CrossOrigin
    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.emptyresultset.code,vos);
        }
    }

    /**查询风场、项目、线路列表数据结果**/
    @GetMapping("/powerAjaxAll")
    @ResponseBody
    @CrossOrigin
    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);
        }
        if(vos !=null && !vos.isEmpty())
        {
            return	AjaxResult.successData(AjaxStatus.success.code,vos);
        }else
        {
            return	AjaxResult.successData(AjaxStatus.emptyresultset.code,vos);
        }
    }
}
