package com.yihu.wlyy.service.third.ehr;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.patient.PatientDao;
import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.util.HttpClientUtil;
import com.yihu.wlyy.util.IdCardUtil;
import com.yihu.wlyy.util.SystemConf;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hzp on 2016/10/25.
 * EHR档案服务
 */
@Service
public class EhrService extends BaseService {

    @Autowired
    PatientDao patientDao;

    @Autowired
    ObjectMapper  objectMapper;

    private String ehrUrl = SystemConf.getInstance().getEhrServices();   //EHR档案服务地址
    private String ehrBaseUrl = SystemConf.getInstance().getEhrServicesBase(); //EHR底层服务地址


    /**
     * 获取档案包含相关细表
     * @param profileId
     * @return
     */
    private List<String> containTables(String profileId)
    {
        List<String>  re = new ArrayList<>();
        String params = "{\"q\":\"profile_id:"+profileId+"\"}";
        String response = HttpClientUtil.get(ehrBaseUrl + "resources/query/sub_data?queryParams="+URLEncoder.encode(params), "UTF-8");
        JSONObject obj = new JSONObject(response);
        if(obj.has("detailModelList") && obj.getInt("totalCount")>0)
        {
            JSONArray array = obj.getJSONArray("detailModelList");
            if(array!=null && array.length()>0)
            {
               for(int i=0;i<array.length();i++)
               {
                   JSONObject item = array.getJSONObject(i);
                   String rowkey = item.getString("rowkey");
                   String table = rowkey.substring(rowkey.indexOf("$")+1);
                   table =  table.substring(0,table.indexOf("$"));

                   if(!re.contains(table))
                   {
                       re.add(table);
                   }
               }
            }
        }

        return re;
    }

    /**
     * 事件基本信息 (主表)
     */
    private JSONObject getEventBaseInfo(String profileId){
        JSONObject re = null;
        try {
            String params = "{\"q\":\"rowkey:"+profileId+"\"}";
            String url = ehrBaseUrl + "resources/query/master_data?queryParams="+URLEncoder.encode(params);
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject obj = new JSONObject(response);
            if(obj.has("detailModelList") && obj.getInt("totalCount")>0)    //obj.getBoolean("successFlg") &&
            {
                JSONArray array = obj.getJSONArray("detailModelList");
                if(array!=null && array.length()>0)
                {
                    re = array.getJSONObject(0);
                }
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        return re;
    }

    /*****************************************************************************************************************/
    /**
     * 获取门诊记录
     */
    public String getOutpatientRecord(String strSSID) {
        //通过ssid获取身份证
        Patient patient = patientDao.findBySsc(strSSID);
        if(patient!=null)
        {
            String url = ehrUrl + patient.getIdcard() +"/profile/medical_events?events_type=0&demographic_id=" + patient.getIdcard();
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONArray jsonArray = new JSONArray(response);
            JSONArray resultArray = new JSONArray();
            for (int i=0; i<jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("END_TIME",jsonObject.has("EHR_000065")?jsonObject.optString("EHR_000065"):jsonObject.optString("event_date")); //就诊时间
                item.put("ORG_NAME",jsonObject.optString("org_name")); //机构名称
                item.put("ORG_CODE",jsonObject.optString("org_code")); //机构代码
                item.put("UNIONSSID",strSSID); //社保卡号
                item.put("NAME",jsonObject.optString("EHR_000004")); //姓名
                item.put("EVENT",jsonObject.optString("event_no")); //事件号
                String profileId = jsonObject.getString("rowkey");
                item.put("SERIAL",profileId); //rowkey
                //包含模板
                JSONObject catalog = new JSONObject();
                catalog.put("0101","门诊基本诊疗信息");

                List<String> tables = containTables(profileId);

                //判断是否有费用
                if(tables.contains("HDSC01_07")){
                    catalog.put("0102","门诊费用明细");
                }
                //判断是否有检查报告
                if(tables.contains("HDSD01_01")){
                    catalog.put("0131","门诊检查报告单");
                }
                //判断是否有检验报告
                if(tables.contains("HDSD02_01")){
                    catalog.put("0121","门诊检验报告单");
                }
                //判断是否有门诊用药记录
                if(tables.contains("HDSC01_09")){
                    catalog.put("0141","门诊用药记录");
                }

                if(tables.contains("HDSD02_05")){
                    catalog.put("0116","门诊手术记录");
                }

                item.put("CATALOG",catalog); //档案类别
                resultArray.put(item);
                //{"EHR_000004":"都心青","EHR_000005":"1000222","client_id":"1FtXTrSL8D","EHR_000084":"2","event_type":"0","EHR_000082":"心血管内科","EHR_000083":"2","event_no":"20271619","EHR_000080":"KH91987","EHR_000081":"078","event_date":"2016-07-14T15:09:04Z","rowkey":"jkzl_1000222_20271619_1468480144000","Diagnosis":null,"EHR_000079":"徐小鹏","cda_version":"56395d75b854","EHR_000077":"2","EHR_000078":"08","EHR_000075":"70","EHR_000152":"2016-07-14 15:09:04","EHR_000076":"73","EHR_000153":"患者在厦门工作，以便就近就医","EHR_000073":"50","EHR_000150":"对患者转诊过程的描述","EHR_000074":"2","EHR_000151":"ZZ201511200030","EHR_000071":"100","EHR_000072":"36.7","patient_id":"1000222","EHR_000070":"160","org_code":"jkzl","EHR_000149":"厦门大学附属中山医院","EHR_000147":"肝胆外科","EHR_000068":"20271619","EHR_000145":"肝胆外科","EHR_000069":"2","EHR_000146":"福建省沙县医院","EHR_000066":"0592-5982816","EHR_000143":"今后治疗方案的详细描述","data_from":"线下采集","EHR_000067":"李碧玉","EHR_000064":"2","EHR_000065":"2016-07-14 15:09:04","EHR_000142":"医师对患者实施康复措施指导的描述","EHR_000062":"2","EHR_000063":"20002","EHR_000060":"15","demographic_id":"360101200006011131","EHR_000061":"2","org_name":"ESB对接","profile_type":"0","EHR_000019":"1","EHR_000017":"360101200006011131","EHR_000059":"2","EHR_000058":"F"}

            }
            return resultArray.toString();
        }
        else{
            return "[]";
        }
    }

    /**
     * 获取住院记录
     */
    public String getHospitalizationRecord(String strSSID) {
        //通过ssid获取身份证
        Patient patient = patientDao.findBySsc(strSSID);
        if(patient!=null)
        {
            String url = ehrUrl + patient.getIdcard() +"/profile/medical_events?events_type=1&demographic_id=" + patient.getIdcard();
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONArray jsonArray = new JSONArray(response);
            JSONArray resultArray = new JSONArray();
            for (int i=0; i<jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("END_TIME",jsonObject.has("EHR_000230")?jsonObject.optString("EHR_000230"):jsonObject.optString("event_date")); //入院时间
                item.put("ORG_NAME",jsonObject.optString("org_name")); //机构名称
                item.put("ORG_CODE",jsonObject.optString("org_code")); //机构代码
                item.put("UNIONSSID",strSSID); //社保卡号
                item.put("NAME",jsonObject.optString("EHR_000004")); //姓名
                item.put("EVENT",jsonObject.optString("event_no")); //事件号
                String profileId = jsonObject.optString("rowkey");
                item.put("SERIAL",profileId); //rowkey
                //包含模板
                JSONObject catalog = new JSONObject();
                catalog.put("0201","住院诊疗基本信息");
                //catalog.put("0212","入院病历记录");
                JSONObject master = getEventBaseInfo(profileId);
                if(master.has("EHR_000155"))
                {
                    catalog.put("0213","出院小结");
                }
                if(master.has("EHR_000255")) {
                    catalog.put("0217","死亡记录");
                }

                List<String> tables = containTables(profileId);


                if(tables.contains("HDSC02_04")){
                    catalog.put("0202","住院费用明细");
                }



                if(tables.contains("HDSC02_12")){
                    catalog.put("0214","长期医嘱");
                }

                if(tables.contains("HDSC02_11")){
                    catalog.put("0215","临时医嘱");
                }

                if(tables.contains("HDSD02_05")){
                    catalog.put("0216","住院手术记录");
                }


                if(tables.contains("HDSD02_01")){
                    catalog.put("0221","住院检验报告");
                }

                if(tables.contains("HDSD01_01")){
                    catalog.put("0231","住院检查报告");
                }

                if(tables.contains("HDSC01_09")){
                    catalog.put("0241","住院用药记录");
                }

                item.put("CATALOG",catalog); //档案类别
                resultArray.put(item);
                //{"EHR_000004":"都心青","EHR_000005":"1000222","client_id":"1FtXTrSL8D","EHR_000084":"2","event_type":"0","EHR_000082":"心血管内科","EHR_000083":"2","event_no":"20271619","EHR_000080":"KH91987","EHR_000081":"078","event_date":"2016-07-14T15:09:04Z","rowkey":"jkzl_1000222_20271619_1468480144000","Diagnosis":null,"EHR_000079":"徐小鹏","cda_version":"56395d75b854","EHR_000077":"2","EHR_000078":"08","EHR_000075":"70","EHR_000152":"2016-07-14 15:09:04","EHR_000076":"73","EHR_000153":"患者在厦门工作，以便就近就医","EHR_000073":"50","EHR_000150":"对患者转诊过程的描述","EHR_000074":"2","EHR_000151":"ZZ201511200030","EHR_000071":"100","EHR_000072":"36.7","patient_id":"1000222","EHR_000070":"160","org_code":"jkzl","EHR_000149":"厦门大学附属中山医院","EHR_000147":"肝胆外科","EHR_000068":"20271619","EHR_000145":"肝胆外科","EHR_000069":"2","EHR_000146":"福建省沙县医院","EHR_000066":"0592-5982816","EHR_000143":"今后治疗方案的详细描述","data_from":"线下采集","EHR_000067":"李碧玉","EHR_000064":"2","EHR_000065":"2016-07-14 15:09:04","EHR_000142":"医师对患者实施康复措施指导的描述","EHR_000062":"2","EHR_000063":"20002","EHR_000060":"15","demographic_id":"360101200006011131","EHR_000061":"2","org_name":"ESB对接","profile_type":"0","EHR_000019":"1","EHR_000017":"360101200006011131","EHR_000059":"2","EHR_000058":"F"}

            }
            return resultArray.toString();
        }
        else{
            return "[]";
        }
    }


    /**
     * 获取检查检验列表
     */
    public String getInspectionAndChecking(String strSSID)
    {
        //通过ssid获取身份证
        Patient patient = patientDao.findBySsc(strSSID);
        if(patient!=null) {
            String url = ehrUrl + patient.getIdcard() +"/profile/medical_events?demographic_id=" + patient.getIdcard();
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONArray jsonArray = new JSONArray(response);
            JSONArray resultArray = new JSONArray();
            for (int i=0; i<jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);


                JSONObject item = new JSONObject();
                item.put("END_TIME",jsonObject.optString("event_date")); //创建时间
                item.put("ORG_NAME",jsonObject.optString("org_name")); //机构名称
                item.put("ORG_CODE",jsonObject.optString("org_code")); //机构代码
                item.put("UNIONSSID",strSSID); //社保卡号
                item.put("NAME",jsonObject.optString("EHR_000004")); //姓名
                item.put("EVENT",jsonObject.optString("event_no")); //事件号
                String profileId = jsonObject.optString("rowkey");
                item.put("SERIAL",profileId); //rowkey

                //包含模板
                JSONObject catalog = new JSONObject();
                List<String> tables = containTables(profileId);
                //0门诊 1住院
                String eventType =  jsonObject.optString("event_type");
                if(eventType.equals("0"))
                {
                    //判断是否有检查报告
                    if(tables.contains("HDSD01_01")){
                        catalog.put("0131","门诊检查报告单");
                    }
                    //判断是否有检验报告
                    if(tables.contains("HDSD02_01")){
                        catalog.put("0121","门诊检验报告单");
                    }
                }
                else if(eventType.equals("1")){
                    if(tables.contains("HDSD02_01")){
                        catalog.put("0221","住院检验报告");
                    }

                    if(tables.contains("HDSD01_01")){
                        catalog.put("0231","住院检查报告");
                    }
                }

                if(catalog.length()>0) {
                    item.put("CATALOG", catalog); //档案类别
                    resultArray.put(item);
                }
            }
            return resultArray.toString();
        }
        else{
            return "[]";
        }
    }

    /**
     * 用药记录列表
     */
    public String getDrugsList(String strSSID) {
        //通过ssid获取身份证
        Patient patient = patientDao.findBySsc(strSSID);
        if(patient!=null) {
            String url = ehrUrl + patient.getIdcard() +"/profile/medical_events?demographic_id=" + patient.getIdcard();
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONArray jsonArray = new JSONArray(response);
            JSONArray resultArray = new JSONArray();
            for (int i=0; i<jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);


                JSONObject item = new JSONObject();
                item.put("END_TIME",jsonObject.optString("event_date")); //创建时间
                item.put("ORG_NAME",jsonObject.optString("org_name")); //机构名称
                item.put("ORG_CODE",jsonObject.optString("org_code")); //机构代码
                item.put("UNIONSSID",strSSID); //社保卡号
                item.put("NAME",jsonObject.optString("EHR_000004")); //姓名
                item.put("EVENT",jsonObject.optString("event_no")); //事件号
                String profileId = jsonObject.optString("rowkey");
                item.put("SERIAL",profileId); //rowkey

                //包含模板
                JSONObject catalog = new JSONObject();
                List<String> tables = containTables(profileId);
                //0门诊 1住院
                String eventType =  jsonObject.optString("event_type");

                //判断是否有检查报告
                if(tables.contains("HDSC01_09")){
                    if(eventType.equals("0")) {
                        catalog.put("0141", "门诊用药记录");
                    }
                    else{
                        catalog.put("0241","住院用药记录");
                    }
                }


                if(catalog.length()>0) {
                    item.put("CATALOG", catalog); //档案类别
                    resultArray.put(item);
                }
            }
            return resultArray.toString();
        }
        else{
            return "";
        }
    }

    /***************************************** 档案数据 ****************************************************************/
    /**
     * 获取档案数据
     * @return
     */
    public String getHealthData(String strSSID,String strEvent,String strCatalog,String strSerial)
    {
        System.out.print("ssid:"+strSSID+",rowkey:"+strSerial+",catalog:"+strCatalog+"\r\n");
        String re = "";
        try {
            /******************************** 主表数据 ********************************************************/
            if (strCatalog.equals("0101")) {     //门诊基本诊疗信息
                re =  get0101(strSSID,strSerial);
            }
            else if (strCatalog.equals("0201")) {   //住院诊疗基本信息
                re = get0201(strSSID,strSerial);
            }
            else if (strCatalog.equals("0213")) {   //出院小结
                re = get0213(strSSID,strSerial);
            }
            else if (strCatalog.equals("0217")) {   //死亡记录
                re = get0217(strSSID,strSerial);
            }

            /******************************** 细表数据 ********************************************************/
            else if (strCatalog.equals("0102")) {   //门诊费用明细
                re = get0102(strSSID,strSerial);
            }
            else if (strCatalog.equals("0131")) {   //门诊检查报告单
                re = getExamination(strSSID,strSerial,"0131");
            }
            else if (strCatalog.equals("0121")) {   //门诊检验报告单
                re = getLaboratory(strSSID,strSerial,"0121");
            }
            else if (strCatalog.equals("0141")) {   //门诊用药记录
                re = getDrug(strSSID,strSerial,"0141");
            }
            else if (strCatalog.equals("0116")) {   //门诊手术记录
                re = get0116(strSSID,strSerial);
            }
            else if (strCatalog.equals("0202")) {   //住院费用明细
                re = get0202(strSSID,strSerial);
            }
            else if (strCatalog.equals("0214")) {   //长期医嘱
                re = get0214(strSSID,strSerial);
            }
            else if (strCatalog.equals("0215")) {   //临时医嘱
                re = get0215(strSSID,strSerial);
            }
            else if (strCatalog.equals("0216")) {   //住院手术记录
                re = get0216(strSSID,strSerial);
            }
            else if (strCatalog.equals("0221")) {   //住院检验报告
                re = getLaboratory(strSSID,strSerial,"0221");
            }
            else if (strCatalog.equals("0231")) {   //住院检查报告
                re = getExamination(strSSID,strSerial,"0231");
            }
            else if (strCatalog.equals("0241")) {   //住院用药记录
                re = getDrug(strSSID,strSerial,"0241");
            }

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        System.out.print(re);
        return re;
    }

    //门诊基本诊疗信息
    public String get0101(String ssid,String profileId)
    {
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {
            //门诊症状
            String symptomUrl = ehrUrl + URLEncoder.encode("{demographic_id}")+"/outpatient/symptom?profile_id=" + profileId;
            String symptomResponse = HttpClientUtil.get(symptomUrl, "UTF-8");
            JSONObject symptomObj = new JSONObject(symptomResponse);
            String symptomString = "";
            if (symptomObj.getBoolean("successFlg") && symptomObj.getInt("totalCount") > 0) {
                JSONArray array = symptomObj.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject item = array.getJSONObject(i);
                        symptomString += "<item code=\"" + item.optString("EHR_001204") + "\" codeSystem=\"CV5101.27\">" + item.optString("EHR_001204_VALUE")+ "</item>\n";
                    }
                }
            }

            //门诊诊断
            String diagnosisUrl = ehrUrl + URLEncoder.encode("{demographic_id}")+"/outpatient/diagnosis?profile_id=" + profileId;
            String diagnosisResponse = HttpClientUtil.get(diagnosisUrl, "UTF-8");
            JSONObject diagnosisObj = new JSONObject(diagnosisResponse);
            String diagnosisString = "";
            if (diagnosisObj.getBoolean("successFlg") && diagnosisObj.getInt("totalCount") > 0) {
                JSONArray array = diagnosisObj.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject item = array.getJSONObject(i);
                        diagnosisString += "<item>\n" +
                                "            <icd code=\""+item.optString("EHR_000109") +"\" codeSystem=\"ICD-10\">"+item.optString("EHR_000109_VALUE") +"</icd>\n" +
                                "            <result code=\"" + item.optString("EHR_000110") + "\" codeSystem=\"CV5501.11\">"+item.optString("EHR_000110_VALUE")+"</result>\n" +
                                "            <prop code=\"" + item.optString("EHR_000107") + "\" codeSystem=\"STD_DIAGNOSIS_PROP\">"+item.optString("EHR_000107") +"</prop>\n" +
                                "</item>\n";
                    }
                }
            }

            String re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<ClinicalDocument>\n" +
                    "  <version code=\"2.0.0.0\" date=\"2009-12-01\">根据卫生部标准第一次修订</version>\n" +
                    "  <ehr code=\"0101\" codeSystem=\"STD_EHR\">门诊基本诊疗信息</ehr>\n" +
                    "  <title>门诊基本诊疗信息</title>\n" +
                    "  <org code=\"" + info.optString("org_code") + "\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name")+"</org>\n" +
                    "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                    "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                    "  <recordTarget>\n" +
                    "    <patient>\n" +
                    "      <id extension=\"" + ssid + "\"></id>\n" +
                    "      <name>" + info.optString("EHR_000004") + "</name>\n" +
                    "      <sex code=\"" + info.optString("EHR_000019") + "\" codeSystem=\"GB/T2261.1-2003\">" + info.optString("EHR_000019_VALUE")  + "</sex>\n" +
                    "      <birthDate>" + info.optString("EHR_000007") + "</birthDate>\n" +
                    "      <marriage code=\"" + info.optString("EHR_000014") + "\" codeSystem=\"GB/T 2261.2-2003\">" + info.optString("EHR_000014_VALUE")+ "</marriage>\n" +
                    "    </patient>\n" +
                    "  </recordTarget>\n" +
                    "  <component>\n" +
                    "    <section>\n" +
                    "      <code code=\"common\" codeSystem=\"\"  displayName=\"门诊基本诊疗信息\"/>\n" +
                    "      <entry>\n" +
                    "        <onsetTime>" + info.optString("EHR_000065") + "</onsetTime>\n" +
                    "        <treatTime>" + info.optString("EHR_000065") + "</treatTime>\n" +
                    "        <diagnosisDate>" + info.optString("EHR_000065") + "</diagnosisDate>\n" +
                    "        <reg>\n" +
                    "          <value>" + info.optString("EHR_000068") + "</value>\n" +
                    "          <type code=\"【挂号类别代码(字典STD_REG_TYPE)】\" codeSystem=\"STD_REG_TYPE\">【挂号类别名称】</type>\n" +
                    "        </reg>\n" +
                    "        <sec>\n" +
                    "          <value>保险号</value>\n" +
                    "          <type code=\"【保险类别代码(字典STD_SEC_TYPE)】\" codeSystem=\"STD_SEC_TYPE\">【保险类别名称】</type>\n" +
                    "        </sec>\n" +
                    "        <dept code=\"" + info.optString("EHR_000081") + "\" codeSystem=\"GB/T 17538-1998\">" + info.optString("EHR_000082") + "</dept>\n" +
                    "        <doctor>\n" +
                    "          <id extension=\"" + info.optString("EHR_000080") + "\"/>\n" +
                    "          <name>" + info.optString("EHR_000079") + "</name>\n" +
                    "          <title code=\"【职称代码(字典STD_TECH_TITLE)】\" codeSystem=\"GB/T 8561-1988\">【职称名称】</title>\n" +
                    "        </doctor>\n" +
                    "        <symptom>\n" +
                    symptomString +
                    "        </symptom>\n" +
                    "        <diagnosis>\n" +
                    diagnosisString +
                    "        </diagnosis>\n" +
                    "      </entry>\n" +
                    "    </section>\n" +
                    "  </component>\n" +
                    "</ClinicalDocument>";


            return re;
        }
        else{
            return "";
        }
    }

    //住院诊疗基本信息
    public String get0201(String ssid,String profileId)
    {
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {
            //住院症状
            String symptomUrl = ehrUrl + URLEncoder.encode("{demographic_id}")+"/hospitalized/symptom?profile_id=" + profileId;
            String symptomResponse = HttpClientUtil.get(symptomUrl, "UTF-8");
            JSONObject symptomObj = new JSONObject(symptomResponse);
            String symptomString = "";
            if (symptomObj.getBoolean("successFlg") && symptomObj.getInt("totalCount") > 0) {
                JSONArray array = symptomObj.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject item = array.getJSONObject(i);
                        symptomString += "<item code=\"" + item.optString("EHR_000301") + "\" codeSystem=\"CV5101.27\">" + item.optString("EHR_000302") + "</item>\n";
                    }
                }
            }

            //住院诊断
            String diagnosisUrl = ehrUrl + URLEncoder.encode("{demographic_id}")+"/hospitalized/diagnosis?profile_id=" + profileId;
            String diagnosisResponse = HttpClientUtil.get(diagnosisUrl, "UTF-8");
            JSONObject diagnosisObj = new JSONObject(diagnosisResponse);
            String diagnosisString = "";
            if (diagnosisObj.getBoolean("successFlg") && diagnosisObj.getInt("totalCount") > 0) {
                JSONArray array = diagnosisObj.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject item = array.getJSONObject(i);
                        diagnosisString +="<item>\n" +
                                        "      <icd code=\""+item.optString("EHR_000293")+"\" codeSystem=\"ICD-10\">"+item.optString("EHR_000295")+"</icd>\n" +
                                        "</item>\n";
                    }
                }
            }

            String re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<ClinicalDocument>\n" +
                    "  <version code=\"2.0.0.1\" date=\"2011-06-01\">根据卫生部标准第2次修订</version>\n" +
                    "  <ehr code=\"0201\" codeSystem=\"STD_EHR\">住院诊疗基本信息</ehr>\n" +
                    "  <title>住院诊疗基本信息</title>\n" +
                    "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                    "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                    "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                    "  <recordTarget>\n" +
                    "    <patient>\n" +
                    "      <id extension=\""+ssid+"\"></id>\n" +
                    "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                    "      <sex code=\"" + info.optString("EHR_000019") + "\" codeSystem=\"GB/T2261.1-2003\">" + info.optString("EHR_000019_VALUE") + "</sex>\n" +
                    "      <birthDate>" + info.optString("EHR_000007") + "</birthDate>\n" +
                    "      <marriage code=\"" + info.optString("EHR_000014") + "\" codeSystem=\"GB/T 2261.2-2003\">" + info.optString("EHR_000014_VALUE") + "</marriage>\n" +
                    "    </patient>\n" +
                    "  </recordTarget>\n" +
                    "  <component>\n" +
                    "    <section>\n" +
                    "      <code code=\"common\" codeSystem=\"\" displayName=\"住院诊疗基本信息\"/>\n" +
                    "      <entry>\n" +
                    "        <residence>\n" +
                    "          <serial>"+info.optString("EHR_000234") +"</serial>\n" +
                    "          <code>"+info.optString("EHR_000236")+"</code>\n" +
                    "        </residence>\n" +
                    "        <sec>\n" +
                    "          <type code=\"【保险类别(字典STD_SEC_TYPE)】\" codeSystem=\"STD_SEC_TYPE\">【保险类别名称】</type>\n" +
                    "          <value>"+info.optString("EHR_000232")+"</value>\n" +
                    "        </sec>\n" +
                    "        <in>\n" +
                    "          <time>"+info.optString("EHR_000230")+"</time>\n" +
                    "          <dept code=\""+info.optString("EHR_000228")+"\" codeSystem=\"GB/T 17538-1998\">"+info.optString("EHR_000229")+"</dept>\n" +
                    "          <bed>"+info.optString("EHR_000227")+"</bed>\n" +
                    "        </in>\n" +
                    "        <move>\n" +
                    //EHR转诊业务有问题
                    /*"          <item>\n" +
                    "            <time>转科时间</time>\n" +
                    "            <dept code=\"转科科室代码(字典STD_DEPT)\" codeSystem=\"GB/T 17538-1998\">转科后科室名称</dept>\n" +
                    "            <bed>转科后床号</bed>\n" +
                    "          </item>\n" +*/
                    "        </move>\n" +
                    "        <out>\n" +
                    "          <time>"+info.optString("EHR_000224")+"</time>\n" +
                    "          <dept code=\""+info.optString("EHR_000222")+"\" codeSystem=\"GB/T 17538-1998\">"+info.optString("EHR_000223")+"</dept>\n" +
                    "          <bed>"+info.optString("EHR_000221")+"</bed>\n" +
                    "        </out>\n" +
                    "        <onsetTime>"+info.optString("EHR_000230")+"</onsetTime>\n" +
                    "        <diagnosisDate>"+info.optString("EHR_000230")+"</diagnosisDate>\n" +
                    "        <cause code=\""+info.optString("EHR_000237")+"\" codeSystem=\"CV5401.04\">"+info.optString("EHR_000237_VALUE")+"</cause>\n" +
                    "       <symptom>\n" +
                    symptomString +
                    "        </symptom>\n" +
                    "        <illStatus code=\""+info.optString("EHR_000239")+"\" codeSystem=\"CV5502.18\">"+info.optString("EHR_000239_VALUE")+"</illStatus>\n" +
                    "        <infectiousStatus code=\""+info.optString("EHR_000238")+"\" codeSystem=\"STD_JUDGE\">"+info.optString("EHR_000238_VALUE")+"</infectiousStatus>\n" +
                    "        <inDiagnosis>\n" +
                    diagnosisString+
                    "        </inDiagnosis>\n" +
                    "        <outDiagnosis>\n" +
                    "          <item>\n" +
                    "            <icd code=\""+info.optString("EHR_000158")+"\" codeSystem=\"ICD-10\">"+info.optString("EHR_000159")+"</icd>\n" +
                    "            <result code=\""+info.optString("EHR_000166")+"\" codeSystem=\"CV5501.11\">"+info.optString("EHR_000166_VALUE")+"</result>\n" +
                    "            <prop code=\"【诊断性质代码(字典STD_DIAGNOSIS_PROP)】\" codeSystem=\"STD_DIAGNOSIS_PROP\">【诊断性质名称】</prop>\n" +
                    "          </item>\n" +
                    "        </outDiagnosis>\n" +
                    "        <deathTime>"+info.optString("EHR_000255")+"</deathTime>\n" +
                    "        <causeOfDeath code=\""+info.optString("EHR_000258")+"\" codeSystem=\"ICD-10\">"+info.optString("EHR_000258_VALUE")+"</causeOfDeath>\n" +
                    "      </entry>\n" +
                    "    </section>\n" +
                    "  </component>\n" +
                    "</ClinicalDocument>";

            return re;

        }
        else{
            return "";
        }
    }

    //出院小结
    public String get0213(String ssid,String profileId)
    {
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {

            String re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<ClinicalDocument>\n" +
                    "  <version code=\"2.0.0.0\" date=\"2009-12-01\">根据卫生部标准第一次修订</version>\n" +
                    "  <ehr code=\"0213\" codeSystem=\"STD_EHR\">出院小结</ehr>\n" +
                    "  <title>出院小结</title>\n" +
                    "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                    "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                    "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                    "  <recordTarget>\n" +
                    "    <patient>\n" +
                    "      <id extension=\""+ssid+"\"></id>\n" +
                    "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                    "      <sex code=\"" + info.optString("EHR_000019") + "\" codeSystem=\"GB/T2261.1-2003\">" + info.optString("EHR_000019_VALUE") + "</sex>\n" +
                    "      <nation code=\""+ info.optString("EHR_000016") + "\" codeSystem=\"GB 3304-1991\">"+ info.optString("EHR_000016_VALUE") +"</nation>\n" +
                    "      <work code=\""+ info.optString("EHR_000022") +"\" codeSystem=\"GB/T 6565-1999\">"+ info.optString("EHR_000022_VALUE") +"</work>\n" +
                    "      <birthAdd>"+ info.optString("EHR_000015") +"</birthAdd>\n" +
                    "      <birthDate>" + info.optString("EHR_000007") + "</birthDate>\n" +
                    "      <age>"+ IdCardUtil.getAgeForIdcard(info.optString("demographic_id") )+"</age>\n" +
                    "      <marriage code=\"" + info.optString("EHR_000014") + "\" codeSystem=\"GB/T 2261.2-2003\">" + info.optString("EHR_000014_VALUE") + "</marriage>\n" +
                    "      <address>【现住址】</address>\n" +
                    "      <organization>"+ info.optString("EHR_000009") +"</organization>\n" +
                    "    </patient>\n" +
                    "  </recordTarget>\n" +
                    "  <residentDoctor>\n" +
                    "    <time value=\""+ info.optString("EHR_000173") +"\"/>\n" +
                    "    <id extension=\""+ info.optString("EHR_000171") +"\"/>\n" +
                    "    <name>"+ info.optString("EHR_000172") +"</name>\n" +
                    "  </residentDoctor>\n" +
                    "  <inChargeDoctor>\n" +
                    "    <time value=\""+ info.optString("EHR_000169") +"\"/>\n" +
                    "    <id extension=\""+ info.optString("EHR_000167") +"\"/>\n" +
                    "    <name>"+ info.optString("EHR_000168") +"</name>\n" +
                    "  </inChargeDoctor>\n" +
                    "  <component>\n" +
                    "    <section>\n" +
                    "      <code code=\"common\" codeSystem=\"\" displayName=\"出院小结\"/>\n" +
                    "      <entry>\n" +
                    "        <residence>\n" +
                    "          <serial>"+ info.optString("EHR_000234") +"</serial>\n" +
                    "          <code>"+ info.optString("EHR_000236") +"</code>\n" +
                    "        </residence>\n" +
                    "        <inTime value=\""+ info.optString("EHR_000162") +"\"/>\n" +
                    "        <outTime value=\""+ info.optString("EHR_000155") +"\"/>\n" +
                    "        <inDept code=\""+ info.optString("EHR_000228") +"\" codeSystem=\"GB/T 2261.2-2003\">"+ info.optString("EHR_000229") +"</inDept>\n" +
                    "        <bed>"+ info.optString("EHR_000227") +"</bed>\n" +
                    "        <inHospitalDay>"+ info.optString("EHR_000170") +"</inHospitalDay>\n" +
                    "        <inDiagnosis code=\""+ info.optString("EHR_000163") +"\" codeSystem=\"ICD-10\">"+ info.optString("EHR_000164") +"</inDiagnosis>\n" +
                    "        <inCondition>"+ info.optString("EHR_000161") +"</inCondition>\n" +
                    "        <remedialCourse>"+ info.optString("EHR_000165") +"</remedialCourse>\n" +
                    "        <outCondition>"+ info.optString("EHR_000161") +"</outCondition>\n" +
                    "        <outDiagnosis code=\""+ info.optString("EHR_000158") +"\" codeSystem=\"ICD-10\">"+ info.optString("EHR_000159") +"\n" +
                    "        <outOrder>"+ info.optString("EHR_000157") +"</outOrder>\n" +
                    "        <remedialResult>"+ info.optString("EHR_000166") +"</remedialResult>\n" +
                    "      </entry>\n" +
                    "    </section>\n" +
                    "  </component>\n" +
                    "</ClinicalDocument>";

            return re;

        }
        else{
            return "";
        }
    }

    //死亡记录
    public String get0217(String ssid,String profileId)
    {
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {
            //死亡链（业务对接不上）

            String re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<ClinicalDocument>\n" +
                    "  <version code=\"2.0.0.0\" date=\"2009-12-01\">根据卫生部标准第一次修订</version>\n" +
                    "  <ehr code=\"0217\" codeSystem=\"STD_EHR\">死亡医学证明</ehr>\n" +
                    "  <title>死亡医学证明</title>\n" +
                    "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                    "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                    "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                    "  <recordTarget>\n" +
                    "    <patient>\n" +
                    "      <id extension=\""+ssid+"\"></id>\n" +
                    "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                    "      <sex code=\"" + info.optString("EHR_000019") + "\" codeSystem=\"GB/T2261.1-2003\">" + info.optString("EHR_000019_VALUE") + "</sex>\n" +
                    "      <nation code=\""+ info.optString("EHR_000016") + "\" codeSystem=\"GB 3304-1991\">"+ info.optString("EHR_000016_VALUE") +"</nation>\n" +
                    "      <work code=\""+ info.optString("EHR_000022") +"\" codeSystem=\"GB/T 6565-1999\">"+ info.optString("EHR_000022_VALUE") +"</work>\n" +
                    "      <birthAdd>"+ info.optString("EHR_000015") +"</birthAdd>\n" +
                    "      <birthDate>" + info.optString("EHR_000007") + "</birthDate>\n" +
                    "      <age>"+ IdCardUtil.getAgeForIdcard(info.optString("demographic_id") )+"</age>\n" +
                    "      <marriage code=\"" + info.optString("EHR_000014") + "\" codeSystem=\"GB/T 2261.2-2003\">" + info.optString("EHR_000014_VALUE") + "</marriage>\n" +
                    "      <degree code=\""+ info.optString("EHR_000020") + "\" codeSystem=\"GB/T 4658-1984\">"+ info.optString("EHR_000020_VALUE") + "</degree>\n" +
                    "      <country code=\""+ info.optString("EHR_000010") + "\" codeSystem=\"GB/T 2659-2000\">"+ info.optString("EHR_000010_VALUE") + "</country>\n" +
                    "      <idno>"+ info.optString("demographic_id") + "</idno>\n" +
                    "      <!--现住址-->\n" +
                    "      <address>\n" +
                    "        <type code=\"09\" codeSystem=\"CV0300.01\">现住址</type>\n" +
                    "        <adminDivision code=\"【行政区划代码(字典STD_ADMIN_DIVISION)】\" codeSystem=\"GB/T 2260-2007\">【行政区划名称】</adminDivision>\n" +
                    "        <country code=\"【国籍代码(字典STD_COUNTRY)】\" codeSystem=\"GB/T 2659-2000\">【国家名称】</country>\n" +
                    "        <state>【省份】</state>\n" +
                    "        <city>【市】</city>\n" +
                    "        <county>【区、县】</county>\n" +
                    "        <street>【街道、镇】</street>\n" +
                    "        <village>【村、街、路、弄】</village>\n" +
                    "        <houseNo>【门牌号】</houseNo>\n" +
                    "        <streetAddressLine>"+ info.optString("EHR_000241") +"</streetAddressLine>\n" +
                    "        <postalCode>【邮政编码】</postalCode>\n" +
                    "      </address>\n" +
                    "      <!--户口地址-->\n" +
                    "      <registeredAddress>\n" +
                    "        <type code=\"01\" codeSystem=\"CV0300.01\">户口地址</type>\n" +
                    "        <adminDivision code=\"【行政区划代码(字典STD_ADMIN_DIVISION)】\" codeSystem=\"GB/T 2260-2007\">【行政区划名称】</adminDivision>\n" +
                    "        <country code=\""+ info.optString("EHR_000010") +"\" codeSystem=\"GB/T 2659-2000\">"+ info.optString("EHR_000010_VALUE") +"</country>\n" +
                    "        <state>【省份】</state>\n" +
                    "        <city>【市】</city>\n" +
                    "        <county>【区、县】</county>\n" +
                    "        <street>【街道、镇】</street>\n" +
                    "        <village>【村、街、路、弄】</village>\n" +
                    "        <houseNo>【门牌号】</houseNo>\n" +
                    "        <streetAddressLine>"+ info.optString("EHR_000013") +"</streetAddressLine>\n" +
                    "        <postalCode>"+ info.optString("EHR_000012") +"</postalCode>\n" +
                    "      </registeredAddress>\n" +
                    "      <organization telephone=\"【联系电话】\" postalCode=\"【邮政编码】\" address=\"【地址】\">"+ info.optString("EHR_000009") +"</organization>\n" +
                    "    </patient>\n" +
                    "  </recordTarget>\n" +
                    "  <contactPerson>\n" +
                    "    <type>"+ info.optString("EHR_000252") +"</type>\n" +
                    "    <name>"+ info.optString("EHR_000253") +"</name>\n" +
                    "    <telephone>【联系人：电话】</telephone>\n" +
                    "    <address>"+ info.optString("EHR_000251") +"</address>\n" +
                    "  </contactPerson>\n" +
                    "  <author>\n" +
                    "    <time>"+info.optString("EHR_000262")+"</time>\n" +
                    "    <id extension=\""+info.optString("EHR_000263")+"\"/>\n" +
                    "    <name>"+info.optString("EHR_000264")+"</name>\n" +
                    "  </author>\n" +
                    "  <component>\n" +
                    "    <section>\n" +
                    "      <!--档案子类别，用于类别的扩展，code子类别编码；displayName名称。当前为固定值-->\n" +
                    "      <code code=\"common\" codeSystem=\"\" displayName=\"死亡医学证明\"/>\n" +
                    "      <entry>\n" +
                    "        <inTime>"+info.optString("EHR_000250")+"</inTime>\n" +
                    "        <deathTime>"+info.optString("EHR_000255")+"</deathTime>\n" +
                    "        <placeType code=\""+info.optString("EHR_000254")+"\" codeSystem=\"CV8500.01\">"+info.optString("EHR_000254_VALUE")+"</placeType>\n" +
                    "        <causeOfDeath>\n" +
                   /* "          <causeOfDeathA>\n" +
                    "            <type code=\"死因链代码(字典STD_DEATH_CAUSE_CHAIN)：固定值1\" codeSystem=\"CV8500.05\">死因链名称</type>\n" +
                    "            <icd10 code=\"疾病ICD10编码(字典STD_ICD)\" codeSystem=\"ICD-10\">疾病名称</icd10>\n" +
                    "            <interval code=\"时间间隔单位类别(字典STD_TIME_INTERVAL_UNIT)：固定值4\">发病到死亡的大概时间间隔(单位：日)</interval>\n" +
                    "            <detail>直接死亡原因A(导致死亡的最后疾病或情况)</detail>\n" +
                    "          </causeOfDeathA>\n" +
                    "          <causeOfDeathB>\n" +
                    "            <type code=\"死因链代码(字典STD_DEATH_CAUSE_CHAIN)：固定值2\" codeSystem=\"CV8500.05\">死因链名称</type>\n" +
                    "            <icd10 code=\"疾病ICD10编码(字典STD_ICD)\" codeSystem=\"ICD-10\">疾病名称</icd10>\n" +
                    "            <interval code=\"时间间隔单位类别(字典STD_TIME_INTERVAL_UNIT)：固定值4\">发病到死亡的大概时间间隔(单位：日)</interval>\n" +
                    "            <detail>直接死亡原因B(引起A的最后疾病或情况)</detail>\n" +
                    "          </causeOfDeathB>\n" +
                    "          <causeOfDeathC>\n" +
                    "            <type code=\"死因链代码(字典STD_DEATH_CAUSE_CHAIN)：固定值3\" codeSystem=\"CV8500.05\">死因链名称</type>\n" +
                    "            <icd10 code=\"疾病ICD10编码(字典STD_ICD)\" codeSystem=\"ICD-10\">疾病名称</icd10>\n" +
                    "            <interval code=\"时间间隔单位类别(字典STD_TIME_INTERVAL_UNIT)：固定值4\">发病到死亡的大概时间间隔(单位：日)</interval>\n" +
                    "            <detail>直接死亡原因C(引起B的最后疾病或情况)</detail>\n" +
                    "          </causeOfDeathC>\n" +
                    "          <causeOfDeathD>\n" +
                    "            <type code=\"死因链代码(字典STD_DEATH_CAUSE_CHAIN)：固定值4\" codeSystem=\"CV8500.05\">死因链名称</type>\n" +
                    "            <icd10 code=\"疾病ICD10编码(字典STD_ICD)\" codeSystem=\"ICD-10\">疾病名称</icd10>\n" +
                    "            <interval code=\"时间间隔单位类别(字典STD_TIME_INTERVAL_UNIT)：固定值4\">发病到死亡的大概时间间隔(单位：日)</interval>\n" +
                    "            <detail>直接死亡原因D(引起C的最后疾病或情况)</detail>\n" +
                    "          </causeOfDeathD>\n" +
                    "          <causeOfDeathOther1>\n" +
                    "            <type code=\"死因链代码(字典STD_DEATH_CAUSE_CHAIN)：固定值9\" codeSystem=\"CV8500.05\">死因链名称</type>\n" +
                    "            <interval code=\"时间间隔单位类别(字典STD_TIME_INTERVAL_UNIT)：固定值4\">发病到死亡的大概时间间隔(单位：日)</interval>\n" +
                    "            <detail>促进死亡，但与导致死亡的疾病或情况无关的其他重要情况1</detail>\n" +
                    "          </causeOfDeathOther1>\n" +
                    "          <causeOfDeathOther2>\n" +
                    "            <type code=\"死因链代码(字典STD_DEATH_CAUSE_CHAIN)：固定值9\" codeSystem=\"CV8500.05\">死因链名称</type>\n" +
                    "            <interval code=\"时间间隔单位类别(字典STD_TIME_INTERVAL_UNIT)：固定值4\">发病到死亡的大概时间间隔(单位：日)</interval>\n" +
                    "            <detail>促进死亡，但与导致死亡的疾病或情况无关的其他重要情况2</detail>\n" +
                    "          </causeOfDeathOther2>\n" +
                    "          <causeOfDeathOther3>\n" +
                    "            <type code=\"死因链代码(字典STD_DEATH_CAUSE_CHAIN)：固定值9\" codeSystem=\"CV8500.05\">死因链名称</type>\n" +
                    "            <interval code=\"时间间隔单位类别(字典STD_TIME_INTERVAL_UNIT)：固定值4\">发病到死亡的大概时间间隔(单位：日)</interval>\n" +
                    "            <detail>促进死亡，但与导致死亡的疾病或情况无关的其他重要情况3</detail>\n" +
                    "          </causeOfDeathOther3>\n" +*/
                    "        </causeOfDeath>\n" +
                    "        <deathOrg code=\""+info.optString("EHR_000256")+"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("EHR_000257")+"</deathOrg>\n" +
                    "        <deathcatalog code=\""+info.optString("EHR_000258")+"\" codeSystem=\"STD_DEAD_CATALOG\">"+info.optString("EHR_000258_VALUE")+"</deathcatalog>\n" +
                    "        <highestDiagnosis>\n" +
                    "          <level code=\""+info.optString("EHR_000260")+"\" codeSystem=\"CV8500.03\">"+info.optString("EHR_000260_VALUE")+"</level>\n" +
                    "          <org code=\""+info.optString("EHR_000259")+"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("EHR_000259")+"</org>\n" +
                    "          <diagnosisGist code=\""+info.optString("EHR_000261")+"\" codeSystem=\"CV8500.02\">"+info.optString("EHR_000261")+"</diagnosisGist>\n" +
                    "        </highestDiagnosis>\n" +
                    "        <remedialCourse>"+info.optString("EHR_000266")+"</remedialCourse>\n" +
                    "      </entry>\n" +
                    "    </section>\n" +
                    "  </component>\n" +
                    "</ClinicalDocument>";

            return re;

        }
        else{
            return "";
        }
    }

    //检查报告单(取第一条)
    public String getExamination(String ssid,String profileId,String catalog)
    {
        String re = "";
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);
        String catalogName = "";
        if(catalog.equals("0131"))
        {
            catalogName = "门诊检查报告单";
        }
        else if(catalog.equals("0231"))
        {
            catalogName = "住院检查报告";
        }

        if(info!=null) {
            //获取检查报告
            String url = ehrUrl + URLEncoder.encode("{demographic_id}")+"/examination?profile_id=" + profileId;
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject json = new JSONObject(response);
            if (json.getBoolean("successFlg") && json.getInt("totalCount") > 0) {
                JSONArray array = json.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    JSONObject obj = array.getJSONObject(0);

                    re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                            "<ClinicalDocument>\n" +
                            "  <version code=\"2.0.0.0\" date=\"2009-12-01\">根据卫生部标准第一次修订</version>\n" +
                            "  <ehr code=\""+catalog+"\" codeSystem=\"STD_EHR\">"+catalogName+"</ehr>\n" +
                            "  <title>"+catalogName+"</title>\n" +
                            "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                            "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                            "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                            "  <recordTarget>\n" +
                            "    <patient>\n" +
                            "      <id extension=\""+ssid+"\"></id>\n" +
                            "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                            "      <sex code=\"" + info.optString("EHR_000019") + "\" codeSystem=\"GB/T2261.1-2003\">" + info.optString("EHR_000019_VALUE") + "</sex>\n" +
                            "      <birthDate>" + info.optString("EHR_000007") + "</birthDate>\n" +
                            "      <marriage code=\"" + info.optString("EHR_000014") + "\" codeSystem=\"GB/T 2261.2-2003\">" + info.optString("EHR_000014_VALUE") + "</marriage>\n" +
                            "    </patient>\n" +
                            "  </recordTarget>\n" +
                            "  <author>\n" +
                            "    <time>"+obj.optString("EHR_000318")+"</time>\n" +
                            "    <id extension=\""+obj.optString("EHR_000345")+"\"/>\n" +
                            "    <name>【报告者：姓名】</name>\n" +
                            "  </author>\n" +
                            "  <authenticator>\n" +
                            "    <time>"+obj.optString("EHR_000341")+"</time>\n" +
                            "    <id extension=\""+obj.optString("EHR_000340")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000342")+"</name>\n" +
                            "  </authenticator>\n" +
                            "  <participant>\n" +
                            "    <time>"+obj.optString("EHR_000338")+"</time>\n" +
                            "    <id extension=\""+obj.optString("EHR_000337")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000339")+"</name>\n" +
                            "    <dept>"+obj.optString("EHR_000336")+"</dept>\n" +
                            "  </participant>\n" +
                            "  <inFulfillmentOf>\n" +
                            "    <order>\n" +
                            "      <id>"+obj.optString("EHR_000323")+"</id>\n" +
                            "      <priority code=\""+obj.optString("EHR_000324")+"\" codeSystem=\"HL7 27\">"+obj.optString("EHR_000324_VALUE")+"</priority>\n" +
                            "    </order>\n" +
                            "  </inFulfillmentOf>\n" +
                            "  <component>\n" +
                            "    <section>\n" +
                            "      <code code=\""+obj.optString("EHR_000330")+"\" displayName=\""+obj.optString("EHR_000330_VALUE")+"\" bodyPartCode=\""+obj.optString("EHR_000326")+"\" bodyPartName=\""+obj.optString("EHR_000326_VALUE")+"\" examCode=\""+obj.optString("EHR_000333")+"\" examName=\""+obj.optString("EHR_000333_VALUE")+"\"/>\n" +
                            "      <recognition code=\""+obj.optString("EHR_000334")+"\" codesystem=\"STD_LAB_EXAM_RECOGNITION\"/>\n" +
                            "      <entry>\n" +
                            "        <performer>\n" +
                            "          <assignedEntity>"+obj.optString("EHR_000327")+"</assignedEntity>\n" +
                            "          <name>"+obj.optString("EHR_000329")+"</name>\n" +
                            "          <time>"+obj.optString("EHR_000328")+"</time>\n" +
                            "        </performer>\n" +
                            "        <playingDevice>\n" +
                            "          <name>【检查设备名称】</name>\n" +
                            "        </playingDevice>\n" +
                            "        <clinicDiagnosis>"+obj.optString("EHR_000335")+"</clinicDiagnosis>\n" +
                            "        <patientCondition>"+obj.optString("EHR_000319")+"</patientCondition>\n" +
                            "        <examCause>"+obj.optString("EHR_000331")+"</examCause>\n" +
                            "        <description>"+obj.optString("EHR_000348")+"</description>\n" +
                            "        <isAbnormal code=\""+obj.optString("EHR_000344")+"\" codeSystem=\"\">"+(obj.optString("EHR_000344").equals("Y")?"Y":"N")+"</isAbnormal>\n" +
                            "      </entry>\n" +
                            "    </section>\n" +
                            "  </component>\n" +
                            "</ClinicalDocument>";
                }
            }
        }

        return re;
    }

    //检验报告单(取第一条)
    public String getLaboratory(String ssid,String profileId,String catalog)
    {
        String re = "";
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);
        String catalogName = "";
        if(catalog.equals("0121"))
        {
            catalogName = "门诊检验报告单";
        }
        else if(catalog.equals("0221"))
        {
            catalogName = "住院检验报告";
        }

        if(info!=null) {
            //获取检查报告
            String url = ehrUrl + URLEncoder.encode("{demographic_id}")+"/laboratory?profile_id=" + profileId;
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject json = new JSONObject(response);
            if (json.getBoolean("successFlg") && json.getInt("totalCount") > 0) {
                JSONArray array = json.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    JSONObject obj = array.getJSONObject(0);

                    //检验报告项目
                    String observationUrl = ehrUrl + URLEncoder.encode("{demographic_id}")+"/laboratory/project?profile_id=" + profileId;
                    String observationResponse = HttpClientUtil.get(observationUrl, "UTF-8");
                    JSONObject observationJson = new JSONObject(observationResponse);
                    String observationString  = "";
                    if (observationJson.getBoolean("successFlg") && observationJson.getInt("totalCount") > 0) {
                        JSONArray observationArray = observationJson.getJSONArray("detailModelList");
                        if (observationArray != null && observationArray.length() > 0) {
                            for (int i = 0; i < observationArray.length(); i++) {
                                JSONObject item = observationArray.getJSONObject(i);
                                observationString +="<observation>\n" +
                                        "          <code code=\""+item.optString("EHR_000392")+"\" codeSystem=\"2.16.840.1.113883.6.1\" codeSystemName=\"LOINC\" displayName=\""+item.optString("EHR_000392_VALUE")+"\"/>\n" +
                                        "          <recognition code=\""+item.optString("EHR_000389")+"\" codesystem=\"STD_LAB_EXAM_RECOGNITION\"/>\n" +
                                        "          <type code=\""+item.optString("EHR_000386")+"\" codeSystem=\"STD_OBSERVATION_TYPE\">"+item.optString("EHR_000386_VALUE")+"</type>\n" +
                                        "          <value>"+item.optString("EHR_000387")+"</value>\n" +
                                        "          <unit>"+item.optString("EHR_000383")+"</unit>\n" +
                                        "          <referenceRange>\n" +
                                        "            <observationRange>\n" +
                                        "              <notes>"+item.optString("EHR_000382")+"</notes>\n" +
                                        "              <low>\n" +
                                        "                <value>"+item.optString("EHR_000391")+"</value>\n" +
                                        "                <unit>"+item.optString("EHR_000383")+"</unit>\n" +
                                        "              </low>\n" +
                                        "              <high>\n" +
                                        "                <value>"+item.optString("EHR_000390")+"</value>\n" +
                                        "                <unit>"+item.optString("EHR_000383")+"</unit>\n" +
                                        "              </high>\n" +
                                        "            </observationRange>\n" +
                                        "          </referenceRange>\n" +
                                        "          <effectiveTime>"+item.optString("EHR_000384")+"</effectiveTime>\n" +
                                        "          <interpretationCode code=\""+item.optString("EHR_000388")+"\" codeSystem=\"2.16.840.1.113883.5.83\">"+item.optString("EHR_000388_VALUE")+"</interpretationCode>\n" +
                                        "        </observation>\n" ;
                            }
                        }
                    }


                    re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                            "<ClinicalDocument>\n" +
                            "  <version code=\"2.0.0.0\" date=\"2009-12-01\">根据卫生部标准第一次修订</version>\n" +
                            "  <ehr code=\""+catalog+"\" codeSystem=\"STD_EHR\">"+catalogName+"</ehr>\n" +
                            "  <title>"+catalogName+"</title>\n" +
                            "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                            "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                            "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                            "  <recordTarget>\n" +
                            "    <patient>\n" +
                            "      <id extension=\""+ssid+"\"></id>\n" +
                            "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                            "      <sex code=\"" + info.optString("EHR_000019") + "\" codeSystem=\"GB/T2261.1-2003\">" + info.optString("EHR_000019_VALUE") + "</sex>\n" +
                            "      <birthDate>" + info.optString("EHR_000007") + "</birthDate>\n" +
                            "      <marriage code=\"" + info.optString("EHR_000014") + "\" codeSystem=\"GB/T 2261.2-2003\">" + info.optString("EHR_000014_VALUE") + "</marriage>\n" +
                            "    </patient>\n" +
                            "  </recordTarget>\n" +
                            "  <author>\n" +
                            "    <time>" + obj.optString("EHR_000379") + "</time>\n" +
                            "    <id extension=\""+ obj.optString("EHR_000377") + "\"/>\n" +
                            "    <name>"+ obj.optString("EHR_000378") +"</name>\n" +
                            "  </author>\n" +
                            "  <authenticator>\n" +
                            "    <time>"+ obj.optString("EHR_000375") +"</time>\n" +
                            "    <id extension=\""+ obj.optString("EHR_000374") +"\"/>\n" +
                            "    <name>"+ obj.optString("EHR_000376") +"</name>\n" +
                            "  </authenticator>\n" +
                            "  <participant>\n" +
                            "    <time>"+ obj.optString("EHR_000372") +"</time>\n" +
                            "    <id extension=\""+ obj.optString("EHR_000370") +"\"/>\n" +
                            "    <name>"+ obj.optString("EHR_000373") +"</name>\n" +
                            "    <dept>"+ obj.optString("EHR_000371") +"</dept>\n" +
                            "  </participant>\n" +
                            "  <inFulfillmentOf>\n" +
                            "    <order>\n" +
                            "      <id>"+ obj.optString("EHR_000360") +"</id>\n" +
                            "      <priority code=\""+ obj.optString("EHR_000361") +"\" codeSystem=\"HL7 27\">"+ obj.optString("EHR_000361_VALUE") +"</priority>\n" +
                            "    </order>\n" +
                            "  </inFulfillmentOf> \n" +
                            "  <component>\n" +
                            "    <section>\n" +
                            "      <code code=\"【检验的专业类型编码(字典STD_LAB_EXAM_TYPE)】\" codeSystem=\"2.16.840.1.113883.6.1\"  displayName=\"【检验的专业类型名称】\"/>\n" +
                            "      <entry>\n" +
                            "        <id>"+ obj.optString("EHR_000354") +"</id>\n" +
                            "        <performer>\n" +
                            "          <assignedEntity>"+ obj.optString("EHR_000365") +"</assignedEntity>\n" +
                            "          <name>"+ obj.optString("EHR_000367") +"</name>\n" +
                            "          <time>"+ obj.optString("EHR_000366") +"</time>\n" +
                            "        </performer>\n" +
                            "        <classCode>"+ obj.optString("EHR_000357") +"</classCode>\n" +
                            "        <specimenDeterminer code=\""+ obj.optString("EHR_000355") +"\" codeSystem=\"HL7 0163\">"+ obj.optString("EHR_000355_VALUE") +"</specimenDeterminer>\n" +
                            "        <risk code=\""+ obj.optString("EHR_000358") +"\" codeSystem=\"HL7 0489\">"+ obj.optString("EHR_000358_VALUE") +"</risk>\n" +
                            "        <quantity code=\""+ obj.optString("EHR_000359") +"\" codeSystem=\"HL7 0491\">"+ obj.optString("EHR_000359_VALUE") +"</quantity>\n" +
                            "        <rejectReason code=\""+ obj.optString("EHR_000356") +"\" codeSystem=\"HL7 0490\">"+ obj.optString("EHR_000356_VALUE") +"</rejectReason>\n" +
                            "        <playingDevice>\n" +
                            "          <name>【检验设备名称】</name>\n" +
                            "        </playingDevice>\n" +
                            observationString +
                            "      </entry>\n" +
                            "    </section>\n" +
                            "  </component>\n" +
                            "</ClinicalDocument>";

                }
            }
        }

        return re;
    }

    //长期医嘱
    public String get0214(String ssid,String profileId)
    {
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {
            String orderString = "";

            //获取长期医嘱
            String url = ehrUrl + URLEncoder.encode("{demographic_id}")+"/hospitalized/orders/longtime?profile_id=" + profileId;
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject obj = new JSONObject(response);
            if (obj.getBoolean("successFlg") && obj.getInt("totalCount") > 0) {
                JSONArray array = obj.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject item = array.getJSONObject(i);
                        orderString += "<item>\n" +
                                "            <orderTime>"+item.optString("EHR_000279") +"</orderTime>\n" +
                                "            <orderDoctor>"+item.optString("EHR_000280") +"</orderDoctor>\n" +
                                "            <checkNurse>"+item.optString("EHR_000281") +"</checkNurse>\n" +
                                "            <stopTime>"+item.optString("EHR_000277") +"</stopTime>\n" +
                                "            <stopDoctor>"+item.optString("EHR_000278") +"</stopDoctor>\n" +
                                "            <stopNurse>"+item.optString("EHR_000276") +"</stopNurse>\n" +
                                "            <orderCode>"+item.optString("EHR_000282") +"</orderCode>\n" +
                                "            <stdCode>"+item.optString("EHR_000283") +"</stdCode>\n" +
                                "            <orderName>"+item.optString("EHR_000285") +"</orderName>\n" +
                                "            <freq>"+item.optString("EHR_000275") +"</freq>\n" +
                                "</item>\n";
                    }
                }
            }


            String re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<ClinicalDocument>\n" +
                    "  <version code=\"2.0.0.0\" date=\"2009-12-01\">根据卫生部标准第一次修订</version>\n" +
                    "  <ehr code=\"0214\" codeSystem=\"STD_EHR\">长期医嘱</ehr>\n" +
                    "  <title>长期医嘱</title>\n" +
                    "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                    "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                    "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                    "  <recordTarget>\n" +
                    "    <patient>\n" +
                    "      <id extension=\""+ssid+"\"></id>\n" +
                    "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                    "    </patient>\n" +
                    "  </recordTarget>\n" +
                    "  <component>\n" +
                    "    <section>\n" +
                    "      <code code=\"common\" codeSystem=\"\" displayName=\"长期医嘱\"/>\n" +
                    "      <entry>\n" +
                    "        <order>\n" +
                    orderString +
                    "        </order>\n" +
                    "      </entry>\n" +
                    "    </section>\n" +
                    "  </component>\n" +
                    "</ClinicalDocument>";

            return re;

        }
        else{
            return "";
        }
    }

    //临时医嘱
    public String get0215(String ssid,String profileId)
    {
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {
            String orderString = "";

            //获取长期医嘱
            String url = ehrUrl + URLEncoder.encode("{demographic_id}")+"/hospitalized/orders/longtime?profile_id=" + profileId;
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject obj = new JSONObject(response);
            if (obj.getBoolean("successFlg") && obj.getInt("totalCount") > 0) {
                JSONArray array = obj.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject item = array.getJSONObject(i);
                        orderString += "          <item>\n" +
                                "            <orderTime>"+item.optString("EHR_000207") +"</orderTime>\n" +
                                "            <orderDoctor>"+item.optString("EHR_000208") +"</orderDoctor>\n" +
                                "            <checkNurse>"+item.optString("EHR_000209") +"</checkNurse>\n" +
                                "            <execNurse>"+item.optString("EHR_000218") +"</execNurse>\n" +
                                "            <execTime>"+item.optString("EHR_000219") +"</execTime>\n" +
                                "            <orderCode>"+item.optString("EHR_000210") +"</orderCode>\n" +
                                "            <stdCode>"+item.optString("EHR_000211") +"</stdCode>\n" +
                                "            <orderName>"+item.optString("EHR_000213") +"</orderName>\n" +
                                "          </item>\n";
                    }
                }
            }

            String re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<ClinicalDocument>\n" +
                    "  <version code=\"2.0.0.0\" date=\"2009-12-01\">根据卫生部标准第一次修订</version>\n" +
                    "  <ehr code=\"0215\" codeSystem=\"STD_EHR\">临时医嘱</ehr>\n" +
                    "  <title>临时医嘱</title>\n" +
                    "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                    "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                    "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                    "  <recordTarget>\n" +
                    "    <patient>\n" +
                    "      <id extension=\""+ssid+"\"></id>\n" +
                    "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                    "    </patient>\n" +
                    "  </recordTarget>\n" +
                    "  <component>\n" +
                    "    <section>\n" +
                    "      <code code=\"common\" codeSystem=\"\" displayName=\"临时医嘱\"/>\n" +
                    "      <entry>\n" +
                    "        <order>\n" +
                    orderString +
                    "        </order>\n" +
                    "      </entry>\n" +
                    "    </section>\n" +
                    "  </component>\n" +
                    "</ClinicalDocument>";

            return re;

        }
        else{
            return "";
        }
    }

    //门诊手术记录 (取第一条)
    public String get0116(String ssid,String profileId)
    {
        String re = "";
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {
            //获取手术记录
            String url = ehrUrl + URLEncoder.encode("{demographic_id}")+"/laboratory?profile_id=" + profileId;
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject json = new JSONObject(response);
            if (json.getBoolean("successFlg") && json.getInt("totalCount") > 0) {
                JSONArray array = json.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    JSONObject obj = array.getJSONObject(0);
                    re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                            "<ClinicalDocument>\n" +
                            "  <version code=\"2.0.0.0\" date=\"2009-12-01\">根据卫生部标准第一次修订</version>\n" +
                            "  <ehr code=\"0116\" codeSystem=\"STD_EHR\">门诊手术记录</ehr>\n" +
                            "  <title>门诊手术记录</title>\n" +
                            "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                            "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                            "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                            "  <recordTarget>\n" +
                            "    <patient>\n" +
                            "      <id extension=\""+ssid+"\"></id>\n" +
                            "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                            "    </patient>\n" +
                            "  </recordTarget>\n" +
                            "  <author>\n" +
                            "    <time>"+obj.optString("EHR_000408")+"</time>\n" +
                            "    <id extension=\""+obj.optString("EHR_000409")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000410")+"</name>\n" +
                            "  </author>\n" +
                            "  <surgeon>\n" +
                            "    <id extension=\""+obj.optString("EHR_000424")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000425")+"</name>\n" +
                            "  </surgeon>\n" +
                            "  <assistant1>\n" +
                            "    <id extension=\""+obj.optString("EHR_000404")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000405")+"</name>\n" +
                            "  </assistant1>\n" +
                            "  <assistant2>\n" +
                            "    <id extension=\""+obj.optString("EHR_000406")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000407")+"</name>\n" +
                            "  </assistant2>\n" +
                            "  <anesthesiologist>\n" +
                            "    <id extension=\""+obj.optString("EHR_000412")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000413")+"</name>\n" +
                            "  </anesthesiologist>\n" +
                            "  <component>\n" +
                            "    <section>\n" +
                            "      <!--档案子类别，用于类别的扩展，code子类别编码；displayName名称。当前为固定值-->\n" +
                            "      <code code=\"common\" codeSystem=\"\" displayName=\"门诊手术记录\"/>\n" +
                            "      <entry>\n" +
                            "        <time>"+obj.optString("EHR_000417")+"</time>\n" +
                            "        <preDiagnosis code=\""+obj.optString("EHR_000426")+"\" codeSystem=\"ICD-10\">"+obj.optString("EHR_000426_VALUE")+"</preDiagnosis>\n" +
                            "        <midDiagnosis code=\""+obj.optString("EHR_000427")+"\" codeSystem=\"ICD-10\">"+obj.optString("EHR_000427_VALUE")+"</midDiagnosis>\n" +
                            "        <operation code=\""+obj.optString("EHR_000416")+"\" codeSystem=\"ICD-9-CM\">"+obj.optString("EHR_000418")+"</operation>\n" +
                            "        <part code=\""+obj.optString("EHR_000419")+"\" codeSystem=\"CV5201.23\">"+obj.optString("EHR_000420")+"</part>\n" +
                            "        <anesthesia>"+obj.optString("EHR_000411")+"</anesthesia>\n" +
                            "        <operationCourse>"+obj.optString("EHR_000422")+"</operationCourse>\n" +
                            "      </entry>\n" +
                            "    </section>\n" +
                            "  </component>\n" +
                            "</ClinicalDocument>";


                }
            }

        }
        return re;
    }

    //住院手术记录  (取第一条)
    public String get0216(String ssid,String profileId)
    {
        String re = "";
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {
            //获取手术记录
            String url = ehrUrl + URLEncoder.encode("{demographic_id}")+"/laboratory?profile_id=" + profileId;
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject json = new JSONObject(response);
            if (json.getBoolean("successFlg") && json.getInt("totalCount") > 0) {
                JSONArray array = json.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    JSONObject obj = array.getJSONObject(0);
                    re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                            "<ClinicalDocument>\n" +
                            "  <version code=\"2.0.0.0\" date=\"2009-12-01\">根据卫生部标准第一次修订</version>\n" +
                            "  <ehr code=\"0216\" codeSystem=\"STD_EHR\">住院手术记录</ehr>\n" +
                            "  <title>住院手术记录</title>\n" +
                            "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                            "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                            "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                            "  <recordTarget>\n" +
                            "    <patient>\n" +
                            "      <id extension=\""+ssid+"\"></id>\n" +
                            "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                            "    </patient>\n" +
                            "  </recordTarget>\n" +
                            "  <author>\n" +
                            "    <time>"+obj.optString("EHR_000408")+"</time>\n" +
                            "    <id extension=\""+obj.optString("EHR_000409")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000410")+"</name>\n" +
                            "  </author>\n" +
                            "  <surgeon>\n" +
                            "    <id extension=\""+obj.optString("EHR_000424")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000425")+"</name>\n" +
                            "  </surgeon>\n" +
                            "  <assistant1>\n" +
                            "    <id extension=\""+obj.optString("EHR_000404")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000405")+"</name>\n" +
                            "  </assistant1>\n" +
                            "  <assistant2>\n" +
                            "    <id extension=\""+obj.optString("EHR_000406")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000407")+"</name>\n" +
                            "  </assistant2>\n" +
                            "  <anesthesiologist>\n" +
                            "    <id extension=\""+obj.optString("EHR_000412")+"\"/>\n" +
                            "    <name>"+obj.optString("EHR_000413")+"</name>\n" +
                            "  </anesthesiologist>\n" +
                            "  <component>\n" +
                            "    <section>\n" +
                            "      <!--档案子类别，用于类别的扩展，code子类别编码；displayName名称。当前为固定值-->\n" +
                            "      <code code=\"common\" codeSystem=\"\" displayName=\"住院手术记录\"/>\n" +
                            "      <entry>\n" +
                            "        <time>"+obj.optString("EHR_000417")+"</time>\n" +
                            "        <preDiagnosis code=\""+obj.optString("EHR_000426")+"\" codeSystem=\"ICD-10\">"+obj.optString("EHR_000426_VALUE")+"</preDiagnosis>\n" +
                            "        <midDiagnosis code=\""+obj.optString("EHR_000427")+"\" codeSystem=\"ICD-10\">"+obj.optString("EHR_000427_VALUE")+"</midDiagnosis>\n" +
                            "        <operation code=\""+obj.optString("EHR_000416")+"\" codeSystem=\"ICD-9-CM\">"+obj.optString("EHR_000418")+"</operation>\n" +
                            "        <part code=\""+obj.optString("EHR_000419")+"\" codeSystem=\"CV5201.23\">"+obj.optString("EHR_000420")+"</part>\n" +
                            "        <anesthesia>"+obj.optString("EHR_000411")+"</anesthesia>\n" +
                            "        <operationCourse>"+obj.optString("EHR_000422")+"</operationCourse>\n" +
                            "      </entry>\n" +
                            "    </section>\n" +
                            "  </component>\n" +
                            "</ClinicalDocument>";

                }
            }

        }

        return re;
    }

    //门诊费用明细
    public String get0102(String ssid,String profileId)
    {
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {
            String costString = "";
            //门诊费用汇总
            String url = ehrUrl + URLEncoder.encode("{demographic_id}")+"/outpatient/cost/master?profile_id=" + profileId;
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject json = new JSONObject(response);
            if (json.getBoolean("successFlg") && json.getInt("totalCount") > 0) {
                JSONArray array = json.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    for(int i=0;i<array.length();i++)
                    {
                        JSONObject item = array.getJSONObject(i);
                        String type =  item.optString("EHR_000044");
                        costString += "<group>\n" +
                                "          <type code=\""+item.optString("EHR_000044")+"\" codeSystem=\"CV5600.01\">"+item.optString("EHR_000044_VALUE")+"</type>\n" +
                                "          <payway code=\"【门诊费用支付方式代码(字典STD_OUTPATIENT_PAY_WAY)】\" codeSystem=\"CV5600.02\">【门诊费用支付方式】</payway>\n" +
                                "          <fee>"+item.optString("EHR_000045")+"</fee>\n";

                        //获取门诊费用详细
                        String urlDetail = ehrUrl + URLEncoder.encode("{demographic_id}")+"/outpatient/cost/detail?profile_id=" + profileId;
                        String responseDetail = HttpClientUtil.get(urlDetail, "UTF-8");
                        JSONObject jsonDetail = new JSONObject(responseDetail);
                        if (jsonDetail.getBoolean("successFlg") && jsonDetail.getInt("totalCount") > 0) {
                            JSONArray arrayDetail = jsonDetail.getJSONArray("detailModelList");
                            if (arrayDetail != null && arrayDetail.length() > 0) {
                                for(int j=0;j<arrayDetail.length();j++)
                                {
                                    JSONObject itemDetail = arrayDetail.getJSONObject(i);
                                    if(itemDetail.optString("EHR_000050").equals(type)) {
                                        costString += "<item>\n" +
                                                "    <time>" + itemDetail.optString("EHR_000054") + "</time>\n" +
                                                "    <value code=\"【市标准扣费代码(以医保为蓝本的厦门编码)】\" >【标准项目名称】</value>\n" +
                                                "    <custome code=\"" + itemDetail.optString("EHR_000056") + "\">" + itemDetail.optString("EHR_000057") + "</custome>\n" +
                                                "    <quantity>" + itemDetail.optString("EHR_000055") + "</quantity>\n" +
                                                "    <unit>" + itemDetail.optString("EHR_000048") + "</unit>\n" +
                                                "    <price>" + itemDetail.optString("EHR_000047") + "</price>\n" +
                                                "    <total>" + itemDetail.optString("EHR_000049") + "</total>\n" +
                                                "    <notes>" + itemDetail.optString("EHR_000046") + "</notes>\n" +
                                                "</item>\n";
                                    }
                                }
                            }
                        }

                        costString += " </group>\n";
                    }
                }
            }



            String re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<ClinicalDocument>\n" +
                    "  <version code=\"2.0.0.1\" date=\"2011-06-01\">根据卫生部标准第2次修订</version>\n" +
                    "  <ehr code=\"0102\" codeSystem=\"STD_EHR\"></ehr>\n" +
                    "  <title>门诊费用明细</title>\n" +
                    "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                    "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                    "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                    "  <recordTarget>\n" +
                    "    <patient>\n" +
                    "      <id extension=\""+ssid+"\"></id>\n" +
                    "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                    "    </patient>\n" +
                    "  </recordTarget>\n" +
                    "  <component>\n" +
                    "    <section>\n" +
                    "      <code code=\"common\" codeSystem=\"\"  displayName=\"门诊费用明细\"/>\n" +
                    "      <entry>\n" +
                    costString +
                    "      </entry>\n" +
                    "    </section>\n" +
                    "  </component>\n" +
                    "</ClinicalDocument>";;

            return re;

        }
        else{
            return "";
        }
    }

    //住院费用明细
    public String get0202(String ssid,String profileId)
    {
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);

        if(info!=null) {
            String costString = "";
            //住院费用汇总
            String url = ehrUrl + URLEncoder.encode("{demographic_id}")+"/hospitalized/cost/master?profile_id=" + profileId;
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject json = new JSONObject(response);
            if (json.getBoolean("successFlg") && json.getInt("totalCount") > 0) {
                JSONArray array = json.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    for(int i=0;i<array.length();i++)
                    {
                        JSONObject item = array.getJSONObject(i);
                        String type =  item.optString("EHR_000174");
                        costString += "<group>\n" +
                                "          <type code=\""+item.optString("EHR_000174")+"\" codeSystem=\"CV5600.01\">"+item.optString("EHR_000174_VALUE")+"</type>\n" +
                                "          <payway code=\"【医疗付款方式代码】\" codeSystem=\"CV5600.02\">【医疗付款方式代码】</payway>\n" +
                                "          <fee>"+item.optString("EHR_000175")+"</fee>\n";

                        //获取住院费用详细
                        String urlDetail = ehrUrl + URLEncoder.encode("{demographic_id}")+"/hospitalized/cost/detail?profile_id=" + profileId;
                        String responseDetail = HttpClientUtil.get(urlDetail, "UTF-8");
                        JSONObject jsonDetail = new JSONObject(responseDetail);
                        if (jsonDetail.getBoolean("successFlg") && jsonDetail.getInt("totalCount") > 0) {
                            JSONArray arrayDetail = jsonDetail.getJSONArray("detailModelList");
                            if (arrayDetail != null && arrayDetail.length() > 0) {
                                for(int j=0;j<arrayDetail.length();j++)
                                {
                                    JSONObject itemDetail = arrayDetail.getJSONObject(i);
                                    if(itemDetail.optString("EHR_000187").equals(type)) {
                                        costString += "<item>\n" +
                                                "    <time>" + itemDetail.optString("EHR_000054") + "</time>\n" +
                                                "    <value code=\"【市标准扣费代码(以医保为蓝本的厦门编码)】\" >【标准项目名称】</value>\n" +
                                                "    <custome code=\"" + itemDetail.optString("EHR_000185") + "\">" + itemDetail.optString("EHR_000186") + "</custome>\n" +
                                                "    <quantity>" + itemDetail.optString("EHR_000184") + "</quantity>\n" +
                                                "    <unit>" + itemDetail.optString("EHR_000178") + "</unit>\n" +
                                                "    <price>" + itemDetail.optString("EHR_000177") + "</price>\n" +
                                                "    <total>" + itemDetail.optString("EHR_000179") + "</total>\n" +
                                                "    <notes>" + itemDetail.optString("EHR_000176") + "</notes>\n" +
                                                "</item>\n";
                                    }
                                }
                            }
                        }

                        costString += " </group>\n";
                    }
                }
            }

            String re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<ClinicalDocument>\n" +
                    "  <version code=\"2.0.0.1\" date=\"2011-06-01\">根据卫生部标准第2次修订</version>\n" +
                    "  <ehr code=\"0202\" codeSystem=\"STD_EHR\">住院费用明细</ehr>\n" +
                    "  <title>住院费用明细</title>\n" +
                    "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                    "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                    "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                    "  <recordTarget>\n" +
                    "    <patient>\n" +
                    "      <id extension=\""+ssid+"\"></id>\n" +
                    "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                    "    </patient>\n" +
                    "  </recordTarget>\n" +
                    "  <component>\n" +
                    "    <section>\n" +
                    "      <code code=\"common\" codeSystem=\"\" displayName=\"住院费用明细\"/>\n" +
                    "      <entry>\n" +
                    costString +
                    "      </entry>\n" +
                    "    </section>\n" +
                    "  </component>\n" +
                    "</ClinicalDocument>";

            return re;

        }
        else{
            return "";
        }
    }
    /************************************ 未完成 ******************************************************************/

    //获取药品
    public String getDrug(String ssid,String profileId,String catalog)
    {
        String re = "";
        //事件主要信息
        JSONObject info = getEventBaseInfo(profileId);
        String catalogName = "";
        if(catalog.equals("0141"))
        {
            catalogName = "门诊用药记录";
        }
        else if(catalog.equals("0241"))
        {
            catalogName = "住院用药记录";
        }

        if(info!=null) {
            String medicineString = "";
            //处方主表
            String url = ehrUrl + URLEncoder.encode("{demographic_id}")+"/profile/medication/master?profile_id=" + profileId;
            String response = HttpClientUtil.get(url, "UTF-8");
            JSONObject json = new JSONObject(response);
            if (json.getBoolean("successFlg") && json.getInt("totalCount") > 0) {
                JSONArray array = json.getJSONArray("detailModelList");
                if (array != null && array.length() > 0) {
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject item = array.getJSONObject(i);
                        String prescriptionNo = item.optString("EHR_000086");
                        String type = item.optString("EHR_001203");
                        medicineString += "<group>\n" +
                                "    <method code=\"【用药途径代码(字典STD_MEDICINE_ROUTE)】\" codeSystem=\"CV5201.22\">【用药使用方法和途径名称】</method>\n" +
                                "    <freq>【用药频率】</freq>\n" +
                                "    <startDate>【用药开始日期时间】</startDate>\n" +
                                "    <endDate>【用药停止日期时间】</endDate>\n" +
                                "    <days>【用药天数】</days>\n";


                        //处方细表
                        String urlDetail = ehrUrl + "profile/medication/detail/"+prescriptionNo+"?prescription_no=" + prescriptionNo+"&type="+type;
                        String responseDetail = HttpClientUtil.get(urlDetail, "UTF-8");
                        JSONObject jsonDetail = new JSONObject(responseDetail);
                        if (jsonDetail.getBoolean("successFlg") && jsonDetail.getInt("totalCount") > 0) {
                            JSONArray arrayDetail = jsonDetail.getJSONArray("detailModelList");
                            if (arrayDetail != null && arrayDetail.length() > 0) {
                                for (int j = 0; j < arrayDetail.length(); j++) {
                                    JSONObject itemDetail = arrayDetail.getJSONObject(j);

                                    //中药
                                    if(type.equals("1"))
                                    {
                                        medicineString +=   "<medicine>\n" +
                                                "    <offerTime>【发药日期时间】</offerTime>\n" +
                                                "    <value code=\"【市药品标准编码(以医保为蓝本的厦门编码)】\" >【市药品标准名称】</value>\n" +
                                                "    <DiaCode>【诊疗代码】</DiaCode>\n" +
                                                "    <custome code=\"【院内药品编码】\">"+itemDetail.optString("EHR_000131")+"</custome>\n" +
                                                "    <cnType code=\"【中药类别代码（字典STD_CN_MEDICINE）】\" codeSystem=\"CV5301.05\">【中药类别名称】</cnType>\n" +
                                                "    <type code=\"【药物类型代码（字典STD_MEDICINE_TYPE）】\" codeSystem=\"CV5301.06\">【药物类型名称】</type>\n" +
                                                "    <form code=\""+itemDetail.optString("EHR_000130")+"\" codeSystem=\"CV5301.01\">"+itemDetail.optString("EHR_000130_VALUE")+"</form>\n" +
                                                "    <spec>"+itemDetail.optString("EHR_000129")+"</spec>\n" +
                                                "    <doseQuantity>"+itemDetail.optString("EHR_000132")+"</doseQuantity>\n" +
                                                "    <doseUnit>"+itemDetail.optString("EHR_000133")+"</doseUnit>\n" +
                                                "    <totalQuantity>"+itemDetail.optString("EHR_000135")+"</totalQuantity>\n" +
                                                "    <totalUnit>"+itemDetail.optString("EHR_000133")+"</totalUnit>\n" +
                                                "    <notes>"+itemDetail.optString("EHR_000141")+"</notes>\n" +
                                                "</medicine>\n";
                                    }
                                    //西药
                                    {
                                        medicineString +=   "<medicine>\n" +
                                                "    <offerTime>【发药日期时间】</offerTime>\n" +
                                                "    <value code=\"【市药品标准编码(以医保为蓝本的厦门编码)】\" >【市药品标准名称】</value>\n" +
                                                "    <DiaCode>【诊疗代码】</DiaCode>\n" +
                                                "    <custome code=\"【院内药品编码】\">"+itemDetail.optString("EHR_000100")+"</custome>\n" +
                                                "    <cnType code=\"【中药类别代码（字典STD_CN_MEDICINE）】\" codeSystem=\"CV5301.05\">【中药类别名称】</cnType>\n" +
                                                "    <type code=\"【药物类型代码（字典STD_MEDICINE_TYPE）】\" codeSystem=\"CV5301.06\">【药物类型名称】</type>\n" +
                                                "    <form code=\""+itemDetail.optString("EHR_000099")+"\" codeSystem=\"CV5301.01\">"+itemDetail.optString("EHR_000099_VALUE")+"</form>\n" +
                                                "    <spec>"+itemDetail.optString("EHR_000098")+"</spec>\n" +
                                                "    <doseQuantity>"+itemDetail.optString("EHR_000101")+"</doseQuantity>\n" +
                                                "    <doseUnit>"+itemDetail.optString("EHR_000102")+"</doseUnit>\n" +
                                                "    <totalQuantity>"+itemDetail.optString("EHR_000104")+"</totalQuantity>\n" +
                                                "    <totalUnit>"+itemDetail.optString("EHR_000102")+"</totalUnit>\n" +
                                                "    <notes>【备注】</notes>\n" +
                                                "</medicine>\n";
                                    }

                                }
                            }
                        }

                        medicineString += "</group>\n";
                    }
                }
            }
            re = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<ClinicalDocument>\n" +
                    "  <version code=\"2.0.0.1\" date=\"2011-06-01\">根据卫生部标准第2次修订</version>\n" +
                    "  <ehr code=\""+catalog+"\" codeSystem=\"STD_EHR\">"+catalogName+"</ehr>\n" +
                    "  <title>"+catalogName+"</title>\n" +
                    "  <org code=\""+info.optString("org_code") +"\" codeSystem=\"STD_HEALTH_ORG\">"+info.optString("org_name") +"</org>\n" +
                    "  <id extension=\"" + profileId + "\" eventno=\"" + info.optString("event_no") + "\"></id>\n" +
                    "  <effectiveTime value=\"" + info.optString("create_date") + "\"/>\n" +
                    "  <recordTarget>\n" +
                    "    <patient>\n" +
                    "      <id extension=\""+ssid+"\"></id>\n" +
                    "      <name>"+info.optString("EHR_000004")+"</name>\n" +
                    "    </patient>\n" +
                    "  </recordTarget>\n" +
                    "  <component>\n" +
                    "    <section>\n" +
                    "      <code code=\"common\" codeSystem=\"\"  displayName=\""+catalogName+"\"/>\n" +
                    "      <entry>\n" +
                    medicineString +
                    "      </entry>\n" +
                    "    </section>\n" +
                    "  </component>\n" +
                    "</ClinicalDocument>";
        }

        return re;
    }



}
