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.Windturbinecurvefittingmonth;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.service.WindturbinecurvefittingmonthService;
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 PowerSaturationMonthController {

    @Resource
    private WindturbinecurvefittingmonthService windturbinecurvefittingmonthService;

    /**月功率饱和列表**/
    @PostMapping("/powersaturationmonthlist")
    @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 = "year", value = "年份", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "string", paramType = "query")})

    public AjaxResult powersaturationmonthlist(Tablepar tablepar, String wtId, String year, String month) {

        PageInfo<Windturbinecurvefittingmonth> vos=new PageInfo<>();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(year)  && StringUtils.notEmp(month))
        {

            vos=windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(tablepar, wtId, year,month);
        }

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

    }
    /**月功率饱和图表**/
    @PostMapping("/powersaturationmonthchart")
    @ResponseBody
    @ApiOperation(value = "月功率饱和图表", notes = "月功率饱和图表")
    @ApiImplicitParams({
             @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "string", paramType = "query")})

    public AjaxResult powersaturationmonthchart(String wtId, String year, String month) {

        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(year)  && StringUtils.notEmp(month))
        {
            List<Windturbinecurvefittingmonth> wcls=windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wtId, year,month);
            if (!wcls.isEmpty()) {
//				Collections.sort(wcls, new Comparator<Windturbinecurvefittingmonth>() {
//					@Override
//					public int compare(Windturbinecurvefittingmonth arg0, Windturbinecurvefittingmonth 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 (Windturbinecurvefittingmonth 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);
        }
    }
}
