package com.yeejoin.amos.kgd.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yeejoin.amos.kgd.service.AnalysisResultService;
import com.yeejoin.amos.kgd.service.impl.AnalysisResultServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;


@RestController
@Api(tags = "数据Api")
@RequestMapping(value = "/data/select")
public class AnalysisResultController {

    @Autowired
    AnalysisResultServiceImpl analysisResultService;

    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @ApiOperation(value = "根据设备ID查询数据，幅值与时间")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel selectByDeviceId(@RequestParam String deviceId){
        JSONObject jsonData = analysisResultService.getDataByDeviceId(deviceId);
        return ResponseHelper.buildResponse(jsonData);
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @ApiOperation(value = "根据设备ID查询，幅值与频率")
    @RequestMapping(value = "/analysisAllData", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel selectAnalysisDataByDeviceId(@RequestParam String deviceId){
        JSONObject jsonData = analysisResultService.getAnalysisDataByDeviceId(deviceId);
        return ResponseHelper.buildResponse(jsonData);
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @ApiOperation(value = "根据设备id、高频，查var_name的所有类别，下拉栏的")
    @RequestMapping(value = "/varName", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel selectVarNameByDeviceId(@RequestParam String deviceId){
        JSONArray jsonArray = analysisResultService.getVarNameByDeviceId(deviceId);
        return ResponseHelper.buildResponse(jsonArray);
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @ApiOperation(value = "根据设备ID和低频，查温度，data")
    @RequestMapping(value = "/temperature", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel selectTemperatureByDeviceId(String deviceId){
        JSONObject jsonData = analysisResultService.getTemperatureByDeviceId(deviceId);
        return ResponseHelper.buildResponse(jsonData);
    }



    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @ApiOperation(value = "根据设备ID和varName查幅值与时间，data")
    @RequestMapping(value = "/analysisData", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel selectAnalysisDataByDeviceIdAndStatus(@RequestParam String deviceId, @RequestParam String varName){
        JSONObject jsonData = analysisResultService.getAnalysisDataByDeviceIdAndVarName(deviceId, varName);
        return ResponseHelper.buildResponse(jsonData);
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @ApiOperation(value = "根据设备ID和状态查故障率")
    @RequestMapping(value = "/faultRate", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel selectFaultRateByDeviceIdAndStatus(@RequestParam String deviceId, @RequestParam String varName){
        JSONObject jsonData = analysisResultService.getFaultRateByDeviceIdAndStatus(deviceId, varName);
        return ResponseHelper.buildResponse(jsonData);
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @ApiOperation(value = "根据设备ID和状态查故障类型占比")
    @RequestMapping(value = "/faultTypeProportion", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel selectFaultTypeProportionByDeviceIdAndStatus(@RequestParam String deviceId, @RequestParam String varName){
        JSONArray jsonArray = analysisResultService.getFaultTypeProportionByDeviceIdAndStatus(deviceId, varName);
        return ResponseHelper.buildResponse(jsonArray);
    }


    @Value("${kgd.data.switchNumber}")
    private int switchNumber;
    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @ApiOperation(value = "根据设备ID查询开关量信息")
    @RequestMapping(value = "/switchData", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel selectSwitchDataByDeviceId(@RequestParam String deviceId){
        System.out.println(switchNumber);
        JSONObject jsonData = analysisResultService.getSwitchDataByDeviceId(deviceId, switchNumber);
        return ResponseHelper.buildResponse(jsonData);
    }


    @Value("${kgd.data.predictiveNumber}")
    private int predictiveNumber;

    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @ApiOperation(value = "根据设备ID和varName查询最近n条预测结果")
    @RequestMapping(value = "/predictiveTrend", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel selectPredictionResultByDeviceId(@RequestParam String deviceId,
                                                   @RequestParam String vaName){
        JSONObject jsonData = analysisResultService.getPredictionResultByDeviceId(deviceId, vaName, predictiveNumber);
        return ResponseHelper.buildResponse(jsonData);
    }

}
