package com.gyee.runeconomy.controller.goodness;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.util.DateUtils;
import com.gyee.common.vo.benchmark.ValueVo;
import com.gyee.runeconomy.dto.DataVo;
import com.gyee.runeconomy.dto.R;
import com.gyee.runeconomy.dto.ResultMsg;
import com.gyee.runeconomy.dto.SawVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.service.auto.*;
import com.gyee.runeconomy.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.GetMapping;
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 IProEconWindturbineGoodnessService proEconWindturbineGoodnessService;
    @Resource
    private IProEconWtwindDayInfoService proEconWtwindDayInfoService;
    @Resource
    private IProEconWtAlysisDayService proEconWtAlysisDayService;

    @Resource
    private IProEconWtCurveFittingMonthService proEconWtCurveFittingMonthService;

    private String jfpl;

    @Resource
    private ITurbineInfoDayService turbineInfoDayService;


    /**
     * 获得明细页面显示信息
     *
     * @param wtId
     * @param recorddate
     * @return
     */
    @GetMapping("/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 R wadAjax(String wtId, String recorddate) {

        ProEconWtAlysisDay wtday = new ProEconWtAlysisDay();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate)) {

            Date date = DateUtils.parseDate(recorddate);
            QueryWrapper<ProEconWtAlysisDay> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("record_date", date).eq("windturbine_id", wtId);
            List<ProEconWtAlysisDay> wtadls = proEconWtAlysisDayService.list(queryWrapper);
            //                    .stream().filter(i->i.getWindturbineId().equals(wtId )
            //           && i.getRecordDate().compareTo(date)==0
            //            ).collect(Collectors.toList());
            if (!wtadls.isEmpty()) {
                wtday = wtadls.get(0);

                QueryWrapper<TurbineInfoDay> qw = new QueryWrapper<>();
                qw.lambda().eq(TurbineInfoDay::getRecordDate, date).eq(TurbineInfoDay::getTurbineId, wtId);
                List<TurbineInfoDay> turbineInfoDays = turbineInfoDayService.list(qw);
                TurbineInfoDay tb = turbineInfoDays.get(0);
                wtday.setRfdl(Math.round((tb.getRfdl() / 1000) * 100.0) / 100.0);
                wtday.setRyfdl(Math.round((tb.getLlfdl() / 1000) * 100.0) / 100.0);
                wtday.setNpjfs(tb.getPjfs());
            }
        }
        if (null != wtday) {
            return R.data(ResultMsg.ok(wtday));
        } else {
            return R.data(ResultMsg.error());
        }

    }


    /*
     * 单台风机当月报警排行
     */
    @PostMapping("/bjphlist")
    @ResponseBody
    @ApiOperation(value = "单台风机当月报警排行", notes = "单台风机当月报警排行")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public R bjphlist(Integer pageNum, Integer pageSize, 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.parseDate(recorddate));
            Date beginDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date endDate = cal.getTime();
            //            resultList = alarmsnapService.getWarningRecordsTop(tablepar, wtId, beginDate, endDate);

        }
        if (null != resultList) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.data(ResultMsg.error());
        }
    }

    /*
     * 获取明细页面功率曲线
     */
    @GetMapping("/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 R glchat(String wtId, String recorddate) {

        List<DataVo> datavos = new ArrayList<>();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate)) {
            Date date = DateUtils.parseDate(recorddate);
            Map<String, Map<Double, ProBasicModelPowerRd>> modelPowermap = CacheContext.theoreticalPowerMap;
            Map<String, ProBasicEquipment> wtmap = CacheContext.wtmap;
            Map<Double, ProBasicModelPowerRd> map = null;

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;

            QueryWrapper<ProEconWtCurveFittingMonth> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("windturbine_id", wtId).eq("year", String.valueOf(year)).eq("month", String.valueOf(month));

            List<ProEconWtCurveFittingMonth> wcls = proEconWtCurveFittingMonthService.list(queryWrapper);
            //                    .stream().filter(i->
            //                    i.getWindturbineId().equals(wtId)
            //                    && i.getYear().equals(String.valueOf(year)) && i.getMonth().equals(String.valueOf(month))
            //            ).collect(Collectors.toList());
            if (!wcls.isEmpty()) {
                if (wtmap.containsKey(wtId)) {
                    ProBasicEquipment wt = wtmap.get(wtId);
                    if (modelPowermap.containsKey(wt.getModelId())) {
                        map = modelPowermap.get(wt.getModelId());
                    }
                }
                double temp = 0.0;
                for (ProEconWtCurveFittingMonth 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()))) {
                            ProBasicModelPowerRd mp = map.get(String.valueOf(wc.getSpeed()));
                            vo.setValue4(mp.getEnsurePower());// 保证功率曲线
                            temp = mp.getEnsurePower();
                        } else {
                            vo.setValue4(temp);// 保证功率曲线
                        }

                        datavos.add(vo);
                    }

                }
            }
        }
        if (null != datavos) {
            return R.data(ResultMsg.ok(datavos));
        } else {
            return R.data(ResultMsg.error());
        }
    }

    /*
     * 获取风机风资源
     */
    @GetMapping("/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 R 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);

            QueryWrapper<ProEconWtwindDayInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("windturbine_id", wtId).eq("record_date", date);
            List<ProEconWtwindDayInfo> wtdls = proEconWtwindDayInfoService.list(queryWrapper);
            //                    .stream().filter(i->
            //                    i.getWindturbineId().equals(wtId)
            //                    && i.getRecordDate().compareTo(date) ==0
            //            ).collect(Collectors.toList());


            if (!wtdls.isEmpty()) {

                ProEconWtwindDayInfo 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 (null != map) {
            return R.data(ResultMsg.ok(map));
        } else {
            return R.data(ResultMsg.error());
        }
    }


}
