package com.lantone.qc.kernel.catalogue.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.AttendingDoctorWardDoc;
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 : THR0701
 * @Description : 首次主治医师查房中鉴别诊断跟首程一致
 * @Author : 胡敬
 * @Date: 2020-04-20 16:38
 */
@Component
public class THR0701 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getThreeLevelWardDocs().size() == 0 || inputInfo.getFirstCourseRecordDoc() == null) {
            return;
        }
        List<AttendingDoctorWardDoc> attendDocs = inputInfo.getThreeLevelWardDocs().get(0).getAttendingDoctorWardDocs();
        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 (attendDocs.size() == 0) {
            return;
        }
        AttendingDoctorWardDoc firstAttendDoc = attendDocs.get(0);
        Map<String, String> firstAttendStructureMap = firstAttendDoc.getStructureMap();
        if (inputInfo.getBeHospitalizedDoc() != null) {
            String admisDateStr = inputInfo.getBeHospitalizedDoc().getStructureMap().get("入院日期");
            String recordDateStr = firstAttendStructureMap.get("查房日期");
            if (CatalogueUtil.isEmpty(admisDateStr) || CatalogueUtil.isEmpty(recordDateStr)) {
                return;
            }
            //如果首次查房超过48小时则不判断该条规则
            if (CatalogueUtil.compareTime(StringUtil.parseDateTime(admisDateStr), StringUtil.parseDateTime(recordDateStr), 48 * 60L)) {
                return;
            }
        }
        ThreeLevelWardLabel firstAttendLabel = firstAttendDoc.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.contains(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;
    }
}