package com.gic.haoban.manage.web.utils.data;

import com.alibaba.fastjson.JSONObject;
import com.gic.commons.exception.DataApiException;
import com.gic.commons.util.DataApiUtils;
import com.gic.dubbo.entity.ProviderLocalTag;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @Author guojx
 * @Date 2024/1/11 15:33
 */
@Slf4j
public class MapThreadHandlerRequest implements Callable<Map<String, List<JSONObject>>> {

    private JSONObject jsonObject;

    private String apolloKey;

    private String webObjName;

    private String traceId;

    public MapThreadHandlerRequest(JSONObject jsonObject, String apolloKey, String webObjName) {
        this.jsonObject = JSONObject.parseObject(jsonObject.toJSONString());
        this.apolloKey = apolloKey;
        this.webObjName = webObjName;
    }

    @Override
    public Map<String, List<JSONObject>> call() throws Exception {
        try {
            //log.info("并发调用入口：{}-{}", apolloKey, webObjName);
            ProviderLocalTag providerLocalTag = ProviderLocalTag.tag.get();
            providerLocalTag.traceId = traceId;
            Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), apolloKey);
            List<JSONObject> list = DataApiUtils.getDataList(res);
            Map<String, List<JSONObject>> map = new HashMap<>(2);
            map.put(webObjName, list);
            return map;
        } catch (DataApiException e) {
//            if (DataApiException.OVER_HANDLE_ERROR.equals(e.getCode())) {
//                throw new DataApiException(e.getCode(), e.getMessage());
//            }
            log.info("{}调用错误:{}", apolloKey, e.getMessage(), e);
        } catch (Exception e) {
            log.info("{}调用错误:{}", apolloKey, e.getMessage(), e);
        }
        return null;
    }

    public String getTraceId() {
        return traceId;
    }

    public MapThreadHandlerRequest setTraceId(String traceId) {
        this.traceId = traceId;
        return this;
    }
}
