package com.lantone.structure.facade.tran;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.common.util.ListUtil;
import com.lantone.common.util.StringUtil;
import com.lantone.structure.facade.tran.util.CommonAnalysisUtil;
import com.lantone.structure.model.Development;
import com.lantone.structure.model.doc.BeHospitalizedDoc;
import com.lantone.structure.model.entity.*;
import com.lantone.structure.model.label.PastLabel;
import com.lantone.structure.model.label.PersonalLabel;
import com.lantone.structure.util.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Description:入院记录
 * @author: cy
 * @time: 2021/3/05 16:12
 */
@Slf4j
public class BeHospitalizedTran extends TargetTran {
    @Override
    public Map<String, String> convert(String text) {
        BeHospitalizedDoc beHospitalizedDoc = new BeHospitalizedDoc();
        beHospitalizedDoc.setText(text);
        inputInfo.setBeHospitalizedDoc(beHospitalizedDoc);
        Map<String, String> structureMap = cutWord(text);
        Map<String, String> retMap = new HashMap<String, String>();
        beHospitalContrast(inputInfo.getBeHospitalizedDoc(), structureMap);
        inputInfo.getBeHospitalizedDoc().setStructureMap(structureMap);
        mapKeyContrastCommon(structureMap,stagesContrasts,retMap);
        aiProcess();
        lastSovle(inputInfo.getBeHospitalizedDoc(),retMap);
        return retMap;
    }

    private Map<String, String> cutWord(String text) {
        Map<String, String> sourceMap = Maps.newHashMap();
        List<String> titles = CommonAnalysisUtil.sortTitles(
                Lists.newArrayList("姓    名","性    别","产前检查","年    龄","出 生 地","末次月经","职    业","民    族","预 产 期","其    他","婚    姻","联系地址","记录时间","入院时间","出院时间","病史陈述者","与患者关系","主    诉","主  诉","入院情况","现病史","既往","既往史","目前使用的药物","个人史","月经史","婚育史","家族史","流行病史","流行病学史","一般情况","皮肤黏膜", "浅表淋巴结", "头部及其器官","颈部","胸部","肺脏","腹部","外生殖器","肛门、直肠","脊柱四肢","神经系统","其他","表格<辅助检查表>内容","表格<诊断>内容","补充诊断","再次补充诊断","第三次补充诊断","修正诊断","最后诊断","诊疗经过","诊治经过","出院情况","出院诊断","出院医嘱"),
                text
        );
        CommonAnalysisUtil.cutByTitles(text, titles, 0, sourceMap);
        if(sourceMap.containsKey("记录时间") || sourceMap.containsKey("肺脏")
                || sourceMap.containsKey("其    他") || sourceMap.containsKey("流行病史")|| sourceMap.containsKey("流行病学史")|| sourceMap.containsKey("流行病史") || sourceMap.containsKey("出生地")|| sourceMap.containsKey("既往")|| sourceMap.containsKey("目前使用的药物")|| sourceMap.containsKey("产前检查")){
            sourceMap.remove("记录时间");
            sourceMap.remove("既往");
            sourceMap.remove("肺脏");
            sourceMap.remove("其    他");
            sourceMap.remove("流行病学史");
            sourceMap.remove("流行病史");
            sourceMap.remove("出生地");
            sourceMap.remove("目前使用的药物");
            sourceMap.remove("产前检查");
        }
        return sourceMap;
    }


    private List<String> stagesContrasts = Lists.newArrayList(
            "姓    名=患者姓名",
            "姓名=患者姓名",
            "性    别=性别代码",
            "性别=性别代码",
            "民    族=民族",
            "民族=民族",
            "婚    姻=婚姻状况代码",
            "婚姻=婚姻状况代码",
            "职    业=职业类别代码",
            "职业=职业类别代码",
            "入院时间=入院日期时间",
            "出院时间=出院日期时间",
            "病史陈述者=病史陈述者姓名",
            "与患者关系=陈述者与患者的关系代码",
            "主    诉=主诉",
            "主  诉=主诉",
            "一般情况=体格检查--一般状况检查结果",
            "皮肤黏膜=体格检查--皮肤和黏膜检查结果",
            "浅表淋巴结=体格检查--全身浅表淋巴结检查结果",
            "头部及其器官=体格检查--头部及其器官检查结果",
            "颈部=体格检查--颈部检查结果",
            "胸部=体格检查--胸部检查结果",
            "腹部=体格检查--腹部检查结果",
            "肛门、直肠=体格检查--肛门指诊检查结果描述",
            "外生殖器=体格检查--外生殖器检查结果",
            "神经系统=体格检查--神经系统检查结果",
            "表格<辅助检查表>内容=辅助检查结果",
            "其他=体格检查-其他检查结果",
            "诊疗经过=诊疗过程描述",
            "诊治经过=诊疗过程描述",
            "出院诊断=出院诊断-西医诊断名称",
            "联系地址=出院诊断-地址全称（非结构化）"

    );
    public void params(String str,Map<String, String> retMap) {
        if(StringUtil.isNotEmpty(retMap.get(str))){
            String caseStr = retMap.get(str);
                String date = extractDate(caseStr);
                if(StringUtil.isNotEmpty(date)){
                    retMap.put(str,date);
                }
        }
    }

    public void beHospitalContrast ( BeHospitalizedDoc beHospitalizedDoc,Map<String, String> retMap) {
        if(beHospitalizedDoc != null){
            String text = beHospitalizedDoc.getText();
            String[] firSplit = text.split("\n");

            params("末次月经",retMap);
            params("预 产 期",retMap);
            //年龄
            if(StringUtil.isNotEmpty(retMap.get("年    龄")) || StringUtil.isNotEmpty(retMap.get("年龄"))) {
                String value = retMap.get("年    龄");
                if(StringUtil.isEmpty(value)){
                    value = retMap.get("年龄");
                }
                if (value.contains("岁")) {
                    retMap.put("年龄（岁）", value);
                    retMap.remove("年龄");
                }
                if (value.contains("月")) {
                    retMap.put("年龄（月）", value);
                    retMap.remove("年龄");
                }
            }

            if(StringUtils.isNotEmpty(retMap.get("个人史"))){
                List<String> titles = Arrays.asList("小学","小学肄业","半文盲","文盲","初中","初级中学","初中肄业","初级中学肄业","中学肄业","高中","高中肄业","技工学校","技工学校肄业","职高","职业高中","职高肄业","职业高中肄业","中等专业学校","中专","中专肄业","中等专业学校肄业","大学","专科学校","大专","专科","专科肄业","本科","本科肄业","研究生","研究生肄业","硕士研究生","硕士研究生肄业","博士研究生","博士研究生肄业");
                String personHistory = retMap.get("个人史").replace("未受过教育","文盲");
                String smokingNum = "";
                for (String title : titles) {
                    if(personHistory.contains(title)){
                        title = title .replace("半文盲","90").replace("文盲","90")
                                .replace("小学肄业","83").replace("小学","81")
                                .replace("初中肄业","73").replace("初级中学肄业","73")
                                .replace("初中","71").replace("初级中学","71")
                                .replace("高中肄业","63").replace("高中","61")
                                .replace("技工学校肄业","49") .replace("技工学校","47")
                                .replace("职高肄业","46").replace("职业高中肄业","46")
                                .replace("职高","44").replace("职业高中","44")
                                .replace("中专肄业","43").replace("中等专业学校肄业","43")
                                .replace("中等专业学校","41").replace("中专","41")
                                .replace("专科肄业","33").replace("专科学校","31").replace("大专","31")
                                .replace("本科肄业","23").replace("大学","28").replace("本科","21")
                                .replace("研究生肄业","19").replace("研究生","17")
                                .replace("硕士研究生肄业","16").replace("硕士研究生","14")
                                .replace("博士研究生肄业","13").replace("博士研究生","11");
                        retMap.put("学历代码",title);
                    }
                }

                if(personHistory.contains("吸烟")){
                    if(personHistory.contains("嗜烟")){
                        if(personHistory.indexOf("嗜烟")<personHistory.indexOf("吸烟")){
                            personHistory = personHistory.substring(personHistory.indexOf("嗜烟"));
                        }
                    }else{
                        personHistory = personHistory.substring(personHistory.indexOf("吸烟"));
                    }
                    if(personHistory.contains("饮酒")){
                        personHistory = personHistory.substring(0,personHistory.indexOf("饮酒"));
                    }
                }

                personHistory = personHistory.replace("嗜烟", "").replace("目前", "").replace("香烟", "").replace("1/3", "0.3").replace("余", "").replace("根", "支").replace("酗酒", "饮酒").replace("白酒", "饮酒").replace("日均", "每天").replace("每日", "每天").replace("／", "/").replace("/天", "/日").replace("。", ",").replace("，", ",").replace("抽烟", "吸烟").replace("平均", "").replace("平素", "").replace("大概", "").replace("估计", "").replace("差不多", "").replace("每日约", "").replace("约", "").replace("半", "0.5").replace("一", "1").replace("壹", "1").replace("二", "2").replace("贰", "2").replace("两", "2").replace("三", "3").replace("叁", "3");

                String flagSmoke = "每天吸烟";
                if(!personHistory.contains("/日")){
                    if(!personHistory.contains(flagSmoke) && personHistory.contains("每天")){
                        personHistory = personHistory.replace("每天","每天吸烟");
                    }
                    String flag = "支";
                    if(personHistory.contains("包")){
                        flag = "包";
                    }
                    if(personHistory.contains(flag)){
                        personHistory =  personHistory.substring(personHistory.indexOf("每天吸烟")+"每天".length(),personHistory.indexOf(flag)+flag.length())+"/日";
                    }
                }

                if(!personHistory.contains("否认吸烟") && personHistory.contains("吸烟") && personHistory.contains("/日")){
                    personHistory = smokeMethod(personHistory).replace("吸烟","");
                    personHistory =  personHistory.substring(personHistory.lastIndexOf(",")+",".length());
                    if(personHistory.contains("支") && !personHistory.contains("包")){
                        personHistory = personHistory.replace("支", "");
                        smokingNum = personHistory;
                    }
                    if(personHistory.contains("包")){
                        List<Double> smokeNum = new ArrayList<>();
                        String  personHistorySec = personHistory.replace("包","").replace("~","-").replace("~","-").replace("—","-").replace("到","-").replace("至","-").replace("每天","");
                        try {
                        if(personHistorySec.contains("-")){
                            for (String str : personHistorySec.split("-")) {
                                    smokeNum.add(Double.valueOf(str));
                                }
                            }else{
                            smokeNum.add(Double.valueOf(personHistorySec));
                        }
                        if(ListUtil.isNotEmpty(smokeNum)){
                            Double collect = smokeNum.stream().collect(Collectors.averagingDouble(obj -> obj));
                            personHistory = (int)Math.ceil(collect * 20)+"";
                            smokingNum = personHistory;
                            }


                        }
                         catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    }
                }
                if(StringUtils.isNotEmpty(smokingNum)){
                    retMap.put("日吸烟量（支）",smokingNum);
                }

            }

            //发育情况代码
            if(StringUtil.isNotEmpty(retMap.get("个人史"))){
                List<String> caseStr = Arrays.asList("预防接种史");
             /*   List<String> standStr = Arrays.asList("正常");
                List<String> badStr = Arrays.asList("迟缓","缓慢","落后","欠理想");*/
                String casePerson = retMap.get("个人史");
                String development = "";
                if(casePerson.contains("生长发育史")){
                    development = casePerson.substring(casePerson.indexOf("生长发育史")+"生长发育史".length());
                }
                for (String str : caseStr) {
                    if(development.contains(str)){
                        development = development.substring(0,development.indexOf(str));
                    }
                }
                retMap.put("生长发育史",development);
              /*  for (String str : standStr) {
                    if(casePerson.contains(str)) {
                        retMap.put("发育情况代码","1");
                    }
                }

                for (String str : badStr) {
                    if(casePerson.contains(str)) {
                        retMap.put("发育情况代码","2");
                    }
                }*/

            }

            //精神状态正常标志
            if(StringUtil.isNotEmpty(retMap.get("现病史"))){
                List<String> caseStr = Arrays.asList("精神状态","精神");
                List<String> standStr = Arrays.asList("好","正常","良好","可","尚可","较好","非常好","一般","较一般");
                List<String> badStr = Arrays.asList("不正常","非正常","疾病","衰弱","差","较差","欠佳","软","偏软","较软");
                String caseMoment = retMap.get("现病史");
                for (String str : caseStr) {
                    if(caseMoment.contains(str)){
                    caseMoment = caseMoment.substring(caseMoment.indexOf(str)+str.length());
                    }
                }
                caseMoment = caseMoment.replace(",", "，").replace("。", "，").replace("、", "，");
                if(caseMoment.contains("，")){
                    caseMoment = caseMoment.split("，")[0];
                }
                for (String str : standStr) {
                    if(caseMoment.contains(str)) {
                        retMap.put("精神状态正常标志","1");
                    }
                }

                for (String str : badStr) {
                    if(caseMoment.contains(str)) {
                        retMap.put("精神状态正常标志","0");
                    }
                }
            }


            //病史陈述者
            if(StringUtil.isNotEmpty(retMap.get("病史陈述者"))){
                String casePerson = retMap.get("病史陈述者");
                if(casePerson.contains("本人")){
                    String name = retMap.get("姓名");
                    if(StringUtil.isNotEmpty(name)){
                        retMap.put("病史陈述者",name);
                    }
                    if(StringUtil.isEmpty(name) && StringUtil.isNotEmpty(retMap.get("姓    名"))){
                        name = retMap.get("姓    名");
                        retMap.put("病史陈述者",name);
                    }
                }
            }

            //出生地
            parseBirthAddress(retMap);

            //24小时出入院症状名称
            if(StringUtil.isNotEmpty(retMap.get("主    诉")) || StringUtil.isNotEmpty(retMap.get("主诉"))) {
                String fir = retMap.get("主    诉");
                String sec = retMap.get("主诉");
                if(StringUtil.isNotEmpty(fir)){
                    retMap.put("症状名称",fir);
                }
                if(StringUtil.isNotEmpty(sec)){
                    retMap.put("症状名称",sec);
                }
            }
            //24小时出入院现病史
            if(StringUtil.isEmpty(retMap.get("现病史")) && StringUtil.isNotEmpty(retMap.get("入院情况"))) {
                String value = retMap.get("入院情况");
                String placeName = "";
                if(value.contains("查体：")){
                    value = value.substring(0, value.lastIndexOf("查体："));
                }
                if(value.contains("辅助检查：")){
                    value = value.substring(0, value.lastIndexOf("辅助检查："));
                }
                if(value.contains("收住我科")){
                    placeName = "收住我科";
                }
                if(value.contains("收治我科")){
                    placeName = "收治我科";
                }
                if(value.contains("门诊收住")){
                    placeName = "门诊收住";
                }
                if( value.contains("收入院")){
                    placeName = "收入院";
                }
                if( value.contains("收治入院")){
                    placeName = "收治入院";
                }
                if( value.contains("收住入院")){
                    placeName = "收住入院";
                }
                if(StringUtil.isNotEmpty(placeName)){
                    value = value.substring(0, value.lastIndexOf(placeName)+placeName.length());
                }
                if(StringUtil.isNotEmpty(value)){
                  retMap.put("现病史",value);
                    retMap.put("症状描述",value);
                }
            }
            //体格检查指标
            String firRetStr= "";
            if(StringUtil.isNotEmpty(retMap.get("流行病史"))){
                String flagFir = retMap.get("流行病史");
                if(flagFir.contains("体  格  检  查")){
                    firRetStr =  flagFir;
                }
            }
            if(StringUtil.isNotEmpty(retMap.get("家族史"))){
                String flagSec= retMap.get("家族史");
                if(flagSec.contains("体  格  检  查")){
                    firRetStr =  flagSec;
                }
            }
            if(StringUtil.isNotEmpty(firRetStr)){
                if(firRetStr.contains("体  格  检  查")){
                    firRetStr = firRetStr.substring(0, firRetStr.lastIndexOf("体  格  检  查"));
                    retMap.put("家族史",firRetStr);
                    String firStr = text.substring(text.lastIndexOf("体  格  检  查"));
                    String[] splits = firStr.split("\n");
                    String split =splits[0];
                    for (int i = 1; i < splits.length; i++) {
                      if(StringUtils.isEmpty(splits[i].trim())){
                          continue;
                      }
                      if((splits[i].contains("T") || splits[i].contains("t"))&& (splits[i].contains("mmHg") || splits[i].contains("BP") || splits[i].contains("Bp"))){
                          split = splits[i];
                          break;
                      }
                    }

                    List<String> checkList = Arrays.asList("专  科  检  查", "其他：","一般情况");
                    for (String str : checkList) {
                        if(split.contains(str)){
                            split = split.substring(0, split.lastIndexOf(str));
                        }
                    }

                    split = split.replace(":","").replace("：","")
                            .replace("t","T").replace("p","P").replace("r","R")
                            .replace("bP","BP");


                        if(split.contains("T") && split.contains("℃") ){
                            String t = split.substring(split.indexOf("T")+"T".length(),split.indexOf("℃")+"℃".length()).replace("体温","");
                            split = split.substring(split.indexOf(t)+t.length());
                            if(StringUtil.isNotEmpty(t)){
                            retMap.put("体格检查--体温（℃）", t);
                            }
                        }
                        if(split.contains("P") && split.contains("分")){
                            String p = split.substring(0,split.indexOf("分")+"分".length());
                           if(!p.contains("P") && p.contains("脉搏")){
                               p = p.replace("脉搏","P");
                              }
                               p = p.substring(p.indexOf("P")+"P".length());
                            split = split.substring(split.indexOf(p)+p.length());
                            if(StringUtil.isNotEmpty(p)){
                            retMap.put("体格检查--脉率（次/min）", p);
                            }
                        }
                        if(split.contains("R") && split.contains("分")){
                            String r = split.substring(split.indexOf("R")+"R".length(),split.indexOf("分")+"分".length()).replace("呼吸","");
                            split = split.substring(split.indexOf(r)+r.length());
                            if(StringUtil.isNotEmpty(r)){
                            retMap.put("体格检查--呼吸频率（次/min）", r);
                            }
                        }
                            String bp = "Bp";
                        if(split.contains("BP")){
                            bp = "BP";
                        };
                        if(split.contains(bp) && split.contains("mmHg")){
                            String flag = "／";
                            if(split.contains("/")){
                                flag = "/";
                            }
                            String b = split.substring(split.indexOf(bp)+bp.length(),split.indexOf(flag)).replace("血压","");
                            String p = split.substring(split.indexOf(flag)+flag.length(),split.lastIndexOf("mmHg")).replace("血压","");
                            if(StringUtil.isNotEmpty(b)){
                                retMap.put("体格检查--收缩压（mmHg）", b);
                            }
                            if(StringUtil.isNotEmpty(p)){
                                retMap.put("体格检查--舒张压（mmHg）", p);
                            }
                        }

                }

                if(StringUtil.isEmpty(retMap.get("流行病史"))){
                    retMap.put("家族史", firRetStr);
                }else{
                    retMap.remove("流行病史");
                }

            }

                if(text.contains("专　科　检　查") || text.contains("专科检查")){
                    String firStr = "";
                    if(text.contains("专　科　检　查")){
                         firStr = text.substring(text.lastIndexOf("专　科　检　查"));
                    }
                    if(text.contains("专科检查")){
                         firStr = text.substring(text.lastIndexOf("专科检查"));
                    }
                    firStr =  firStr.replace("辅  助  检  查","辅  助  检  查");
                    List<String> checkList = Arrays.asList("辅  助  检  查","表格<辅助检查表>内容");
                    for (String str : checkList) {
                        if(firStr.contains(str)){
                            firStr = firStr.substring(0, firStr.lastIndexOf(str));
                        }
                    }
                    firStr =  firStr.replace("宫底高","宫底").replace("胎心率","胎心").replace(",","，").replace("；","，").replace(";","，");
                    String[] split = firStr.split("，");
                    if(split.length>0){
                        for (String str : split) {
                            if(str.contains("骶耻外径") ){
                                String t = str.substring(str.indexOf("骶耻外径")+"骶耻外径".length()).replace("cm","");
                                if(StringUtil.isNotEmpty(t)){
                                    retMap.put("骶耻外径", t);
                                }
                            }
                            if(str.contains("坐骨结节间径") && str.contains("cm") ){
                                String t = str.substring(str.indexOf("坐骨结节间径")+"坐骨结节间径".length()).replace("cm","");
                                if(StringUtil.isNotEmpty(t)){
                                    retMap.put("坐骨结节间径", t);
                                }
                            }
                            if(str.contains("宫底") && str.contains("cm") ){
                                String t = str.substring(str.indexOf("宫底")+"宫底".length()).replace("cm","");
                                if(StringUtil.isNotEmpty(t)){
                                    retMap.put("宫底高度", t);
                                }
                            }
                            if(str.contains("宫缩")){
                                String t = str.substring(str.indexOf("宫缩")+"宫缩".length());
                                if(StringUtil.isNotEmpty(t)){
                                    retMap.put("宫缩情况", t);
                                }
                            }
                            if(str.contains("腹围") && str.contains("cm") ){
                                String t = str.substring(str.indexOf("腹围")+"腹围".length()).replace("cm","");
                                if(StringUtil.isNotEmpty(t)){
                                    retMap.put("腹围", t);
                                }
                            }
                            if(str.contains("胎心") && str.contains("次/分") ){
                                String t = str.substring(str.indexOf("胎心")+"胎心".length(),str.indexOf("次/分")+"次/分".length()).replace("胎儿","").replace("次/分","");
                                if(StringUtil.isNotEmpty(t)){
                                    retMap.put("胎心率", t);
                                }
                            }
                            if(str.contains("宫颈") ){
                                String t = str.substring(str.indexOf("宫颈")+"宫颈".length());
                                if(StringUtil.isNotEmpty(t)){
                                    retMap.put("宫颈情况", t);
                                }
                            }
                            if(str.contains("宫口") ){
                                String t = str.substring(str.indexOf("宫口")+"宫口".length());
                                if(StringUtil.isNotEmpty(t)){
                                    retMap.put("宫口情况", t);
                                }
                            }
                            if(str.contains("先露") ){
                                String t = str.substring(str.indexOf("先露")+"先露".length());
                                String tr = str.substring(str.indexOf("先露"));
                                if(StringUtil.isNotEmpty(t)){
                                    retMap.put("先露位置", t);
                                    if(StringUtil.isNotEmpty(tr) && !tr.contains("cm")){
                                        retMap.put("胎方位", tr);
                                    }
                                }
                            }
                        }
                    }
                }

            //体格检查-身高、体重
            if(StringUtil.isNotEmpty(retMap.get("一般情况"))){
                String condition = retMap.get("一般情况");
                String weightStr = "kg";
                if(condition.contains("KG")){
                    weightStr = "KG";
                }
                if(condition.contains("体重") && condition.contains(weightStr)){
                    String weight = condition.substring(condition.indexOf("体重") + "体重".length(), condition.indexOf(weightStr) + weightStr.length());
                    if(StringUtil.isNotEmpty(weight)){
                        retMap.put("体格检查--体重（kg）", weight);
                    }
                }
                String heightStr = "cm";
                if(condition.contains("CM")){
                    heightStr = "CM";
                }
                if(condition.contains("身高") && condition.contains(heightStr)){
                    String height = condition.substring(condition.indexOf("身高") + "身高".length(), condition.indexOf(heightStr) + heightStr.length());
                    if(StringUtil.isNotEmpty(height)){
                        retMap.put("体格检查--身高（cm）", height);
                    }
                }
            }

            //脊柱四肢
            if(StringUtil.isNotEmpty(retMap.get("脊柱四肢"))){
                String fir = retMap.get("脊柱四肢");
                if(fir.contains("详见") && fir.contains("检查")){
                    retMap.put("体格检查--脊柱检查结果",fir);
                    retMap.put("体格检查--四肢检查结果",fir);
                }else{
                    if(fir.contains("。") && fir.split("。").length>1){
                        String[] split = fir.split("。");
                        if(StringUtil.isNotEmpty(split[0])){
                            retMap.put("体格检查--脊柱检查结果", split[0]);
                        }
                        if(split.length>1 && StringUtil.isNotEmpty(split[1])){
                            retMap.put("体格检查--四肢检查结果", split[1]);
                        }
                    }else{
                        if(fir.contains("四肢")){
                            String sec = fir.substring(0, fir.lastIndexOf("四肢"));
                            String thr = fir.substring( fir.lastIndexOf("四肢"));
                            if(StringUtil.isNotEmpty(sec)){
                                retMap.put("体格检查--脊柱检查结果",sec);
                            }
                            if(StringUtil.isNotEmpty(thr)){
                                retMap.put("体格检查--四肢检查结果",thr);
                            }
                        }
                    }
                }

                retMap.remove("脊柱四肢");
            }
            //其他
            if(StringUtil.isNotEmpty(retMap.get("其他"))){
                String others = retMap.get("其他");
                others =  others.replace("专　科　检　查","专  科  检  查");
                if(others.contains("专  科  检  查") &&  others.contains("辅  助  检  查")){
                    String retStr = others.substring(others.lastIndexOf("专  科  检  查") + "专  科  检  查".length(), others.lastIndexOf("辅  助  检  查"));
                    if(StringUtil.isNotEmpty(retStr)){
                        retMap.put("专科情况",retStr);
                    }
                }

               String otherStr = others.replace("专 科 检 查","专  科  检  查");
                if(otherStr.contains("专  科  检  查")){
                    String retStr = otherStr.substring(0, otherStr.lastIndexOf("专  科  检  查"));
                    if(retStr.contains("专  科  检  查")){
                        retStr = retStr.substring(0, retStr.lastIndexOf("专  科  检  查"));
                    }
                    if(StringUtil.isNotEmpty(retStr)) {
                        retMap.put("其他", retStr);
                    }
                }
                if(others.contains("表格<基本信息>内容")){
                    String retStr = others.substring(0, others.lastIndexOf("表格<基本信息>内容"));
                    if(StringUtil.isNotEmpty(retStr)) {
                        retMap.put("其他", retStr);
                    }
                }

            }

            //表格<辅助检查表>内容
            if(StringUtil.isNotEmpty(retMap.get("表格<辅助检查表>内容"))){
                String fir = retMap.get("表格<辅助检查表>内容");
               if(fir.contains("点击增加行")){
                   String retStr = fir.substring(0, fir.lastIndexOf("点击增加行"));
                   if(StringUtil.isNotEmpty(retStr)){
                       retMap.put("表格<辅助检查表>内容",retStr);
                   }
               }
            }
            //入院日期时间
            if(StringUtil.isNotEmpty(retMap.get("入院时间"))) {
                String date = retMap.get("入院时间");
                         date = extractDate(date);
                         if(StringUtil.isNotEmpty(date)){
                             retMap.put("入院时间",date);
                         }
            }
            //出院日期时间
            if(StringUtil.isNotEmpty(retMap.get("出院时间"))) {
                String date = retMap.get("出院时间");
                date = extractDate(date);
                if(StringUtil.isNotEmpty(date)){
                    retMap.put("出院时间",date);
                }
            }

            //初步诊断/时间
            if(StringUtil.isNotEmpty(retMap.get("表格<诊断>内容"))) {
                String ret = retMap.get("表格<诊断>内容");
                String fir = ret;
                if(fir.contains("初步诊断：")){
                     fir = fir.substring(fir.lastIndexOf("初步诊断：")+"初步诊断：".length());
                    if(StringUtil.isNotEmpty(fir.trim()) && fir.contains("年") && fir.contains("月")){
                        String date = extractDate(fir);
                        if(StringUtil.isNotEmpty(date)){
                            fir = fir.substring(0,fir.lastIndexOf(date));
                        }
                    }
                    if(StringUtil.isNotEmpty(fir.trim())&& fir.contains("/")){
                        fir= fir.substring(0,fir.lastIndexOf("/"));
                    }
                    if(StringUtil.isNotEmpty(fir.trim()) && fir.contains("医师签名")){
                        fir= fir.substring(0,fir.lastIndexOf("医师签名"));
                    }else{
                        if( !fir.contains("日  期") && fir.contains("年") && fir.contains("月")){
                            String date = lastTime(fir);
                            if(StringUtil.isNotEmpty(date)) {
                                fir= fir.substring(fir.lastIndexOf("初步诊断：")+"初步诊断：".length(),fir.lastIndexOf(date));
                            }
                        }
                    }
                    if(StringUtil.isNotEmpty(fir) &&  StringUtil.isNotEmpty(fir.trim())){
                        retMap.put("初步诊断-西医诊断名称",fir);
                        //兼容24小时出入院记录
                        retMap.put("入院诊断-西医诊断名称",fir);

                    }
                    if(ret.contains("日  期")){
                        String date = ret.substring(ret.indexOf("日  期") + "日  期".length());
                        date = extractDate(date);
                        if(StringUtil.isNotEmpty(date)) {
                            retMap.put("初步诊断日期",date);
                        }
                    }
                }
                retMap.remove("表格<诊断>内容");
            }

            //补充诊断/时间
            StringBuffer sb = new StringBuffer();
            StringBuffer sbDate = new StringBuffer();
            if(StringUtil.isNotEmpty(retMap.get("补充诊断"))){
                String ret = retMap.get("补充诊断");
                String fir = ret;
                    if( fir.contains("医师签名")){
                        fir= fir.substring(0,fir.lastIndexOf("医师签名"));
                    }else{
                        if(!fir.contains("日  期") &&  fir.contains("年") && fir.contains("月")){
                            String date = lastTime(fir);
                            if(StringUtil.isNotEmpty(date)) {
                                fir= fir.substring(0,fir.lastIndexOf(date));
                            }
                        }
                    }
                    if(StringUtil.isNotEmpty(fir)){
                        sb.append("补充诊断名称："+fir+"  ");
                        retMap.remove("补充诊断");
                }
                if(ret.contains("日  期")){
                    String date = ret.substring(ret.indexOf("日  期") + "日  期".length());
                    date = extractDate(date);
                    if(StringUtil.isNotEmpty(date)) {
                        sbDate.append("补充诊断日期："+date+"  ");
                    }
                }
            }

            //再次补充诊断/时间
            if(StringUtil.isNotEmpty(retMap.get("再次补充诊断"))){
                String ret = retMap.get("再次补充诊断");
                String fir = ret;
                if( fir.contains("医师签名")){
                    fir= fir.substring(0,fir.lastIndexOf("医师签名"));
                }else{
                    if(!fir.contains("日  期") &&  fir.contains("年") && fir.contains("月")){
                        String date = lastTime(fir);
                        if(StringUtil.isNotEmpty(date)) {
                            fir= fir.substring(0,fir.lastIndexOf(date));
                        }
                    }
                }
                if(StringUtil.isNotEmpty(fir)){
                    sb.append("再次补充诊断："+fir+"  ");
                    retMap.remove("再次补充诊断");
                }
                if(ret.contains("日  期")){
                    String date = ret.substring(ret.indexOf("日  期") + "日  期".length());
                    date = extractDate(date);
                    if(StringUtil.isNotEmpty(date)) {
                        sbDate.append("再次补充诊断日期："+date+"  ");
                    }
                }
            }

            //第三次补充诊断/时间
            if(StringUtil.isNotEmpty(retMap.get("第三次补充诊断"))){
                String ret = retMap.get("第三次补充诊断");
                String fir = ret;
                if( fir.contains("医师签名")){
                    fir= fir.substring(0,fir.lastIndexOf("医师签名"));
                }else{
                    if(!fir.contains("日  期") && fir.contains("年") && fir.contains("月")){
                        String date = lastTime(fir);
                        if(StringUtil.isNotEmpty(date)) {
                            fir= fir.substring(0,fir.lastIndexOf(date));
                        }
                    }
                }
                if(StringUtil.isNotEmpty(fir)){
                    sb.append("第三次补充诊断："+fir+"  ");
                    retMap.remove("第三次补充诊断");
                }
                if(ret.contains("日  期")){
                    String date = ret.substring(ret.indexOf("日  期") + "日  期".length());
                    date = extractDate(date);
                    if(StringUtil.isNotEmpty(date)) {
                        sbDate.append("第三次补充诊断日期："+date+"  ");
                    }
                }
            }

            if (StringUtil.isNotEmpty(sb.toString())) {
                String str = sb.toString();
                if(str.contains("：")){
                    String[] split = str.split("：");
                    if(split.length>2){
                        retMap.put("补充诊断名称",str);
                    }else{
                        retMap.put("补充诊断名称",split[1]);
                    }
                }
            }
            if (StringUtil.isNotEmpty(sbDate.toString())) {
                String str = sbDate.toString();
                if(str.contains("：")){
                    String[] split = str.split("：");
                    if(split.length>2){
                        retMap.put("补充诊断日期",str);
                    }else{
                        retMap.put("补充诊断日期",split[1]);
                    }
                }
            }

            //修正诊断/时间
            if(StringUtil.isNotEmpty(retMap.get("修正诊断"))){
                String ret = retMap.get("修正诊断");
                String fir = ret;
                if( fir.contains("医师签名")){
                     fir = fir.split("医师签名")[0];
                }else{
                    if(!fir.contains("日  期") &&  fir.contains("年") && fir.contains("月")){
                        String date = lastTime(fir);
                        if(StringUtil.isNotEmpty(date)) {
                            fir= fir.substring(0,fir.lastIndexOf(date));
                        }
                    }
                }
                if(StringUtil.isNotEmpty(fir)){
                    retMap.put("修正诊断-西医诊断名称",fir);
                    retMap.remove("修正诊断");
                }
                if(ret.contains("日  期")){
                    String date = ret.substring(ret.indexOf("日  期") + "日  期".length());
                    date = extractDate(date);
                    if(StringUtil.isNotEmpty(date)) {
                        retMap.put("修正诊断日期",date);
                    }
                }
            }

            //最后诊断/时间
            if(StringUtil.isNotEmpty(retMap.get("最后诊断"))){
                String ret = retMap.get("最后诊断");
                String fir = ret;
                if( fir.contains("医师签名")){
                    fir= fir.substring(0,fir.lastIndexOf("医师签名"));
                }else{
                    if(!fir.contains("日  期") &&  fir.contains("年") && fir.contains("月")){
                        String date = lastTime(fir);
                        if(StringUtil.isNotEmpty(date)) {
                            fir= fir.substring(0,fir.lastIndexOf(date));
                        }
                    }
                }
                if(StringUtil.isNotEmpty(fir)){
                    retMap.put("确定诊断-西医诊断名称",fir);
                    retMap.remove("最后诊断");
                }
                if(ret.contains("日  期")){
                    String date = ret.substring(ret.indexOf("日  期") + "日  期".length());
                    date = extractDate(date);
                    if(StringUtil.isNotEmpty(date)) {
                        retMap.put("确定诊断日期",date);
                    }
                }
            }
            //出院医嘱
            if(StringUtil.isNotEmpty(retMap.get("出院医嘱"))){
                String doctorDavice = retMap.get("出院医嘱");
                String Date = extractDate(doctorDavice);
                if(StringUtil.isNotEmpty(Date)){
                    String retStr = doctorDavice.substring(0, doctorDavice.lastIndexOf(Date));
                    retMap.put("出院医嘱",retStr);
                    retMap.put("出院医嘱开立日期时间",Date);
                }
            }

            //区别连个模块
                    Boolean flag = false;
                    String firStr = text.substring(0,30);
                    if(firStr.contains("24h")|| firStr.contains("二十四") || firStr.contains("日间")
                    || firStr.contains("24小时") || firStr.contains("出入院记录")) {
                        flag = true;
                    }

                //日间
                if(flag){
                    retMap.remove("初步诊断-西医诊断名称");
                    retMap.remove("姓名");
                    retMap.remove("姓    名");
                    retMap.remove("其他");
                    retMap.remove("骶耻外径");
                    retMap.remove("坐骨结节间径");
                    retMap.remove("宫底高度");
                    retMap.remove("腹围");
                    retMap.remove("胎心率");
                    retMap.remove("宫颈情况");
                    retMap.remove("宫缩情况");
                    retMap.remove("宫口情况");
                    retMap.remove("先露位置");
                    retMap.remove("胎方位");
                    retMap.remove("学历代码");
                    retMap.remove("日吸烟量（支）");
                    retMap.remove("精神状态正常标志");
                    retMap.remove("发育情况代码");
                    retMap.remove("末次月经");
                    retMap.remove("预产期");
                    //入院记录
                }else{
                    retMap.remove("出院时间");
                    retMap.remove("症状名称");
                    retMap.remove("入院诊断-西医诊断名称");
                }
            }
        }

    public static void parseBirthAddress(Map<String,String> retMap){
        if(StringUtil.isNotEmpty(retMap.get("联系地址"))) {
            String value = retMap.get("联系地址");
            String fir = value;
            String sec = value;
            String thr = value;
            String fou = value;
            String fiv = value;
            String six = value;
            //地址-省（自治区、直辖市）
            if (fir.contains("省") || fir.contains("自治区") || fir.contains("直辖市")) {
                if(fir.contains("省")){
                    fir = fir.substring(0, fir.lastIndexOf("省") + "省".length());
                    retMap.put("地址-省（自治区、直辖市）", fir);
                }
                if(fir.contains("自治区")){
                    fir = fir.substring(0, fir.lastIndexOf("自治区") + "自治区".length());
                    retMap.put("地址-省（自治区、直辖市）", fir);
                }
                if(fir.contains("直辖市")){
                    fir = fir.substring(0, fir.lastIndexOf("直辖市") + "直辖市".length());
                    retMap.put("地址-省（自治区、直辖市）", fir);
                }
            }

            //地址-市（地区、州）
            if (sec.contains("市") || sec.contains("地区") || sec.contains("州")) {
                String firRet= retMap.get("地址-省（自治区、直辖市）");
                Boolean flag = true;
                if(sec.contains("市") && flag ){
                    if(StringUtil.isNotEmpty(firRet)){
                        sec = sec.substring(sec.indexOf(firRet)+firRet.length(), sec.lastIndexOf("市") + "市".length());
                    }else{
                        sec = sec.substring(0, sec.lastIndexOf("市") + "市".length());
                    }
                  retMap.put("地址-市（地区、州）", sec);
                    flag = false;
                }
                if(sec.contains("地区") && flag){
                    if(StringUtil.isNotEmpty(firRet)){
                    sec = sec.substring(sec.indexOf(firRet)+firRet.length(), sec.lastIndexOf("地区") + "地区".length());
                    }else{
                        sec = sec.substring(0, sec.lastIndexOf("地区") + "地区".length());
                    }
                    retMap.put("地址-市（地区、州）", sec);
                    flag = false;
                }
                if(sec.contains("州") && flag ){
                    if(StringUtil.isNotEmpty(firRet)){
                    sec = sec.substring(sec.indexOf(firRet)+firRet.length(), sec.lastIndexOf("州") + "州".length());
                    }else{
                        sec = sec.substring(0, sec.lastIndexOf("州") + "州".length());
                    }
                    retMap.put("地址-市（地区、州）", sec);
                    flag = false;
                }
            }

            // 地址-县（区）
            if (!thr.contains("地区") && thr.contains("县") && thr.contains("区")) {
                String firRet= retMap.get("地址-省（自治区、直辖市）");
                String secRet= retMap.get("地址-市（地区、州）");
                Boolean flag = true;
                if(thr.contains("地区") && flag){
                    if(StringUtil.isNotEmpty(secRet)){
                    thr = thr.substring(thr.indexOf(secRet)+secRet.length(), thr.lastIndexOf("地区") + "地区".length());
                    }else{
                        if(StringUtil.isNotEmpty(firRet)){
                            thr = thr.substring(thr.indexOf(firRet)+firRet.length(), thr.lastIndexOf("地区") + "地区".length());
                        }else{
                            thr = thr.substring(0, thr.lastIndexOf("地区") + "地区".length());
                        }
                    }
                    retMap.put("地址-县（区）", thr);
                    flag = false;
                }
                if(thr.contains("区") && flag){
                    if(StringUtil.isNotEmpty(secRet)){
                    thr = thr.substring(sec.indexOf(secRet)+secRet.length(), thr.lastIndexOf("区") + "区".length());
                    }
                    else{
                        if(StringUtil.isNotEmpty(firRet)){
                            thr = thr.substring(thr.indexOf(firRet)+firRet.length(), thr.lastIndexOf("区") + "区".length());
                        }else{
                            thr = thr.substring(0, thr.lastIndexOf("区") + "区".length());
                        }
                    }
                    retMap.put("地址-县（区）", thr);
                    flag = false;
                }
            }

            //地址-乡（镇、街道办事处）
            if (fou.contains("乡") || fou.contains("镇")|| fou.contains("街道办事处")) {
                String firRet= retMap.get("地址-省（自治区、直辖市）");
                String secRet= retMap.get("地址-市（地区、州）");
                String thrRet= retMap.get("地址-县（区）");
                Boolean flag = true;
                if(fou.contains("乡") && flag){
                    if(StringUtil.isNotEmpty(thrRet)){
                    fou = fou.substring(fou.indexOf(thrRet)+thrRet.length(), fou.lastIndexOf("乡") + "乡".length());
                    }else{
                        if(StringUtil.isNotEmpty(secRet)){
                            fou = fou.substring(fou.indexOf(secRet)+secRet.length(), fou.lastIndexOf("乡") + "乡".length());
                        }else{
                            if(StringUtil.isNotEmpty(firRet)){
                                fou = fou.substring(fou.indexOf(firRet)+firRet.length(), fou.lastIndexOf("乡") + "乡".length());
                            }else{
                                fou = fou.substring(0, fou.lastIndexOf("乡") + "乡".length());
                            }
                        }
                    }
                    retMap.put("地址-乡（镇、街道办事处）", fou);
                    flag = false;
                }
                if(fou.contains("镇") && flag ){
                    if(StringUtil.isNotEmpty(thrRet)){
                    fou = fou.substring(fou.indexOf(thrRet)+thrRet.length(), fou.lastIndexOf("镇") + "镇".length());
                    }else{
                        if(StringUtil.isNotEmpty(secRet)){
                            fou = fou.substring(fou.indexOf(secRet)+secRet.length(), fou.lastIndexOf("镇") + "镇".length());
                        }else{
                            if(StringUtil.isNotEmpty(firRet)){
                                fou = fou.substring(fou.indexOf(firRet)+firRet.length(), fou.lastIndexOf("镇") + "镇".length());
                            }else{
                                fou = fou.substring(0, fou.lastIndexOf("镇") + "镇".length());
                            }
                        }
                    }
                    retMap.put("地址-乡（镇、街道办事处）", fou);
                    flag = false;
                }
                if(fou.contains("街道办事处") && flag){
                    if(StringUtil.isNotEmpty(thrRet)){
                    fou = fou.substring(fou.indexOf(thrRet)+thrRet.length(), fou.lastIndexOf("街道办事处") + "街道办事处".length());
                    }else{
                        if(StringUtil.isNotEmpty(secRet)){
                            fou = fou.substring(fou.indexOf(secRet)+secRet.length(), fou.lastIndexOf("街道办事处") + "街道办事处".length());
                        }else{
                            if(StringUtil.isNotEmpty(firRet)){
                                fou = fou.substring(fou.indexOf(firRet)+firRet.length(), fou.lastIndexOf("街道办事处") + "街道办事处".length());
                            }else{
                                fou = fou.substring(0, fou.lastIndexOf("街道办事处") + "街道办事处".length());
                            }
                        }
                    }
                    retMap.put("地址-乡（镇、街道办事处）", fou);
                    flag = false;
                }
            }
            //地址-村（街、路、弄等）
            if (fiv.contains("村") || fiv.contains("街")|| fiv.contains("弄")) {
                String firRet= retMap.get("地址-省（自治区、直辖市）");
                String secRet= retMap.get("地址-市（地区、州）");
                String thrRet= retMap.get("地址-县（区）");
                String fouRet= retMap.get("地址-乡（镇、街道办事处）");
                Boolean flag = true;
                if(fiv.contains("村") && flag ){
                    if(StringUtil.isNotEmpty(fouRet)){
                    fiv = fiv.substring(fiv.indexOf(fouRet)+fouRet.length(), fiv.lastIndexOf("村") + "村".length());
                    }else{
                       if(StringUtil.isNotEmpty(thrRet)){
                           fiv = fiv.substring(fiv.indexOf(thrRet)+thrRet.length(), fiv.lastIndexOf("村") + "村".length());
                       }else{
                           if(StringUtil.isNotEmpty(secRet)){
                               fiv = fiv.substring(fiv.indexOf(secRet)+secRet.length(), fiv.lastIndexOf("村") + "村".length());
                           }else{
                               if(StringUtil.isNotEmpty(firRet)){
                                   fiv = fiv.substring(fiv.indexOf(firRet)+firRet.length(), fiv.lastIndexOf("村") + "村".length());
                               }else{
                                   fiv = fiv.substring(0, fiv.lastIndexOf("村") + "村".length());
                               }
                           }
                       }
                    }
                    retMap.put("地址-村（街、路、弄等）", fiv);
                    flag = false;
                }
                if(fiv.contains("街") && flag ){
                    if(StringUtil.isNotEmpty(fouRet)){
                    fiv = fiv.substring(sec.indexOf(fouRet)+fouRet.length(), fiv.lastIndexOf("街") + "街".length());
                    }
                    else{
                        if(StringUtil.isNotEmpty(thrRet)){
                            fiv = fiv.substring(fiv.indexOf(thrRet)+thrRet.length(), fiv.lastIndexOf("街") + "街".length());
                        }else{
                            if(StringUtil.isNotEmpty(secRet)){
                                fiv = fiv.substring(fiv.indexOf(secRet)+secRet.length(), fiv.lastIndexOf("街") + "街".length());
                            }else{
                                if(StringUtil.isNotEmpty(firRet)){
                                    fiv = fiv.substring(fiv.indexOf(firRet)+firRet.length(), fiv.lastIndexOf("街") + "街".length());
                                }else{
                                    fiv = fiv.substring(0, fiv.lastIndexOf("街") + "街".length());
                                }
                            }
                        }
                    }
                    retMap.put("地址-村（街、路、弄等）", fiv);
                    flag = false;
                }
                if(fiv.contains("路") && flag ){
                    if(StringUtil.isNotEmpty(fouRet)){
                    fiv = fiv.substring(sec.indexOf(fouRet)+fouRet.length(), fiv.lastIndexOf("路") + "路".length());
                    } else{
                        if(StringUtil.isNotEmpty(thrRet)){
                            fiv = fiv.substring(fiv.indexOf(thrRet)+thrRet.length(), fiv.lastIndexOf("路") + "路".length());
                        }else{
                            if(StringUtil.isNotEmpty(secRet)){
                                fiv = fiv.substring(fiv.indexOf(secRet)+secRet.length(), fiv.lastIndexOf("路") + "路".length());
                            }else{
                                if(StringUtil.isNotEmpty(firRet)){
                                    fiv = fiv.substring(fiv.indexOf(firRet)+firRet.length(), fiv.lastIndexOf("路") + "路".length());
                                }else{
                                    fiv = fiv.substring(0, fiv.lastIndexOf("路") + "路".length());
                                }
                            }
                        }
                    }
                    retMap.put("地址-村（街、路、弄等）", fiv);
                    flag = false;
                }
                if(fiv.contains("弄") && flag){
                    if(StringUtil.isNotEmpty(fouRet)){
                    fiv = fiv.substring(sec.indexOf(fouRet)+fouRet.length(), fiv.lastIndexOf("弄") + "弄".length());
                    } else{
                        if(StringUtil.isNotEmpty(thrRet)){
                            fiv = fiv.substring(fiv.indexOf(thrRet)+thrRet.length(), fiv.lastIndexOf("弄") + "弄".length());
                        }else{
                            if(StringUtil.isNotEmpty(secRet)){
                                fiv = fiv.substring(fiv.indexOf(secRet)+secRet.length(), fiv.lastIndexOf("弄") + "弄".length());
                            }else{
                                if(StringUtil.isNotEmpty(firRet)){
                                    fiv = fiv.substring(fiv.indexOf(firRet)+firRet.length(), fiv.lastIndexOf("弄") + "弄".length());
                                }else{
                                    fiv = fiv.substring(0, fiv.lastIndexOf("街") + "街".length());
                                }
                            }
                        }
                    }
                    retMap.put("地址-村（街、路、弄等）", fiv);
                    flag = false;
                }
            }
            //地址-门牌号码
            String firRet= retMap.get("地址-省（自治区、直辖市）");
            String secRet= retMap.get("地址-市（地区、州）");
            String thrRet= retMap.get("地址-县（区）");
            String fouRet= retMap.get("地址-乡（镇、街道办事处）");
            String fivRet= retMap.get("地址-村（街、路、弄等）");
            if (StringUtil.isNotEmpty(fivRet)) {
                six = six.substring(six.indexOf(fivRet)+fivRet.length());
                if (StringUtil.isNotEmpty(six)) {
                    retMap.put("地址-门牌号码", six);
                }
            }else{
                if (StringUtil.isNotEmpty(fouRet)) {
                    six = six.substring(six.indexOf(fouRet)+fouRet.length());
                }else{
                    if (StringUtil.isNotEmpty(thrRet)) {
                        six = six.substring(six.indexOf(thrRet)+thrRet.length());
                    }else{
                        if (StringUtil.isNotEmpty(secRet)) {
                            six = six.substring(six.indexOf(secRet)+secRet.length());
                        }else{
                            if (StringUtil.isNotEmpty(firRet)) {
                                six = six.substring(six.indexOf(firRet)+firRet.length());
                            }
                        }
                    }
                }
                if (StringUtil.isNotEmpty(six)) {
                    retMap.put("地址-门牌号码", six);
                }
            }
        }
    }

    public String smokeMethod(String personHistory){
            personHistory =  personHistory.substring(0,personHistory.lastIndexOf("/日"));
            if(personHistory.contains("吸烟") && personHistory.contains("/日")){
                personHistory = smokeMethod(personHistory);
            }else{
                return personHistory;
            }
                return personHistory;

    }
    public static void lastSovle(BeHospitalizedDoc beHospitalizedDoc , Map<String,String> rep) {
               if(StringUtil.isNotEmpty(rep.get("既往史"))){
                   rep.remove("既往史");
               }
                rep.remove("生长发育史");
        if( beHospitalizedDoc != null && beHospitalizedDoc.getPastLabel() != null){
            PastLabel pastLabel = beHospitalizedDoc.getPastLabel();
            if(pastLabel != null){
                //输血史
                List<BloodTransfusion> bloodTransfusions = pastLabel.getBloodTransfusions();
                commomRecord(bloodTransfusions,rep,"输血史");

                //过敏史
                List<Allergy> allergies = pastLabel.getAllergies();
                commomRecord(allergies,rep,"过敏史");

                //手术史
                List<Operation> operations = pastLabel.getOperations();
                commomRecord(operations,rep,"手术史");

                //传染病史
                List<DiagInfectious> diagInfectiouses = pastLabel.getDiagInfectiouses();
                commomRecord(diagInfectiouses,rep,"传染病史");
                flagMethod(rep);

                //疾病史
                List<Diag> diags= pastLabel.getDiags();
                commomRecord(diags,rep,"疾病史");

                //预防接种史
                List<Vaccinate> vaccinates = pastLabel.getVaccinates();
                commomRecord(vaccinates,rep,"预防接种史");
            }
        }

        if( beHospitalizedDoc != null && beHospitalizedDoc.getPersonalLabel() != null){
            PersonalLabel personalLabel = beHospitalizedDoc.getPersonalLabel();
            if(personalLabel != null){
                //生长发育史
                Development development = personalLabel.getDevelopment();
                if(null != development && StringUtils.isNotEmpty(development.getName())){
                    String name = development.getName().replace("正立型","1")
                            .replace("无力型","2").replace("超力型","3");
                    rep.put("发育情况代码",name);
                }
            }

        }
    }

    public static void flagMethod( Map<String,String> rep){
      if(StringUtils.isNotEmpty(rep.get("传染病史"))){
        String str = rep.get("传染病史");
          rep.put("患者传染性标志","0");
        if(str.contains(".")){
            String[] split = str.split("\\.");

            for (String st : split) {
                if(st.equals("1")){
                    continue;
                }
                if(!st.contains("否认")){
                    rep.put("患者传染性标志","1");
                    break;
                }
            }
        }else{
            if(!str.contains("否认")){
                rep.put("患者传染性标志","1");
            }
        }
    }
    }

    public static <T> void commomRecord(List<T> list, Map<String, String> rep,String rocordName){
        try {
            if(ListUtil.isNotEmpty(list)){
                if(list.size()>1){
                    StringBuffer sb = new StringBuffer();
                    StringBuffer sbx = new StringBuffer();
                    int count = 0;
                    int sum = 0;
                    for (T bean : list) {
                        Map<String, Object> objectMap = MapUtil.objectToMap(bean);
                        String name = (String) objectMap.get("name");
                        if( StringUtil.isNotEmpty(name)){
                            if(!name.contains("否认")){
                                sum++;
                                sbx.append(sum+"."+name);
                            }else{
                                count++;
                                sb.append(count+"."+name);
                            }
                        }
                     }
                    if(StringUtil.isNotEmpty(sb.toString())&&StringUtil.isNotEmpty(sbx.toString())){
                        rep.put(rocordName,sbx.toString()+"; "+sb.toString());
                    }else{
                        if(StringUtil.isNotEmpty(sb.toString())){
                            rep.put(rocordName,sb.toString());
                        }
                        if(StringUtil.isNotEmpty(sbx.toString())){
                            rep.put(rocordName,sbx.toString());
                        }
                    }

                }else{
                    Map<String, Object> objectMap = MapUtil.objectToMap((list.get(0)));
                    String name = (String) objectMap.get("name");
                    if(StringUtil.isNotEmpty(name)){
                        rep.put(rocordName,name);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void mapKeyContrastCommon(Map sourceMap, List<String> keyContrasts, Map<String, String> retMap) {
        Map<String, String> sourceMap_ = MapUtil.copyMap(sourceMap);
        String[] arry = null;
        String sourceKey = null, targetKey;
        Set<String> removeKey = new HashSet<>();
        for (String keyContrast : keyContrasts) {
            arry = keyContrast.split("=");
            sourceKey = arry[0];
            if (arry.length == 1) {
                targetKey = arry[0];
            } else {
                targetKey = arry[1];
            }
            if (StringUtil.isNotBlank(sourceMap_.get(sourceKey))
                    && (!retMap.containsKey(targetKey) || StringUtil.isBlank(retMap.get(targetKey)))) {
                retMap.put(targetKey, sourceMap_.get(sourceKey));
            }
            removeKey.add(sourceKey);
        }
        Set<String> keySet = retMap.keySet();
        for (String key : sourceMap_.keySet()) {
            if (!keySet.contains(key) && !removeKey.contains(key)) { // 如果之前已放过key就不用放了
                retMap.put(key, sourceMap_.get(key));
            }
        }
    }


    /**
     * 抽取文本中的第一个时间
     *
     * @param top
     * @return
     */
    public static String extractDate(String top) {
        Pattern pattern = Pattern.compile("[0-9]{4}[-][0-9]{1,2}[-][0-9]{1,2}");
        Matcher matcher = pattern.matcher(top);
        if (matcher.find()) {
            return matcher.group(0);
        } else {
            Pattern p1 = Pattern.compile("[0-9]{4}年[0-9]+月[0-9]+日");
            Matcher m1 = p1.matcher(top);
            if (m1.find()) {
                return m1.group(0);
            }
        }
        return null;
    }

    /**
     * 抽取文本中的最后时间
     *
     * @param top
     * @return
     */
    public static String lastTime(String top) {
        int length = top.trim().length();
        String substring = top.trim().substring(length - 22, length);
        String stringDate = "";
        if( substring.contains("年")){
            String[] time = substring.split("年");
            String timeYear = time[0].trim();
            timeYear = extractDate(timeYear);
            if( substring.contains("月")){
                String[] timeLater = time[1].split("月");
                String timeMouth = timeLater[0].trim();
                if(substring.contains("日")){
                    String[] timeLast = timeLater[1].split("日");
                    String timeDate = timeLast[0].trim();
                    if(StringUtil.isNotEmpty(timeYear) && StringUtil.isNotEmpty(timeMouth) && StringUtil.isNotEmpty(timeDate)){
                        stringDate = timeYear + "年" + timeMouth + "月" + timeDate + "日" ;
                    }
                }
            }
        }
        Date date = null;
        try {
            date = DateUtils.parseDate(stringDate, StringUtil.dateFormats);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        if(date!= null){
            return stringDate;
        }else{
            return "";
        }
    }

    public static String parseString(String text){
        if(text.contains("/")){
            text = text.substring(text.lastIndexOf("/")+1);
            text= parseString(text);
        }
        return text;
    }
}