package com.lantone.structure.ai.process;


import com.alibaba.fastjson.JSONObject;
import com.lantone.common.util.ListUtil;
import com.lantone.structure.ai.model.EntityEnum;
import com.lantone.structure.ai.model.Lemma;
import com.lantone.structure.model.label.DailyCourseRecordLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 处理日常病程记录
 */
public class EntityDailyCourseRecord extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityDailyCourseRecord.class);

    public DailyCourseRecordLabel extractEntity(JSONObject aiOut) {
        DailyCourseRecordLabel dailyCourseRecordLabel= new DailyCourseRecordLabel();
        try {
            //医嘱信息
            StringBuffer sb = new StringBuffer();
            StringBuffer xb = new StringBuffer();
            StringBuffer bb = new StringBuffer();
            String drugNames = "",laboratorys,auxoiliarys;
            //药品
            List<Lemma> drugName = createEntityTree(aiOut, EntityEnum.DRUG_NAME.toString());
            for (Lemma lemma : drugName) {
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                        if (relationLemmaRec.getProperty().equals(EntityEnum.STOPNOW.toString())) {
                            drugNames = relationLemmaRec.getText()+text;
                            sb.append(drugNames+"、");
                        }
                        if (relationLemmaRec.getProperty().equals(EntityEnum.CONTINUE.toString())) {
                            drugNames = relationLemmaRec.getText()+text;
                            sb.append(drugNames+"、");
                        }
                    }
                }else{
                    sb.append(text+" ");
                }
            }
            Boolean sbxflag = false;
            String str = sb.toString();
            StringBuffer sbx = new StringBuffer();
            StringBuffer sbxx = new StringBuffer();
            if(str.contains("、")){
                String[] split = str.split("、");
                if(split.length>1){
                    for (int i = 0; i < split.length; i++) {
                        if(!(split[i].contains(EntityEnum.STOPNOW.toString()) || split[i].contains(EntityEnum.CONTINUE.toString()))){
                            sbx.append(split[i]+"、");
                        }else{
                            sbxx.append(split[i]+"、");
                        }
                    }
                }
            }else{
                sbxflag = true;
            }
            sb = new StringBuffer();
            if(ListUtil.isNotEmpty(drugName)){
                sb.append("药品：");
            }
            if(!sbxflag){
                if(sbx.toString().endsWith("、")){
                    sbx.deleteCharAt(sbx.toString().length()-1);
                    sbx.append(";");
                }
                sb.append(sbx.toString());
                sb.append(sbxx.toString());
            }else{
                sb.append(str);
            }

            str = sb.toString();
            if(str.endsWith("、")){
                sb.deleteCharAt(str.length()-1);
            }
            //实验室检查
            List<Lemma> laboratory = createEntityTree(aiOut, EntityEnum.LABORATORY.toString());
            for (Lemma lemma : laboratory) {
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                        if (relationLemmaRec.getProperty().equals(EntityEnum.REPATE.toString())) {
                            laboratorys = relationLemmaRec.getText()+text;
                            xb.append(laboratorys+"、");
                        }
                        if (relationLemmaRec.getProperty().equals(EntityEnum.OTHERREPATE.toString())) {
                            laboratorys = relationLemmaRec.getText()+text;
                            xb.append(laboratorys+"、");
                        }
                    }
                }else{
                    xb.append(text+" ");
                }
            }
            String xtr = xb.toString();
            StringBuffer xbx = new StringBuffer();
            StringBuffer xbxx = new StringBuffer();
            Boolean xbxflag = false;
            if(xtr.contains("、")){
                String[] split = xtr.split("、");
                if(split.length>1){
                    for (int i = 0; i < split.length; i++) {
                        if(!(split[i].contains(EntityEnum.STOPNOW.toString()) || split[i].contains(EntityEnum.CONTINUE.toString()))){
                            xbx.append(split[i]+"、");
                        }else{
                            xbxx.append(split[i]+"、");
                        }
                    }
                }
            }else{
                xbxflag = true;
            }
            if(ListUtil.isNotEmpty(laboratory)){
                sb.append("  实验室检查：");
            }
            if(!xbxflag){
                if(xbx.toString().endsWith("、")){
                    xbx.deleteCharAt(xbx.toString().length()-1);
                    xbx.append(";");
                }
                sb.append(xbx.toString());
                sb.append(xbxx.toString());
            }else{
                sb.append(xtr);
            }

            str = sb.toString();
            if(str.endsWith("、")){
                sb.deleteCharAt(str.length()-1);
            }
            //辅助检查
            List<Lemma> auxoiliary = createEntityTree(aiOut, EntityEnum.AUXILIARY_EXAMINATION.toString());
            for (Lemma lemma : auxoiliary) {
                String text = lemma.getText();
                if (lemma.isHaveChildren()) {
                    for (Lemma relationLemmaRec : lemma.getRelationLemmas()) {
                        if (relationLemmaRec.getProperty().equals(EntityEnum.REPATE.toString())) {
                            auxoiliarys = relationLemmaRec.getText()+text;
                            bb.append(auxoiliarys+"、");
                        }
                        if (relationLemmaRec.getProperty().equals(EntityEnum.OTHERREPATE.toString())) {
                            auxoiliarys = relationLemmaRec.getText()+text;
                            bb.append(auxoiliarys+"、");
                        }
                    }
                }else{
                    bb.append(text+" ");
                }
            }
            Boolean bbxflag = false;
            String btr = bb.toString();
            StringBuffer bbx = new StringBuffer();
            StringBuffer bbxx = new StringBuffer();
            if(btr.contains("、")){
                String[] split = xtr.split("、");
                if(split.length>1){
                    for (int i = 0; i < split.length; i++) {
                        if(!(split[i].contains(EntityEnum.STOPNOW.toString()) || split[i].contains(EntityEnum.CONTINUE.toString()))){
                            bbx.append(split[i]+"、");
                        }else{
                            bbxx.append(split[i]+"、");
                        }
                    }
                }
            }else{
                bbxflag = true;
            }
            if(ListUtil.isNotEmpty(auxoiliary)){
                sb.append("  辅助检查：");
            }
            if(!bbxflag){
                if(bbx.toString().endsWith("、")){
                    bbx.deleteCharAt(bbx.toString().length()-1);
                    bbx.append(";");
                }
                sb.append(bbx.toString());
                sb.append(bbxx.toString());
            }else{
                sb.append(btr);
            }
            str = sb.toString();
            if(str.endsWith("、")){
                sb.deleteCharAt(str.length()-1);
            }

            //手术名称
            List<Lemma> operation = createEntityTree(aiOut, EntityEnum.OPERATION.toString());
            if(ListUtil.isNotEmpty(operation)){
                sb.append("  手术名称：");
            }
            for (Lemma lemma : operation) {
                String text = lemma.getText();
                sb.append(text+" ");
            }
            dailyCourseRecordLabel.setDoctorAdviceRecord(sb.toString());
            //停止
          //  List<Lemma> stop = createEntityTree(aiOut, EntityEnum.STOPNOW.toString());
            //继续
          //  List<Lemma> continues = createEntityTree(aiOut, EntityEnum.CONTINUE.toString());
            //复查
           // List<Lemma> repate = createEntityTree(aiOut, EntityEnum.REPATE.toString());
            //其他复查
           // List<Lemma> otherRepate = createEntityTree(aiOut, EntityEnum.OTHERREPATE.toString());
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return dailyCourseRecordLabel;
    }
}
