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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.Content;
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.model.doc.ward.AttendingDoctorWardDoc;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName : THR0602
 * @Description : 主刀医师在术后48小时内无查房记录
 * @Author : 胡敬
 * @Date: 2020-03-30 14:03
 */
@Component
public class THR0602 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        long operationCount = operationDocs.stream().filter(operationDoc -> operationDoc.getOperationRecordDoc() != null).count();
        if (operationCount == 0 || ListUtil.isEmpty(threeLevelWardDocs)) {
            return;
        }
        //所有医师查房记录
        List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDocs.get(0).getAllDoctorWradDocs();
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> operationRecordStructureMap = operationRecordDoc.getStructureMap();
            String operationEndDateStr = operationRecordStructureMap.get("手术结束时间");
            if (CatalogueUtil.isEmpty(operationEndDateStr)) {
                continue;
            }

            //和系统当前时间做比较，如果未超过48小时，条目不做判断
            Date admisDate = StringUtil.parseDateTime(operationEndDateStr);
            boolean isExceed = CatalogueUtil.compareTime(admisDate, new Date(), 48 * 60L);
            if (!isExceed) {
                return;
            }

            Map<String, String> doctorRecord = extractWardRecord(
                    allDoctorWradDocs,
                    operationEndDateStr,
                    48 * 60);
            if(doctorRecord != null && doctorRecord.size()>0){
                if (!doctorRecord.containsKey("主刀")) {
                    status.set("-1");
                    return;
                }
            }

        }


    }

    /**
     * 抽取住院duration分钟内查房记录并取第一条主刀医师查房记录
     *
     * @param allDoctorWradDocs
     * @param admisTime
     * @param duration
     * @return
     */
    private static Map<String, String> extractWardRecord(List<ThreeLevelWardDoc> allDoctorWradDocs, String admisTime, int duration) {
        Map<Date, String> dateRecord = new HashMap<>();
        List<String> sortRecord = new ArrayList<>();
        Map<String, String> doctorRecord = new HashMap<>();
        String recordTime = "";
        Date admisDate = StringUtil.parseDateTime(admisTime);
        if (admisDate == null) {
            return doctorRecord;
        }
        for (ThreeLevelWardDoc attendingDoctorWardDoc : allDoctorWradDocs) {
            Map<String, String> threeLevelWardStructureMap = attendingDoctorWardDoc.getStructureMap();
            recordTime = threeLevelWardStructureMap.get("查房日期");
            Date recordDate = StringUtil.parseDateTime(recordTime);
            if (recordDate == null) {
                continue;
            }
            if (admisDate.before(recordDate) && !CatalogueUtil.compareTime(admisDate, recordDate, Long.valueOf(duration))) {
                String wardTitle = threeLevelWardStructureMap.get("查房标题");
                wardTitle = StringUtil.isBlank(wardTitle) ? "" : wardTitle;
                dateRecord.put(recordDate, wardTitle);
            }
        }
        dateRecord.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(
                x -> sortRecord.add(x.getValue())
        );
        //按时间排好序查房记录的第一条主刀医师查房记录存进doctorRecord
        for (String record : sortRecord) {
            if (!record.contains("主刀")) {
                continue;
            }
            doctorRecord.put("主刀", record);
            break;
        }
        return doctorRecord;
    }
}