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.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.model.doc.operation.OperationRecordDoc;
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 : THR0587
 * @Description : 术后病程录没有连续记录3天
 * @Author : 胡敬
 * @Date: 2020-03-30 16:17
 */
@Component
public class THR0587 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        if (ListUtil.isEmpty(operationDocs) || ListUtil.isEmpty(threeLevelWardDocs)) {
            return;
        }
        //所有查房记录的日期天
        List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDocs.get(0).getAllDoctorWradDocs();
        List<Integer> wardRecordDayList = getWardRecordDay(allDoctorWradDocs);
        String OperationEndDateStr = "";
        //转入日期后应该有的查房记录的日期天
        List<Integer> rollInDay = null;
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> operationRecordStructureMap = operationRecordDoc.getStructureMap();
            OperationEndDateStr = operationRecordStructureMap.get("手术结束时间");
            if (CatalogueUtil.isEmpty(OperationEndDateStr)) {
                continue;
            }
            Date OperationEndDate = StringUtil.parseDateTime(OperationEndDateStr);
            rollInDay = new ArrayList<>();
            for (int i = 1; i <= 3; i++) {
                int wardRecordDay = DateUtil.getDay(DateUtil.addDate(OperationEndDate, 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;
    }
}