package com.engine.htsc.jgxt.service.imp;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.jgxt.dao.Jgxtbase;
import com.engine.htsc.jgxt.service.JgxtApiServer;
import com.engine.htsc.payment.util.HttpUtil;
import org.apache.http.client.methods.HttpPost;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;


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

/**
 * @author xf
 * @version 1.0.0
 * @ClassName JgxtApiServer.java
 * @createTime 2021年09月22日 09:41:00
 */
public class JgxtApiServerImp  extends Service implements JgxtApiServer {

    @Override
    public JSONObject letterencry(Map<String, Object> map) {
        JSONObject jsonObject=new JSONObject();
        String imagefileid= Util.null2String(map.get("imagefileid"));
        if("".equals(imagefileid)){
            jsonObject.put("msg","参数有误！");
            return jsonObject;
        }
        Map<String,Object> fileidmap=Jgxtbase.getgatewayfileid(imagefileid);
        if(fileidmap==null || fileidmap.size()<=0){
            jsonObject.put("msg","获取存储网关有误！");
            return jsonObject;
        }
        String fileid=Util.null2String(fileidmap.get("fileid"));
        if("".equals(fileid)){
            jsonObject.put("msg","获取存储网关有误！");
            return jsonObject;
        }
        try {


            PropBean propBean= new PropBean();
            String url = propBean.getPropName("jgxt.url.letterencry");
            String appSysId=propBean.getPropName("jgxt.url.appSysid");
            String token=propBean.getPropName("jgxt.url.token");
            String systemId=propBean.getPropName("jgxt.url.systemId");
            String uservalue=propBean.getPropName("jgxt.url.user");
            String functionId=propBean.getPropName("jgxt.url.functionId");
            String traceId=propBean.getPropName("jgxt.url.traceId");
            String env=propBean.getPropName("jgxt.url.env");
            Map<String, String> hdeadmap = new HashMap<>();
            hdeadmap.put("appSysId",appSysId);
            hdeadmap.put("token",token);
            JSONObject jsonobj=new JSONObject();
            JSONObject jsonobj2=new JSONObject();
            jsonobj.put("systemId",systemId);
            jsonobj.put("user",uservalue);
            jsonobj.put("functionId",functionId);
            jsonobj.put("traceId",traceId);
            jsonobj2.put("fileid",fileid);
            jsonobj2.put("env",env);
            jsonobj.put("data",jsonobj2);
            String result=new HTUtil().httpPostRaw(url,jsonobj.toJSONString(),hdeadmap,null);

            if("".equals(result)){
                jsonObject.put("msg","函件解析有误！");
                return jsonObject;
            }
            jsonObject.put("data",JSONObject.parseObject(result));
        }catch (Exception exo){
            jsonObject.put("msg","函件解析有误！");
        }
        return jsonObject;
    }

    @Override
    public JSONObject freeNode(String requestid) {
        //获取前端配置中心值
        String propName = new PropBean().getPropName("jgxt.end.nodename");
        List<String> list = Jgxtbase.getFreeNodeName(requestid);
        return Jgxtbase.freenode(propName, list);
    }
}
