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

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.ThreeLevelWardDoc;
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.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 手术患者无术前讨论记录
 * @author: rengb
 * @time: 2020/3/23 15:09
 */
@Component
public class PRE0328 extends QCCatalogue {

    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        status.set("0");
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (operationDocs == null || operationDocs.size() == 0) {
            return;
        }
        if (inputInfo.getBeHospitalizedDoc() != null
                && inputInfo.getBeHospitalizedDoc().getStructureMap().get("现病史") != null
                && inputInfo.getBeHospitalizedDoc().getStructureMap().get("现病史").contains("急诊手术")) {
            return;
        }
        boolean emergencyOperation = findEmergencyOperation(inputInfo);
        boolean emergencyOperationFromWardRecord = findEmergencyOperationFromWardRecord(inputInfo);

        int i = getOperationSum(operationDocs), j = 0;
        for (OperationDoc operationDoc : operationDocs) {
            if (operationDoc.getPreoperativeDiscussionDoc() != null) {
                j++;
            }
        }
        /* 如果首次病程录的诊疗计划里有急诊手术，则术前小结的数量加一 */
        if (emergencyOperation || emergencyOperationFromWardRecord) {
            if (j == 0) {
                j++;
            }
        }
        if (i > 0 && i > j) {
            status.set("-1");
            info.set("手术记录不一致");
        }
    }

    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;
    }

    private boolean findEmergencyOperationFromWardRecord(InputInfo inputInfo) {
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        if (threeLevelWardDocs.size() == 0) {
            return false;
        }
        List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDocs.get(0).getAllDoctorWradDocs();
        for (ThreeLevelWardDoc threeLevelWardDoc : allDoctorWradDocs) {
            String content = threeLevelWardDoc.getStructureMap().get("病情记录");
            // 根据医学部要求，加入“急症”和“术”
            if (StringUtil.isNotBlank(content)) {
                String regex = ".*急诊.*术.*|.*急症.*术.*";
                if (content.matches(regex)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取手术记录次数
     * @param operationDocs
     * @return
     */
    private int getOperationSum(List<OperationDoc> operationDocs) {
        List<Map<String, Date>> operationDateList = new ArrayList<>();
        Map<String, Date> operationDate = null;
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> structureMap = operationRecordDoc.getStructureMap();
            String operationStartDateStr = structureMap.get("手术开始时间");
            String operationEndDateStr = structureMap.get("手术结束时间");
            if (StringUtil.isNotBlank(operationStartDateStr) && StringUtil.isNotBlank(operationEndDateStr)) {
                Date operationStartDate = StringUtil.parseDateTime(operationStartDateStr);
                Date operationEndDate = StringUtil.parseDateTime(operationEndDateStr);
                if (operationStartDate != null && operationEndDate != null) {
                    /* 放第一个手术记录的时间到operationDateList */
                    if (operationDateList.size() == 0) {
                        operationDate = new HashMap<>();
                        operationDate.put("手术开始时间", operationStartDate);
                        operationDate.put("手术结束时间", operationEndDate);
                        operationDateList.add(operationDate);
                        continue;
                    }
                    /* 如果其中一个手术记录的开始时间到结束时间之间还包含另一个手术，就不往operationDateList里加 */
                    boolean findInnerOperation = false;
                    for (Map<String, Date> date : operationDateList) {
                        Date listStartDate = date.get("手术开始时间");
                        Date listEndDate = date.get("手术结束时间");
                        if (operationStartDate.before(listStartDate) && listEndDate.before(operationEndDate)){
                            date.put("手术开始时间", operationStartDate);
                            date.put("手术结束时间", operationEndDate);
                        }
                        if (listStartDate.before(operationStartDate) && operationEndDate.before(listEndDate)) {
                            findInnerOperation = true;
                            break;
                        }
                    }
                    if (!findInnerOperation) {
                        operationDate = new HashMap<>();
                        operationDate.put("手术开始时间", operationStartDate);
                        operationDate.put("手术结束时间", operationEndDate);
                        operationDateList.add(operationDate);
                    }
                }
            }
        }
        return operationDateList.size();
    }

}