package weaver.interfaces.htsc.workflow;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class OtheEndRequestToModelAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String status = "end";
        String requestid = requestInfo.getRequestid();
        PropBean pb = new PropBean();
        String url = pb.getPropName("xplat_zhfw");
        String tablename = JAVATools.getMaintable(requestid);
        String numbertable = JAVATools.getmainunnumber(requestid);
        JAVATools.SetCmdwritelog("request:" + requestid + " 查询主表的表名:" + tablename + " numbertable：" + numbertable);
        String sqlmain = "select * from " + tablename + " where requestid=" + requestid;
        Map<String, String> map = new HashMap<>();
        map.put("requestid", requestid);
        Map<String, Object> mapmain = JAVATools.GetTableToMap(sqlmain, map);
        if (!JAVATools.ismapisnotnull(mapmain)) {
            //数据获取为空
            return null;
        }
        JSONObject json = new JSONObject();
        json.put("json", JSONUtils.toJSONString(mapmain));
        json.put("status", status);
        String returnValue = "这是默认返回值，接口调用失败";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            //第一步：创建HttpClient对象
            httpClient = HttpClients.createDefault();
            //第二步：创建httpPost对象
            HttpPost httpPost = new HttpPost(url);
            //第三步：给httpPost设置JSON格式的参数
            StringEntity requestEntity = new StringEntity(json.toString(), "utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(requestEntity);
            //第四步：发送HttpPost请求，获取返回值
            returnValue = httpClient.execute(httpPost, responseHandler); //调接口获取返回值时，必须用此方法
        } catch (Exception e) {
            e.printStackTrace();
            return Action.FAILURE_AND_CONTINUE;
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //第五步：处理返回值
        return Action.SUCCESS;
    }
}
