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

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.OperationDiscussionDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.model.doc.operation.OperationRecordDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : OPE02930
 * @Description : 术后首次病程未即刻完成
 * @Author : 胡敬
 * @Date: 2020-06-15 17:30
 */
@Component
public class OPE02930 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        //        boolean isOperativePatient = true;//是手术患者(暂时默认是)
        //        if (isOperativePatient) {
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (operationDocs == null || operationDocs.size() == 0) {
            status.set("0");
            return;
        }
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> operationRecordStructureMap = operationRecordDoc.getStructureMap();
            String operationStartDateStr = operationRecordStructureMap.get("手术开始时间");
            String operationEndDateStr = operationRecordStructureMap.get("手术结束时间");
            if (StringUtil.isEmpty(operationStartDateStr) || StringUtil.isEmpty(operationEndDateStr)) {
                continue;
            }
            Date operationStartDate = StringUtil.parseDateTime(operationStartDateStr);
            Date operationEndDate = StringUtil.parseDateTime(operationEndDateStr);
            if (operationStartDate == null || operationEndDate == null) {
                continue;
            }
            for (OperationDoc o : operationDocs) {
                OperationDiscussionDoc operationDiscussionDoc = o.getOperationDiscussionDoc();
                Map<String, String> structureMap = operationDiscussionDoc.getStructureMap();
                String discussionStartDateStr = structureMap.get("手术开始时间");
                String discussionEndDateStr = structureMap.get("手术结束时间");
                if (StringUtil.isBlank(discussionStartDateStr) || StringUtil.isBlank(discussionEndDateStr)) {
                    continue;
                }
                Date discussionStartDate = StringUtil.parseDateTime(discussionStartDateStr);
                Date discussionEndDate = StringUtil.parseDateTime(discussionEndDateStr);
                if (discussionStartDate == null || discussionEndDate == null) {
                    continue;
                }
                if (operationStartDate.equals(discussionStartDate) && operationEndDate.equals(discussionEndDate)) {
                    String discussionRecordDateStr = structureMap.get("记录时间");
                    if (StringUtil.isBlank(discussionRecordDateStr)) {
                        status.set("0");
                        return;
                    }
                    Date discussionRecordDate = StringUtil.parseDateTime(discussionRecordDateStr);
                    if (discussionRecordDate == null) {
                        status.set("0");
                        return;
                    }

                    if (CatalogueUtil.compareTime(discussionEndDate, discussionRecordDate, 15 * 60L)) {
                        status.set("-1");
                        return;
                    }
                }
            }
        }
        //        }
    }
}