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

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.DoctorAdviceDoc;
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.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 {
        /**
         * 1：如果急诊手术【入院时间-手术开始时间小于30分钟】则可不用写术前小结。
         * 2：如果入院时间-手术开始时间小于30分钟，则术前讨论、术前小结次数+1。
         * 3：如果手术记录次数（第一次手术的日期内有其他手术不算次数） 大于 术前讨论、术前小结次数，则出错
         */
        status.set("0");
        List<DoctorAdviceDoc> doctorAdviceDocs = inputInfo.getDoctorAdviceDocs();
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (operationDocs == null || operationDocs.size() == 0) {
            return;
        }
        boolean emergencyOperation = false;
        if (inputInfo.getBeHospitalizedDoc() != null) {
            Map<String, String> beHospitalStructureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
            String admisTime = beHospitalStructureMap.get("入院日期");
            //取手术记录第一次手术开始时间和入院时间比较，相差30分钟内不报缺陷
            OperationRecordDoc operationRecordDoc = operationDocs.get(0).getOperationRecordDoc();
            String startTime = "";
            if (operationRecordDoc != null) {
                startTime = operationRecordDoc.getStructureMap().get("手术开始时间");
            }
            if (StringUtil.isNotBlank(startTime) && StringUtil.isNotBlank(admisTime) &&
                    !startTime.contains("年月日") && !startTime.contains("时分") && DateUtil.parseDateTime(startTime) != null) {
                if (!CatalogueUtil.compareTime(
                        StringUtil.parseDateTime(admisTime),
                        StringUtil.parseDateTime(startTime),
                        Long.valueOf(30))) {//入院到手术未超过30分钟，则可不用写术前小结
                    emergencyOperation = true;
                }
            }
        }

        int i = getOperationSum(operationDocs); // 获取手术记录次数
        int j = 0;  // 获取术前讨论、术前小结次数
        for (OperationDoc operationDoc : operationDocs) {
            if (operationDoc.getPreoperativeDiscussionDoc() != null) {
                j++;
            }
        }
        /* 如果入院时间-手术开始时间小于30分钟，则术前讨论、术前小结次数+1*/
        if (emergencyOperation) {
            if (j == 0) {
                j++;
            }
        }
        //医嘱中包含“冠状动脉造影术”，且无术前讨论.则报规则
        for (DoctorAdviceDoc doctorAdviceDoc : doctorAdviceDocs) {
            Map<String, String> doctorAdviceStructuerMap = doctorAdviceDoc.getStructureMap();
            String advicename = doctorAdviceStructuerMap.get("医嘱项目名称");
            if (StringUtil.isNotBlank(advicename) && advicename.contains("冠状动脉造影术") && j == 0) {
                status.set("-1");
                info.set("手术记录不一致");
            }
        }

        if (i > 0 && i > j) {
            status.set("-1");
            info.set("手术记录不一致");
        }
    }

    /**
     * 获取手术记录次数
     *
     * @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) && DateUtil.parseDateTime(operationStartDateStr) != null) {
                Date operationStartDate = StringUtil.parseDateTime(operationStartDateStr);
                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();
    }

}