package com.huatek.busi.controller;

import java.time.LocalDate;
import java.util.Map;

import com.huatek.busi.dto.curve.CircleCurveSensorRequestDto;
import com.huatek.busi.service.BusiGeneratorAmplService;
import com.huatek.busi.vo.echarts.CircleEchartLineDataVo;
import com.huatek.torch.frame.exception.OwlException;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.constant.ApiConstant.Tags;
import com.huatek.busi.dto.curve.CurveSensorRequestDto;
import com.huatek.busi.service.curve.CurveSensorEntityService;
import com.huatek.busi.vo.echarts.EchartLineDataVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 二级告警页面曲线图接口API
 * 
 * @author anker_cheng
 * @date 2019/09/02
 */
@RestController
@Api(tags = {Tags.BUSI_SUB_PAGE_CURVE})
@RequestMapping("/app/busiSubPageCurve")
public class BusiSubPageCurveController {
    @Reference(check = false)
    private CurveSensorEntityService curveSensorEntityService;
    @Reference(check = false)
    private BusiGeneratorAmplService busiGeneratorAmplService;


    @ApiOperation(value = "历史数据曲线图查询", response = EchartLineDataVo.class)
    @PostMapping(value = "/history")
    public Map<String, Object> history(@RequestBody CurveSensorRequestDto param) {
    	if(param.getStartDate() == null) {
    		param.setStartDate(LocalDate.now().plusDays(-1));
    	}
    	if(param.getEndDate() == null) {
    		param.setEndDate(LocalDate.now().plusDays(-1));
    	}
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", curveSensorEntityService.getHistoryData(param.check()));
    }

    @ApiOperation(value = "趋势图数据查询", response = EchartLineDataVo.class)
    @PostMapping(value = "/trend")
    public Map<String, Object> trend(@RequestBody CurveSensorRequestDto param) {
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", curveSensorEntityService.getTrendData(param.check()));
    }

    @ApiOperation(value = "趋势图数据查询", response = EchartLineDataVo.class)
    @PostMapping(value = "/trendForCircle")
    public Map<String, Object> trend(@RequestBody CircleCurveSensorRequestDto param) {
        // 参数校验
        if (ObjectUtils.isEmpty(param.getStartTime())) {
            throw new OwlException("请选择开始时间");
        }
        if (ObjectUtils.isEmpty(param.getEndTime())) {
            throw new OwlException("请选择结束时间");
        }
        // todo 约束时间范围
        if (ObjectUtils.isEmpty(param.getSensorCode())) {
            throw new OwlException("请选择传感器");
        }
        if (ObjectUtils.isEmpty(param.getMainTreeId())) {
            throw new OwlException("请选择发电机");
        }
        CircleEchartLineDataVo vo = busiGeneratorAmplService.getTrendForCircleData(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
    }

    @ApiOperation(value = "变压器运行状态图查询", response = EchartLineDataVo.class)
    @PostMapping(value = "/status")
    public Map<String, Object> status(@RequestBody CurveSensorRequestDto param) {
    	if(param.getStartDate() == null) {
    		param.setStartDate(LocalDate.now().plusDays(-1));
    	}
    	if(param.getEndDate() == null) {
    		param.setEndDate(LocalDate.now().plusDays(-1));
    	}
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", curveSensorEntityService.getTransformerRunStateData(param.check()));
    }
}