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

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.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.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : DUT0599
 * @Description : 转科后病程录没有连续记录3天
 * @Author : 胡敬
 * @Date: 2020-03-30 15:06
 */
@Component
public class DUT0599 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        TransferRecordDoc transferRecordDoc = inputInfo.getTransferRecordDocs();
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        if (transferRecordDoc == null || ListUtil.isEmpty(threeLevelWardDocs)) {
            return;
        }
        //所有查房记录的日期天
        List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDocs.get(0).getAllDoctorWradDocs();
        List<Date> wardRecordDayList = getWardRecordDay(allDoctorWradDocs);
        String rollInRecordDateStr = "";
        //转入日期后应该有的查房记录的日期天
        List<Date> rollInDay = null;
        List<TransferIntoDoc> transferIntoDocs = transferRecordDoc.getTransferIntoDocs();
        for (TransferIntoDoc transferIntoDoc : transferIntoDocs) {
            Map<String, String> transferOutStructureMap = transferIntoDoc.getStructureMap();
            rollInRecordDateStr = transferOutStructureMap.get("转科日期");
            if (CatalogueUtil.isEmpty(rollInRecordDateStr)) {
                continue;
            }
            Date rollInRecordDate = StringUtil.parseDateTime(rollInRecordDateStr);
            if (inputInfo.getLeaveHospitalDoc() != null) {
                Map<String, String> leaveHospitalStructureMap = inputInfo.getLeaveHospitalDoc().getStructureMap();
                String dischargeTime = leaveHospitalStructureMap.get(Content.dischargeTime);
                //如果住院天数小于3天则不判断该条规则
                if ( rollInRecordDate != null && StringUtil.isNotBlank(dischargeTime)) {
                    if (!CatalogueUtil.compareTime(rollInRecordDate, StringUtil.parseDateTime(dischargeTime), 3 * 24 * 60L)) {
                        continue;
                    }
                }
            }
            for (int i = 1; i <= 3; i++) {
                rollInDay = new ArrayList<>();
                Date wardRecordDay = DateUtil.dateZeroClear(DateUtil.addDate(rollInRecordDate, i));
                rollInDay.add(wardRecordDay);
            }
            if (!wardRecordDayList.containsAll(rollInDay)) {
                status.set("-1");
                return;
            }
        }
    }

    /**
     * 所有查房记录的日期天
     *
     * @param threeLevelWardDocs
     */
    private List<Date> getWardRecordDay(List<ThreeLevelWardDoc> threeLevelWardDocs) {
        List<Date> dateRecordDay = new ArrayList<>();
        String recordTime = "";
        for (ThreeLevelWardDoc threeLevelWardDoc : threeLevelWardDocs) {
            Map<String, String> rescueStructureMap = threeLevelWardDoc.getStructureMap();
            recordTime = rescueStructureMap.get("查房日期");
            if (StringUtil.isBlank(recordTime)){
                continue;
            }
            Date recordDate = StringUtil.parseDateTime(recordTime);
            if (recordDate == null) {
                continue;
            }
            dateRecordDay.add(DateUtil.dateZeroClear(recordDate));
        }
        return dateRecordDay;
    }
}