package com.lantone.qc.kernel.catalogue.yiwu.threelevelward;

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

/**
 * @ClassName : THR02900
 * @Description : 术后24小时内无主刀或一助查房记录
 * @Author : 胡敬
 * @Date: 2020-05-27 14:23
 */
@Component
public class THR02900 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getOperationDocs().size() == 0) {
            return;
        }
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        List<ThreeLevelWardDoc> allDoctorWradDocs = new ArrayList<>();
        if (inputInfo.getThreeLevelWardDocs() != null && inputInfo.getThreeLevelWardDocs().size() > 0) {
            allDoctorWradDocs = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs();
        }
        //标题
        String allTitle = "";
        //病情记录
        String allPathography = "";
        //一助或助手
        String firstAssistant = "";
        //手术结束时间
        String opeEndTime = "";
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> structureMap = operationRecordDoc.getStructureMap();
            String chiefSurgeon = structureMap.get("主刀医师");
            firstAssistant = structureMap.get("助手");
            opeEndTime = structureMap.get("结束时间");
            Date opeEndDate = CatalogueUtil.parseStringDate(opeEndTime);
            if (opeEndDate == null) {
                continue;
            }
            if (!CatalogueUtil.compareTime(
                    StringUtil.parseDateTime(opeEndTime),
                    StringUtil.parseDateTime(DateUtil.nowString()),
                    24 * 60L)) {//如果接收未超过6小时，规则不判断
                return;
            } else {
                if ((StringUtil.isNotEmpty(chiefSurgeon) || StringUtil.isNotEmpty(firstAssistant)) && ListUtil.isEmpty(allDoctorWradDocs)) {
                    status.set("-1");
                    return;
                }
            }
            if (ListUtil.isNotEmpty(allDoctorWradDocs)) {
                for (ThreeLevelWardDoc wardDoc : allDoctorWradDocs) {
                    Map<String, String> wardStructureMap = wardDoc.getStructureMap();
                    String recordDateStr = wardStructureMap.get("查房日期");
                    String recordTitle = wardStructureMap.get("查房标题");
                    String pathography = wardStructureMap.get("病情记录");
                    Date recordDate = CatalogueUtil.parseStringDate(recordDateStr);
                    if (StringUtil.isBlank(recordTitle) || StringUtil.isBlank(pathography) || recordDate == null) {
                        continue;
                    }
                    if (opeEndDate.before(recordDate) && !CatalogueUtil.compareTime(opeEndDate, recordDate, 24 * 60L)) {
                        allTitle += recordTitle;
                        allPathography += pathography;
                    }
                }
            }
            //查房标题中有主刀
            if (allTitle.contains("主刀")) {
                return;
            }
            //查房标题对比
            if (StringUtil.isNotBlank(chiefSurgeon) && !allTitle.contains(chiefSurgeon) &&
                    StringUtil.isNotBlank(firstAssistant) && !allTitle.contains(firstAssistant)) {
                status.set("-1");
                return;
            }
            //病情记录对比
            if ((StringUtil.isNotBlank(chiefSurgeon) && allPathography.contains(chiefSurgeon)) ||
                    (StringUtil.isNotBlank(firstAssistant) && allPathography.contains(firstAssistant))) {
                return;
            }
        }
    }
}