package com.zksy.visualization.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zksy.common.core.domain.Result;
import com.zksy.visualization.config.IccConfigProperty;
import com.zksy.visualization.domain.request.ChannelSubtypeRequest;
import com.zksy.visualization.domain.request.RealTimeDataRequest;
import com.zksy.visualization.domain.response.ChannelSubtypeResponse;
import com.zksy.visualization.domain.response.RealTimeDataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 * @version 1.0
 * @project dh-server-micro
 * @description 动环管理
 * @date 2024/12/19 16:23:14
 */
@RestController
@Api(tags = "动环管理", description = "动环管理")
@RequestMapping("/rotatingRing")
@Slf4j
public class RotatingRingController {
    @Autowired
    private OkHttpClient httpClient;
    @Autowired
    private IccConfigProperty iccConfigProperty;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @ApiOperation(value = "通道子类型分页查询")
    @GetMapping("/channelSubtype")
    public Result ChannelSubtype(ChannelSubtypeRequest channelSubtypeRequest){
        String authorization = redisTemplate
                .opsForValue()
                .get("Authorization:" + iccConfigProperty.getUsername());
        HttpUrl url = new HttpUrl.Builder()
                .scheme("https")
                .host(iccConfigProperty.getHost())
                .addPathSegments("/evo-apigw/evo-pmms/dictionary/devChildTypeList")
                .addQueryParameter("pageNo", String.valueOf(channelSubtypeRequest.getPageNo()))
                .addQueryParameter("pageSize",String.valueOf(channelSubtypeRequest.getPageSize()))
                .addQueryParameter("searchKey",String.valueOf(channelSubtypeRequest.getSearchKey()))
                .build();

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", authorization)
                .build();
        try {
            Response response = httpClient.newCall(request).execute();
            if(!response.isSuccessful()){
                return Result.error("通道子类型分页查询失败");
            }
            ResponseBody responseBody = response.body();
            if (responseBody != null) {
                String responseString = responseBody.string();
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode rootNode = objectMapper.readTree(responseString);
                JsonNode success = rootNode.path("success");
                if(success.isBoolean() && success.asBoolean()) {
                    JsonNode dataNode = rootNode.path("data");
                    if (dataNode.isObject()) {
                        ChannelSubtypeResponse channelSubtypeResponse = objectMapper.treeToValue(dataNode, ChannelSubtypeResponse.class);
                        return Result.ok(channelSubtypeResponse);
                    } else {
                        return Result.ok(null);
                    }
                }else{
                    JsonNode errMsgNode = rootNode.path("errMsg");
                    String errMsg = errMsgNode.isTextual() ? errMsgNode.asText() : "未知错误";
                    return Result.error(errMsg);
                }
            }
        } catch (Exception e) {
            log.error("通道子类型分页查询失败:{}", e);
        }
        return Result.error("通道子类型分页查询失败");
    }

    @ApiOperation(value = "分页查询实时数据信息")
    @GetMapping("/realTimeData")
    public Result RealTimeData(RealTimeDataRequest realTimeDataRequest){
        String authorization = redisTemplate
                .opsForValue()
                .get("Authorization:" + iccConfigProperty.getUsername());
        HttpUrl url = new HttpUrl.Builder()
                .scheme("https")
                .host(iccConfigProperty.getHost())
                .addPathSegments("/evo-apigw/evo-pmms/real/data/nodeId="+realTimeDataRequest.getNodeId()+"" +
                        "&devChildType="+realTimeDataRequest.getDevChildType()+"&pageNo="+realTimeDataRequest.getPageNo()+"" +
                        "&pageSize="+realTimeDataRequest.getPageSize())
                .build();

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", authorization)
                .build();
        try {
            Response response = httpClient.newCall(request).execute();
            if(!response.isSuccessful()){
                return Result.error("分页查询实时数据信息失败");
            }
            ResponseBody responseBody = response.body();
            if (responseBody != null) {
                String responseString = responseBody.string();
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode rootNode = objectMapper.readTree(responseString);
                    JsonNode dataNode = rootNode.path("data");
                    if (dataNode.isObject()) {
                        RealTimeDataResponse realTimeDataResponse = objectMapper.treeToValue(dataNode, RealTimeDataResponse.class);
                        return Result.ok(realTimeDataResponse);
                    } else {
                        return Result.ok(null);
                    }
            }
        } catch (Exception e) {
            log.error("分页查询实时数据信息失败:{}", e);
        }
        return Result.error("分页查询实时数据信息失败");
    }

}
