package com.gyee.frame.controller.goodness;

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.DataVo;
import com.gyee.frame.model.custom.SawVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.model.custom.ValueVo;
import com.gyee.frame.service.*;
import com.gyee.frame.service.goodness.WindturbinegoodnessService;
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.util.*;

/**
 * 风机明细信息页面
 */
@Controller
@RequestMapping("/goodness")
@Api(value = "单机信息总览详细信息",tags =  "单机信息总览详细信息")
public class WindturbineGoodnessDetailController {
    @Resource
    private WindturbinegoodnessService windturbinegoodnessService;
    @Resource
    private InputoroutputspeedtotalService inputoroutputspeedtotalServicenegoodnessService;
    @Resource
    private WarningRecordsService warningRecordsService;
    @Resource
    private Brownoutsevent2Service brownoutsevent2Service;
    @Resource
    private Shutdownevent2Service shutdownevent2Service;
    @Resource
    private WarningInfoDayService warningInfoDayService;
    @Resource
    private WindturbineanalysisdayService windturbineanalysisdayService;
    @Resource
    private WindturbineinfodayService windturbineinfodayService;
    @Resource
    private WindturbinecurvefittingService windturbinecurvefittingService;
    @Resource
    private WindturbinethewindinfoService windturbinethewindinfoService;
    @Resource
    private WindpowerstationthewindinfoService windpowerstationthewindinfoService;

    private String jfpl;


    /**
     * 获得明细页面显示信息
     *
     * @param wtId
     * @param recorddate
     * @return
     */
    @PostMapping("/wadAjax")
    @ResponseBody
    @ApiOperation(value = "查询明细页面显示信息", notes = "查询明细页面显示信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult wadAjax(String wtId, String recorddate) {

        Windturbineanalysisday wtday = new Windturbineanalysisday();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate)) {
            Date date = DateUtils.parseDate(recorddate);
            List<Windturbineanalysisday> wadls = windturbineanalysisdayService.comparelist(wtId, date);

            List<WindTurbineInfoDay> wtdls = windturbineinfodayService.getWindturbineinfo(wtId, date);

            if (null != wadls && !wadls.isEmpty() && null != wtdls && !wtdls.isEmpty()) {

                wtday = wadls.get(0);
                WindTurbineInfoDay wt = wtdls.get(0);
                double fdl = null != wt.getGeneratingcapacity2() ? wt.getGeneratingcapacity2() : 0.0;
                double gz = null != wt.getLossofpower2() ? wt.getLossofpower2() : 0.0;
                double jx = null != wt.getLossofpower3() ? wt.getLossofpower3() : 0.0;
                double qf = null != wt.getLossofpower4() ? wt.getLossofpower4() : 0.0;
                double xd = null != wt.getLossofpower1() ? wt.getLossofpower1() : 0.0;
                double sl1 = null != wt.getMainlossin() ? wt.getMainlossin() : 0.0;
                double sl2 = null != wt.getFaultlossin() ? wt.getFaultlossin() : 0.0;
                double sl3 = null != wt.getElectriclossout() ? wt.getElectriclossout() : 0.0;
                double sl4 = null != wt.getWeatherlossout() ? wt.getWeatherlossout() : 0.0;

                wtday.setC4(StringUtils.round(fdl + gz + jx + qf + xd + sl1 + sl2 + sl3 + sl4, 2));
                wtday.setC31(StringUtils.round(gz + jx + qf + xd + sl1 + sl2 + sl3 + sl4, 2));
                wtday.setC5(getMonthC5(wtId, date));
            }

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

    }


    private double getMonthC5(String wtId, Date recorddate) {

        List<WindTurbineInfoDay> wtdls;

        Calendar c = Calendar.getInstance();
        c.setTime(recorddate);
        c.set(Calendar.DAY_OF_MONTH, 1);
        Date beginDate = DateUtils.truncate(c.getTime());
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date endate = DateUtils.truncate(c.getTime());

        wtdls = windturbineinfodayService.getWindturbineinfo(beginDate, endate, wtId);
        double fdl = 0.0;
        double gz = 0.0;
        double jx = 0.0;
        double qf = 0.0;
        double xd = 0.0;
        double sl1 = 0.0;
        double sl2 = 0.0;
        double sl3 = 0.0;
        double sl4 = 0.0;
        if (null != wtdls && !wtdls.isEmpty()) {
            for (WindTurbineInfoDay wt : wtdls) {
                fdl = fdl + (null != wt.getGeneratingcapacity2() ? wt.getGeneratingcapacity2() : 0.0);
                gz = gz + (null != wt.getLossofpower2() ? wt.getLossofpower2() : 0.0);
                jx = jx + (null != wt.getLossofpower3() ? wt.getLossofpower3() : 0.0);
                qf = qf + (null != wt.getLossofpower4() ? wt.getLossofpower4() : 0.0);
                xd = xd + (null != wt.getLossofpower1() ? wt.getLossofpower1() : 0.0);
                sl1 = sl1 + (null != wt.getMainlossin() ? wt.getMainlossin() : 0.0);
                sl2 = sl2 + (null != wt.getFaultlossin() ? wt.getFaultlossin() : 0.0);
                sl3 = sl3 + (null != wt.getElectriclossout() ? wt.getElectriclossout() : 0.0);
                sl4 = sl4 + (null != wt.getWeatherlossout() ? wt.getWeatherlossout() : 0.0);
            }
            return StringUtils.round(fdl + gz + jx + qf + xd + sl1 + sl2 + sl3 + sl4, 2);
        }
        return 0.0;
    }

    /*
     * 单台风机当月报警排行
     */
    @PostMapping("/bjphlist")
    @ResponseBody
    @ApiOperation(value = "单台风机当月报警排行", notes = "单台风机当月报警排行")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tablepar", value = "分页排序对象", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult bjphlist(Tablepar tablepar, String wtId, String recorddate) {
        List<SawVo> resultList =new ArrayList<>();
        if(StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);

            cal.setTime(DateUtils.parseStrtoDate(recorddate));
            Date beginDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date endDate = cal.getTime();
           resultList = warningRecordsService.getWarningRecordsTop5(tablepar, wtId, beginDate, endDate);

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

    /*
     * 获取明细页面功率曲线
     */
    @PostMapping("/glchat")
    @ResponseBody
    @ApiOperation(value = "获取明细页面功率曲线", notes = "获取明细页面功率曲线")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult glchat(String wtId, String recorddate) {

        List<DataVo> datavos = new ArrayList<>();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate)) {
            Date date = DateUtils.parseStrtoDate(recorddate);
            Map<String, Map<String, ModelPower>> modelPowermap = InitialRunner.modelPowermap;
            Map<String, Windturbine> wtmap = InitialRunner.wtmap;
            Map<String, ModelPower> map = null;

            List<Windturbinecurvefitting> wcls = windturbinecurvefittingService.comparechatAjax(wtId, date);
            if (!wcls.isEmpty()) {
                if (wtmap.containsKey(wtId)) {
                    Windturbine wt = wtmap.get(wtId);
                    if (modelPowermap.containsKey(wt.getModelid())) {
                        map = modelPowermap.get(wt.getModelid());
                    }
                }
                double temp = 0.0;
                for (Windturbinecurvefitting wc : wcls) {
                    DataVo vo = new DataVo();

                    int c = (int) wc.getSpeed().intValue();

                    if (c == wc.getSpeed()) {
                        vo.setValue1(wc.getSpeed());// 风速
                        vo.setValue2(wc.getActualpower());// 实际拟合功率
                        vo.setValue3(wc.getOptimalpower());// 最优拟合功率

                        if (null != map && StringUtils.notEmp(wc.getSpeed()) && map.containsKey(String.valueOf(wc.getSpeed()))) {
                            ModelPower mp = map.get(String.valueOf(wc.getSpeed()));
                            vo.setValue4(mp.getEnsurepower());// 保证功率曲线
                            temp = mp.getEnsurepower();
                        } else {
                            vo.setValue4(temp);// 保证功率曲线
                        }

                        datavos.add(vo);
                    }

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

    /*
     * 获取风机风资源
     */
    @PostMapping("/fjfzy")
    @ResponseBody
    @ApiOperation(value = "获取风机风资源", notes = "获取风机风资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult fjfzy(String wtId, String recorddate) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<ValueVo> vos = new ArrayList<ValueVo>();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate)) {

            Date date = DateUtils.parseDate(recorddate);
            List<Windturbinethewindinfo> wtdls = windturbinethewindinfoService.fzy(wtId, date);


            if (!wtdls.isEmpty()) {

                Windturbinethewindinfo winfo = wtdls.get(0);
                int c = (int) winfo.getCb().intValue();

                jfpl = "静风频率为" + c;
                for (int i = 0; i < 16; i++) {
                    ValueVo vo = new ValueVo();

                    switch (i) {
                        case 0:
                            // vo.setName("N("+winfo.getN4()+"/"+winfo.getN()+")");
                            vo.setName("N");
                            vo.setData1(winfo.getN2());
                            break;
                        case 1:
                            // vo.setName("NNE("+winfo.getNne4()+"/"+winfo.getNne()+")");
                            vo.setName("NNE");
                            vo.setData1(winfo.getNne2());
                            break;
                        case 2:
                            // vo.setName("NE("+winfo.getNe4()+"/"+winfo.getNe()+")");
                            vo.setName("NE");
                            vo.setData1(winfo.getNe2());
                            break;
                        case 3:
                            // vo.setName("ENE("+winfo.getEne4()+"/"+winfo.getEne()+")");
                            vo.setName("ENE");
                            vo.setData1(winfo.getEne2());

                            break;
                        case 4:
                            // vo.setName("E("+winfo.getE4()+"/"+winfo.getE()+")");
                            vo.setName("E");
                            vo.setData1(winfo.getE2());
                            break;
                        case 5:
                            // vo.setName("ESE("+winfo.getEse()+"/"+winfo.getEse()+")");
                            vo.setName("ESE");
                            vo.setData1(winfo.getEse2());
                            break;
                        case 6:
                            // vo.setName("SE("+winfo.getSe4()+"/"+winfo.getSe()+")");
                            vo.setName("SE");
                            vo.setData1(winfo.getSe2());
                            break;
                        case 7:
                            // vo.setName("SSE("+winfo.getSse4()+"/"+winfo.getSse()+")");
                            vo.setName("SSE");
                            vo.setData1(winfo.getSse2());
                            break;
                        case 8:
                            // vo.setName("S("+winfo.getS4()+"/"+winfo.getS()+")");
                            vo.setName("S");
                            vo.setData1(winfo.getS2());
                            break;
                        case 9:
                            // vo.setName("SSW("+winfo.getSsw4()+"/"+winfo.getSsw()+")");
                            vo.setName("SSW");
                            vo.setData1(winfo.getSsw2());
                            break;
                        case 10:
                            // vo.setName("SW("+winfo.getSw4()+"/"+winfo.getSw()+")");
                            vo.setName("SW");
                            vo.setData1(winfo.getSw2());
                            break;
                        case 11:
                            // vo.setName("WSW("+winfo.getWsw4()+"/"+winfo.getWsw()+")");
                            vo.setName("WSW");
                            vo.setData1(winfo.getWsw2());

                            break;
                        case 12:
                            // vo.setName("W("+winfo.getW4()+"/"+winfo.getW()+")");
                            vo.setName("W");
                            vo.setData1(winfo.getW2());
                            break;
                        case 13:
                            // vo.setName("WNW("+winfo.getWnw4()+"/"+winfo.getWnw()+")");
                            vo.setName("WNW");
                            vo.setData1(winfo.getWnw2());
                            break;
                        case 14:
                            // vo.setName("NW("+winfo.getNw4()+"/"+winfo.getNw()+")");
                            vo.setName("NW");
                            vo.setData1(winfo.getNw2());
                            break;
                        case 15:
                            // vo.setName("NNW("+winfo.getNnw4()+"/"+winfo.getNnw()+")");
                            vo.setName("NNW");
                            vo.setData1(winfo.getNnw2());
                            break;

                        default:
                            break;
                    }

                    vos.add(vo);
                }


                map.put("jfpl", jfpl);
                map.put("data", vos);
            }


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



}
