package com.lantone.structure.ai.process;


import com.alibaba.fastjson.JSONObject;
import com.lantone.structure.ai.model.Lemma;
import com.lantone.structure.model.entity.*;
import com.lantone.structure.model.label.OperationRecordLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * 输血记录处理
 */
public class EntityOperationRecord extends EntityProcess {
    private Logger logger = LoggerFactory.getLogger(EntityOperationRecord.class);

    public OperationRecordLabel extractEntity(JSONObject aiOut) {
        OperationRecordLabel operationRecordLabel = new OperationRecordLabel();
        try {
            //介入物名称
            List<Lemma> interventionLemma = createEntityTree(aiOut, "介入物名称");
            List<Intervention> interventionList = new ArrayList<>();
            for (Lemma lemma : interventionLemma) {
                Intervention intervention = new Intervention();
                intervention.setName(lemma.getText());
                interventionList.add(intervention);
            }
            operationRecordLabel.setInterventions(interventionList);

            //皮肤消毒描述
            operationRecordLabel.setSkinSterilize(findFirstT(createEntityTree(aiOut, "皮肤消毒描述"), new SkinSterilize()));

            //手术切口描述
            List<Lemma> operationIncisionLemma = createEntityTree(aiOut, "手术切口描述");
            List<OperationIncision> operationIncisionList = new ArrayList<>();
            for (Lemma lemma : operationIncisionLemma) {
                OperationIncision operationIncision = new OperationIncision();
                operationIncision.setName(lemma.getText());
                operationIncisionList.add(operationIncision);
            }
            operationRecordLabel.setOperationIncision(operationIncisionList);

            //输液量（mL）
            operationRecordLabel.setTransfuseQuantity(findFirstT(createEntityTree(aiOut, "输液量"), new TransfuseQuantity()));

            //术前用药
            operationRecordLabel.setPreoperativeDrugs(findFirstT(createEntityTree(aiOut, "术前用药"), new PreoperativeDrugs()));

            //术中用药
            operationRecordLabel.setDuringDrugs(findFirstT(createEntityTree(aiOut, "药物名称"), new DuringDrugs()));

            //引流材料名称
            List<Lemma> drainageMaterialsNameLemma = createEntityTree(aiOut, "引流材料名称");
            List<DrainageMaterialsName> drainageMaterialsNameList = new ArrayList<>();
            for (Lemma lemma : drainageMaterialsNameLemma) {
                DrainageMaterialsName drainageMaterialsName = new DrainageMaterialsName();
                drainageMaterialsName.setName(lemma.getText());
                drainageMaterialsNameList.add(drainageMaterialsName);
            }
            operationRecordLabel.setDrainageMaterialsNames(drainageMaterialsNameList);

            //引流材料数目
            List<Lemma> drainageMaterialsAmountLemma = createEntityTree(aiOut, "引流材料数目");
            List<DrainageMaterialsAmount> drainageMaterialsAmountList = new ArrayList<>();
            for (Lemma lemma : drainageMaterialsAmountLemma) {
                DrainageMaterialsAmount drainageMaterialsAmount = new DrainageMaterialsAmount();
                drainageMaterialsAmount.setName(lemma.getText());
                drainageMaterialsAmountList.add(drainageMaterialsAmount);
            }
            operationRecordLabel.setDrainageMaterialsAmounts(drainageMaterialsAmountList);

            //放置部位
            List<Lemma> placePositionLemma = createEntityTree(aiOut, "放置部位");
            List<PlacePosition> placePositionList = new ArrayList<>();
            for (Lemma lemma : placePositionLemma) {
                PlacePosition placePosition = new PlacePosition();
                placePosition.setName(lemma.getText());
                placePositionList.add(placePosition);
            }
            operationRecordLabel.setPlacePositions(placePositionList);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return operationRecordLabel;
    }
}
