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.Windturbinepowercurvefitting;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.service.WindturbinepowercurvefittingService;
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.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/powersaturation")
@Api(value = "总单机饱和度统计",tags =  "总单机饱和度统计")
public class PowerSaturationAmonutController {
    @Resource
    private WindturbinepowercurvefittingService windturbinepowercurvefittingService;

    /**总功率饱和列表**/
    @PostMapping("/powersaturationamonutlist")
    @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")})

    public AjaxResult powersaturationamonutlist(Tablepar tablepar, String wtId) {

        PageInfo<Windturbinepowercurvefitting> vos=new PageInfo<>();
        if (StringUtils.notEmp(wtId) )
        {
            vos=windturbinepowercurvefittingService.getWindturbinepowercurvefitting(tablepar, wtId);
        }

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

    }
    /**总功率饱和图表**/
    @PostMapping("/powersaturationamonutchart")
    @ResponseBody
    @ApiOperation(value = "查询总功率饱和图表", notes = "查询总功率饱和图表")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")})

    public AjaxResult powersaturationamonutchart(String wtId) {

        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) )
        {

            List<Windturbinepowercurvefitting> wcls=windturbinepowercurvefittingService.getWindturbinepowercurvefittingByWtId(wtId);

            if (!wcls.isEmpty()) {
//				Collections.sort(wcls, new Comparator<Windturbinepowercurvefitting>() {
//					public int compare(Windturbinepowercurvefitting arg0, Windturbinepowercurvefitting arg1) {
//						int result = 0;
//						if (arg0.getSpeed() > arg1.getSpeed())
//							result = 1;
//						else
//							result = 0;
//						return result;
//					}
//				});
                if (wtmap.containsKey(wtId)) {
                    Windturbine wt = wtmap.get(wtId);
                    if (modelPowermap.containsKey(wt.getModelid())) {
                        map = modelPowermap.get(wt.getModelid());
                    }
                }

                double temp = 0.0;
                for (Windturbinepowercurvefitting 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.success.code, vos);
        }
    }
}
