package com.lantone.qc.kernel.structure.ai.process;

import com.alibaba.fastjson.JSONObject;
import com.lantone.qc.kernel.structure.ai.model.EntityEnum;
import com.lantone.qc.kernel.structure.ai.model.Lemma;
import com.lantone.qc.pub.model.entity.BetterFinding;
import com.lantone.qc.pub.model.entity.Chief;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.entity.DoctorAdvice;
import com.lantone.qc.pub.model.entity.Notes;
import com.lantone.qc.pub.model.entity.OutcomeCure;
import com.lantone.qc.pub.model.entity.OutcomeToBetter;
import com.lantone.qc.pub.model.entity.PositiveFinding;
import com.lantone.qc.pub.model.label.LeaveHospitalDoctorAdviceLabel;
import com.lantone.qc.pub.model.label.LeaveHospitalLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : EntityProcessLeaveHospital
 * @Description : 出院小结
 * @Author : 胡敬
 * @Date: 2020-03-20 11:20
 */
public class EntityProcessLeaveHospital extends EntityProcess {
    public LeaveHospitalLabel extractEntity(JSONObject aiOut) {
        LeaveHospitalLabel leaveHospitalLabel = new LeaveHospitalLabel();
        //主诉
        List<Lemma> chiefLemmas = createEntityTree(aiOut, EntityEnum.CHIEF.toString());
        List<Chief> chiefs = new ArrayList<>();
        for (Lemma lemma : chiefLemmas) {
            Chief chief = new Chief();
            chief.setName(lemma.getText());
            chiefs.add(chief);
        }
        leaveHospitalLabel.setChiefs(chiefs);
        //注意事项
        List<Lemma> notesLemmas = createEntityTree(aiOut, EntityEnum.NOTES.toString());
        List<Notes> noteses = new ArrayList<>();
        for (Lemma lemma : notesLemmas) {
            Notes notes = new Notes();
            notes.setName(lemma.getText());
            noteses.add(notes);
        }
        leaveHospitalLabel.setNoteses(noteses);
        //医嘱
        List<Lemma> keyWordForDoctorAdvicesesLemmas = createEntityTree(aiOut, EntityEnum.KEY_WORD_FOR_DOCTOR_ADVICES.toString());
        List<Lemma> keyWordForPartLemmas = createEntityTree(aiOut, EntityEnum.KEY_WORD_FOR_PART.toString());
        DoctorAdvice doctorAdvice = new DoctorAdvice();
        int keyWordForDoctorAdvicesesStart = 0, partStart = 0, minPart = 0;
        for (Lemma doctorAdviceLemma : keyWordForDoctorAdvicesesLemmas) {
            keyWordForDoctorAdvicesesStart = Integer.parseInt(doctorAdviceLemma.getPosition());
            for (Lemma part : keyWordForPartLemmas) {
                partStart = Integer.parseInt(part.getPosition());
                if (minPart == 0 || partStart > keyWordForDoctorAdvicesesStart && partStart < minPart) {
                    minPart = partStart;
                }
            }
        }
        //出院诊断
        List<Lemma> diagLemmas = createEntityTree(aiOut, EntityEnum.DIEASE.toString());
        List<Diag> diags = new ArrayList<>();
        for (Lemma lemma : diagLemmas) {
            Diag diag = new Diag();
            diag.setHospitalDiagName(lemma.getText());
            diags.add(diag);
        }
        leaveHospitalLabel.setDiags(diags);
        String content = aiOut.getString("content");
        String doctorAdviceContent = content.substring(keyWordForDoctorAdvicesesStart, minPart);
        doctorAdvice.setName(doctorAdviceContent);
        leaveHospitalLabel.setDoctorAdvices(doctorAdvice);
        //好转表现
        List<Lemma> betterFindingLemmas = createEntityTree(aiOut, EntityEnum.BETTER_FINDING.toString());
        List<BetterFinding> betterFindings = new ArrayList<>();
        for (Lemma lemma : betterFindingLemmas) {
            BetterFinding betterFinding = new BetterFinding();
            betterFinding.setName(lemma.getText());
            betterFindings.add(betterFinding);
        }
        leaveHospitalLabel.setBetterFindings(betterFindings);

        //转归情况-好转
        List<Lemma> outcomeToBetterLemmas = createEntityTree(aiOut, EntityEnum.OUTCOME_TO_BETTER.toString());
        List<OutcomeToBetter> outcomeToBetters = new ArrayList<>();
        for (Lemma lemma : outcomeToBetterLemmas) {
            OutcomeToBetter outcomeToBetter = new OutcomeToBetter();
            outcomeToBetter.setName(lemma.getText());
            outcomeToBetters.add(outcomeToBetter);
        }
        leaveHospitalLabel.setOutcomeToBetters(outcomeToBetters);

        //转归情况-治愈
        List<Lemma> outcomeCureLemmas = createEntityTree(aiOut, EntityEnum.OUTCOME_CURE.toString());
        List<OutcomeCure> outcomeCures = new ArrayList<>();
        for (Lemma lemma : outcomeCureLemmas) {
            OutcomeCure outcomeCure = new OutcomeCure();
            outcomeCure.setName(lemma.getText());
            outcomeCures.add(outcomeCure);
        }
        leaveHospitalLabel.setOutcomeCures(outcomeCures);

        //阳性表现
        List<Lemma> positiveFindingLemmas = createEntityTree(aiOut, EntityEnum.POSITIVE_FINDING.toString());
        List<PositiveFinding> positiveFindings = new ArrayList<>();
        for (Lemma lemma : positiveFindingLemmas) {
            PositiveFinding positiveFinding = new PositiveFinding();
            positiveFinding.setName(lemma.getText());
            positiveFindings.add(positiveFinding);
        }
        leaveHospitalLabel.setPositiveFindings(positiveFindings);
        return leaveHospitalLabel;
    }
}