package com.lantone.qc.kernel.catalogue.operationdiscussion;

import com.lantone.qc.kernel.catalogue.QCCatalogue;
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.operation.OperationDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

/**
 * @Description: 手术患者缺术前讨论或术前小结
 * @author: 胡敬
 * @time: 2020/3/26 10:23
 */
@Component
public class OPE0370 extends QCCatalogue {
    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        status.set("0");
        //医嘱
        //        List<DoctorAdviceDoc> doctorAdviceDocs = inputInfo.getDoctorAdviceDocs();
        //        if (ListUtil.isEmpty(doctorAdviceDocs)) {
        //            return;
        //        }
        //        boolean isOperativePatient = CatalogueUtil.isOperativePatients(doctorAdviceDocs);
        boolean isOperativePatient = true;//是手术患者(暂时默认是)
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();

        boolean emergencyOperation = findEmergencyOperation(inputInfo);
        if (operationDocs == null || operationDocs.size() < 1) {
            return;
        }
        int i = 0, j = 0;
        for (OperationDoc operationDoc : operationDocs) {
            if (operationDoc.getOperationRecordDoc() != null) {
                i++;
            }
            if (operationDoc.getPreoperativeDiscussionDoc() != null) {
                j++;
            }
        }
        /* 如果首次病程录的诊疗计划里有急诊手术，则术前小结的数量加一 */
        if (emergencyOperation) {
            j++;
        }
        /* 手术记录数量和术前小结数量不一致则报错 */
        if (i != j) {
            status.set("-1");
            info.set("手术记录不一致");
        }
        //            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
        //            if (operationRecordDoc == null) {
        //                continue;
        //            }
        //            Map<String, String> operationRecordStructureMap = operationRecordDoc.getStructureMap();
        //            String preoperativeDiscussion = operationRecordStructureMap.get("术前讨论");
        //            if (CatalogueUtil.isEmpty(preoperativeDiscussion)) {
        //                status.set("-1");
        //                return;
        //            }
    }

    private boolean findEmergencyOperation(InputInfo inputInfo) {
        FirstCourseRecordDoc firstCourseRecordDoc = inputInfo.getFirstCourseRecordDoc();
        if (firstCourseRecordDoc != null) {
            String treatPlan = firstCourseRecordDoc.getStructureMap().get("诊疗计划");
            if (StringUtil.isNotBlank(treatPlan)) {
                String regex = ".*急诊.*术.*";
                return treatPlan.matches(regex);
            }
        }
        return false;
    }
}