package com.lantone.qc.kernel.catalogue.ninghaiyiyi.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.MedicalRecordInfoDoc;
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.DateUtil;
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.HashMap;
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");
        if (outputInfo.getResult() != null) {
            Map<String, Object> thr03090 = outputInfo.getResult().get("THR03090");
            if (thr03090 != null && thr03090.size() > 0 && thr03090.get("status").equals("-1")) {
                return;
            }
        }

        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();
        int operationCount = getOperationSum(operationDocs); // 获取手术记录次数
//        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 = "";
            List<Date> operDateList = new ArrayList<>();
            for (OperationDoc operationDoc : operationDocs) {
                if (operationDoc.getOperationRecordDoc() != null) {
                    Map<String, String> operationDocStructureMap = operationDoc.getOperationRecordDoc().getStructureMap();
                    operationStartDate = operationDocStructureMap.get("手术日期");
                    if (StringUtil.isNotBlank(operationStartDate)) {
                        if (operationStartDate.contains("年月日")) {
                            continue;
                        }
                        if (operationStartDate.contains("结束")) {
                            operationStartDate = operationStartDate.substring(0, operationStartDate.indexOf("结束")).replace("开始:", "");
                        }
                        //取不到时间不判断规则
                        if (StringUtil.parseDateTime(operationStartDate) == null) {
                            continue;
                        }
                        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--;
                        }
                    }
                }
            }

            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))
                                    && (((StringUtil.isNotBlank(makeTitle) && makeTitle.contains("主刀")) || (StringUtil.isNotBlank(writTitle) && writTitle.contains("主刀")))
                                    || ((StringUtil.isNotBlank(makeTitle) && makeTitle.contains("术前")) || (StringUtil.isNotBlank(writTitle) && writTitle.contains("术前"))))) {
                                operateCount++;
                            }
                        }
                    }
                } else {//手术开始时间跟入院时间 小于30分钟，规则不判断
                    return;
                }
            }
        }

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


    /**
     * 获取手术记录次数
     *
     * @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("手术日期");
            if (StringUtil.isNotBlank(operationStartDateStr) && !operationStartDateStr.contains("年月日")) {
                if (operationStartDateStr.contains("结束")) {
                    operationStartDateStr = operationStartDateStr.substring(0, operationStartDateStr.indexOf("结束")).replace("开始:", "");
                }
                Date operationStartDate = StringUtil.parseDateTime(operationStartDateStr);
                if(operationStartDate == null){
                    continue;
                }
                operationStartDate = DateUtil.dateZeroClear(operationStartDate);
                if (operationStartDate != null) {
                    /* 放第一个手术记录的日期到operationDateList */
                    if (operationDateList.size() == 0) {
                        operationDate = new HashMap<>();
                        operationDate.put("手术开始日期", operationStartDate);
                        operationDateList.add(operationDate);
                        continue;
                    }
                    /* 如果其中一个手术记录的开始时间到结束时间之间还包含另一个手术，就不往operationDateList里加 */
                    boolean findInnerOperation = false;
                    for (Map<String, Date> date : operationDateList) {
                        Date listStartDate = DateUtil.dateZeroClear(date.get("手术开始日期"));
                        if (listStartDate.equals(operationStartDate)) {
                            findInnerOperation = true;
                            break;
                        }
                    }
                    if (!findInnerOperation) {
                        operationDate = new HashMap<>();
                        operationDate.put("手术开始日期", operationStartDate);
                        operationDateList.add(operationDate);
                    }
                }
            }
        }
        return operationDateList.size();
    }
}