package com.zjsgfa.framework.task;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import com.zjsgfa.common.utils.StringUtils;
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.io.StringReader;
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);
        try {

            System.out.println(mxfxjgb);
            Map<String, Object> map = new HashMap<>();
            map.put("stream", false);
            Map<String, Object> map2 = new HashMap<>();
            map2.put("field_name","text");
            map2.put("type","input");
            if(StringUtils.isNotEmpty(mxfxjgb.getTsc())){
                map2.put("value",mxfxjgb.getTsc()+"  \n 分析内容为："+mxfxjgb.getRwnr());
            }else {
                map2.put("value",mxfxjgb.getRwnr());
            }
            List<Map> listMap =new ArrayList<>();
            listMap.add(map2);
            map.put("content",listMap);
            map.put("value",mxfxjgb.getRwnr());
            String url = "https://agent.ai.sinopec.com/aicoapi/gateway/v2/workflow/api_run/506d22a5e4e8479b879a836368002ac8";
            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);
        } catch (Exception e) {
            mxfxjgb.setJxzt("解析失败");
            mxfxjgbMapper.updateMxfxjgb(mxfxjgb);
        }
    }
    public void ryParams2(Long mxid)
    {
        Mxfxjgb mxfxjgb = mxfxjgbMapper.selectMxfxjgbById(mxid);
        try {

            System.out.println(mxfxjgb);
            Map<String, Object> map = new HashMap<>();
            map.put("prompt", mxfxjgb.getRwnr());
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(map);
            String url = "http://10.68.249.59:33331/api/chat/1";
//            String key="278GANwseRJ98YbADs3G1tj7Fg40sI7d";
//            Gson gson = new GsonBuilder().setPrettyPrinting().create();
//            String json = gson.toJson(map);
//            String result2 = HttpRequest.post(url).body(json).execute().body();
            String result2 = HttpRequest.post(url).body(json).execute().body();
            System.out.println(result2);

            JSONObject jsonObject = JSON.parseObject(result2);
            Object error = jsonObject.get("error");

            JsonObject apiResponse = JsonParser.parseString(result2).getAsJsonObject();
            // 提取message.content字段

            JsonObject message = apiResponse.getAsJsonObject("message");
            String content = message.get("content").getAsString();
            System.out.println("content"+content);
            mxfxjgb.setFhjg(content);
            mxfxjgb.setJxzt("解析完成");
            mxfxjgbMapper.updateMxfxjgb(mxfxjgb);
        } catch (Exception e) {
            mxfxjgb.setJxzt("解析失败");
            mxfxjgbMapper.updateMxfxjgb(mxfxjgb);
        }
    }
}
