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.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Description: 手术患者无术前讨论记录(邵逸夫)
 * @author: zhoutg
 * @time: 2020/3/23 15:09
 */
@Component
public class PRE03025 extends QCCatalogue {

    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        /**
         * 1：入院记录【现病史】包含".*急诊.*术.*|.*急症.*术.*"不报错。
         * 2：如果术前讨论、术前小结次数条数为0 且( 首次病程录【诊疗计划】或查房记录【病情记录】包含".*急诊.*术.*|.*急症.*术.*")，则术前讨论、术前小结次数+1。
         * 3：如果手术记录次数（同一日期天多次手术算1次） 大于 术前讨论、术前小结次数，则出错
         */
        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("现病史").matches(".*急诊.*术.*|.*急症.*术.*")) {
                && match_word(inputInfo.getBeHospitalizedDoc().getStructureMap().get("现病史"))) {
            return;
        }
        //判断首程里是否有急诊
        boolean emergencyOperation = findEmergencyOperation(inputInfo);

        boolean emergencyOperationFromWardRecord = findEmergencyOperationFromWardRecord(inputInfo);
        if(emergencyOperation || emergencyOperationFromWardRecord){
            return;
        }


        int i = getOperationSum(operationDocs); // 获取手术记录次数
        int j = 0;  // 获取术前讨论、术前小结次数
        for (OperationDoc operationDoc : operationDocs) {
            if (operationDoc.getPreoperativeDiscussionDoc() != null) {
                j++;
            }
        }
        /* 如果首次病程录的诊疗计划里有急诊手术 或 查房记录的病情记录里有急诊手术，则术前小结的数量加1 */
        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)) {
                return match_word(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) {
        int num = 0;
        List<String> dateStrList = new ArrayList<>();
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> structureMap = operationRecordDoc.getStructureMap();
            String dateStr = structureMap.get("手术日期");

            if (StringUtil.isNotBlank(dateStr)) {
                if (!dateStrList.contains(dateStr)) {
                    dateStrList.add(dateStr);
                    num++;
                }
            }
        }
        return num;
    }

    private boolean match_word(String content){
        String regex = "(?<=急诊)(.+)(?=术)";
        Pattern p = Pattern.compile(regex);
        boolean b = p.matcher(content).find();
        return b;
    }
}