package com.inspect.base.core.service;

import com.alibaba.fastjson.JSONObject;
import com.inspect.base.core.utils.HttpClientUtils;
import com.inspect.base.core.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RefreshScope
@ConditionalOnProperty(
        name = {"tcp.client.enable"},
        havingValue = "true"
)
public class SyncDataToUpstreamService {
    @Value("${tcp.client.send}")
    String sendUrl;

    public AjaxResult postMessage(String msg) {
        return this.postMessage(null, msg);
    }

    public AjaxResult postMessage(String type, String msg) {
        log.debug("sendUrl: " + sendUrl);
        try {
            String resultJson = HttpClientUtils.postJson(sendUrl, msg, null);
            AjaxResult ajaxResult = JSONObject.parseObject(resultJson, AjaxResult.class);
            return ajaxResult;
        } catch (Exception e) {
            log.error("上报消息到上级系统捕获异常", e);
            return AjaxResult.fail(500, "上报消息失败");
        }
    }
}
