package com.lantone.qc.kernel.catalogue.beilun.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.apache.commons.lang3.time.DateUtils;
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;
            }
            if (!isDate(operationStartDateStr) || !isDate(operationEndDateStr)) {
                continue;
            }
            Date operationStartDate = StringUtil.parseDateTime(operationStartDateStr);
            Date operationEndDate = StringUtil.parseDateTime(operationEndDateStr);
            for (OperationDoc o : operationDocs) {
                OperationDiscussionDoc operationDiscussionDoc = o.getOperationDiscussionDoc();
                if (operationDiscussionDoc == null) {
                    continue;
                }
                Map<String, String> structureMap = operationDiscussionDoc.getStructureMap();
                String discussionRecordDateStr = structureMap.get("病历日期");
                if (StringUtil.isBlank(discussionRecordDateStr)) {
                    status.set("0");
                    return;
                }

                if (!isDate(discussionRecordDateStr)) {
                    status.set("0");
                    return;
                }
                Date discussionRecordDate = StringUtil.parseDateTime(discussionRecordDateStr);
                if (operationEndDate.before(discussionRecordDate) || DateUtils.isSameDay(operationEndDate, discussionRecordDate)) {
                    if (CatalogueUtil.compareTime(operationEndDate, discussionRecordDate, 2 * 60L)) {
                        status.set("-1");
                        return;
                    }
                }
            }
        }
    }

    /**
     * 判断是否为日期
     *
     * @param dateStr
     */
    private boolean isDate(String dateStr) {
        try {
            StringUtil.parseDateTime(dateStr);
            return true;
        } catch (IllegalArgumentException e) {
            return false;
        }
    }
}