package com.rongwei.sfcommon.sys.service.impl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.rongwe.scentity.domian.AspAccidentReport;
import com.rongwe.scentity.domian.AspDhzyDo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.sfcommon.sys.dao.AspAccidentReportDao;
import com.rongwei.sfcommon.sys.dao.DhzyDataUploadDao;
import com.rongwei.sfcommon.sys.service.AspAccidentReportService;
import com.rongwei.sfcommon.sys.service.DhzyDataUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Service
public class DhzyDataUploadServiceImpl extends ServiceImpl<DhzyDataUploadDao, AspDhzyDo> implements DhzyDataUploadService {
    private static final Logger logger = LoggerFactory.getLogger(DhzyDataUploadServiceImpl.class);


    @Override
    public R getDataUpload() {
        // 获取本机的IP地址
        String ipAddress = getLocalIpAddress();
        String apiUrl = "http://" + ipAddress + "/api/TSZY/t_dhzy/add";

        String jsonString = "{\"key\":\"value\"}"; // 这里替换为你的JSON字符串

        String apiReturnStr = HttpRequest.post(apiUrl)
                .header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                .header("jsonContent-Type", MediaType.APPLICATION_JSON_VALUE)
                .header("client", "1")
                .body(jsonString) // 将JSON字符串作为请求体发送
                .execute()
                .body();

        logger.info("返回报文：{}", apiReturnStr);
        return R.ok("200", "DATA", apiReturnStr);
    }


    // 获取本机IP地址的方法
    public static String getLocalIpAddress() {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            return inetAddress.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        }
    }
}
