package com.zjsgfa.framework.task;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.zjsgfa.project.zjsgfa.domain.Mxfxjgb;
import com.zjsgfa.project.zjsgfa.mapper.MxfxjgbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("mxdyTask")
public class MxdyTask {

    @Autowired
    private MxfxjgbMapper mxfxjgbMapper;


    public void ryParams(Long mxid)
    {

        Mxfxjgb mxfxjgb = mxfxjgbMapper.selectMxfxjgbById(mxid);
        System.out.println(mxfxjgb);
        Map<String, Object> map = new HashMap<>();
        map.put("stream", false);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("field_name","data");
        map2.put("type","input");
        map2.put("value",mxfxjgb.getRwnr());
        List<Map> listMap =new ArrayList<>();
        listMap.add(map2);
        map.put("content",listMap);
        String url = "https://agent.ai.sinopec.com/aicoapi/gateway/v2/workflow/api_run/53f4afd436d2417180cb27d6dc0a3e17";
        String key="278GANwseRJ98YbADs3G1tj7Fg40sI7d";
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(map);
        String result2 = HttpRequest.post(url).header("Authorization","Bearer "+key).body(json).execute().body();
        System.out.println(result2);
//        // 解析API响应
        String textPresentation ="";
        // 解析为顶层JsonObject
        JsonObject root = gson.fromJson(result2, JsonObject.class);

        // 逐层获取"文本呈现"内容
        // 1. 获取外层data节点
        JsonObject outerData = root.getAsJsonObject("data");
        // 2. 获取内层data节点
        JsonObject innerData = outerData.getAsJsonObject("data");
        // 3. 获取"文本呈现"字段的值
        textPresentation = innerData.get("文本呈现").getAsString();

        // 打印结果
        System.out.println("文本呈现内容：\n" + textPresentation);

        mxfxjgb.setFhjg(textPresentation);
        mxfxjgb.setJxzt("解析完成");
        mxfxjgbMapper.updateMxfxjgb(mxfxjgb);
    }
}
