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

import com.google.common.collect.Lists;
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.transferrecord.TransferIntoDoc;
import com.lantone.qc.pub.model.doc.transferrecord.TransferRecordDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName : DUT0598
 * @Description : 接科记录未在24h内完成
 * @Author : 胡敬
 * @Date: 2020-03-30 15:06
 */
@Component
public class DUT0598 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        TransferRecordDoc transferRecordDoc = inputInfo.getTransferRecordDocs();
        if (transferRecordDoc == null) {
            return;
        }
        List<TransferIntoDoc> transferIntoDocs = transferRecordDoc.getTransferIntoDocs();
        String rollInRecordDateStr = "", recordDateStr = "", removeDate = "";
        Date rollOutRecordDate = null, recordDate = null;
        if (transferIntoDocs == null || transferIntoDocs.get(0).getStructureMap() == null) {
            return;
        }
        //转入时间
        rollInRecordDateStr = transferIntoDocs.get(0).getStructureMap().get("转入时间");
        if (StringUtil.isBlank(rollInRecordDateStr)) {
            return;
        }
        String[] split = rollInRecordDateStr.replace("[", "").replace("]", "").trim().split(",");
//        List<String> dateLists = Arrays.asList(split);
        List<String> dateLists = Lists.newArrayList();
        for (String date : split) {
            dateLists.add(date);
        }
        dateLists = dateLists.stream().sorted().collect(Collectors.toList());
        for (TransferIntoDoc transferIntoDoc : transferIntoDocs) {
            Map<String, Date> maps = new HashMap<>();
            Map<String, String> transferOutStructureMap = transferIntoDoc.getStructureMap();
            recordDateStr = transferOutStructureMap.get("病历日期");
            for (String date : dateLists) {
                if (CatalogueUtil.isEmpty(date) || CatalogueUtil.isEmpty(recordDateStr)) {
                    continue;
                }
                //转入日期
                rollOutRecordDate = StringUtil.parseDateTime(date);
                //记录日期
                recordDate = StringUtil.parseDateTime(recordDateStr);
                if (rollOutRecordDate != null && recordDate != null &&
                        (rollOutRecordDate.before(recordDate) || DateUtils.isSameDay(rollOutRecordDate, recordDate))) {
                    if (maps.size() == 0) {
                        removeDate = date;
                        maps.put("转入时间", rollOutRecordDate);
                        maps.put("记录时间", recordDate);
                    } else {
                        if (recordDate.getTime() - rollOutRecordDate.getTime() < maps.get("记录时间").getTime() - maps.get("转入时间").getTime()) {
                            removeDate = date;
                            maps.put("转入时间", rollOutRecordDate);
                        }
                    }
                }
            }

            if (maps.size() != 0) {
                boolean compareTime = CatalogueUtil.compareTime(maps.get("转入时间"), maps.get("记录时间"), (long) 24 * 60);
                boolean sameDay = DateUtils.isSameDay(maps.get("转入时间"), maps.get("记录时间"));
                dateLists.remove(removeDate);
                if (compareTime && !sameDay) {
                    status.set("-1");
                    return;
                }
            }
        }
    }

}