package com.gyee.frame.controller.yaw;

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.custom.YawValuesVo;
import com.gyee.frame.model.custom.YawVo;
import com.gyee.frame.service.yaw.YawService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/yaw")
@Api(value = "偏航对风分析", tags = "偏航对风分析")
public class YawController {

    private static final String format = "yyyy-MM-dd HH:mm";
    @Resource
    private YawService YawService;



    @PostMapping("/getPassRate")
    @ResponseBody
    @ApiOperation(value = "功率或风速合格率", notes = "功率或风速合格率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, 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 = "type", value = "类型 1 功率 2风速", required = true, dataType = "string", paramType = "query")})

    public AjaxResult getPassRate(String wtId, String beginDate, String endDate,String type) throws Exception {

        List<YawVo>  vos = new ArrayList<>();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate)  && StringUtils.notEmp(endDate) && StringUtils.notEmp(type)) {

            List<YawVo> ls=new ArrayList<>();
            if(type.equals("1"))
            {
                ls = YawService.getYawPowerTotal(wtId, DateUtils.parseDate(beginDate), DateUtils.parseDate(endDate));

                if(!ls.isEmpty())
                {
                    for(YawVo vo:ls)
                    {
                        if(vo.getPower()>0)
                        {
                            int hgsl=vo.getQualified();
                            int bhgsl=vo.getUnqualified();
                            if((hgsl+bhgsl)!=0)
                            {
                                double rate = new BigDecimal(hgsl).divide(new BigDecimal(hgsl+bhgsl), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                vo.setPassrate(rate);
                            }else
                            {
                                vo.setPassrate(100.0);
                            }

                            vo.setPower(vo.getPower()*100);
                            vos.add(vo);
                        }
                    }
                }
            }else  if(type.equals("2"))
            {
                ls = YawService.getYawSpeedTotal(wtId, DateUtils.parseDate(beginDate), DateUtils.parseDate(endDate));
                if(!ls.isEmpty())
                {
                    for(YawVo vo:ls)
                    {
                        if(vo.getSpeed()>2)
                        {
                            int hgsl=vo.getQualified();
                            int bhgsl=vo.getUnqualified();
                            if((hgsl+bhgsl)!=0)
                            {
                                double rate = new BigDecimal(hgsl).divide(new BigDecimal(hgsl+bhgsl), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                vo.setPassrate(rate);
                            }else
                            {
                                vo.setPassrate(100.0);
                            }
                            vos.add(vo);
                        }
                    }
                }
            }
        }
        if (null != vos) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        }

    }


    @PostMapping("/getRanges")
    @ResponseBody
    @ApiOperation(value = "偏航对风合格区域分析", notes = "偏航对风合格区域分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, 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 = "value", value = "功率、风速对应数值", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型 1 功率 2风速", required = true, dataType = "string", paramType = "query")})

    public AjaxResult getRanges(String wtId, String beginDate, String endDate,Integer value,String type) throws Exception {

        YawValuesVo vo=new YawValuesVo();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate)  && StringUtils.notEmp(endDate) && StringUtils.notEmp(value) && StringUtils.notEmp(type) ) {
            List<YawValuesVo>  vos = new ArrayList<>();
            if(type.equals("1"))
            {
                vos = YawService.getYawValuesPowerTotal(wtId, DateUtils.parseDate(beginDate), DateUtils.parseDate(endDate),value);

                if(!vos.isEmpty())
                {
                    vo=vos.get(0);

                }
            }else  if(type.equals("2"))
            {
                vos = YawService.getYawValuesSpeedTotal(wtId, DateUtils.parseDate(beginDate), DateUtils.parseDate(endDate),value);
                if(!vos.isEmpty())
                {
                    vo=vos.get(0);

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

    }



    @PostMapping("/getTotalRanges")
    @ResponseBody
    @ApiOperation(value = "所有风速偏航对风合格区域分析", notes = "所有风速偏航对风合格区域分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, 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 = "type", value = "类型 1 功率 2风速", required = true, dataType = "string", paramType = "query")})

    public AjaxResult getTotalRanges(String wtId, String beginDate, String endDate,String type) throws Exception {

        YawValuesVo vo=new YawValuesVo();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate)  && StringUtils.notEmp(endDate) && StringUtils.notEmp(type) ) {
            List<YawValuesVo>  vos = new ArrayList<>();
            if(type.equals("1"))
            {
                vos = YawService.getYawValuesPowerTotalAll(wtId, DateUtils.parseDate(beginDate), DateUtils.parseDate(endDate));

                if(!vos.isEmpty())
                {
                    vo=vos.get(0);

                }
            }else  if(type.equals("2"))
            {
                vos = YawService.getYawValuesSpeedTotalAll(wtId, DateUtils.parseDate(beginDate), DateUtils.parseDate(endDate));
                if(!vos.isEmpty())
                {
                    vo=vos.get(0);

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

    }

    @PostMapping("/getWppointnum")
    @ResponseBody
    @ApiOperation(value = "获得数据查询测点数量统计", notes = "获得数据查询测点数量统计")
    @ApiImplicitParams({})

    public AjaxResult getWppointnum() throws Exception {

        Map<String,Integer> wppointnummap=new HashMap<>();

        wppointnummap= InitialRunner.wppointnummap;
        if (null != wppointnummap) {
            return AjaxResult.successData(AjaxStatus.success.code, wppointnummap);
        } else {
            return AjaxResult.successData(AjaxStatus.success.code, wppointnummap);
        }

    }
}
