package com.lantone.qc.kernel.catalogue.ywzxyy.threelevelward;

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.FirstCourseRecordDoc;
import com.lantone.qc.pub.model.doc.ward.DirectorDoctorWardDoc;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.model.label.ThreeLevelWardLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClassName : THR0702
 * @Description : 首次主任医师查房中鉴别诊断跟首程一致
 * @Author : 胡敬
 * @Date: 2020-04-20 16:38
 */
@Component
public class THR02880 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getThreeLevelWardDocs().size() == 0 || inputInfo.getFirstCourseRecordDoc() == null) {
            return;
        }
        List<DirectorDoctorWardDoc> directorDocs = inputInfo.getThreeLevelWardDocs().get(0).getDirectorDoctorWardDocs();
        FirstCourseRecordDoc firstCourseRecordDoc = inputInfo.getFirstCourseRecordDoc();
        Map<String, String> structureMap = firstCourseRecordDoc.getStructureMap();
        String diffDiagStruct = structureMap.get("鉴别诊断");
        if (StringUtil.isNotBlank(diffDiagStruct) && (diffDiagStruct.contains("诊断明确") || diffDiagStruct.contains("无需鉴别"))) {
            status.set("0");
            return;
        }
        String firstCourseDiffDiag = getDiffDiag(firstCourseRecordDoc);
        if (StringUtil.isBlank(firstCourseDiffDiag)) {
            status.set("0");
            return;
        }
        if (directorDocs.size() == 0) {
            return;
        }
        DirectorDoctorWardDoc firstDirectorDoc = directorDocs.get(0);
        Map<String, String> firstAttendStructureMap = firstDirectorDoc.getStructureMap();
        if (inputInfo.getBeHospitalizedDoc() != null) {
            String admisDateStr = inputInfo.getBeHospitalizedDoc().getStructureMap().get("入院日期");
            String recordDateStr = firstAttendStructureMap.get("查房日期");
            if (CatalogueUtil.isEmpty(admisDateStr) || CatalogueUtil.isEmpty(recordDateStr)) {
                return;
            }
            //如果首次查房超过72小时则不判断该条规则
            if (CatalogueUtil.compareTime(StringUtil.parseDateTime(admisDateStr), StringUtil.parseDateTime(recordDateStr), 72 * 60L)) {
                return;
            }
        }
        ThreeLevelWardLabel firstAttendLabel = firstDirectorDoc.getThreeLevelWardLabel();
        if (firstAttendLabel == null) {
            return;
        }
        if (firstAttendLabel.getDiffDiag().size() != 0) {
            for (Diag diffDiag : firstAttendLabel.getDiffDiag()) {
                String diffDiagName = diffDiag.getHospitalDiagName();
                if (StringUtil.isBlank(diffDiagName)) {
                    continue;
                }
                if (firstCourseDiffDiag.equals(diffDiagName)) {
                    status.set("-1");
                    return;
                }
            }

        }
        if (StringUtil.isNotBlank(firstAttendLabel.getDiffDiagText())) {
            if (firstAttendLabel.getDiffDiagText().equals(firstCourseDiffDiag)) {
                status.set("-1");
            }
        }

        if (firstAttendLabel.getDiffDiag().size() == 0 && StringUtil.isBlank(firstAttendLabel.getDiffDiagText())) {
            status.set("0");
        }
    }

    /**
     * 获取首程鉴别诊断
     *
     * @param firstCourseRecordDoc
     * @return
     */
    private String getDiffDiag(FirstCourseRecordDoc firstCourseRecordDoc) {
        String diffDiag = "";
        DiagLabel differentialDiagLabel = firstCourseRecordDoc.getDifferentialDiagLabel();
        if (StringUtil.isNotBlank(firstCourseRecordDoc.getStructureMap().get("鉴别诊断"))) {
            diffDiag = firstCourseRecordDoc.getStructureMap().get("鉴别诊断");
        } else if (differentialDiagLabel != null && StringUtil.isNotBlank(differentialDiagLabel.getText())) {
            diffDiag = differentialDiagLabel.getText();
        }
        return diffDiag;
    }
}