package com.gyee.frame.controller.warn;

import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.custom.PointVo;
import com.gyee.frame.service.FaultPlaybackService;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/fault")
@Api(value = "停机故障回溯", tags = "停机故障回溯")
public class FaultPlaybackController {

    private static final String format = "yyyy-MM-dd HH:mm";
    @Resource
    private FaultPlaybackService faultPlaybackService;


//    <option value="0">风机</option>
//				<option value="1">齿轮箱</option>
//				<option value="2">发电机</option>
//				<option value="3">变桨</option>
//
//    				<option value="0">所有测点</option>
//				<option value="1">遥测测点</option>
//				<option value="2">遥信测点</option>
    @PostMapping("/queryPointVos")
    @ResponseBody
    @ApiOperation(value = "停机故障回溯查询", notes = "停机故障回溯查询")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "bjtype", value = "部件类型", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "cdtype", value = "测点类型", required = true, dataType = "string", paramType = "query")})

    public AjaxResult queryPointVos(String wtId, String bjtype, String cdtype) throws Exception {

        List<PointVo> result=new ArrayList<>();
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(bjtype) && StringUtils.notEmp(cdtype)) {

            result=faultPlaybackService.queryPointVos( wtId, Integer.valueOf(bjtype), Integer.valueOf(cdtype));

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

    }
    @PostMapping("/getFaultPlayback")
    @ResponseBody
    @ApiOperation(value = "停机故障回溯查询", notes = "停机故障回溯查询")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pointids", value = "测点", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "stopTime", value = "测点类型", required = true, dataType = "string", paramType = "query")})

    public AjaxResult getFaultPlayback(String wtId, String pointids, String stopTime) throws Exception {

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

        if(StringUtils.notEmp(wtId) && StringUtils.notEmp(pointids) && StringUtils.notEmp(stopTime))
        {
            SimpleDateFormat df =new SimpleDateFormat(format);
            String[] ids=pointids.split(",");

            map=faultPlaybackService.getDataValue(wtId, ids, df.parse(stopTime));
        }

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

    }

}
