package com.engine.web.sl;


import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.custom.sl.entity.EsbRequestHeader;
import com.engine.custom.sl.entity.TravelToEsbBean;
import com.engine.util.SocketClientUtil;
import com.engine.util.XMLUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class SLController {
    @Path("/getCWInfo")
    @GET
    public String getDataById(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> param = ParamUtil.request2Map(request);
        String workCode = param.get("workCode").toString();
        String timeStr = generateTimestamp();
        EsbRequestHeader esbRequestHeader = new EsbRequestHeader();
        esbRequestHeader.setService_sn(timeStr+workCode);
        esbRequestHeader.setSystem_id("0170");
        esbRequestHeader.setRequester_id("0258");
        esbRequestHeader.setBranch_id("cwgl00002");
        esbRequestHeader.setChannel_id("02");
        esbRequestHeader.setService_time(timeStr);
        esbRequestHeader.setNeed_request("");
        esbRequestHeader.setSvcCd("300130001");
        esbRequestHeader.setSvcScn("39");
        String bnkSrlNo = esbRequestHeader.getRequester_id() +  timeStr + workCode;
        esbRequestHeader.setBnkSrlNo(bnkSrlNo);
        esbRequestHeader.setFileFlg("0");
        String Service_Body = "<Service_Body><request><operatorNo>"+ workCode +"</operatorNo></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));
        // out.print("请求=="+ serviceXML);
        SocketClientUtil scketClient = new SocketClientUtil("14.1.71.90",10149);
        String send = null;
        try {
            send = scketClient.send(serviceXML);
        } catch (Exception e) {
            return e.getMessage();
        }
        String substring = send.substring(0, 8);
        String substring1 = send.substring(8);
        Map<String, String> map = XMLUtils.parseXMLToMap(substring1);
        return (JSONObject.toJSONString(map));
    }


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