package com.jojubanking.boot.module.system.util.xytj;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

//import com.saye.hrs.dto.PatientFeeSettledQuery;
//import com.saye.hrs.dto.Result;
//import com.saye.hrs.vo.FeeInfoVO;
//import com.saye.hrs.vo.FeeSettledInfoVO;
import com.jojubanking.boot.module.system.controller.app.xytjinfo.StatusDefine;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
//import com.saye.hrs.commons.date.DateDUtil;
//import com.saye.hrs.model.StatusDefine;


public class WSUtil {

    private static final Logger logger = LoggerFactory.getLogger(WSUtil.class);
    public static RestTemplate restTemplate = new RestTemplate();

    //外网生产地址
//    public static String soapUrl = "http://12.0.2.1:10062/KWSService.asmx";
    //内网生产地址
    public static String soapUrl = "http://168.168.0.43:9192/WebService.asmx";

    //通过39转发的测试地址
//    public static String soapUrl = "http://218.84.202.146:58080/WebService.asmx";

    static HttpHeaders headers;
    static {
        headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
    }
    private static String czyh = "0421";
    /**
     * 调用webservice接口 his系统的
     * @param url
     * @param tranCode
     * @param inputXml
     * @return
     */
    public static HashMap<String,String> webserviceRequest(String url, String tranCode, String inputXml) {
        logger.info("请求参数----------"+inputXml);
        HashMap<String,String> resultMap=new HashMap<>();
        String errCode="0";
        String errMsg="";
        try {
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("text/xml;charset=UTF-8");
            // 请求类型ContentType text/xml;charset=UTF-8
            headers.setContentType(type);

            //请求数据体
            StringBuffer soapRequestData = new StringBuffer("");
            soapRequestData.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:kws=\"http://www.winning.com.cn/KWS/\">");
            soapRequestData.append("<soapenv:Header/>");
            soapRequestData.append("<soapenv:Body>");
            soapRequestData.append("<kws:WebBusiness>");
            soapRequestData.append("<kws:TranCode>");

            //交易码
            soapRequestData.append(tranCode);

            soapRequestData.append("</kws:TranCode>");
            soapRequestData.append("<kws:InXml>");

            //<>符号要转码
            inputXml=inputXml.replaceAll("<","&lt;");
            inputXml=inputXml.replaceAll(">","&gt;");

            soapRequestData.append(inputXml);

            soapRequestData.append("</kws:InXml>");
            soapRequestData.append("</kws:WebBusiness>");
            soapRequestData.append("</soapenv:Body>");
            soapRequestData.append("</soapenv:Envelope>");

            // 请求
            HttpEntity<String> request = new HttpEntity<String>(soapRequestData + "", headers);

            // 返回结果
            String str = restTemplate.postForObject(url, request, String.class);
            logger.info("返回结果----------"+str);
            Document document= DocumentHelper.parseText(str);
            Element rootElt = document.getRootElement();

            //解析
            Iterator body = rootElt.elementIterator("Body");
            Element recordEless = (Element) body.next();

            Iterator callHisResponse = recordEless.elementIterator("WebBusinessResponse");
            Element hisResponse = (Element) callHisResponse.next();

            Iterator callHisResult = hisResponse.elementIterator("OutXml");
            Element hisResult = (Element)callHisResult.next();

            String text = hisResult.getText();

            resultMap.put("outXml",text);
        } catch (Exception e) {
            logger.info("返回结果异常----------"+e.toString());
            errCode="999";
            errMsg=e.getMessage();
        }
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /**
     * 调用webservice接口 his系统的
     * @param url
     * @param tranCode
     * @param inputXml
     * @return
     */
    public static HashMap<String,String> webserviceRequestTest(String url, String tranCode, String inputXml) {
        logger.info("请求参数----------"+inputXml);
        HashMap<String,String> resultMap=new HashMap<>();
        String errCode="0";
        String errMsg="";
        try {
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("text/xml;charset=UTF-8");
            // 请求类型ContentType text/xml;charset=UTF-8
            headers.setContentType(type);

            //请求数据体
            StringBuffer soapRequestData = new StringBuffer("");
            soapRequestData.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">");
            soapRequestData.append("<soapenv:Header/>");
            soapRequestData.append("<soapenv:Body>");
            soapRequestData.append("<tem:WebBusiness>");
            soapRequestData.append("<tem:TranCode>");

            //交易码
            soapRequestData.append(tranCode);

            soapRequestData.append("</tem:TranCode>");
            soapRequestData.append("<tem:InXml>");

            //<>符号要转码
            inputXml=inputXml.replaceAll("<","&lt;");
            inputXml=inputXml.replaceAll(">","&gt;");

            soapRequestData.append(inputXml);

            soapRequestData.append("</tem:InXml>");
            soapRequestData.append("</tem:WebBusiness>");
            soapRequestData.append("</soapenv:Body>");
            soapRequestData.append("</soapenv:Envelope>");

            // 请求
            HttpEntity<String> request = new HttpEntity<String>(soapRequestData + "", headers);

            // 返回结果
            String str = restTemplate.postForObject(url, request, String.class);
            logger.info("返回结果----------"+str);
            Document document= DocumentHelper.parseText(str);
            Element rootElt = document.getRootElement();

            //解析
            Iterator body = rootElt.elementIterator("Body");
            Element recordEless = (Element) body.next();

            Iterator callHisResponse = recordEless.elementIterator("WebBusinessResponse");
            Element hisResponse = (Element) callHisResponse.next();

            Iterator callHisResult = hisResponse.elementIterator("OutXml");
            Element hisResult = (Element)callHisResult.next();

            String text = hisResult.getText().replaceAll("\\p{C}", "");

            resultMap.put("outXml",text);
        } catch (Exception e) {
            logger.info("返回结果异常----------"+e.toString());
            errCode="999";
            errMsg=e.getMessage();
        }
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /** 获取患者信息
     * @param
     * @return
     */
    public static HashMap<Object,Object> getPatient(String cardno,String cardtype) throws Exception {

        String errMsg = "";
        List<HashMap<Object,Object>> list = new ArrayList<>();;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><cardno>"+cardno+"</cardno>" +  //卡号
                "<cardtype>"+cardtype+"</cardtype>" +    //卡类型
                "<czyh>"+czyh+"</czyh></params></request>";
        HashMap<String,String> map = webserviceRequestTest(soapUrl,"101",inputXml);
//        addHisLog("根据卡号获取患者",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            String code = codeElm.getText();
            String msg = msgElm.getText();

            if (!"0".equals(code)){
                errCode=code;
                errMsg=msg;
            }

            if ("0".equals(code)){
                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();

                while (itemIterator.hasNext()){
                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();

                    Element brztEl = item.element("brzt");
                    String brzt = brztEl==null?"":brztEl.getText();

                    Element patidEl = item.element("patid");
                    String patid = patidEl==null?"":patidEl.getText();

                    Element cardnoEl = item.element("cardno");
                    String cardno1 = cardnoEl==null?"":cardnoEl.getText();

                    Element cardtypeEl = item.element("cardtype");
                    String cardtype1 = cardtypeEl==null?"":cardtypeEl.getText();

                    Element pzlxEl = item.element("pzlx");
                    String pzlx = pzlxEl==null?"":pzlxEl.getText();

                    Element hzxmEl = item.element("hzxm");
                    String hzxm = hzxmEl==null?"":hzxmEl.getText();

                    Element ybdmEl = item.element("ybdm");
                    String ybdm = ybdmEl==null?"":ybdmEl.getText();

                    Element sexEl = item.element("sex");
                    String sex = sexEl==null?"":sexEl.getText();

                    Element sfzhEl = item.element("sfzh");
                    String sfzh = sfzhEl==null?"":sfzhEl.getText();

                    Element lxdzEl = item.element("lxdz");
                    String lxdz = lxdzEl==null?"":lxdzEl.getText();

                    Element ghbzEl = item.element("ghbz");
                    String ghbz = ghbzEl==null?"":ghbzEl.getText();

                    Element lxdhEl = item.element("lxdh");
                    String lxdh = lxdhEl==null?"":lxdhEl.getText();

                    Element zhyeEl = item.element("zhye");
                    String zhye = zhyeEl==null?"":zhyeEl.getText();

                    BigDecimal bd = new BigDecimal(zhye);
                    bd = bd.setScale(2, RoundingMode.HALF_UP);
                    zhye = bd.toString();

                    Element ispwdEl = item.element("ispwd");
                    String ispwd = ispwdEl==null?"":ispwdEl.getText();

                    Element djrqEl = item.element("djrq");
                    String djrq = djrqEl==null?"":djrqEl.getText();

                    Element lrrqEl = item.element("lrrq");
                    String lrrq = lrrqEl==null?"":lrrqEl.getText();

                    Element gsbzEl = item.element("gsbz");
                    String gsbz = gsbzEl==null?"":gsbzEl.getText();

                    Element blhEl = item.element("blh");
                    String blh = blhEl==null?"":blhEl.getText();

                    Element birthEl = item.element("birth");
                    String birth = birthEl==null?"":birthEl.getText();

                    Element zhbzEl = item.element("zhbz");
                    String zhbz = zhbzEl==null?"":zhbzEl.getText();

                    addMap.put("brzt",brzt);
                    addMap.put("patid",patid);
                    addMap.put("cardno",cardno1);
                    addMap.put("cardtype",cardtype1);
                    addMap.put("pzlx",pzlx);
                    addMap.put("hzxm",hzxm);
                    addMap.put("ybdm",ybdm);
                    addMap.put("sex",sex);
                    addMap.put("sfzh",sfzh);
                    addMap.put("lxdz",lxdz);
                    addMap.put("ghbz",ghbz);
                    addMap.put("lxdh",lxdh);
                    addMap.put("zhye",zhye);
                    addMap.put("ispwd",ispwd);
                    addMap.put("djrq",djrq);
                    addMap.put("lrrq",lrrq);
                    addMap.put("gsbz",gsbz);
                    addMap.put("blh",blh);
                    addMap.put("birth",birth);
                    addMap.put("zhbz",zhbz);
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }


    /** 获取科室信息
     * @param
     * @return
     */
    public static HashMap<Object,Object> getOffice(String ksrq, String jsrq, String lb) throws DocumentException {

        List<HashMap<Object,Object>> list = null;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><ksrq>"+ksrq+"</ksrq>" +  //开始日期
                "<jsrq>"+jsrq+"</jsrq>" +          //结束日期
                "<lb>"+lb+"</lb>" +                //类别
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","301",inputXml);
        addHisLog("获取科室",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        String errMsg = map.get("errMsg");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();

            if ("0".equals(errCode)){
                list = new ArrayList<>();
                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();

                while (itemIterator.hasNext()){
                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();

                    Element ksdmEl = item.element("ksdm");
                    String ksdm = ksdmEl==null?"":ksdmEl.getText();

                    Element ksmcEl = item.element("ksmc");
                    String ksmc = ksmcEl==null?"":ksmcEl.getText();

                    Element ysdmEl = item.element("ysdm");
                    String ysdm = ysdmEl==null?"":ysdmEl.getText();

                    Element ysmcEl = item.element("ysmc");
                    String ysmc = ysmcEl==null?"":ysmcEl.getText();

                    addMap.put("ksdm",ksdm);
                    addMap.put("ksmc",ksmc);
                    addMap.put("ysdm",ysdm);
                    addMap.put("ysmc",ysmc);
                    list.add(addMap);
                }
                /*for(int i=0;i<20;i++) {
                	 HashMap<Object,Object> addMap=new HashMap<>();
                     addMap.put("ksdm",1);
                     addMap.put("ksmc","外科"+i);
                     addMap.put("ysdm","");
                     addMap.put("ysmc","");
                     list.add(addMap);
                }*/
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /** 获取某些天科室排班信息
     * @param
     * @return
     */
    public static HashMap<Object,Object> getOfficeSchedule(String ksrq, String jsrq, String lb,String kmdm) throws DocumentException {

        List<HashMap<Object,Object>> list = new ArrayList<>();
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><ksrq>"+ksrq+"</ksrq>" +  //开始日期
                "<jsrq>"+jsrq+"</jsrq>" +          //结束日期
                "<lb>"+lb+"</lb>" +                //类别
                "<kmdm>"+kmdm+"</kmdm>" +          //科室代码
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","302",inputXml);
        addHisLog("获取科室排班",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        String errMsg = map.get("errMsg");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();

            if ("0".equals(errCode)){
                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();

                while (itemIterator.hasNext()){
                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();

                    Element kyysEl = item.element("kyys");
                    String kyys = kyysEl==null?"":kyysEl.getText();

                    Element yyzsEl = item.element("yyzs");
                    String yyzs = yyzsEl==null?"":yyzsEl.getText();

                    Element pbmxidEl = item.element("pbmxid");
                    String pbmxid = pbmxidEl==null?"":pbmxidEl.getText();

                    Element zxrqEl = item.element("zxrq");
                    String zxrq = zxrqEl==null?"":zxrqEl.getText();

                    Element kssjEl = item.element("kssj");
                    String kssj = kssjEl==null?"":kssjEl.getText();

                    Element jssjEl = item.element("jssj");
                    String jssj = jssjEl==null?"":jssjEl.getText();

                    Element weekdayEl = item.element("weekday");
                    String weekday = weekdayEl==null?"":weekdayEl.getText();

                    Element ghfEl = item.element("ghf");
                    String ghf = ghfEl==null?"":ghfEl.getText();

                    Element zlfEl = item.element("zlf");
                    String zlf = zlfEl==null?"":zlfEl.getText();

                    addMap.put("kyys",kyys);
                    addMap.put("yyzs",yyzs);
                    addMap.put("pbmxid",pbmxid);
                    addMap.put("zxrq",zxrq);
                    addMap.put("kssj",kssj);
                    addMap.put("jssj",jssj);
                    addMap.put("weekday",weekday);
                    addMap.put("ghf",ghf);
                    addMap.put("zlf",zlf);
                    String hour = DateDUtil.getCurrentDate("HH:mm");
                    String date = DateDUtil.getCurrentDate("yyyyMMdd");
                    if(date.equals(zxrq) && jssj.compareTo(hour)>0) {
                        list.add(addMap);
                    }else if(!date.equals(zxrq)){
                        list.add(addMap);
                    }
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /** 预约登记
     * @param
     * @return
     */
    public static HashMap<Object,Object> order(String patid, String pbmxid, String yyhx, String sjdjl) throws DocumentException {

        List<HashMap<Object,Object>> list = null;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><patid>"+patid+"</patid>" +  	//病人唯一码
                "<pbmxid>"+pbmxid+"</pbmxid>" +         //排班明细 id 序号
                "<czyh>"+czyh+"</czyh>" +               //操作员号
                "<yyhx>"+yyhx+"</yyhx>" +          		//预约号序类型(0 时间段预约 1 时间点预约)
                "<sjdjl>"+sjdjl+"</sjdjl>" +            //时间段(302 交易返回 kssj)
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","303",inputXml);
        addHisLog("预约",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        String errMsg = map.get("errMsg");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            String code = codeElm.getText().toString();
            String msg = msgElm.getText().toString();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();

            if ("0".equals(errCode)){
                list = new ArrayList<>();
                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();

                while (itemIterator.hasNext()){
                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();

                    Element yyrqEl = item.element("yyrq");
                    String yyrq = yyrqEl==null?"":yyrqEl.getText();

                    Element yyhxEl = item.element("yyhx");
                    String yyhx1 = yyhxEl==null?"":yyhxEl.getText();

                    Element xhEl = item.element("xh");
                    String xh = xhEl==null?"":xhEl.getText();

                    Element sjdEl = item.element("sjd");
                    String sjd = sjdEl==null?"":sjdEl.getText();

                    addMap.put("yyrq",yyrq);
                    addMap.put("yyhx",yyhx1);
                    addMap.put("xh",xh);
                    addMap.put("sjd",sjd);
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /** 取消预约
     * @param
     * @return
     */
    public static HashMap<Object,Object> cancelOrder(String yylsh) throws DocumentException {

        List<HashMap<Object,Object>> list = null;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><yylsh>"+yylsh+"</yylsh>" +   //303返回的预约流水号
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","305",inputXml);
        addHisLog("取消预约",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        String errMsg = map.get("errMsg");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();

            if ("0".equals(errCode)){
                list = new ArrayList<>();
                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();

                while (itemIterator.hasNext()){
                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();

                    Element yylshEl = item.element("yylsh");
                    String yylsh1 = yylshEl==null?"":yylshEl.getText();

                    addMap.put("yylsh",yylsh1);
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /** 获取某人预约登记信息
     * @param
     * @return
     */
    public static HashMap<Object,Object> getOrder(String patid) throws Exception {

        String errMsg = "";
        List<HashMap<Object,Object>> list = new ArrayList<>();
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><patid>"+patid+"</patid>" +  //病人唯一码
                "<czyh>"+czyh+"</czyh></params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","304",inputXml);
        addHisLog("预约记录",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();

            if ("0".equals(errCode)){
                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();

                while (itemIterator.hasNext()){
                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();

                    Element yyrqEl = item.element("yyrq");
                    String yyrq = yyrqEl==null?"":yyrqEl.getText();

                    Element yyxhEl = item.element("yyxh");
                    String yyxh = yyxhEl==null?"":yyxhEl.getText();

                    Element yyhxEl = item.element("yyhx");
                    String yyhx = yyhxEl==null?"":yyhxEl.getText();

                    Element yylshEl = item.element("yylsh");
                    String yylsh = yylshEl==null?"":yylshEl.getText();

                    Element sjdEl = item.element("sjd");
                    String sjd = sjdEl==null?"":sjdEl.getText();

                    Element yyksmcEl = item.element("yyksmc");
                    String yyksmc = yyksmcEl==null?"":yyksmcEl.getText();

                    Element yyysmcEl = item.element("yyysmc");
                    String yyysmc = yyysmcEl==null?"":yyysmcEl.getText();

                    Element yylbEl = item.element("yylb");
                    String yylb = yylbEl==null?"":yylbEl.getText();

                    Element ksdmEl = item.element("ksdm");
                    String ksdm = ksdmEl==null?"":ksdmEl.getText();

                    Element ysdmEl = item.element("ysdm");
                    String ysdm = ysdmEl==null?"":ysdmEl.getText();

                    Element pbmxxhEl = item.element("pbmxxh");
                    String pbmxxh = pbmxxhEl==null?"":pbmxxhEl.getText();

                    Element jlztEl = item.element("jlzt");
                    String jlzt = jlztEl==null?"":jlztEl.getText();

                    addMap.put("yyrq",yyrq);//预约日期
                    addMap.put("yyxh",yyxh);//预约序号
                    addMap.put("yyhx",yyhx);//预约号序
                    addMap.put("yylsh",yylsh);//预约登记流水号(预约取消时使用)
                    addMap.put("sjd",sjd);//预约时间段
                    addMap.put("yyksmc",yyksmc);//预约科室名称
                    addMap.put("yyysmc",yyysmc);//预约医生名称
                    addMap.put("yylb",yylb);//预约类别(科室、与家、特需等)
                    addMap.put("ksdm",ksdm);//科室代码
                    addMap.put("ysdm",ysdm);//医生代码
                    addMap.put("pbmxxh",pbmxxh); //排班明细序号
                    addMap.put("jlzt",jlzt); //预约状态
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }


    /** 获取某人检查报告
     * @param
     * @return
     */
    public static HashMap<Object,Object> getInspectReport(String cardno,String ksrq,String jsrq,String repno,String lb,String brlx,String bglx) throws Exception {

        String errMsg = "";
        List<HashMap<Object,Object>> list =new ArrayList<>();;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><cardno>"+cardno+"</cardno>" +  //卡号
                "<ksrq>"+ksrq+"</ksrq>" +				 //开始日期
                "<jsrq>"+jsrq+"</jsrq>" +				 //结束日期
                "<repno>"+repno+"</repno>" +			 //报告序号@lb=0 该值可为空  @lb=1 该值为必填
                "<lb>"+lb+"</lb>" +						 //类别 0 主记录   1 结果明细
                "<brlx>"+brlx+"</brlx>" +				 //1 门诊     2 住院
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","109",inputXml);
        addHisLog("检查报告",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();


            if ("0".equals(errCode)){

                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();
                if("0".equals(lb)) {  //主记录
                    while (itemIterator.hasNext()){

                        HashMap<Object,Object> addMap=new HashMap<>();
                        Element item = (Element) itemIterator.next();

                        Element cardnoEl = item.element("cardno");
                        String cardno1 = cardnoEl==null?"":cardnoEl.getText();

                        Element hzxmEl = item.element("hzxm");
                        String hzxm = hzxmEl==null?"":hzxmEl.getText();

                        Element sexEl = item.element("sex");
                        String sex = sexEl==null?"":sexEl.getText();

                        Element ageEl = item.element("age");
                        String age = ageEl==null?"":ageEl.getText();

                        Element sjksmcEl = item.element("sjksmc");
                        String sjksmc = sjksmcEl==null?"":sjksmcEl.getText();

                        Element HisOrderNameEl = item.element("HisOrderName");
                        String HisOrderName = HisOrderNameEl==null?"":HisOrderNameEl.getText();

                        Element jcksmcEl = item.element("jcksmc");
                        String jcksmc = jcksmcEl==null?"":jcksmcEl.getText();

                        Element reprqEl = item.element("reprq");
                        String reprq = reprqEl==null?"":reprqEl.getText();

                        Element repnoEl = item.element("repno");
                        String repno1 = repnoEl==null?"":repnoEl.getText();

                        Element bglxEl = item.element("bglx");
                        String bglx1 = bglxEl==null?"":bglxEl.getText();

                        if(bglx.equals(bglx1)) {
                            addMap.put("cardno",cardno1);
                            addMap.put("bglx",bglx1);
                            addMap.put("hzxm",hzxm);
                            addMap.put("sex",sex);
                            addMap.put("age",age);
                            addMap.put("sjksmc",sjksmc);
                            addMap.put("HisOrderName",HisOrderName);
                            addMap.put("jcksmc",jcksmc);
                            addMap.put("reprq",reprq);
                            addMap.put("repno",repno1);
                            addMap.put("bglx",bglx1);
                            list.add(addMap);
                        }
                    }

                }else if("1".equals(lb)){    //明细
                    while (itemIterator.hasNext()){

                        HashMap<Object,Object> addMap=new HashMap<>();
                        Element item = (Element) itemIterator.next();
                        //
                        Element xmmcEl = item.element("xmmc");
                        String xmmc = xmmcEl==null?"":xmmcEl.getText();
                        //
                        Element xmdmEl = item.element("xmdm");
                        String xmdm = xmdmEl==null?"":xmdmEl.getText();
                        //
                        Element xmjgEl = item.element("xmjg");
                        String xmjg = xmjgEl==null?"":xmjgEl.getText();
                        //
                        Element xmdwEl = item.element("xmdw");
                        String xmdw = xmdwEl==null?"":xmdwEl.getText();
                        //
                        Element jgckzEl = item.element("jgckz");
                        String jgckz = jgckzEl==null?"":jgckzEl.getText();

                        addMap.put("xmmc",xmmc);
                        addMap.put("xmdm",xmdm);
                        addMap.put("xmjg",xmjg);
                        addMap.put("xmdw",xmdw);
                        addMap.put("jgckz",jgckz);
                        list.add(addMap);
                    }
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /** 获取某人充值记录
     * @param
     * @return
     */
    public static HashMap<Object,Object> getRecharge(String cardno,String cardtype,String kssj,String jssj) throws Exception {

        String errMsg = "";
        List<HashMap<Object,Object>> list = new ArrayList<>();;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><cardno>"+cardno+"</cardno>" +  //卡号
                "<cardtype>"+cardtype+"</cardtype>" +    //卡类型：1、磁卡 2、保障卡 3、IC 卡
                "<kssj>"+kssj+"</kssj>" +                //开始日期
                "<jssj>"+jssj+"</jssj>" +        //结束日期
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","703",inputXml);
        addHisLog("充值记录",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            String code = codeElm.getText();
            String msg = msgElm.getText();

            if (!"0".equals(code)){
                errCode=code;
                errMsg=msg;
            }

            if ("0".equals(code)){
                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();

                while (itemIterator.hasNext()){
                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();

                    Element patidEl = item.element("patid");
                    String patid = patidEl==null?"":patidEl.getText();

                    Element cardnoEl = item.element("cardno");
                    String cardno1 = cardnoEl==null?"":cardnoEl.getText();

                    Element cardtypeEl = item.element("cardtype");
                    String cardtype1 = cardtypeEl==null?"":cardtypeEl.getText();

                    Element hzxmEl = item.element("hzxm");
                    String hzxm = hzxmEl==null?"":hzxmEl.getText();

                    Element sexEl = item.element("sex");
                    String sex = sexEl==null?"":sexEl.getText();

                    Element jzxhEl = item.element("jzxh");
                    String jzxh = jzxhEl==null?"":jzxhEl.getText();

                    Element czlxEl = item.element("czlx");
                    String czlx = czlxEl==null?"":czlxEl.getText();

                    Element czyhEl = item.element("czyh");
                    String czyh = czyhEl==null?"":czyhEl.getText();

                    Element lrrqEl = item.element("lrrq");
                    String lrrq = lrrqEl==null?"":lrrqEl.getText();

                    Element lshEl = item.element("lsh");
                    String lsh = lshEl==null?"":lshEl.getText();

                    Element fphEl = item.element("fph");
                    String fph = fphEl==null?"":fphEl.getText();

                    Element jyjeEl = item.element("jyje");
                    String jyje = jyjeEl==null?"":jyjeEl.getText();

                    BigDecimal bd = new BigDecimal(jyje);
                    bd = bd.setScale(2, RoundingMode.HALF_UP);
                    jyje = bd.toString();

                    Element yjyeEl = item.element("yjye");
                    String yjye = yjyeEl==null?"":yjyeEl.getText();

                    Element memoEl = item.element("memo");
                    String memo = memoEl==null?"":memoEl.getText();
                    addMap.put("patid",patid);
                    addMap.put("cardno",cardno1);
                    addMap.put("cardtype",cardtype1);
                    addMap.put("hzxm",hzxm);
                    addMap.put("sex",sex);
                    addMap.put("jzxh",jzxh);
                    addMap.put("czlx",czlx);
                    addMap.put("czyh",czyh);
                    addMap.put("lrrq",lrrq);
                    addMap.put("lsh",lsh);
                    addMap.put("fph",fph);
                    addMap.put("jyje",jyje);
                    addMap.put("yjye",yjye);
                    addMap.put("memo",memo);
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /**
     * @description 获取某人缴费主信息
     */
    public static HashMap<Object,Object> getPayList(String patid,String ksrq,String jsrq) throws Exception {

        String errMsg = "";
        List<HashMap<Object,Object>> list =new ArrayList<>();;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><patid>"+patid+"</patid>" +  	 //患者id
                "<ksrq>"+ksrq+"</ksrq>" +				 //开始日期
                "<jsrq>"+jsrq+"</jsrq>" +				 //结束日期
                "<czyh>"+czyh+"</czyh>" +				 //操作员号
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","606",inputXml);
        addHisLog("缴费主信息",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();


            if ("0".equals(errCode)){

                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();
                while (itemIterator.hasNext()){

                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();
                    //预约日期
                    Element sjhEl = item.element("sjh");
                    String sjh = sjhEl==null?"":sjhEl.getText();
                    //预约序号
                    Element fphEl = item.element("fph");
                    String fph = fphEl==null?"":fphEl.getText();
                    //预约号序
                    Element sfczyEl = item.element("sfczy");
                    String sfczy = sfczyEl==null?"":sfczyEl.getText();

                    Element sfrqEl = item.element("sfrq");
                    String sfrq = sfrqEl==null?"":sfrqEl.getText();

                    Element ksmcEl = item.element("ksmc");
                    String ksmc = ksmcEl==null?"":ksmcEl.getText();

                    Element brlxEl = item.element("brlx");
                    String brlx = brlxEl==null?"":brlxEl.getText();

                    Element fyzeEl = item.element("fyze");
                    String fyze = fyzeEl==null?"":fyzeEl.getText();

                    BigDecimal bd = new BigDecimal(fyze);
                    bd = bd.setScale(2, RoundingMode.HALF_UP);
                    fyze = bd.toString();

                    Element jlztEl = item.element("jlzt");
                    String jlzt = jlztEl==null?"":jlztEl.getText();

                    Element sflbEl = item.element("sflb");
                    String sflb = sflbEl==null?"":sflbEl.getText();

                    addMap.put("sjh",sjh);
                    addMap.put("fph",fph);
                    addMap.put("sfczy",sfczy);
                    addMap.put("sfrq",sfrq);
                    addMap.put("ksmc",ksmc);
                    addMap.put("brlx",brlx);
                    addMap.put("fyze",fyze);
                    addMap.put("jlzt",jlzt);
                    addMap.put("sflb",sflb);
                    list.add(addMap);
                }
            }
        }
        /*HashMap<Object,Object> addMap=new HashMap<>();
        addMap.put("sjh","123457");
        addMap.put("fph","2467213");
        addMap.put("sfczy","01");
        addMap.put("sfrq","20210501");
        addMap.put("ksmc","耳鼻喉科");
        addMap.put("brlx","自费");
        addMap.put("fyze","3.0000");
        addMap.put("jlzt","正常");
        addMap.put("sflb","挂号");
        list.add(addMap);*/
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }


    /**
     * @description 获取缴费明细信息
     */
    public static HashMap<Object,Object> getPayDetailList(String cxlb,String sjh) throws Exception {

        String errMsg = "";
        List<HashMap<Object,Object>> list =new ArrayList<>();;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><cxlb>"+cxlb+"</cxlb>" +  	 //查询类别传 1、按收费项目查询
                "<sjh>"+sjh+"</sjh>" +				 //收据号(604 交易返回
                "<czyh>"+czyh+"</czyh>" +			 //操作员号
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","605",inputXml);
        addHisLog("缴费明细信息",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();


            if ("0".equals(errCode)){

                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();
                while (itemIterator.hasNext()){

                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();
                    //
                    Element cfxhEl = item.element("cfxh");
                    String cfxh = cfxhEl==null?"":cfxhEl.getText();
                    //
                    Element cfrqEl = item.element("cfrq");
                    String cfrq = cfrqEl==null?"":cfrqEl.getText();
                    //
                    Element xmmcEl = item.element("xmmc");
                    String xmmc = xmmcEl==null?"":xmmcEl.getText();

                    Element cfslEl = item.element("cfsl");
                    String cfsl = cfslEl==null?"":cfslEl.getText();

                    Element cfdjEl = item.element("cfdj");
                    String cfdj = cfdjEl==null?"":cfdjEl.getText();

                    BigDecimal bd = new BigDecimal(cfdj);

                    BigDecimal total_price = bd.multiply(new BigDecimal(cfsl));
                    String totalPrice = total_price.setScale(2, RoundingMode.HALF_UP).toString();

                    bd = bd.setScale(2, RoundingMode.HALF_UP);
                    cfdj = bd.toString();

                    Element cfysEl = item.element("cfys");
                    String cfys = cfysEl==null?"":cfysEl.getText();

                    Element cfksEl = item.element("cfks");
                    String cfks = cfksEl==null?"":cfksEl.getText();

                    Element ypggEl = item.element("ypgg");
                    String ypgg = ypggEl==null?"":ypggEl.getText();

                    Element ypdwEl = item.element("ypdw");
                    String ypdw = ypdwEl==null?"":ypdwEl.getText();

                    Element czyhEl = item.element("czyh");
                    String czyh1 = czyhEl==null?"":czyhEl.getText();


                    addMap.put("totalPrice",totalPrice);
                    addMap.put("cfxh",cfxh);
                    addMap.put("cfrq",cfrq);
                    addMap.put("xmmc",xmmc);
                    addMap.put("cfsl",cfsl);
                    addMap.put("cfdj",cfdj);
                    addMap.put("cfys",cfys);
                    addMap.put("cfks",cfks);
                    addMap.put("ypgg",ypgg);
                    addMap.put("ypdw",ypdw);
                    addMap.put("czyh",czyh1);
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /**
     * @description 门诊充值
     */
    public static HashMap<Object, Object> mzRecharge(String patid, String czje, String czlx, String cardno,
                                                     String cardtype,String zzjjylsh, String czlsh, String tradeNo, String outerOrderNo,
                                                     String innerOrderNo, String paytype) throws Exception{

        String errMsg = "";
        List<HashMap<Object,Object>> list =new ArrayList<>();;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><patid>"+patid+"</patid>" +  	 //病人id
                "<czje>"+czje+"</czje>" +				 //充值金额
                "<czlx>"+czlx+"</czlx>" +				 //1 自助机现金，4 软 POS 戒扫码付
                "<cardno>"+cardno+"</cardno>" +			 //卡号
                "<cardtype>"+cardtype+"</cardtype>" +	 //卡类型：1、磁卡 2、保障卡 3、IC卡
                "<czyh>"+czyh+"</czyh>" +				 //操作员号
                "<zzjjylsh>"+zzjjylsh+"</zzjjylsh>" +	 //自助机交易流水号,902 接口返回，非卫宁付可为空
                "<czlsh>"+czlsh+"</czlsh>" +			 //充值流水号，每次交易必须唯一
                "<tradeNo>"+tradeNo+"</tradeNo>" +		 //三方支付流水号
                "<outerOrderNo>"+outerOrderNo+"</outerOrderNo>" +				 //商户订单号（商户传给支付平台的单据号，卫宁付和 zzjjylsh 相同）
                "<innerOrderNo>"+innerOrderNo+"</innerOrderNo>" +				 //平台订单号
                "<paytype>"+paytype+"</paytype>" +			 //支付方式（卫宁支付平台，8:支付宝 9:微信，其他支付方式如软POS，由 HIS 分配）
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","501",inputXml);
        addHisLog("充值",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();


            if ("0".equals(errCode)){

                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();
                while (itemIterator.hasNext()){

                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();
                    //
                    Element lshEl = item.element("lsh");
                    String lsh = lshEl==null?"":lshEl.getText();
                    //
                    Element lrrqEl = item.element("lrrq");
                    String lrrq = lrrqEl==null?"":lrrqEl.getText();
                    //
                    Element yjyeEl = item.element("yjye");
                    String yjye = yjyeEl==null?"":yjyeEl.getText();

                    Element kyjlshEl = item.element("kyjlsh");
                    String kyjlsh = kyjlshEl==null?"":kyjlshEl.getText();

                    Element kyjjeEl = item.element("kyjje");
                    String kyjje = kyjjeEl==null?"":kyjjeEl.getText();

                    Element kyjzffsEl = item.element("kyjzffs");
                    String kyjzffs = kyjzffsEl==null?"":kyjzffsEl.getText();

                    addMap.put("lsh",lsh);
                    addMap.put("lrrq",lrrq);
                    addMap.put("yjye",yjye);
                    addMap.put("kyjlsh",kyjlsh);
                    addMap.put("kyjje",kyjje);
                    addMap.put("kyjzffs",kyjzffs);
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /**
     * @description 获取住院患者信息
     */
    public static HashMap<Object, Object> getZYPatient(String cardno, String cardtype) throws Exception{

        String errMsg = "";
        List<HashMap<Object,Object>> list =new ArrayList<>();;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params>" +
                "<cardno>"+cardno+"</cardno>" +			 //卡号
                "<cardtype>"+cardtype+"</cardtype>" +	 //卡类型：1、磁卡 2、保障卡 3、IC卡
                "<czyh>"+czyh+"</czyh>" +				 //操作员号
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","804",inputXml);
        String errCode = map.get("errCode");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();


            if ("0".equals(errCode)){

                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();
                while (itemIterator.hasNext()){

                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();

                    Element patidEl = item.element("patid");
                    String patid = patidEl==null?"":patidEl.getText();

                    Element syxhEl = item.element("syxh");
                    String syxh = syxhEl==null?"":syxhEl.getText();

                    Element jsxhEl = item.element("jsxh");
                    String jsxh = jsxhEl==null?"":jsxhEl.getText();

                    Element hzxmEl = item.element("hzxm");
                    String hzxm = hzxmEl==null?"":hzxmEl.getText();

                    Element sfzhEl = item.element("sfzh");
                    String sfzh = sfzhEl==null?"":sfzhEl.getText();

                    Element lxdhEl = item.element("lxdh");
                    String lxdh = lxdhEl==null?"":lxdhEl.getText();


                    addMap.put("patid",patid);
                    addMap.put("jsxh",jsxh);
                    addMap.put("syxh",syxh);
                    addMap.put("hzxm",hzxm);
                    addMap.put("sfzh",sfzh);
                    addMap.put("lxdh",lxdh);
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /**
     * @description 住院充值
     */
    public static HashMap<Object, Object> zyRecharge(String czlx, String syxh, String jsxh, String czje,
                                                     String zzjjylsh, String czlsh, String tradeNo, String outerOrderNo, String innerOrderNo, String paytype) throws Exception{

        String errMsg = "";
        List<HashMap<Object,Object>> list =new ArrayList<>();;
        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params><czlx>"+czlx+"</czlx>" +  	 	 //1 自助机现金，4 软 POS 戒扫码付
                "<syxh>"+syxh+"</syxh>" +				 //首页序号
                "<jsxh>"+jsxh+"</jsxh>" +				 //结算序号
                "<czje>"+czje+"</czje>" +				 //充值金额
                "<czyh>"+czyh+"</czyh>" +				 //操作员号
                "<zzjjylsh>"+zzjjylsh+"</zzjjylsh>" +	 //自助机交易流水号,902 接口返回，非卫宁付可为空
                "<czlsh>"+czlsh+"</czlsh>" +			 //充值流水号，每次交易必须唯一
                "<tradeNo>"+tradeNo+"</tradeNo>" +		 //三方支付流水号
                "<outerOrderNo>"+outerOrderNo+"</outerOrderNo>" +				 //商户订单号（商户传给支付平台的单据号，卫宁付和 zzjjylsh 相同）
                "<innerOrderNo>"+innerOrderNo+"</innerOrderNo>" +				 //平台订单号
                "<paytype>"+paytype+"</paytype>" +			 //支付方式（卫宁支付平台，8:支付宝 9:微信，其他支付方式如软POS，由 HIS 分配）
                "</params></request>";
        HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","803",inputXml);
        String errCode = map.get("errCode");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();


            if ("0".equals(errCode)){

                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();
                while (itemIterator.hasNext()){

                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();
                    //
                    Element lshEl = item.element("lsh");
                    String lsh = lshEl==null?"":lshEl.getText();
                    //
                    Element lrrqEl = item.element("lrrq");
                    String lrrq = lrrqEl==null?"":lrrqEl.getText();
                    //
                    Element yjyeEl = item.element("yjye");
                    String yjye = yjyeEl==null?"":yjyeEl.getText();

                    Element czyhEl = item.element("czyh");
                    String czyh1 = czyhEl==null?"":czyhEl.getText();

                    Element zffsEl = item.element("zffs");
                    String zffs = zffsEl==null?"":zffsEl.getText();

                    addMap.put("lsh",lsh);
                    addMap.put("lrrq",lrrq);
                    addMap.put("yjye",yjye);
                    addMap.put("czyh",czyh1);
                    addMap.put("zffs",zffs);
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }


    public static void addHisLog(String title,String requestContent,String responseContent) {

        HashMap<Object,Object> map = new HashMap<Object,Object>();
        map.put("title", title);
        map.put("requestContent", requestContent);
        map.put("responseContent", responseContent);
        String url =  StatusDefine.IP_PORT+"/api/addHisLog";
        HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(map), headers) ;
        ResponseEntity<String> resp =restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
    }

    /** 自动开单（下园体检）
     * @param
     * @return
     */
    public static HashMap<Object,Object> zdkd(String patid, String ypdm, String lcxmdm, String lcxmbz,
                                              String itemname, String zxks) throws DocumentException {

//        List<HashMap<Object,Object>> list = new ArrayList<>();;

        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params>"+
                "<patid>" + patid + "</patid>"+     //患者唯一信息码
                "<ypdm>" + ypdm + "</ypdm>"+        //收费小项目代码
                "<lcxmdm>" + lcxmdm + "</lcxmdm>"+  //临床项目代码
                "<lcxmbz>" + lcxmbz + "</lcxmbz>"+  //临床项目标志
                "<ypsl>1</ypsl>"+                   //收费项目数量
                "<ksdm>1071</ksdm>"+                    //开单科室代码
                "<czyh></czyh>"+                    //操作员号
                "<ysdm>0568</ysdm>"+               //医生代码
                "<zxks>" + zxks + "</zxks>"+               //执行科室代码
                "</params></request>";
        HashMap<String,String> map = webserviceRequestTest(soapUrl,"999",inputXml);
       // addHisLog("自动开单",inputXml,map.get("outXml"));
        logger.info("inxml:" + inputXml);
        logger.info("outXml:" + map.get("outXml"));

        String errCode = map.get("errCode");
        String errMsg = map.get("errMsg");
        if("0".equals(errCode)){
            String outXml = map.get("outXml");
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            String code = codeElm.getText().toString();
            String msg = msgElm.getText().toString();

            errCode = codeElm.getText();
            errMsg = msgElm.getText();

        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        if(lcxmbz.equals("1")) {
            resultMap.put("xmdm",ypdm);
            resultMap.put("itemname", itemname);
        }else {
            resultMap.put("xmdm",lcxmdm);
            resultMap.put("itemname", itemname);
        }
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /** 建档(患者基本信息登记)
     * @param
     * @return
     */
    public static HashMap<Object,Object> addUser(String hzxm, String sfzh, String lxdh, String mzdm) throws DocumentException {

        String sex = sfzh.substring(sfzh.length()-2, sfzh.length()-1);
        String birth = sfzh.substring(6, 14);
        sex = Integer.valueOf(sex)%2==0?"女":"男";

        List<HashMap<Object,Object>> list = new ArrayList<>();;

        String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
                "<operid>01</operid><password></password><kfsdm></kfsdm>" +
                "<params>"+
                "<blh></blh>" +  					//病历号
                "<hzxm>"+hzxm+"</hzxm>" +  			//患者姓名
                "<czyh>0421</czyh>" +         		//操作员号
                "<cardtype>1</cardtype>" +      	//卡类型
                "<cardno>"+sfzh+"</cardno>" +		//卡号
                "<sex>"+sex+"</sex>" +				//性别
                "<sfzh>"+sfzh+"</sfzh>" +			//身份证号
                "<birth>"+birth+"</birth>" +		//出生年月
                "<ybdm>05</ybdm>" +          		//医保代码
                "<grybh></grybh>" +          		//个人编号
                "<dwybh></dwybh>" +          		//
                "<lxdz></lxdz>" +          			//
                "<lxdh>"+lxdh+"</lxdh>" +          	//联系电话
                "<yzbm></yzbm>" +          			//
                "<lxr></lxr>" +          			//
                "<pzh></pzh>" +          			//
                "<dwbm></dwbm>" +          			//
                "<dwmc></dwmc>" +          			//
                "<qxdm></qxdm>" +          			//
                "<ksrq></ksrq>" +          			//
                "<jsrq></jsrq>" +          			//
                "<ylxm></ylxm>" +          			//
                "<zddm></zddm>" +          			//
                "<zhje></zhje>" +          			//
                "<memo></memo>" +          			//
                "<sbh></sbh>" +          			//
                "<qtkh></qtkh>" +          			//
                "<czje>0</czje>" +          		//充值金额
                "<mzdm>" + mzdm + "</mzdm>" +          		//民族代码
                "</params></request>";
        HashMap<String,String> map = webserviceRequestTest(soapUrl,"102",inputXml);
        //addHisLog("预约",inputXml,map.get("outXml"));
        String errCode = map.get("errCode");
        String errMsg = map.get("errMsg");
        if("0".equals(errCode)){
            String outXml = map.get("outXml").replaceAll("\\p{C}", "");;
            Document document= DocumentHelper.parseText(outXml);

            Element rootElt = document.getRootElement();

            Iterator iterator = rootElt.elementIterator();

            Element codeElm = (Element) iterator.next();
            Element msgElm = (Element) iterator.next();

            String code = codeElm.getText().toString();
            String msg = msgElm.getText().toString();

            if (!"0".equals(code)){
                errCode=code;
                errMsg=msg;
            }
//            errCode = codeElm.getText();
//            errMsg = msgElm.getText();
            if ("0".equals(code)){
                Element Items = (Element) iterator.next();

                Iterator itemIterator = Items.elementIterator();

                while (itemIterator.hasNext()){
                    HashMap<Object,Object> addMap=new HashMap<>();
                    Element item = (Element) itemIterator.next();

                    Element patidEl = item.element("patid");
                    String patid = patidEl==null?"":patidEl.getText();

                    Element blhEl = item.element("blh");
                    String blh = blhEl==null?"":blhEl.getText();

                    Element lrrqEl = item.element("lrrq");
                    String lrrq = lrrqEl==null?"":lrrqEl.getText();

                    Element zhyeEl = item.element("zhye");
                    String zhye = zhyeEl==null?"":zhyeEl.getText();

                    Element gbfeiEl = item.element("gbfei");
                    String gbfei = gbfeiEl==null?"":gbfeiEl.getText();

                    addMap.put("patid",patid);
                    addMap.put("blh",blh);
                    addMap.put("lrrq",lrrq);
                    addMap.put("zhye",zhye);
                    addMap.put("gbfei",gbfei);
                    list.add(addMap);
                }
            }
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("list",list);
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

//
//    public static Result<List<FeeSettledInfoVO>> FindPatientFeeSettled(PatientFeeSettledQuery reqDTO) throws Exception {
//
//        Result<List<FeeSettledInfoVO>> resultObj=null;
//        String errCode="0";
//        String errMsg="";
//        try{
//            List<FeeSettledInfoVO> list = new ArrayList<>();;
//            String inputXml = "<request><partner>320582467223232</partner><sign></sign><timestamp>20170704000513</timestamp>" +
//                    "<operid>01</operid><password></password><kfsdm></kfsdm>" +
//                    "<params><patid>"+reqDTO.getPatid()+"</patid>" +  //患者id
//                    "<ksrq>"+reqDTO.getKsrq()+"</ksrq>" +    //开始日期
//                    "<jsrq>"+reqDTO.getJsrq()+"</jsrq>" +    //结束日期
//                    "<czyh>"+czyh+"</czyh></params></request>";
//            HashMap<String,String> map = webserviceRequest("http://12.0.2.1:10062/KWSService.asmx","606",inputXml);
//            addHisLog("根据患者id获取患者结算记录",inputXml,map.get("outXml"));
//            errCode = map.get("errCode");
//            if("0".equals(errCode)){
//                String outXml = map.get("outXml");
//                Document document= DocumentHelper.parseText(outXml);
//
//                Element rootElt = document.getRootElement();
//
//                Iterator iterator = rootElt.elementIterator();
//
//                Element codeElm = (Element) iterator.next();
//                Element msgElm = (Element) iterator.next();
//
//                String code = codeElm.getText();
//                String msg = msgElm.getText();
//
//                if (!"0".equals(code)){
//                    errCode=code;
//                    errMsg=msg;
//
//                    logger.error("返回结果异常----------"+ map.get("errCode"));
//                    resultObj=Result.returnError(errCode,errMsg);
//                }
//
//                if ("0".equals(code)){
//                    Element Items = (Element) iterator.next();
//
//                    Iterator itemIterator = Items.elementIterator();
//
//                    while (itemIterator.hasNext()){
//                        FeeSettledInfoVO feeSettledInfoVO=new FeeSettledInfoVO();
//
//                        Element item = (Element) itemIterator.next();
//
//                        Element sjhEl = item.element("sjh");
//                        String sjh = sjhEl==null?"":sjhEl.getText();
//
//                        Element fphEl = item.element("fph");
//                        String fph = fphEl==null?"":fphEl.getText();
//
//                        Element sfczyEl = item.element("sfczy");
//                        String sfczy = sfczyEl==null?"":sfczyEl.getText();
//
//                        Element sfrqEl = item.element("sfrq");
//                        String sfrq = sfrqEl==null?"":sfrqEl.getText();
//
//                        Element ksmcEl = item.element("ksmc");
//                        String ksmc = ksmcEl==null?"":ksmcEl.getText();
//
//                        Element brlxEl = item.element("brlx");
//                        String brlx = brlxEl==null?"":brlxEl.getText();
//
//                        Element fyzeEl = item.element("fyze");
//                        String fyze = fyzeEl==null?"":fyzeEl.getText();
//
//                        Element jlztEl = item.element("jlzt");
//                        String jlzt = jlztEl==null?"":jlztEl.getText();
//
//                        Element sflbEl = item.element("sflb");
//                        String sflb = sflbEl==null?"":sflbEl.getText();
//
//                        feeSettledInfoVO.setSjh(sjh);
//                        feeSettledInfoVO.setFph(fph);
//                        feeSettledInfoVO.setSfczy(sfczy);
//                        feeSettledInfoVO.setSfrq(sfrq);
//                        feeSettledInfoVO.setKsmc(ksmc);
//                        feeSettledInfoVO.setJlzt(jlzt);
//                        feeSettledInfoVO.setFyze(fyze);
//                        feeSettledInfoVO.setBrlx(brlx);
//                        feeSettledInfoVO.setSflb(sflb);
//                        list.add(feeSettledInfoVO);
//                    }
//                    resultObj = Result.returnSuccess(list);
//                }
//            }else {
//                logger.error("返回结果异常----------"+ map.get("errCode"));
//                errMsg="获取已结算信息失败";
//                resultObj=Result.returnError(errCode,errMsg);
//            }
//        } catch (Exception e) {
//            logger.error("返回结果异常----------"+e.toString());
//            errCode="999";
//            errMsg=e.getMessage();
//            resultObj=Result.returnError(errCode,errMsg);
//        }
//
//        return resultObj;
//    }
}
