package com.yihu.hos.rest.controllers;

import com.yihu.ehr.dbhelper.jdbc.DBHelper;
import com.yihu.hos.core.constants.CoreConstant;
import com.yihu.hos.core.datatype.DateUtil;
import com.yihu.hos.core.http.HttpClientKit;
import com.yihu.hos.rest.common.configuration.CrawlerConfiguration;
import com.yihu.hos.rest.common.configuration.QLCConfiguration;
import com.yihu.wsgw.api.ServiceBus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/20.
 */
@RestController
@RequestMapping(value = "/api/v1.0/qlc", produces = {"application/json;charset=UTF-8"})//
@Api(protocols = "http", value = "hibernate", hidden = false, description = "ȫ���̶Խӽӿ�")
public class QLCController {
    final Logger logger = LoggerFactory.getLogger(QLCController.class);
    private DBHelper dbHelper = new DBHelper();
    @Autowired
    private QLCConfiguration qlcConfiguration;
    @Autowired
    private CrawlerConfiguration crawlerConfiguration;

    /**
     * xml轉json
     *
     * @param xml
     * @return
     */
    private static String XML2JsonData(String xml) throws Exception {
        JSONArray object = new JSONArray();
        try {
            SAXReader saxReader = new SAXReader(false);
            Document document = DocumentHelper.parseText(xml);
            Element root = document.getRootElement();
            List<Element> elements = root.elements("Data");
            for (Element element : elements) {
                List<Element> elementTemps = element.elements();
                JSONObject jo = new JSONObject();
                for (Element elementTemp : elementTemps) {
                    if (elementTemp.hasContent()) {
                        jo.put(elementTemp.getName(), elementTemp.getText());
                    }
                }
                object.add(jo);
            }
        } catch (Exception e) {
            throw new Exception("返回的xml转JSON错误");
        }
        return object.toString();
    }

    /**
     * 院方_挂号事件推送
     * http://localhost:8081/gateway/transfer?api=17&param={EventType:"1",EventNo:"1",CardType:"1",CardNo:"6001",PatientId:"10001",HospitalId:"1026333"}&requestId="目前没用随便写"
     */
    @RequestMapping(value = "/queryUserInfo", method = RequestMethod.POST)
    @ApiOperation(value = "挂号事件推送", response = Object.class, produces = "application/json", notes = "挂号事件推送")
    public Map queryUserInfo(@ApiParam(value = "卡类型", required = true) @RequestParam(value = "CardType") String CardType,
                             @ApiParam(value = "卡号", required = true) @RequestParam(value = "CardNo") String CardNo,
                             @ApiParam(value = "医院ID", required = true) @RequestParam(value = "HospitalId") String HospitalId,
                             HttpServletRequest request) {
        String regDate;
        try {
            //判断参数是否为空
            isEmpty(CardType, "CardType is null");
            isEmpty(CardNo, "CardNo is null");
            isEmpty(HospitalId, "HospitalId is null");
            //得到挂号信息和病人基本信息
            regDate = getRegAndUserInfo(CardType, CardNo, HospitalId);

        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("CardType", CardType);
        paramMap.put("CardNo", CardNo);
        paramMap.put("ReportType", request.getParameter("ReportType"));
        paramMap.put("ReportId", request.getParameter("ReportId"));
        paramMap.put("State", request.getParameter("State"));
        paramMap.put("HospitalId", HospitalId);
        paramMap.put("RegDate", regDate);
        return paramMap;
    }

    /**
     * 院方_检查/检验报告单推送---数据说明
     * 检验：http://localhost:8081/gateway/transfer?api=18&param={CardType:"1",CardNo:"10003",ReportType:"1",ReportId:"123",State:"2",HospitalId:"1026333"}&requestId="目前没用随便写"
     * 检查：http://localhost:8081/gateway/transfer?api=18&param={CardType:"1",CardNo:"210003",ReportType:"2",ReportId:"201201",State:"2",HospitalId:"1026333"}&requestId="目前没用随便写"
     */
    @RequestMapping(value = "/patientInformation", method = RequestMethod.POST)
    @ApiOperation(value = "检查/检验报告单推送-", response = Object.class, produces = "application/json", notes = "检查/检验报告单推送-")
    public Map patientInformation(@ApiParam(value = "卡类型", required = true) @RequestParam(value = "CardType") String CardType,
                                  @ApiParam(value = "卡号", required = true) @RequestParam(value = "CardNo") String CardNo,
                                  @ApiParam(value = "报告单类型1检验报告2检查报告", required = true) @RequestParam(value = "ReportType") String ReportType,
                                  @ApiParam(value = "报告单号", required = true) @RequestParam(value = "ReportId") String ReportId,
                                  @ApiParam(value = "报告单状态1 报告未出 2报告已出", required = true) @RequestParam(value = "State") String State,
                                  @ApiParam(value = "医院ID", required = true) @RequestParam(value = "HospitalId") String HospitalId,
                                  @ApiParam(value = "就诊时间", required = true) @RequestParam(value = "RegDate") String RegDate) {
        Map<String, String> params = new HashMap<>();
        Object s = null;
        try {
            isEmpty(CardType, "CardType is null");
            isEmpty(CardNo, "CardNo is null");
            isEmpty(ReportType, "ReportType is null");
            isEmpty(ReportId, "ReportId is null");
            isEmpty(State, "State is null");
            isEmpty(HospitalId, "HospitalId is null");

            if ("1".equals(State)) {
                throw new Exception("报告未出，结束");
            }

//            String data ="<Req><TransactionCode>5001</TransactionCode><Data><CardNo>" + CardNo + "</CardNo><ReportId>" + ReportId + "</ReportId><ReportType>" + ReportType + "</ReportType></Data></Req>";
//            String apiparam = "{\"ChannelId\"=\"" + qlcConfiguration.getChannelId() + "\"," +
//                    "\"ParamType\"=1," +
//                    "\"Params\"=\"" + data + "\"," +
//                    "\"Guid\"=\"" + "00000001111111" + "\"," +
//                    "\"V\"=\"3.0.0\"," +
//                    "\"OutType\"=0," +
//                    "\"HospitalId\"=\"" + HospitalId + "\"}";
//            s = getRPCData(qlcConfiguration.getIp() + CoreConstant.COLON + qlcConfiguration.getPort(), qlcConfiguration.getAppId(), "ZEUS.HosHOPWs.GetReportInfo", apiparam);
            s = "<Resp> \n" +
                    "<RespCode>10000</RespCode> \n" +
                    "<RespMessage>成功</RespMessage>\n" +
                    "<Data>\n" +
                    "\t<ReportTitle>报告单2</ReportTitle>\n" +
                    "\t<ClinicCard>K5698012</ClinicCard>\n" +
                    "\t<UserType>0</UserType>\n" +
                    "\t<HosBedNo>6</HosBedNo>\n" +
                    "\t<SigningTime>2015-03-09 09:52:47.0</SigningTime>\n" +
                    "\t<Checker>审核人2</Checker>\n" +
                    "\t<BarCode>456456456</BarCode>\n" +
                    "\t<Sex>2</Sex>\n" +
                    "\t<PatientName>李四</PatientName>\n" +
                    "\t<SampleNumber>Yao77822</SampleNumber>\n" +
                    "\t<SampleType>2</SampleType>\n" +
                    "\t<ApplicationDepartment>口腔科</ApplicationDepartment>\n" +
                    "\t<SubmissionTime>2015-03-10 09:52:47.0</SubmissionTime>\n" +
                    "\t<Rmark>牙齿蛀牙</Rmark>\n" +
                    "\t<ReportTime>2015-03-12 09:52:47.0</ReportTime>\n" +
                    "\t<Inspector>检验员2</Inspector>\n" +
                    "\t<ItemNum>2</ItemNum>\n" +
                    "\t<ClinicNo>门诊号</ClinicNo>\n" +
                    "\t<HosUserNo>住院号</HosUserNo>\n" +
                    "\t<Data_1>\n" +
                    "\t\t\t<ItemDetailsName>xxx1</ItemDetailsName>\n" +
                    "\t\t\t<ResultValue>xxx1</ResultValue>\n" +
                    "\t\t\t<Unit>分</Unit>\n" +
                    "\t\t\t<ReferenceValues>100~250</ReferenceValues>\n" +
                    "\t\t\t<IsNormal></IsNormal>\n" +
                    "<ExFlag></ExFlag>\n" +
                    "<Range></Range>\n" +
                    "<SeFlag></SeFlag>\n" +
                    "<Words></Words>\n" +
                    "<GermName></GermName>\n" +
                    "<GrowStatus></GrowStatus>\n" +
                    "\t</Data_1>\n" +
                    "\t<Data_1>\n" +
                    "\t\t\t<ItemDetailsName>xxx2</ItemDetailsName>\n" +
                    "\t\t\t<ResultValue>xxx2</ResultValue>\n" +
                    "\t\t\t<Unit>分</Unit>\n" +
                    "\t\t\t<ReferenceValues>100~250</ReferenceValues>\n" +
                    "\t\t\t<IsNormal></IsNormal>\n" +
                    "<ExFlag></ExFlag>\n" +
                    "<Range></Range>\n" +
                    "<SeFlag></SeFlag>\n" +
                    "<Words></Words>\n" +
                    "<GermName></GermName>\n" +
                    "<GrowStatus></GrowStatus>\n" +
                    "\t</Data_1>\n" +
                    " </Data>\n" +
                    "</Resp>";
            //解析xml
            SAXReader saxReader = new SAXReader(false);
            Document document = DocumentHelper.parseText(s.toString());
            Element root = document.getRootElement();
            if ("10000".equals(root.element("RespCode").getText())) {
                ///分割數據
                List<Element> elements = root.elements("Data");
                JSONArray jsonArray1 = new JSONArray();//檢驗圖片報告單
                for (Element element : elements) {
                    JSONArray jsonArray = new JSONArray();//檢驗主表
                    List<Element> elementTemps = element.elements();
                    JSONObject jo = new JSONObject();
                    //檢驗主表
                    for (Element elementTemp : elementTemps) {
                        if (elementTemp.hasContent()) {
                            jo.put(elementTemp.getName(), elementTemp.getText());
                        }
                        //檢驗圖片報告單
                        if ("Data_1".equals(elementTemp.getName())) {
                            JSONObject jo1 = new JSONObject();
                            List<Element> elementTemps1 = elementTemp.elements();
                            for (Element elementTemp1 : elementTemps1) {
                                if (elementTemp1.hasContent()) {
                                    jo1.put(elementTemp1.getName(), elementTemp1.getText());
                                }
                            }
                            jsonArray1.add(jo1);
                        }
                    }
                    String evenNo = "";
                    if (StringUtils.isEmpty(jo.get("ClinicNo"))) {
                        evenNo = jo.get("HosUserNo").toString();
                    } else {
                        evenNo = jo.get("ClinicNo").toString();
                    }
                    jsonArray.add(jo);
                    if (ReportType.equals("1")) {
                        //检验报告
                        //上傳主表
                        addMongo("HDSD02_01", evenNo, CardNo, HospitalId, jsonArray);
                        //给每个细表都添加Even_No
                        for (Object o : jsonArray1) {
                            JSONObject jsonObject = (JSONObject) o;
                            jsonObject.put("EventNo", evenNo);
                            jsonObject.put("PatientId", CardNo);
                        }
                        //上傳細表
                        addMongo("HDSD02_03", evenNo, CardNo, HospitalId, jsonArray1);
                    } else {
                        for (Object o : jsonArray) {
                            JSONObject jsonObject = (JSONObject) o;
                            jsonObject.put("EventNo", evenNo);
                            jsonObject.put("PatientId", CardNo);
                        }
                        //检查报告
                        addMongo("HDSD01_01", evenNo, CardNo, HospitalId, jsonArray);
                    }
                    //出發採集上傳

                    String jsonObject = "{\"orgCode\":\"" + HospitalId + "\",\"eventNo\":\"" + evenNo + "\",\"patientId\":\"" + CardNo + "\",\"referenceTime\":\"" + RegDate + "\"}";
                    params.put("patient", jsonObject);

                    String uploadUrl = getUrl(crawlerConfiguration.getUploadIp(), crawlerConfiguration.getStoragePort(), crawlerConfiguration.getUploadApi());
                    String uploadInfo = HttpClientKit.post(uploadUrl, params).getBody();

                    monogoUrlValid(uploadInfo, "数据上传失败");
                }

            } else {
                throw new Exception(s.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return params;
    }

    /**
     * 得到挂号信息的病人基本信息入mongo
     *
     * @param CardType
     * @param CardNo
     * @param HospitalId
     * @return 返回就诊时间RegDate  yyyy-MM-dd HH:mm:ss
     * @throws Exception
     */
    private String getRegAndUserInfo(String CardType, String CardNo, String HospitalId) throws Exception {
        //-----------------------------得到挂号信息--start
        //拼凑RPC参数
        String regDate;
//        String param = "<Req><TransactionCode>5003</TransactionCode><Data><CardType>"+CardType+"</CardType><CardNo>"+CardNo+"</CardNo><IdCardNo></IdCardNo><OrderId></OrderId><ClinicCard></ClinicCard><TimeSlice></TimeSlice><StartTime></StartTime><EndTime></EndTime><RegFlag>2</RegFlag></Data></Req>";
//        String apiparam = "{\"ChannelId\"=\"" + qlcConfiguration.getChannelId() + "\"," +
//                "\"ParamType\"=1," +
//                "\"Params\"=\"" + param + "\"," +
//                "\"Guid\"=\"00000001111111\"," +
//                "\"V\"=\"3.0.0\"," +
//                "\"OutType\"=1," +
//                "\"HospitalId\"=\"" + HospitalId + "\"}";

//        Object s = getRPCData(qlcConfiguration.getIp() + CoreConstant.COLON + qlcConfiguration.getPort(), qlcConfiguration.getAppId(), "ZEUS.HosHOPWs.QueryRegInfo", apiparam);
        Object s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
                "<Resp> \n" +
                "<TransactionCode></TransactionCode> \n" +
                "<RespCode>10000</RespCode>\n" +
                "<RespMessage>成功</RespMessage>\n" +
                "<Data>\n" +
                "  <OrderId>1026333</OrderId>\n" +
                "  <IdCardNo>350181199101251517</IdCardNo>\n" +
                "  <BirthDay>1991-01-25</BirthDay>\n" +
                "  <Mobile>15805935227</Mobile>\n" +
                "  <Name>zlf</Name>\n" +
                "  <Sex>男</Sex>\n" +
                "  <Address></Address>\n" +
                "  <ClinicCard>6001</ClinicCard>\n" +
                "  <ClinicName></ClinicName>\n" +
                "  <DoctorName></DoctorName>\n" +
                "  <DoctorCode></DoctorCode>\n" +
                "  <DeptCode></DeptCode>\n" +
                "  <DeptName></DeptName>\n" +
                "  <PayFee></PayFee>\n" +
                "  <RegFee></RegFee>\n" +
                "  <ClinicFee></ClinicFee>\n" +
                "  <OtherFee></OtherFee>\n" +
                "  <OperatorId></OperatorId>\n" +
                "  <OperatorName></OperatorName>\n" +
                "  <RegDate>2016-08-23 17:00:00</RegDate>\n" +
                "  <TimeSlice></TimeSlice>\n" +
                "  <SqNo></SqNo>\n" +
                "  <Remark></Remark>\n" +
                "<RegFlag>true</RegFlag>\n" +
                "</Data>\n" +
                "</Resp>";
        //解析xml
        Document document = DocumentHelper.parseText(s.toString());
        Element root = document.getRootElement();
        String respCode = root.element("RespCode").getText();
        if ("10000".equals(respCode)) {
            //調用接口存入mongo--挂号信息
            Map<String, String> params = new HashMap<>();
            params.put("dataset", "HDSC01_02");
            params.put("data", XML2JsonData(root.asXML()));
            params.put("orgCode", HospitalId);
            String storageUrl = getUrl(crawlerConfiguration.getStorageIp(), crawlerConfiguration.getStoragePort(), crawlerConfiguration.getStorageApi());
            String storageInfo = HttpClientKit.post(storageUrl, params).getBody();
            monogoUrlValid(storageInfo, "挂号信息mongo入库失败");
            //得到时间 yyyy-MM-dd
            regDate = root.element("Data").element("RegDate").getText();
            //转换成 yyyy-MM-dd HH:mm:ss
            regDate = DateUtil.changeFormat(regDate, DateUtil.DEFAULT_DATE_YMD_FORMAT, DateUtil.DEFAULT_YMDHMSDATE_FORMAT);
        } else {
            throw new Exception(s.toString());
        }
        //-----------------------------得到挂号信息--end
        //-----------------------------得到人口学信息--start
        //拼凑RPC参数
//        param = "<Req><TransactionCode>5003</TransactionCode><Data><CardType>"+CardType+"</CardType><CardNo>"+CardNo+"</CardNo><Mobile>123</Mobile><PatientId></PatientId></Data></Req>";
//        apiparam = "{\"ChannelId\"=\"" + qlcConfiguration.getChannelId() + "\"," +
//                "\"ParamType\"=1," +
//                "\"Params\"=\"" + param + "\"," +
//                "\"Guid\"=\"00000001111111\"," +
//                "\"V\"=\"3.0.0\"," +
//                "\"OutType\"=1," +
//                "\"HospitalId\"=\"" + HospitalId + "\"}";
//        s = getRPCData(qlcConfiguration.getIp() + CoreConstant.COLON + qlcConfiguration.getPort(), qlcConfiguration.getAppId(), "ZEUS.HosHOPWs.QueryUserInfo", apiparam);
        s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
                "<Resp>\n" +
                "<TransactionCode></TransactionCode>\n" +
                "<RespMessage>成功</RespMessage>\n" +
                "<RespCode>10000</RespCode>\n" +
                "<Data>\n" +
                "<PatientId>10001</PatientId>\n" +
                "<ClinicCard>6001</ClinicCard>\n" +
                "<Name>zlf</Name>\n" +
                "<Mobile>123456</Mobile>\n" +
                "<McardNo>6001</McardNo>\n" +
                "<Address>Address</Address>\n" +
                "<Sex>男</Sex>\n" +
                "<Fee>100</Fee>\n" +
                "<BirthDay>1991.1.25</BirthDay>\n" +
                "<Country>中国</Country>\n" +
                "<Nation>汉</Nation>\n" +
                "<BloodCode1>A</BloodCode1>\n" +
                "<BloodCode2></BloodCode2>\n" +
                "<Allergens></Allergens>\n" +
                "<IdCardId>350181199101251517</IdCardId>\n" +
                "</Data>\n" +
                "</Resp>";
        //解析xml
        document = DocumentHelper.parseText(s.toString());
        root = document.getRootElement();
        respCode = root.element("RespCode").getText();
        Map<String, String> params = new HashMap<>();
        if ("10000".equals(respCode)) {
            //調用接口存入mongo--人口学信息
            params.put("dataset", "HDSA00_01");
            params.put("data", XML2JsonData(root.asXML()));
            params.put("orgCode", HospitalId);
            String storageUrl = getUrl(crawlerConfiguration.getStorageIp(), crawlerConfiguration.getStoragePort(), crawlerConfiguration.getStorageApi());
            String storageInfo = HttpClientKit.post(storageUrl, params).getBody();
            monogoUrlValid(storageInfo, "人口学信息mongo入库失败");
        } else {
            throw new Exception(s.toString());
        }
        //-----------------------------得到人口学信息--end
        return regDate;
    }

    private String addMongo(String tableName, String evenNo, String CardNo, String HospitalId, JSONArray jsonArray) throws Exception {
        Map<String, String> params = new HashMap<>();
        params.put("dataset", tableName);
        params.put("data", jsonArray.toString());
        params.put("orgCode", HospitalId);
        String storageUrl = getUrl(crawlerConfiguration.getStorageIp(), crawlerConfiguration.getStoragePort(), crawlerConfiguration.getStorageApi());
        String storageInfo = HttpClientKit.post(storageUrl, params).getBody();
        monogoUrlValid(storageInfo, "mongo入库失败");
        return storageInfo;
    }

    private String getOrgCodeBy(String orgCode) {
        List<org.json.JSONObject> listSYSTEM = dbHelper.query("select * from system_organization where qlc_org_code='" + orgCode + "'");
        return listSYSTEM.get(0).get("code").toString();
    }

    private Object XML2JsonData1(String xml) throws Exception {
        JSONArray object = new JSONArray();
        try {
            SAXReader saxReader = new SAXReader(false);
            Document document = DocumentHelper.parseText(xml);
            Element root = document.getRootElement();
            List<Element> elements = root.elements("Data");
            for (Element element : elements) {
                //得到data节点下面的data_1节点下面的数据
                List<Element> elementsData1s = element.elements();
                for (Element elementsData1 : elementsData1s) {
                    if (element.getName().equals("Data_1")) {
                        List<Element> elementTemps = elementsData1.elements();
                        JSONObject jo = new JSONObject();
                        for (Element elementTemp : elementTemps) {
                            if (elementTemp.hasContent()) {
                                jo.put(elementTemp.getName(), elementTemp.getText());
                            }
                        }
                        object.add(jo);
                    }
                }
            }
        } catch (Exception e) {
            throw new Exception("返回的xml转JSON错误");
        }
        return object.toString();
    }


//    public static void main(String[] args) throws Exception {
//        HttpClientUtil clientUtil=new HttpClientUtil();
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("dataset", "HDSA00_01");
//        params.put("data", XML2JsonData("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
//                "<Resg><TransactionCode>5003</TransactionCode><RespMessage>�ɹ�</RespMessage><RespCode>10000</RespCode><Data><Patient_id>1004295</Patient_id><Hdsd03_01_031>6123457</Hdsd03_01_031><Hdsd00_01_457>2015-04-06 10:00:00.0</Hdsd00_01_457><Jdsc01_02_02>ר������</Jdsc01_02_02><Jdsc01_02_03>025</Jdsc01_02_03><Hdsd00_01_561>��Ѫ���ڿ�</Hdsd00_01_561><Jdsc01_02_04>KH60066</Jdsc01_02_04><Jdsc01_02_05>л־��</Jdsc01_02_05><Card_no>D86124568</Card_no><Card_type>��ᱣ�Ͽ�</Card_type><Publish_org/><Hdsd00_01_560>A100</Hdsd00_01_560><Hdsd00_01_009>�Ż�</Hdsd00_01_009><Hdsd00_01_010>0592-5982866</Hdsd00_01_010><Hdsd00_01_281/><Hdsd00_01_541/><Hdsd00_01_551/><Hdsd00_01_552/><Hdsd00_01_558/><Jdsa00_01_010>70</Jdsa00_01_010><Jdsa00_01_011>83</Jdsa00_01_011><Jdsa00_01_012>178</Jdsa00_01_012><Jdsa00_01_013>98</Jdsa00_01_013><Jdsa00_01_014>70</Jdsa00_01_014><Jdsa00_01_015>16</Jdsa00_01_015><Jdsa00_01_016>36.5</Jdsa00_01_016><Hdsd00_02_052>2</Hdsd00_02_052><Hdsd00_01_570>08</Hdsd00_01_570><Hdsa00_01_018>F</Hdsa00_01_018><Hdsd00_01_540/></Data><Data><Patient_id>1004295</Patient_id><Hdsd03_01_031>6712345</Hdsd03_01_031><Hdsd00_01_457>2016-02-15 10:00:00.0</Hdsd00_01_457><Jdsc01_02_02>��ͨ����</Jdsc01_02_02><Jdsc01_02_03>012</Jdsc01_02_03><Hdsd00_01_561>�ε����</Hdsd00_01_561><Jdsc01_02_04>KH60056</Jdsc01_02_04><Jdsc01_02_05>������</Jdsc01_02_05><Card_no>D86124568</Card_no><Card_type>��ᱣ�Ͽ�</Card_type><Publish_org/><Hdsd00_01_560>A100</Hdsd00_01_560><Hdsd00_01_009>�Ż�</Hdsd00_01_009><Hdsd00_01_010>0592-5982866</Hdsd00_01_010><Hdsd00_01_281/><Hdsd00_01_541/><Hdsd00_01_551/><Hdsd00_01_552/><Hdsd00_01_558/><Jdsa00_01_010>73</Jdsa00_01_010><Jdsa00_01_011>88</Jdsa00_01_011><Jdsa00_01_012>122</Jdsa00_01_012><Jdsa00_01_013>88</Jdsa00_01_013><Jdsa00_01_014>75</Jdsa00_01_014><Jdsa00_01_015>17</Jdsa00_01_015><Jdsa00_01_016>36.5</Jdsa00_01_016><Hdsd00_02_052>2</Hdsd00_02_052><Hdsd00_01_570>08</Hdsd00_01_570><Hdsa00_01_018>F</Hdsa00_01_018><Hdsd00_01_540/></Data><Data><Patient_id>1004295</Patient_id><Hdsd00_01_002>�Ż�</Hdsd00_01_002><Hdsa00_01_016>01</Hdsa00_01_016><Hdsa00_01_017>150121199010219710</Hdsa00_01_017><Hdsa00_01_011>1</Hdsa00_01_011><Hdsa00_01_012>1990-10-21 00:00:00.0</Hdsa00_01_012><Hdsa00_11_051>��</Hdsa00_11_051><Hdsa00_01_013>156</Hdsa00_01_013><Hdsa00_01_014>1</Hdsa00_01_014><Hdsa00_01_035>365500</Hdsa00_01_035><Jdsa00_01_000>������ɳ�ؽ�ɳ���</Jdsa00_01_000><Hdsd00_01_007>̫�ſɿڿ��ֹ�˾</Hdsd00_01_007><Hdsd00_01_008>0592-5982866</Hdsd00_01_008><Hdsa00_01_021>584264572@qq.com</Hdsa00_01_021><Hdsd00_01_015>31</Hdsd00_01_015><Hdsd00_01_016>14</Hdsd00_01_016><Hdsd00_01_017>20</Hdsd00_01_017><Hdsa00_01_037>2010-07-02 00:00:00.0</Hdsa00_01_037><Hdsa00_01_048>4</Hdsa00_01_048><Hdsa00_01_049>4</Hdsa00_01_049><Hdsa00_01_050/></Data></Resg>"));
//        params.put("orgCode", "");
//        System.out.println(clientUtil.doPost("http://172.19.103.56:8080/datapush/datapush",params,null,null));
//
//        //  System.out.println(XML2Json("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
//        //        "<Resg><TransactionCode>5003</TransactionCode><RespMessage>�ɹ�</RespMessage><RespCode>10000</RespCode><Data><Patient_id>1004295</Patient_id><Hdsd03_01_031>6123457</Hdsd03_01_031><Hdsd00_01_457>2015-04-06 10:00:00.0</Hdsd00_01_457><Jdsc01_02_02>ר������</Jdsc01_02_02><Jdsc01_02_03>025</Jdsc01_02_03><Hdsd00_01_561>��Ѫ���ڿ�</Hdsd00_01_561><Jdsc01_02_04>KH60066</Jdsc01_02_04><Jdsc01_02_05>л־��</Jdsc01_02_05><Card_no>D86124568</Card_no><Card_type>��ᱣ�Ͽ�</Card_type><Publish_org/><Hdsd00_01_560>A100</Hdsd00_01_560><Hdsd00_01_009>�Ż�</Hdsd00_01_009><Hdsd00_01_010>0592-5982866</Hdsd00_01_010><Hdsd00_01_281/><Hdsd00_01_541/><Hdsd00_01_551/><Hdsd00_01_552/><Hdsd00_01_558/><Jdsa00_01_010>70</Jdsa00_01_010><Jdsa00_01_011>83</Jdsa00_01_011><Jdsa00_01_012>178</Jdsa00_01_012><Jdsa00_01_013>98</Jdsa00_01_013><Jdsa00_01_014>70</Jdsa00_01_014><Jdsa00_01_015>16</Jdsa00_01_015><Jdsa00_01_016>36.5</Jdsa00_01_016><Hdsd00_02_052>2</Hdsd00_02_052><Hdsd00_01_570>08</Hdsd00_01_570><Hdsa00_01_018>F</Hdsa00_01_018><Hdsd00_01_540/></Data><Data><Patient_id>1004295</Patient_id><Hdsd03_01_031>6712345</Hdsd03_01_031><Hdsd00_01_457>2016-02-15 10:00:00.0</Hdsd00_01_457><Jdsc01_02_02>��ͨ����</Jdsc01_02_02><Jdsc01_02_03>012</Jdsc01_02_03><Hdsd00_01_561>�ε����</Hdsd00_01_561><Jdsc01_02_04>KH60056</Jdsc01_02_04><Jdsc01_02_05>������</Jdsc01_02_05><Card_no>D86124568</Card_no><Card_type>��ᱣ�Ͽ�</Card_type><Publish_org/><Hdsd00_01_560>A100</Hdsd00_01_560><Hdsd00_01_009>�Ż�</Hdsd00_01_009><Hdsd00_01_010>0592-5982866</Hdsd00_01_010><Hdsd00_01_281/><Hdsd00_01_541/><Hdsd00_01_551/><Hdsd00_01_552/><Hdsd00_01_558/><Jdsa00_01_010>73</Jdsa00_01_010><Jdsa00_01_011>88</Jdsa00_01_011><Jdsa00_01_012>122</Jdsa00_01_012><Jdsa00_01_013>88</Jdsa00_01_013><Jdsa00_01_014>75</Jdsa00_01_014><Jdsa00_01_015>17</Jdsa00_01_015><Jdsa00_01_016>36.5</Jdsa00_01_016><Hdsd00_02_052>2</Hdsd00_02_052><Hdsd00_01_570>08</Hdsd00_01_570><Hdsa00_01_018>F</Hdsa00_01_018><Hdsd00_01_540/></Data><Data><Patient_id>1004295</Patient_id><Hdsd00_01_002>�Ż�</Hdsd00_01_002><Hdsa00_01_016>01</Hdsa00_01_016><Hdsa00_01_017>150121199010219710</Hdsa00_01_017><Hdsa00_01_011>1</Hdsa00_01_011><Hdsa00_01_012>1990-10-21 00:00:00.0</Hdsa00_01_012><Hdsa00_11_051>��</Hdsa00_11_051><Hdsa00_01_013>156</Hdsa00_01_013><Hdsa00_01_014>1</Hdsa00_01_014><Hdsa00_01_035>365500</Hdsa00_01_035><Jdsa00_01_000>������ɳ�ؽ�ɳ���</Jdsa00_01_000><Hdsd00_01_007>̫�ſɿڿ��ֹ�˾</Hdsd00_01_007><Hdsd00_01_008>0592-5982866</Hdsd00_01_008><Hdsa00_01_021>584264572@qq.com</Hdsa00_01_021><Hdsd00_01_015>31</Hdsd00_01_015><Hdsd00_01_016>14</Hdsd00_01_016><Hdsd00_01_017>20</Hdsd00_01_017><Hdsa00_01_037>2010-07-02 00:00:00.0</Hdsa00_01_037><Hdsa00_01_048>4</Hdsa00_01_048><Hdsa00_01_049>4</Hdsa00_01_049><Hdsa00_01_050/></Data></Resg>"));
//    }


    /**
     * 判断是否为空
     *
     * @param EventType
     * @param message
     * @throws Exception
     */
    private void isEmpty(String EventType, String message) throws Exception {
        if (StringUtils.isEmpty(EventType)) {
            throw new Exception(message);
        }
    }

    //{"successFlg":true,"message":"数据入库成功！（表：HDSA00_01）","errorCode":0}
    private boolean monogoUrlValid(String info, String message) throws Exception {
        JSONObject jo = JSONObject.fromObject(info);
        if ((Boolean) jo.get("successFlg")) {
            return true;
        } else {
            throw new Exception(message + " --- " + jo.get("message").toString());
        }
    }

    private String getRPCData(String url, String appId, String api, String apiparam) {
        return ServiceBus.getInstance(
                url,
                appId).call(api, apiparam);
    }

    private String getUrl(String ip, String port, String api) {
        String url = CoreConstant.HTTP + CoreConstant.COLON + CoreConstant.BACKSLASH +
                CoreConstant.BACKSLASH + ip + CoreConstant.COLON + port +
                CoreConstant.BACKSLASH + api;
        return url;
    }

}
