package com.sifang.modules.htySap.utils;

import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Db;
import com.sifang.modules.sys.common.global.Global;
import com.sifang.modules.sys.common.utils.IdGen;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

public class SapOrderUtils {
    private static Logger logger = LoggerFactory.getLogger(SapOrderUtils.class);


    /**
     * 派单给师傅
    */
    public static Map<String,String> clientPostSap(String content,String url,String orderNumber) throws IOException {
        String sapUrl = Global.getConfig("hty.sap.url");
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, content);
        Request request = new Request.Builder()
                .url("https://www.nhhyy.com/monitor-api"+url)
                .method("POST", body)
                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();
        ResponseBody body1 = response.body();

        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(body1.byteStream(), "utf-8"))) {
            StringBuilder results = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                results.append(line);
            }
            logger.info("results : {}",results);
            Db.update("insert into crm_order_sap_log VALUES(?,?,?,?,?,now()) ", IdGen.uuid(),orderNumber,url,content,results.toString());
            Map<String,String> mapData = new Gson().fromJson(results.toString(), Map.class);
            return mapData;
        }


    }


}
