package com.lantone.qc.kernel.catalogue.xiamen.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.TransferOutDoc;
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.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName : DUT0597
 * @Description : 转出记录未在转出前完成
 * @Author : 胡敬
 * @Date: 2020-03-30 15:06
 */
@Component
public class DUT0597 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        TransferRecordDoc transferRecordDoc = inputInfo.getTransferRecordDocs();
        if (transferRecordDoc == null) {
            return;
        }
        String rollOutRecordDateStr = "", recordDateStr = "", removeDate = "";
        List<TransferOutDoc> transferOutDocs = transferRecordDoc.getTransferOutDocs();
        if (transferOutDocs == null || transferOutDocs.get(0).getStructureMap() == null) {
            return;
        }
        //转入时间
        rollOutRecordDateStr = transferOutDocs.get(0).getStructureMap().get("转出时间");
        if (StringUtil.isBlank(rollOutRecordDateStr)) {
            return;
        }
        String[] split = rollOutRecordDateStr.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 (TransferOutDoc transferOutDoc : transferOutDocs) {
            Boolean isRemove = false;
            Map<String, String> transferOutStructureMap = transferOutDoc.getStructureMap();
            recordDateStr = transferOutStructureMap.get("病历日期");
            for (String date : dateLists) {
                if (CatalogueUtil.isEmpty(date) || CatalogueUtil.isEmpty(recordDateStr)) {
                    continue;
                }
                Date rollOutRecordDate = StringUtil.parseDateTime(date);
                Date recordDate = StringUtil.parseDateTime(recordDateStr);
                if (rollOutRecordDate == null || recordDate == null || rollOutRecordDate.after(recordDate)
                        || DateUtils.isSameDay(rollOutRecordDate, recordDate)) {
                    isRemove = true;
                    removeDate = date;
                    break;
                } else {
                    status.set("-1");
                    return;
                }
            }
            if (isRemove) {
                dateLists.remove(removeDate);
            }
        }
    }
}