package com.lantone.qc.kernel.catalogue.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.DeathCaseDiscussDoc;
import com.lantone.qc.pub.model.doc.DeathRecordDoc;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : THR02931
 * @Description : 最后一次查房记录时间晚于患者出院时间
 * @Author : 胡敬
 * @Date: 2020-06-16 10:06
 */
@Component
public class THR02931 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getThreeLevelWardDocs().size() == 0 || inputInfo.getLeaveHospitalDoc() == null) {
            return;
        }
        DeathRecordDoc deathRecordDoc = inputInfo.getDeathRecordDoc();
        DeathCaseDiscussDoc deathCaseDiscussDoc = inputInfo.getDeathCaseDiscussDoc();
        if (deathRecordDoc != null || deathCaseDiscussDoc != null) {
            return;
        }
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        String leaveDateStr = leaveHospitalDoc.getStructureMap().get("出院时间");
        if (StringUtil.isBlank(leaveDateStr)) {
            return;
        }
        leaveDateStr = leaveDateStr.split(" ")[0];
        Date leaveDate = StringUtil.parseDateTime(leaveDateStr);
        if (leaveDate == null) {
            return;
        }

        List<ThreeLevelWardDoc> allDoctorWradDocs = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs();
        if (allDoctorWradDocs.size() == 0) {
            return;
        }
        int j = 0; //最后两次查房记录
        for (int i = allDoctorWradDocs.size() - 1; i > 0 && j < 2; i--) {
            j++;
            ThreeLevelWardDoc lastWardDoc = allDoctorWradDocs.get(i);
            Map<String, String> structureMap = lastWardDoc.getStructureMap();
            String title = structureMap.get("查房标题");
            if (StringUtil.isBlank(title) || title.contains("病理报告")) {
                continue;
            }
            String recordDateStr = structureMap.get("查房日期");
            if (StringUtil.isNotBlank(recordDateStr)) {
                recordDateStr = recordDateStr.split(" ")[0];
                Date recordDate = StringUtil.parseDateTime(recordDateStr);
                if (recordDate != null && recordDate.after(leaveDate)) {
                    status.set("-1");
                    return;
                }
            }
        }
    }
}