package com.gyee.frame.controller.health;

import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.custom.*;
import com.gyee.frame.service.health.HealthSubService;
import com.gyee.frame.service.weather.WeatherDay5Service;
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("/healthsub")
@Api(value = "健康管理风机页面" ,tags =  "健康管理风机页面")
public class HealthSubController {

    @Resource
    private HealthSubService healthSubService;

    @Resource
    private WeatherDay5Service weatherDay5Service;


    @PostMapping("/findWtHealthInfo")
    @ResponseBody
    @ApiOperation(value = "获取健康管理风机信息", notes = "获取健康管理风机信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult findWtHealthInfo(String wtId) throws Exception {


        Map<String, Object>  map = new HashMap<>();

        if(StringUtils.notEmp(wtId))
        {


            Calendar cal = Calendar.getInstance();

            Date recodedate = DateUtils.truncate(cal.getTime());
            //获取故障类型和风机健康得分
            Map<String,String>  stoptypemap = healthSubService.findWtInfo(wtId);
            //获取健康管理风机部件基础信息
            Map<String, Map<String, Object>>  partmap = healthSubService.findPartHealthInfo(wtId);
            //故障数据统计
            List<GzfxVo>  gzls = healthSubService.initalGzjfx(wtId,recodedate);
            //预警数据统计
            List<YjfxVo>  yjls = healthSubService.initalYjfx(wtId,recodedate);
            //部件运行数据
            List<YxfxVo>  bjyxls = healthSubService.initalYxfx(wtId,recodedate);
            //获得日、月等级评估
            List<XnfxVo>  djpgls = healthSubService.initalXnfx(wtId,cal);
            //当日停机
            List<ShutdowneventVo>  stopls = healthSubService.initalShutdown(wtId);
            //各个区间曲线偏差率
            List<QxpcVo>  qxpcls = healthSubService.findQxpcList(wtId);
            //温度范围
            List<YxfxVo>  wdls = healthSubService.findWdList(wtId);
            //获得健康评价列表
            List<ValueVo>  jkls = healthSubService.findJudgmentList(wtId);


            map.put("stoptypemap",stoptypemap);
            map.put("partmap",partmap);
            map.put("gzls",gzls);
            map.put("yjls",yjls);
            map.put("bjyxls",bjyxls);
            map.put("djpgls",djpgls);
            map.put("stopls",stopls);
            map.put("qxpcls",qxpcls);
            map.put("wdls",wdls);
            map.put("jkls",jkls);



        }

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

    }

    @PostMapping("/findWtHealthInfo1")
    @ResponseBody
    @ApiOperation(value = "获取健康管理风机信息", notes = "获取健康管理风机信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult findWtHealthInfo1(String wtId) throws Exception {


        Map<String, Object>  map = new HashMap<>();

        if(StringUtils.notEmp(wtId))
        {


            Calendar cal = Calendar.getInstance();

            Date recodedate = DateUtils.truncate(cal.getTime());
//            //获取故障类型和风机健康得分
//            Map<String,String>  stoptypemap = healthSubService.findWtInfo(wtId);
//            //获取健康管理风机部件基础信息
//            Map<String, Map<String, Object>>  partmap = healthSubService.findPartHealthInfo(wtId);
//            //故障数据统计
//            List<GzfxVo>  gzls = healthSubService.initalGzjfx(wtId,recodedate);
//            //预警数据统计
//            List<YjfxVo>  yjls = healthSubService.initalYjfx(wtId,recodedate);
//            //部件运行数据
//            List<YxfxVo>  bjyxls = healthSubService.initalYxfx(wtId,recodedate);
//            //获得日、月等级评估
//            List<XnfxVo>  djpgls = healthSubService.initalXnfx(wtId,cal);
//            //当日停机
//            List<ShutdowneventVo>  stopls = healthSubService.initalShutdown(wtId);
//            //各个区间曲线偏差率
//            List<QxpcVo>  qxpcls = healthSubService.findQxpcList(wtId);
//            //温度范围
//            List<YxfxVo>  wdls = healthSubService.findWdList(wtId);
//            //获得健康评价列表
//            List<ValueVo>  jkls = healthSubService.findJudgmentList(wtId);
//
//
//            map.put("stoptypemap",stoptypemap);
//            map.put("partmap",partmap);
//            map.put("gzls",gzls);
//            map.put("yjls",yjls);
//            map.put("bjyxls",bjyxls);
//            map.put("djpgls",djpgls);
//            map.put("stopls",stopls);
//            map.put("qxpcls",qxpcls);
//            map.put("wdls",wdls);
//            map.put("jkls",jkls);

            //获取故障类型和风机健康得分
            Map<String,String>  stoptypemap = healthSubService.findWtInfo(wtId);
            //获取健康管理风机部件基础信息
            Map<String, Map<String, Object>>  partmap = healthSubService.findPartHealthInfo(wtId);
            //故障数据统计
            List<GzfxVo>  gzls = healthSubService.initalGzjfx(wtId,recodedate);
            //预警数据统计
            List<YjfxVo>  yjls = healthSubService.initalYjfx(wtId,recodedate);


            map.put("stoptypemap",stoptypemap);
            map.put("partmap",partmap);
            map.put("gzls",gzls);
            map.put("yjls",yjls);

        }

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

    }

/********************************************************************************************************/


@PostMapping("/findWtHealthInfo2")
@ResponseBody
@ApiOperation(value = "获取健康管理风机信息", notes = "获取健康管理风机信息")
@ApiImplicitParams({
        @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
})
public AjaxResult findWtHealthInfo2(String wtId) throws Exception {


    Map<String, Object>  map = new HashMap<>();

    if(StringUtils.notEmp(wtId))
    {


        Calendar cal = Calendar.getInstance();

        Date recodedate = DateUtils.truncate(cal.getTime());

        //部件运行数据
        List<YxfxVo>  bjyxls = healthSubService.initalYxfx(wtId,recodedate);
        //获得日、月等级评估
        List<XnfxVo>  djpgls = healthSubService.initalXnfx(wtId,cal);
        //当日停机
        List<ShutdowneventVo>  stopls = healthSubService.initalShutdown(wtId);


        map.put("bjyxls",bjyxls);
        map.put("djpgls",djpgls);
        map.put("stopls",stopls);

    }

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

}

    /********************************************************************************************************/

    @PostMapping("/findWtHealthInfo3")
    @ResponseBody
    @ApiOperation(value = "获取健康管理风机信息", notes = "获取健康管理风机信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult findWtHealthInfo3(String wtId) throws Exception {


        Map<String, Object>  map = new HashMap<>();

        if(StringUtils.notEmp(wtId))
        {


            Calendar cal = Calendar.getInstance();

            Date recodedate = DateUtils.truncate(cal.getTime());

            //各个区间曲线偏差率
            List<QxpcVo>  qxpcls = healthSubService.findQxpcList(wtId);
            //温度范围
            List<YxfxVo>  wdls = healthSubService.findWdList(wtId);
            //获得健康评价列表
            List<ValueVo>  jkls = healthSubService.findJudgmentList(wtId);


            map.put("qxpcls",qxpcls);
            map.put("wdls",wdls);
            map.put("jkls",jkls);
        }

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

    }

    /********************************************************************************************************/

    @PostMapping("/gadaytop5")
    @ResponseBody
    @ApiOperation(value = "获得等级评估前5数据", notes = "获得等级评估前5数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult gadaytop5(String wtId) throws Exception {


        Map<String, Object> map = new HashMap<>();

        if(StringUtils.notEmp(wtId))
        {

            map = healthSubService.gadaytop5(wtId);
        }

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

    }


    @PostMapping("/queryStopTop10")
    @ResponseBody
    @ApiOperation(value = "获取最新的十次停机事件", notes = "获取最新的十次停机事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult queryStopTop10(String wtId) throws Exception {


        List<ShutdowneventVo> vos = new ArrayList<>();

        if(StringUtils.notEmp(wtId))
        {

            vos = healthSubService.queryStopTop10(wtId);
        }

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

    }


    @PostMapping("/getWtMttrandMtbfByBj")
    @ResponseBody
    @ApiOperation(value = "获取风机部件MTTR和MTBF  数组 0健康状态、1部件名称 2 7天健康状态 3 故障进度 4 mtbf 5 mttr 6损失电量", notes = "获取风机部件MTTR和MTBF 数组 0健康状态、1部件名称 2 7天健康状态 3 故障进度 4 mtbf 5 mttr 6损失电量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult getWtMttrandMtbfByBj(String wtId) throws Exception {


        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(wtId))
        {

            map = healthSubService.getWtMttrandMtbfByBj(wtId);
        }

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

    }

    @PostMapping("/getWeatherRealDay5Info")
    @ResponseBody
    @ApiOperation(value = "获取近5天气象数据", notes = "获取近5天气象数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult getWeatherRealDay5Info(String wpId) throws Exception {


        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(wpId))
        {

            map = weatherDay5Service.getWeatherRealDay5Info(wpId);
        }

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

    }



    @PostMapping("/findPowerChar")
    @ResponseBody
    @ApiOperation(value = "获得实际功率和理论功率曲线", notes = "获得实际功率和理论功率曲线")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult findPowerChar(String wtId) throws Exception {


        Map<String, Object[]> map = new HashMap<>();

        if(StringUtils.notEmp(wtId))
        {

            map = healthSubService.findPowerChar(wtId);
        }

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

    }
/**************************************************************************************************************************************************/

    @PostMapping("/hsFjValueIndex")
    @ResponseBody
    @ApiOperation(value = "获取部件图表", notes = "获取部件图表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult hsFjValueIndex(String wtId) throws Exception {


        ActivityVo vo = new ActivityVo();

        if(StringUtils.notEmp(wtId))
        {
            Calendar cal = Calendar.getInstance();

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


            Date endDate = cal.getTime();

            cal.add(Calendar.DAY_OF_MONTH, -1);
            Date beginDate = cal.getTime();
            vo = healthSubService.hsFjValueIndex(wtId,beginDate,endDate);
        }

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

    }


    @PostMapping("/findWtHisValueForBj")
    @ResponseBody
    @ApiOperation(value = "获取风机部件健康曲线图表", notes = "获取风机部件健康曲线图表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult findWtHisValueForBj(String wtId) throws Exception {


        Map<String,Object> map = new HashMap();


        if(StringUtils.notEmp(wtId))
        {

            map = healthSubService.findWtHisValueForBj(wtId);
        }

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

    }


    @PostMapping("/hsFjValueIndexByPart")
    @ResponseBody
    @ApiOperation(value = "获取风机健康部件曲线图", notes = "获取风机健康部件曲线图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "部件类型", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult hsFjValueIndexByPart(String wtId,String type) throws Exception {

        Map<String, Object> result = new HashMap<String, Object>();
        ActivityVo vo=new ActivityVo();

        if(StringUtils.notEmp(wtId) && StringUtils.notEmp(type))
        {
            if (type.equals("1")) {
                vo = healthSubService.getWTChart(wtId);
            } else if (type.equals("2")) {
                vo = healthSubService.getClxChart(wtId);
            } else if (type.equals("3")) {
                vo = healthSubService.getFdjChart(wtId);
            } else if (type.equals("4")) {
                vo = healthSubService.getKzysChart(wtId);
            } else if (type.equals("5")) {
                vo = healthSubService.getJcxtChart(wtId);
            }

            result.put(wtId, healthSubService.getrealValue(wtId, type));
        }


        result.put("activity", vo);

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

    }

}
