package com.lantone.qc.kernel.catalogue.ninghaiyiyi.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.operation.OperationDoc;
import com.lantone.qc.pub.model.doc.ward.DirectorDoctorWardDoc;
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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName : THR0139
 * @Description : 上级医师（副主任医师/主任医师）首次查房无补充诊断/初步诊断/修正诊断
 * @Author : 胡敬
 * @Date: 2020-03-23 14:16
 */
@Component
public class THR0139 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getThreeLevelWardDocs().size() == 0 || inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        List<DirectorDoctorWardDoc> directorDocs = inputInfo.getThreeLevelWardDocs().get(0).getDirectorDoctorWardDocs();
        if (directorDocs.size() == 0) {
            status.set("0");
            return;
        }
        DirectorDoctorWardDoc firstDirectDoc = directorDocs.get(0);
        //先取结构化数据判断
        Map<String, String> firstDirectStructureMap = firstDirectDoc.getStructureMap();
        String admisDateStr = inputInfo.getBeHospitalizedDoc().getStructureMap().get("入院日期");
        String recordDateStr = firstDirectStructureMap.get("查房日期");
        String content = firstDirectStructureMap.get("病情记录");
        if (content.contains("诊断明确") || content.contains("目前诊断") || content.contains("目前考虑") || content.contains("当前诊断") ||
                content.contains("考虑诊断") || content.contains("诊断考虑") || content.contains("诊断基本明确") || content.contains("初步诊断") ||
                content.contains("诊断为") || regexFind(content, "诊断", "基本明确")) {
            status.set("0");
            return;
        }

        if (CatalogueUtil.isEmpty(admisDateStr) || CatalogueUtil.isEmpty(recordDateStr)) {
            status.set("0");
            return;
        }
        //如果首次查房超过72小时则不判断该条规则
        if (CatalogueUtil.compareTime(StringUtil.parseDateTime(admisDateStr), StringUtil.parseDateTime(recordDateStr), 72 * 60L)) {
            status.set("0");
            return;
        }
        //如果首次查房在手术记录之后不提示规则
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (operationDocs != null) {
            for (OperationDoc operationDoc : operationDocs) {
                if (operationDoc.getOperationRecordDoc() != null) {
                    Map<String, String> structureMap = operationDoc.getOperationRecordDoc().getStructureMap();
                    String opeEndDate = structureMap.get("手术结束时间");
                    if (StringUtil.isBlank(opeEndDate)) {
                        opeEndDate = structureMap.get("记录时间");
                    }
                    String regex = ".*\\d.*";
                    if (!opeEndDate.matches(regex)) {
                        continue;
                    }
                    if (StringUtil.isNotBlank(opeEndDate) &&
                            StringUtil.parseDateTime(opeEndDate).before(StringUtil.parseDateTime(recordDateStr))) {
                        status.set("0");
                        return;
                    }
                }
            }
        }
        ThreeLevelWardLabel firstDirectLabel = firstDirectDoc.getThreeLevelWardLabel();
        if (firstDirectLabel == null) {
            return;
        }

        if (firstDirectLabel.getDiags().size() != 0 || StringUtil.isNotBlank(firstDirectLabel.getDiagText())) {
            status.set("0");
        }
    }

    private boolean regexFind(String content, String... str) {
        String s = "";
        for (String word : str) {
            s += word + ".*";
        }
        s = s.substring(0, s.lastIndexOf(".*"));
        Pattern p = Pattern.compile(s);
        Matcher m = p.matcher(content);
        return m.find();
    }
}