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.AnesthesiaRelatedDoc;
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.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 手术记录手术结束时间与手麻系统记录不一致
 * @author: 胡敬
 * @time: 2020/5/11 16:32
 */
@Component
public class OPE02892 extends QCCatalogue {

    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        status.set("0");
        //        boolean isOperativePatient = true;//是手术患者(暂时默认是)
        //        if (isOperativePatient) {
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        List<AnesthesiaRelatedDoc> anesthesiaRelatedDocs = inputInfo.getAnesthesiaRelatedDocs();
        if (operationDocs == null || operationDocs.size() == 0
                || anesthesiaRelatedDocs == null || anesthesiaRelatedDocs.size() == 0) {
            return;
        }

        for (AnesthesiaRelatedDoc anesthesiaRelatedDoc : anesthesiaRelatedDocs) {
            Map<String, String> anesthesiaStructureMap = anesthesiaRelatedDoc.getStructureMap();
            String anesOperationName = anesthesiaStructureMap.get("手术名称");
            String anesOperationEndDateStr = anesthesiaStructureMap.get("手术结束时间");
            if (StringUtil.isBlank(anesOperationName)) {
                continue;
            }
            anesOperationName = StringUtil.removeBlank(anesOperationName);
            if (StringUtil.isBlank(anesOperationEndDateStr)) {
                continue;
            }
            Date anesOperationEndDate = StringUtil.parseDateTime(anesOperationEndDateStr);
            if (anesOperationEndDate == null) {
                continue;
            }
            Map<String, Date> startEndDate = getStartEndDate(operationDocs, anesOperationName);
            if (startEndDate.get("手术结束时间") != null) {
                Date operationEndDate = startEndDate.get("手术结束时间");
                if (!operationEndDate.equals(anesOperationEndDate)) {
                    status.set("-1");
                    return;
                }
            }
        }
    }
    //    }

    /**
     * 获取手术开始时间和结束时间
     *
     * @param operationDocs
     * @param anesOperationName
     * @return
     */
    private Map<String, Date> getStartEndDate(List<OperationDoc> operationDocs, String anesOperationName) {
        Map<String, Date> startEndDateMap = new HashMap<>();
        Date startDate = null, endDate = null;
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> operationStructureMap = operationRecordDoc.getStructureMap();
            String operationName = operationStructureMap.get("手术名称");
            String operationStartDateStr = operationStructureMap.get("手术开始时间");
            String operationEndDateStr = operationStructureMap.get("手术结束时间");
            if (StringUtil.isBlank(operationName)) {
                continue;
            }
            operationName = StringUtil.removeBlank(operationName);
            if (withInOneDay(operationStartDateStr, operationEndDateStr)) {
                if (anesOperationName.contains(operationName)) {
                    Date operationStartDate = StringUtil.parseDateTime(operationStartDateStr);
                    if (startDate == null || operationStartDate.before(startDate)) {
                        startDate = operationStartDate;
                    }
                    Date operationEndDate = StringUtil.parseDateTime(operationEndDateStr);
                    if (endDate == null || operationEndDate.after(endDate)) {
                        endDate = operationEndDate;
                    }
                }
            }
        }
        if (startDate != null) {
            startEndDateMap.put("手术开始时间", startDate);
        }
        if (endDate != null) {
            startEndDateMap.put("手术结束时间", endDate);
        }
        return startEndDateMap;
    }

    /**
     * 确认手术开始时间->手术结束时间是否为24小时内
     *
     * @param firstDateStr
     * @param secondDateStr
     * @return
     */
    private boolean withInOneDay(String firstDateStr, String secondDateStr) {
        if (StringUtil.isBlank(firstDateStr) && StringUtil.isBlank(secondDateStr)) {
            return false;
        }
        Date firstDate = StringUtil.parseDateTime(firstDateStr);
        Date secondDate = StringUtil.parseDateTime(secondDateStr);
        if (firstDate == null || secondDate == null) {
            return false;
        }
        return firstDate.before(secondDate) && !CatalogueUtil.compareTime(firstDate, secondDate, 24 * 60L);
    }

}