package com.shining.cloud.controller.RealTimeInfo;

import com.njcn.enums.app.ReturnCode;
import com.njcn.pojo.commons.ResponseData;
import com.njcn.pojo.commons.ResponseInfoData;
import com.njcn.pojo.commons.ResponseMsgData;
import com.njcn.utils.PubUtils;
import com.njcn.utils.SocketClient;
import com.shining.cloud.enums.information.RealTimeInfoEnum;
import com.shining.cloud.mapper.RealTimeInfo.RealTimeInfoMapper;
import com.shining.cloud.pojo.RealTimeInfo.*;
import com.shining.cloud.service.RealTimeInfo.RealTimeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 实时数据列表
 * @author: denghuajun
 * @time: 2019-12-16 10:30:11
 **/
@RestController
@RequestMapping("/realtimeinfo")
@Api(tags = "实时数据接口")
public class RealTimeInfoController {

    Logger logger = LoggerFactory.getLogger(RealTimeInfoController.class);

    @Autowired
    RealTimeInfoService realTimeInfoService;

    @Resource
    private RealTimeInfoMapper realTimeInfoMapper;

    @PostMapping("getLineBaseInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineIndex", value = "监测点ID", required = true, paramType = "query"),
    })
    @ApiOperation(value = "监测点信息接口", notes = "监测点信息", response = ResponseMsgData.class)
    public ResponseData getLineBaseInfo(int lineIndex) {
        ResponseData responseData;

        try {
            LineBaseInfo lineBaseInfo = new LineBaseInfo();
            lineBaseInfo = realTimeInfoService.getLineBaseInfo(lineIndex);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, RealTimeInfoEnum.GETLINEBASEINFO_SUCCESS.getCode(), RealTimeInfoEnum.GETLINEBASEINFO_SUCCESS.getMsg(), lineBaseInfo);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, RealTimeInfoEnum.GETLINEBASEINFO_FALL.getCode(), RealTimeInfoEnum.GETLINEBASEINFO_FALL.getMsg(), null);
        }
        return responseData;
    }

    @PostMapping("getRealData")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineIndex", value = "监测点ID", required = true, paramType = "query"),
    })
    @ApiOperation(value = "实时数据接口", notes = "实时数据", response = ResponseMsgData.class)
    public ResponseData getRealData(int lineIndex) {
        ResponseData responseData;
        try {
            RealTimeInfo realTimeInfo = realTimeInfoService.getRealTimeInfo(lineIndex);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, RealTimeInfoEnum.GETREALTIMEINFO_SUCCESS.getCode(), RealTimeInfoEnum.GETREALTIMEINFO_SUCCESS.getMsg(), realTimeInfo);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, RealTimeInfoEnum.GETREALTIMEINFO_FALL.getCode(), RealTimeInfoEnum.GETREALTIMEINFO_FALL.getMsg(), null);
        }
        logger.info("调用getRealData**********************************");
        logger.info(responseData.toString());
        return responseData;
    }

    @PostMapping("getHarmRateI")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineIndex", value = "监测点ID", required = true, paramType = "query"),
    })
    @ApiOperation(value = "谐波电流幅值接口", notes = "谐波电流幅值", response = ResponseMsgData.class)
    public ResponseData getHarmRateI(int lineIndex) {
        ResponseData responseData;

        try {

            HarmRateIAndV harmRateIAndV = realTimeInfoService.RealRedisData(lineIndex,2);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, RealTimeInfoEnum.GETHARMRATEIINFO_SUCCESS.getCode(), RealTimeInfoEnum.GETHARMRATEIINFO_SUCCESS.getMsg(), harmRateIAndV);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, RealTimeInfoEnum.GETHARMRATEIINFO_FALL.getCode(), RealTimeInfoEnum.GETHARMRATEIINFO_FALL.getMsg(), null);
        }
        logger.info("调用getHarmRateI**************");
        logger.info(responseData.toString());
        return responseData;
    }

    @PostMapping("getHarmRateV")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineIndex", value = "监测点ID", required = true, paramType = "query"),
    })
    @ApiOperation(value = "谐波电压含有率接口", notes = "谐波电压含有率", response = ResponseMsgData.class)
    public ResponseData getHarmRateV(int lineIndex) {
        ResponseData responseData;

        try {
            HarmRateIAndV harmRateIAndV = realTimeInfoService.RealRedisData(lineIndex,1);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, RealTimeInfoEnum.GETHARMRATEVINFO_SUCCESS.getCode(), RealTimeInfoEnum.GETHARMRATEVINFO_SUCCESS.getMsg(), harmRateIAndV);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, RealTimeInfoEnum.GETHARMRATEVINFO_FALL.getCode(), RealTimeInfoEnum.GETHARMRATEVINFO_FALL.getMsg(), null);
        }
        logger.info("调用getHarmRateV**************");
        logger.info(responseData.toString());
        return responseData;
    }




    @PostMapping("getLineId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIndex", value = "用户ID", required = true, paramType = "query"),
    })
    @ApiOperation(value = "默认监测点接口", notes = "默认监测点", response = ResponseMsgData.class)
    public ResponseData getLineId(String userIndex) {
        ResponseData responseData;

        try {
            LineInfo lineInfo = new LineInfo();
            lineInfo = realTimeInfoService.getLineId(userIndex);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, RealTimeInfoEnum.GETLINEID_SUCCESS.getCode(), RealTimeInfoEnum.GETLINEID_SUCCESS.getMsg(), lineInfo);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, RealTimeInfoEnum.GETLINEID_FALL.getCode(), RealTimeInfoEnum.GETLINEID_FALL.getMsg(), null);
        }
        return responseData;
    }

}