<%@ page import="com.engine.custom.sl.entity.EsbRequestHeader" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.engine.custom.sl.entity.TravelToEsbBean" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="com.engine.util.SocketClientUtil" %>
<%@ page import="com.engine.util.XMLUtils" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.engine.common.util.ParamUtil" %>
<%@ page import="java.net.Socket" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.StringUtil" %>
<%@ page import="java.io.*" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<%

    // Map<String, Object> param = ParamUtil.request2Map(request);
    // String workCode = param.get("workCode").toString();
    String workCode = HrmUserVarify.getUser(request, response).getLoginid();
    String timeStr = generateTimestamp();
    EsbRequestHeader esbRequestHeader = new EsbRequestHeader();
    esbRequestHeader.setService_sn(timeStr+workCode);
    esbRequestHeader.setSystem_id("0275");
    esbRequestHeader.setRequester_id("0258");
    esbRequestHeader.setBranch_id("ITMS0002");
    esbRequestHeader.setChannel_id("17");
    esbRequestHeader.setService_time(timeStr);
    esbRequestHeader.setNeed_request("");
    esbRequestHeader.setSvcCd("500130004");
    esbRequestHeader.setSvcScn("17");
    String bnkSrlNo = esbRequestHeader.getRequester_id() +  timeStr + workCode;
    esbRequestHeader.setBnkSrlNo(bnkSrlNo);
    esbRequestHeader.setFileFlg("0");
    String Service_Body = "<Service_Body><request><path>todo</path><method>getCount</method><args><entry><key>userNum</key><value>"+workCode+"</value></entry></args></request></Service_Body>"  ;
    String Service_Header = TravelToEsbBean.convertObjectToXml(esbRequestHeader,"Service_Header");
    String serviceXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service>";
    // out.print("UTF_8_serviceXML==="+ serviceXML);
    serviceXML = serviceXML +  Service_Header + Service_Body;
    serviceXML = serviceXML + "</Service>";
    // out.print("UTF_8_serviceXML==="+ serviceXML);
    String length = String.format("%08d", serviceXML.length());
    serviceXML =  length + serviceXML;
    serviceXML = new String(serviceXML.getBytes(StandardCharsets.UTF_8));
    String send = "";
    try {
        send = send(serviceXML);
        // out.print("返回=="+send);
    }catch (Exception e){
        // out.print("异常=="+ e.getMessage());
    }
    // SocketClientUtil scketClient = new SocketClientUtil("14.1.76.116",10149);
    // String substring = send.substring(0, 8);
    // String substring1 = send.substring(8);
    // out.print(substring);
    Map<String, String> map = XMLUtils.parseXMLToMap(send);

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("count", Util.getIntValue(map.get("count")));
    out.print(jsonObject.toJSONString());
%>
<%!
    public String send(String request) throws Exception {
        // bs.writeLog("Client Start...");
        BaseBean bs = new BaseBean();
        Socket socket = null;
        try {
            //创建一个流套接字并将其连接到指定主机上的指定端口号
            socket = new Socket("14.1.71.90",10149);
            // String str = "00000612<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service><Service_Header><service_sn>1010157060000081243</service_sn><system_id>0258</system_id><requester_id>0157</requester_id><branch_id>010231100</branch_id><channel_id>01</channel_id><service_time>20230905104426</service_time><need_request>true</need_request><SvcCd>500130004</SvcCd><SvcScn>13</SvcScn><BnkSrlNo>015720230905060000081243</BnkSrlNo><FileFlg>0</FileFlg></Service_Header><Service_Body><request><path>todo</path><method>getCount</method><args><entry><key>userNum</key><value>10913026任广鹏是个大刷币</value></entry></args></request></Service_Body></Service>";
            // System.out.println(str);
            //读取服务器端数据
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
            //向服务器端发送数据
            // PrintStream out = new PrintStream(new OutputStreamWriter(socket.getOutputStream(),StandardCharsets.UTF_8));
            PrintWriter out =  new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),StandardCharsets.UTF_8),true);
            out.println(request);
            char[] datalen=new char[8];//报文前八位为报文体总长度
            input.read(datalen,0,8);
            String lendata=new String (datalen);
            int length=Integer.parseInt(lendata);
            new BaseBean().writeLog("报文长度"+length);
            char[] data=new char[length];
            int datalength = input.read(data,0,length);
            String requestData = new String(data);
            new BaseBean().writeLog("requestData",requestData);
            // String ret = input.readLine();
            System.out.println("服务器端返回过来的是: " + requestData);
            bs.writeLog("服务器端返回过来的是: " + requestData);
            // 如接收到 "OK" 则断开连接
            if (!StringUtil.isEmpty(requestData)) {
                bs.writeLog("客户端将关闭连接");
                System.out.println("客户端将关闭连接: " + requestData);
                Thread.sleep(500);
            }
            out.close();
            input.close();
            return requestData;
        } catch (Exception e) {
            bs.writeLog("客户端异常:" + e.getMessage());
            throw  e;
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    socket = null;
                    bs.writeLog("客户端 finally 异常:" + e.getMessage());
                }
            }
        }
        // return "-1";
    }

    public static String generateTimestamp() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date currentDate = new Date();
        return dateFormat.format(currentDate);
    }
%>