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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
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.FirstCourseRecordDoc;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.model.doc.PacsDoc;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.util.DateUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author HUJING
 * @create 2020-08-17 15:29
 * @desc 异常检查未记录
 **/
@Component
public class THR03071 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        List<PacsDoc> pacsDocs = inputInfo.getPacsDocs();
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        FirstCourseRecordDoc firstCourseRecordDoc = inputInfo.getFirstCourseRecordDoc();
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        if (pacsDocs.size() == 0) {
            return;
        }
        //辅检数据
        StringBuffer sb = new StringBuffer();
        Map<String, List<String>> pacsMap = Maps.newHashMap();
        Map<String, Integer> pacsCount = Maps.newHashMap();
        Map<String, Set<String>> pacsDate = Maps.newHashMap();
        String splitRegex = "[;；]";
        for (PacsDoc pacsDoc : pacsDocs) {
            Map<String, String> structureMap = pacsDoc.getStructureMap();
            String itemName = structureMap.get("报告名称");
            String itemDiag = structureMap.get("检查结果诊断");
            String itemDate = structureMap.get("报告创建时间");
            if (StringUtil.isBlank(itemName) || StringUtil.isBlank(itemDiag)) {
                continue;
            }
//            if (itemName.contains("检查")) {
//                itemName = itemName.substring(0, itemName.indexOf("检查") + 2);
//            }
            String[] itemDiags = itemDiag.split(splitRegex);
            List<String> itemDiagList = Lists.newArrayList(itemDiags);
            Set<String> itemDateList = Sets.newHashSet(itemDate);
            if (pacsMap.containsKey(itemName)) {
                pacsMap.get(itemName).addAll(itemDiagList);
                pacsDate.get(itemName).addAll(itemDateList);
            } else {
                pacsMap.put(itemName, itemDiagList);
                pacsDate.put(itemName, itemDateList);
            }
        }
        //检查项目对应数据初始化
        pacsMap.keySet().stream().forEach(i -> pacsCount.put(i, 0));

        if (threeLevelWardDocs.size() > 0) {
            for (ThreeLevelWardDoc doc : threeLevelWardDocs.get(0).getAllDoctorWradDocs()) {
                Map<String, String> structureMap = doc.getStructureMap();
                String content = CatalogueUtil.structureMapJoin(structureMap, Lists.newArrayList("体检", "病情记录"));
                findPacs(pacsMap, pacsCount, content);
            }
        }

        if (firstCourseRecordDoc != null) {
            Map<String, String> structureMap = firstCourseRecordDoc.getStructureMap();
            String content = CatalogueUtil.structureMapJoin(structureMap, Lists.newArrayList("病例特点"));
            findPacs(pacsMap, pacsCount, content);
        }

        if (leaveHospitalDoc != null) {
            Map<String, String> structureMap = leaveHospitalDoc.getStructureMap();
            String content = CatalogueUtil.structureMapJoin(structureMap, Lists.newArrayList("诊治经过"));
            findPacs(pacsMap, pacsCount, content);
        }

        String reptNm = null;
        List<String> pacsMiss = Lists.newArrayList();
        for (Map.Entry<String, Integer> pacs : pacsCount.entrySet()) {
            reptNm = pacs.getKey().split("=")[0];
            if (pacs.getValue() == 0) {
                pacsMiss.add(pacs.getKey());
            }
            if (pacsMiss.size() > 0) {
                infoAppend(sb, reptNm, pacsDate);
            }
        }

        if (sb.toString().length() > 0) {
            status.set("-1");
            info.set("检查：" + sb.toString().substring(0, sb.toString().length() - 1));
        }
    }

    /**
     * 从文本中查找辅检对应的诊断信息，只要找到一个能对上，pacsCount对应辅检项目的数量就+1
     *
     * @param pacsMap   key：辅检项目  value：List[诊断信息]
     * @param pacsCount key：辅检项目    value：匹配的数量
     * @param content   文本
     */
    private void findPacs(Map<String, List<String>> pacsMap, Map<String, Integer> pacsCount, String content) {
        for (Map.Entry<String, List<String>> map : pacsMap.entrySet()) {
            for (String itemDiag : map.getValue()) {
                if (content.contains(itemDiag)) {
                    pacsCount.put(map.getKey(), pacsCount.get(map.getKey()) + 1);
                    break;
                }
            }
        }
    }

    /**
     * 拼接提示信息
     *
     * @param sb
     * @param reptNm
     * @param pacsDate
     */
    private void infoAppend(StringBuffer sb, String reptNm, Map<String, Set<String>> pacsDate) {
        for (Map.Entry<String, Set<String>> map : pacsDate.entrySet()) {
            if (reptNm.equals(map.getKey())) {
                for (String date : map.getValue()) {
                    String checkDate = DateUtil.formatDateTime(StringUtil.parseDateTime(date));
                    sb.append(reptNm).append("(").append(checkDate).append(")").append("_");
                }
            }
        }
    }

}
