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

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.StagesSummaryDoc;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDiscussionDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.model.doc.transferrecord.TransferIntoDoc;
import com.lantone.qc.pub.model.doc.transferrecord.TransferOutDoc;
import com.lantone.qc.pub.model.doc.transferrecord.TransferRecordDoc;
import com.lantone.qc.pub.util.DateUtil;
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;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName : THR0588
 * @Description : 住院期间连续3天无病程记录
 * @Author : 王世延
 * @Date: 2020-09-8 14:22
 */
@Component
public class THR0588 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        Date leaveDate = null;
        if (inputInfo.getMedicalRecordInfoDoc() != null) {
            Map<String, String> medicalRecordInfoStructureMap = inputInfo.getMedicalRecordInfoDoc().getStructureMap();
            String leaveHospitalTime = medicalRecordInfoStructureMap.get("leaveHospitalDate");
            leaveDate = StringUtil.parseDateTime(leaveHospitalTime);
            if (leaveDate == null) {
//                LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
//                if (leaveHospitalDoc != null) {
//                    leaveDate = StringUtil.parseDateTime(leaveHospitalDoc.getStructureMap().get("出院日期"));
//                }
                leaveDate = new Date();
            }
        }
        //查房记录
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        if (threeLevelWardDocs.size() == 0) {
            return;
        }
        //所有查房记录的日期天
        List<Date> dateThreeLevelDay = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        //所有的查房记录
        List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDocs.get(0).getAllDoctorWradDocs();
        for (ThreeLevelWardDoc doc : allDoctorWradDocs) {
            Date threeLevelDate = StringUtil.parseDateTime(doc.getStructureMap().get("查房日期"));
            if (threeLevelDate == null) {
                continue;
            }
            dateThreeLevelDay.add(threeLevelDate);
        }
        /******************************************术后首程********************************************************/
        if (inputInfo.getOperationDocs().size() > 0) {
            List<String> recordDateList = inputInfo.getOperationDocs().stream().map(OperationDoc::getOperationDiscussionDoc).filter(Objects::nonNull)
                    .map(OperationDiscussionDoc::getStructureMap).filter(i -> StringUtil.isNotBlank(i.get("病历日期")))
                    .map(i -> i.get("病历日期")).collect(Collectors.toList());
            for (String recordDateStr : recordDateList) {
                Date recordDate = StringUtil.parseDateTime(recordDateStr);
                if (recordDate == null) {
                    continue;
                }
                dateThreeLevelDay.add(recordDate);
            }
        }
        /******************************************转入转出********************************************************/
        if (inputInfo.getTransferRecordDocs() != null) {
            TransferRecordDoc transferRecordDocs = inputInfo.getTransferRecordDocs();
            List<String> intoRecordDateList = transferRecordDocs.getTransferIntoDocs()
                    .stream().map(TransferIntoDoc::getStructureMap).filter(i -> StringUtil.isNotBlank(i.get("病历日期")))
                    .map(i -> i.get("病历日期")).collect(Collectors.toList());
            for (String recordDateStr : intoRecordDateList) {
                Date recordDate = StringUtil.parseDateTime(recordDateStr);
                if (recordDate == null) {
                    continue;
                }
                dateThreeLevelDay.add(recordDate);
            }
            intoRecordDateList = transferRecordDocs.getTransferOutDocs()
                    .stream().map(TransferOutDoc::getStructureMap).filter(i -> StringUtil.isNotBlank(i.get("病历日期")))
                    .map(i -> i.get("病历日期")).collect(Collectors.toList());
            for (String recordDateStr : intoRecordDateList) {
                Date recordDate = StringUtil.parseDateTime(recordDateStr);
                if (recordDate == null) {
                    continue;
                }
                dateThreeLevelDay.add(recordDate);
            }
        }
        /******************************************阶段小结********************************************************/
        if (inputInfo.getStagesSummaryDocs().size() > 0) {
            List<String> intoRecordDateList = inputInfo.getStagesSummaryDocs().stream().map(StagesSummaryDoc::getStructureMap)
                    .filter(i -> StringUtil.isNotBlank(i.get("病历日期")))
                    .map(i -> i.get("病历日期")).collect(Collectors.toList());
            for (String recordDateStr : intoRecordDateList) {
                Date recordDate = StringUtil.parseDateTime(recordDateStr);
                if (recordDate == null) {
                    continue;
                }
                dateThreeLevelDay.add(recordDate);
            }
        }
        dateThreeLevelDay = dateThreeLevelDay.stream().sorted().collect(Collectors.toList());
        int timeCha = 259200000;
        //获取连续3天无查房记录的时间
        for (int i = 0; i < dateThreeLevelDay.size(); i++) {
            if (i + 1 < dateThreeLevelDay.size()) {
                if (DateUtil.dateZeroClear(dateThreeLevelDay.get(i + 1)).getTime() - DateUtil.dateZeroClear(dateThreeLevelDay.get(i)).getTime() > timeCha &&
                        dateThreeLevelDay.get(i + 1).before(leaveDate)) {
                    infoAppend(sb, dateThreeLevelDay.get(i), dateThreeLevelDay.get(i + 1));
                }
            }
        }
        if (sb.toString().length() > 0) {
            status.set("-1");
            info.set(sb.toString().substring(0, sb.toString().length() - 1));
        }
    }

    /**
     * 拼接提示信息
     *
     * @param sb
     * @param bfDate
     * @param afDate
     */
    private void infoAppend(StringBuffer sb, Date bfDate, Date afDate) {
        sb.append("(").append(DateUtil.formatDate(bfDate))
                .append("->").append(DateUtil.formatDate(afDate)).append(")").append("、");
    }
}