package com.gyee.frame.controller.powersaturation;

import com.github.pagehelper.PageInfo;
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.ModelPower;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.auto.Windturbinecurvefitting;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.service.WindturbinecurvefittingService;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/powersaturation")
public class PowerSaturationController {

    @Autowired
    private WindturbinecurvefittingService windturbinecurvefittingService;

    /**日功率饱和列表**/
    @GetMapping("/powersaturationlist")
    @ResponseBody
    public AjaxResult powersaturationlist(Tablepar tablepar, String wtId, String recorddate) {

        PageInfo<Windturbinecurvefitting> vos=new PageInfo<>();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {
            Date date=DateUtils.parseStrtoDate(recorddate);
            vos=windturbinecurvefittingService.comparechatAjax(tablepar, wtId, date);
        }

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

    }
    /**日功率饱和图表**/
    @PostMapping("/powersaturationchart")
    @ResponseBody
    public AjaxResult powersaturationchart(String wtId,String recorddate) {

        List<DataVo> vos = new ArrayList<DataVo>();
        Map<String, Map<String, ModelPower>> modelPowermap = InitialRunner.modelPowermap;
        Map<String, Windturbine> wtmap = InitialRunner.wtmap;

        Map<String, ModelPower> map = null;
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {
            Date date=DateUtils.parseStrtoDate(recorddate);
            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);// 保证功率曲线
                        }

                        vos.add(vo);
                    }

                }
            }
        }

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