package com.lantone.qc.kernel.catalogue.xiamen.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.FirstCourseRecordDoc;
import com.lantone.qc.pub.model.doc.MedicalRecordInfoDoc;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.util.ListUtil;
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.List;
import java.util.Map;

/**
 * @Description: 手术患者缺术前主刀医师查房记录
 * @author: WANGSY
 * @time: 2020/11/11 11:22
 */
@Component
public class OPE0369 extends QCCatalogue {

    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        status.set("0");
        MedicalRecordInfoDoc medicalRecordInfoDoc = inputInfo.getMedicalRecordInfoDoc();
        String admisTime = "";
        if (medicalRecordInfoDoc != null && medicalRecordInfoDoc.getStructureMap() != null) {
            //入院日期
            admisTime = medicalRecordInfoDoc.getStructureMap().get("behospitalDate");
            if (CatalogueUtil.isEmpty(admisTime)) {
                return;
            }
        }
        //先判断是否有手术记录
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (operationDocs == null || operationDocs.size() == 0) {
            return;
        }
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        long operationCount = operationDocs.stream().filter(operationDoc -> operationDoc.getOperationRecordDoc() != null).count();
        //主刀查房次数
        long operateCount = 0;
        if (operationCount > 0) {
            //存在手术记录无查房记录
            if (ListUtil.isEmpty(threeLevelWardDocs)) {
                status.set("-1");
                return;
            }
            String operationStartDate = "";
            String operatorPhysician = "";
            List<Date> operDateList = new ArrayList<>();
            for (OperationDoc operationDoc : operationDocs) {
                if (operationDoc.getOperationRecordDoc() != null) {
                    Map<String, String> operationDocStructureMap = operationDoc.getOperationRecordDoc().getStructureMap();
                    String opeStartDate = operationDocStructureMap.get("手术日期");
                    String opeStartTime = operationDocStructureMap.get("开始时间");
                    operatorPhysician = operationDocStructureMap.get("主刀医师");
                    if (StringUtil.isNotBlank(opeStartDate)) {
                        operationStartDate = opeStartDate;
                        if (StringUtil.isNotBlank(opeStartTime)) {
                            operationStartDate = opeStartDate + " " + opeStartTime;
                        }
                    }
                    if (StringUtil.isNotBlank(operationStartDate)) {
                        if (!CatalogueUtil.compareTime(
                                StringUtil.parseDateTime(admisTime),
                                StringUtil.parseDateTime(operationStartDate),
                                Long.valueOf(30))) {
                            continue;
                        }
                        operDateList.add(StringUtil.parseDateTime(operationStartDate));
                    } else {//取不到手术时间
                        return;
                    }
                }
            }

            if (operDateList.size() > 1) {
                for (int i = 0; i < operDateList.size(); i++) {
                    if (i + 1 < operDateList.size()) {
                        if (!CatalogueUtil.compareTime(operDateList.get(i), operDateList.get(i + 1),
                                Long.valueOf(24 * 60))) {//如果手术记录是同一天，需有一次术前主刀查房
                            operationCount--;
                        }
                    }
                }
            }

            //判断是否为急诊手术
            if (findEmergencyOperation(inputInfo)) {
                return;
//                operationCount--;
            }

            ThreeLevelWardDoc threeLevelWardDoc = threeLevelWardDocs.get(0);
            List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDoc.getAllDoctorWradDocs();
            for (ThreeLevelWardDoc threeLevelWard : allDoctorWradDocs) {
                Map<String, String> structureMap = threeLevelWard.getStructureMap();
                String makeTitle = structureMap.get("查房标题");
                String writTitle = structureMap.get("文书标题");
                String recordDateStr = structureMap.get("查房日期");
                if (operDateList.size() > 0) {
                    for (Date date : operDateList) {
                        if (StringUtil.isNotBlank(recordDateStr) && date != null
                                && StringUtil.parseDateTime(recordDateStr).before(date)) {
                            if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(recordDateStr), date, Long.valueOf(24 * 60))
                                    && ((makeTitle.contains("主刀") || (StringUtil.isNotBlank(writTitle) && writTitle.contains("主刀")))
                                    || (makeTitle.contains("术前") || (StringUtil.isNotBlank(writTitle) && writTitle.contains("术前")))
                                    || (StringUtil.isNotBlank(operatorPhysician) && makeTitle.contains(operatorPhysician))
                            )) {
                                operateCount++;
                            }
                        }
                    }
                } else {//手术开始时间跟入院时间 小于30分钟，规则不判断
                    return;
                }
            }
        }

        if (operationCount > 0 && operationCount > operateCount) {
            status.set("-1");
            return;
        }
    }

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