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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.LisDoc;
import com.lantone.qc.pub.model.doc.PacsDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.model.doc.operation.OperationRecordDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 术前未完成常规检查
 * @author: HUJING
 * @time: 2020/8/14 13:14
 */
@Component
public class PRE03064 extends QCCatalogue {
    private List<String> normalCheck = Lists.newArrayList("出凝血时间", "HBSAG", "血常规", "尿常规", "血型", "心电图");

    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        status.set("0");
        List<LisDoc> lisDocs = inputInfo.getLisDocs();
        List<PacsDoc> pacsDocs = inputInfo.getPacsDocs();
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();

        if (operationDocs.size() == 0 || lisDocs.size() == 0 || pacsDocs.size() == 0) {
            return;
        }

        //取化验名称
        Map<String, String> lisRet = Maps.newHashMap();
        lisDocs.stream().map(LisDoc::getStructureMap).forEach(x -> lisRet.put(x.get("报告名称"), x.get("报告创建时间")));
        Map<String, String> newLisRet = processMap(lisRet);
        //取辅检名称
        Map<String, String> pacsRet = Maps.newHashMap();
        pacsDocs.stream().map(PacsDoc::getStructureMap).forEach(x -> pacsRet.put(x.get("报告名称"), x.get("报告创建时间")));
        Map<String, String> newPacsRet = processMap(pacsRet);

        //取手术时间
        List<String> operationDateList = operationDocs.stream().map(OperationDoc::getOperationRecordDoc)
                .filter(Objects::nonNull)
                .map(OperationRecordDoc::getStructureMap).map(x -> x.get("手术日期")).filter(StringUtil::isNotBlank).collect(Collectors.toList());

        List<String> lisKey = new ArrayList<>(newLisRet.keySet());
        List<String> pacsKey = new ArrayList<>(newPacsRet.keySet());
        //lisKey.retainAll(normalCheck);//如果存在相同元素，lisKey中仅保留相同的元素。 如果不存在相同元素，lisKey会变为空。
        //pacsKey.retainAll(normalCheck);//如果存在相同元素，pacsKey中仅保留相同的元素。 如果不存在相同元素，pacsKey会变为空。

        if (operationDateList.size() > 0) {
            String firstOperationDateStr = operationDateList.get(0);
            Date firstOperationDate = StringUtil.parseDateTime(firstOperationDateStr);
            int matchSum = 0;
            List<String> missCheck = Lists.newArrayList();
            getMatchSum(newLisRet, lisKey, firstOperationDate, matchSum, missCheck);
            getMatchSum(newPacsRet, pacsKey, firstOperationDate, matchSum, missCheck);

            if (matchSum < normalCheck.size()) {
                status.set("-1");
                info.set("未完成常规检查:" + missCheck.toString().replaceAll("[\\[\\]]", ""));
            }
        }
//        for (String operationDateStr : operationDateList) {
//            Date operationDate = StringUtil.parseDateTime(operationDateStr);
//            if (operationDate == null) {
//                continue;
//            }
//
//        }
//        System.out.println(operationDateList);

    }

    //获取检查中与必须要的检查匹配数
    private void getMatchSum(Map<String, String> newRet, List<String> keySet, Date firstOperationDate, int matchSum, List<String> miss) {
        for (String check : normalCheck) {
            int checkMiss = 0;
            for (String lis : keySet) {
                if (lis.contains(check)) {
                    String lisDateStr = newRet.get(lis);
                    Date lisDate = StringUtil.parseDateTime(lisDateStr);
                    if (lisDate.before(firstOperationDate)) {
                        matchSum++;
                        break;
                    } else {
                        checkMiss++;
                    }
                } else {
                    checkMiss++;
                }

            }
            //如果所有化验里都没有必要的当前检查（normalCheck里的检查）
            if (checkMiss == keySet.size()) {
                miss.add(check);
            }
        }
    }

    private Map<String, String> processMap(Map<String, String> ret) {
        Map<String, String> newRet = Maps.newHashMap();
        for (Map.Entry<String, String> enrty : ret.entrySet()) {
            String key = enrty.getKey();
            if (key.contains("=")) {
                String pubName = key.split("=")[0];
                if (StringUtil.isNotBlank(pubName)) {
                    newRet.put(pubName, enrty.getValue());
                }
            } else {
                newRet.put(key, enrty.getValue());
            }
        }
        return newRet;
    }
}