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.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
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;

/**
 * @ClassName : THR0596
 * @Description : 病危患者每天应有一次病程记录
 * @Author : 胡敬
 * @Date: 2020-03-28 14:22
 */
@Component
public class THR0596 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getCriticallyIllNoticeDocs().size() == 0 || inputInfo.getThreeLevelWardDocs().size() == 0) {
            return;
        }
        //所有查房记录的日期天
        List<Date> dateRecordDay = new ArrayList<>();
        //危重患者应该有的查房记录的日期天
        List<Date> criticallyDay = new ArrayList<>();
        Date criticallyStartDate = null, criticallyEndDate = null;
        String recordTime = "", content = "";
        List<ThreeLevelWardDoc> allDoctorWradDocs = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs();//查房记录
        for (ThreeLevelWardDoc threeLevelWardDoc : allDoctorWradDocs) {
            Map<String, String> rescueStructureMap = threeLevelWardDoc.getStructureMap();
            recordTime = rescueStructureMap.get("查房日期");
            content = CatalogueUtil.subTitle(rescueStructureMap.get("病情记录"));
            Date recordDate = StringUtil.parseDateTime(recordTime);
            if (recordDate == null) {
                continue;
            }
            if (content.contains("病重") || content.contains("病危")) {
                criticallyStartDate = recordDate;
            } else if (content.contains("病情稳定") || content.contains("病情好转")) {
                criticallyEndDate = recordDate;
            }
            dateRecordDay.add(DateUtil.dateZeroClear(recordDate));
        }
        if (criticallyStartDate == null || criticallyEndDate == null) {
            return;
        }
        int daynum = DateUtil.getDay(criticallyEndDate) - DateUtil.getDay(criticallyStartDate);
        for (int i = 1; i <= daynum; i++) {
            Date wardDay = DateUtil.dateZeroClear(DateUtil.addDate(criticallyStartDate, i));
            criticallyDay.add(wardDay);
        }
        if (!dateRecordDay.containsAll(criticallyDay)) {
            status.set("-1");
        }
    }
}