package com.gyee.frame.controller.powercompare;

import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.benchmarking.PowerCompareDetail;
import com.gyee.frame.service.benchmarking.PowerCompareDetailService;
import com.gyee.frame.util.DateUtils;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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


@Controller
@RequestMapping("/powercomparedetail")
@Api(value = "风机绩效榜明细",tags =  "风机绩效榜明细")
public class PowerCompareDetailController {

    @Resource
    private PowerCompareDetailService powerCompareDetailService;




    /**查询风机列表数据结果**/
    @GetMapping("/powerAjaxDetail")
    @ResponseBody
    @ApiOperation(value = "查询风机绩效榜明细信息", notes = "查询风机绩效榜明细信息")
    @ApiImplicitParams({

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

    public AjaxResult powerAjaxDetail(@RequestParam(value = "beginDate",required = true) String beginDate,
                                      @RequestParam(value = "endDate",required = true) String endDate,
                                      @RequestParam(value = "wpId",required = false) String  wpId,
                                      @RequestParam(value = "pjId",required = false) String  pjId,
                                      @RequestParam(value = "xlId",required = false) String  xlId )  {
        Date begin = DateUtils.parseDate(beginDate);
        Date end = DateUtils.parseDate(endDate);
        List<PowerCompareDetail>  vos = powerCompareDetailService.getDetailWtInfo(begin,end,wpId,pjId,xlId);
//        if(vos !=null && !vos.isEmpty())
//        {
//            vos.stream().forEach(i -> i.setFnlyl(i.getScadafdl()/i.getLlfdl()*100));
//            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())
        {

            Optional<PowerCompareDetail> maxEmp= vos.stream().filter(Objects::nonNull).max(Comparator.comparingDouble(PowerCompareDetail ::getLlfdl));
            PowerCompareDetail wi3 = maxEmp.orElse(new PowerCompareDetail());
            double xAxis=wi3.getLlfdl();
            for (int i=0;i<vos.size();i++)
            {
                PowerCompareDetail wi=vos.get(i);
                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","利用率"+(wi3.getScadafdl()/wi3.getLlfdl()*100));

                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("/powerAjaxDetailAll")
    @ResponseBody
    @ApiOperation(value = "查询风场、项目、线路列表数据结果", notes = "查询风场、项目、线路列表数据结果")
    @ApiImplicitParams({


            @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 powerAjaxDetailAll(@RequestParam(value = "beginDate",required = true) String beginDate,
                                         @RequestParam(value = "endDate",required = true) String endDate,
                                         @RequestParam(value = "queryType",required = true) String  queryType)  {

        List<PowerCompareDetail>  vos=null;
        Date begin = DateUtils.parseDate(beginDate);
        Date end = DateUtils.parseDate(endDate);
        if (queryType.equals("fc")) {
            vos = powerCompareDetailService.getDetailInfo(begin,end,0);
        } else if (queryType.equals("pj")) {
            vos = powerCompareDetailService.getDetailInfo(begin,end,1);
        } else if (queryType.equals("xl")) {
            vos = powerCompareDetailService.getDetailInfo(begin,end,2);
        }
//        if(vos !=null && !vos.isEmpty())
//        {
//            vos.stream().forEach(i -> i.setFnlyl(i.getScadafdl()/i.getLlfdl()*100));
//            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())
        {

            Optional<PowerCompareDetail> maxEmp= vos.stream().filter(Objects::nonNull).max(Comparator.comparingDouble(PowerCompareDetail ::getLlfdl));
            PowerCompareDetail wi3 = maxEmp.orElse(new PowerCompareDetail());
            double xAxis=wi3.getLlfdl();
            for (int i=0;i<vos.size();i++)
            {
                PowerCompareDetail wi=vos.get(i);
                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","利用率"+(wi3.getScadafdl()/wi3.getLlfdl()*100));

                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);
        }
    }
}
