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.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.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.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");
        List<TransferRecordDoc> transferRecordDocs = inputInfo.getTransferRecordDocs();
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        if (ListUtil.isEmpty(transferRecordDocs) || ListUtil.isEmpty(threeLevelWardDocs)) {
            return;
        }
        //所有查房记录的日期天
        List<Integer> wardRecordDayList = getWardRecordDay(threeLevelWardDocs);
        String rollInRecordDateStr = "";
        //转入日期后应该有的查房记录的日期天
        List<Integer> rollInDay = null;
        for (TransferRecordDoc transferRecordDoc : transferRecordDocs) {
            TransferOutDoc transferOutDoc = transferRecordDoc.getTransferOutDoc();
            if (transferOutDoc == null) {
                continue;
            }
            Map<String, String> transferOutStructureMap = transferOutDoc.getStructureMap();
            rollInRecordDateStr = transferOutStructureMap.get("患者于");
            if (CatalogueUtil.isEmpty(rollInRecordDateStr)) {
                continue;
            }
            Date rollInRecordDate = StringUtil.parseDateTime(rollInRecordDateStr);
            rollInDay = new ArrayList<>();
            for (int i = 1; i <= 3; i++) {
                int wardRecordDay = DateUtil.getDay(DateUtil.addDate(rollInRecordDate, i));
                rollInDay.add(wardRecordDay);
            }
            if (!wardRecordDayList.containsAll(rollInDay)) {
                status.set("-1");
                return;
            }
        }
    }

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