package com.lantone.qc.trans.xiamen;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.ClinicBloodEffectDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.FastJsonUtils;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;

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

/**
 * @author 王宇
 * @create 2020-04-30 12:39
 * @desc 输血后效果评价
 **/
@Slf4j
public class XiamenClinicBloodEffectDocTrans extends ModelDocTrans {

    @Override
    public List<ClinicBloodEffectDoc> extract(MedrecVo medrecVo) {
        List<ClinicBloodEffectDoc> retList = Lists.newArrayList();
        List<Map<String, Object>> contentMaps = (List) medrecVo.getContent().get("content");
        contentMaps.forEach(contentMap -> {
            try {
                ClinicBloodEffectDoc clinicBloodEffectDoc = getClinicalBloodDoc(contentMap);
                if (clinicBloodEffectDoc != null) {
                    retList.add(clinicBloodEffectDoc);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        return retList;
    }

    private ClinicBloodEffectDoc getClinicalBloodDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> structureMap = null;
        if (contentMap.get("isParsed") != null && "1".equals(contentMap.get("isParsed").toString())) {
            structureMap = (Map) FastJsonUtils.getJsonToMap(content);
            structuredProcessing(structureMap, wordList);
            structureMap = OrdinaryAssistant.mapKeyContrast(structureMap, keyContrasts, "32");
        }
        if (MapUtils.isNotEmpty(structureMap)) {
            ClinicBloodEffectDoc clinicBloodEffectDoc = ModelDocGenerate.clinicBloodEffectDoc(structureMap);
            clinicBloodEffectDoc.setPageData((Map) structureMap);
            return clinicBloodEffectDoc;
        } else {
            return null;
        }
    }

    private void structuredProcessing(Map<String, String> structureMap, List<String> wordList) {
        for (String word : wordList) {
            StringBuffer sb = new StringBuffer();
            for (Map.Entry<String, String> entity : structureMap.entrySet()) {
                if (entity.getKey().contains(word)) {
                    sb.append(entity.getKey() +"："+ entity.getValue() + "；");
                }
            }
            structureMap.put(word, sb.toString());
        }
    }

    private List<String> wordList = Lists.newArrayList("输血前指征", "输血后指征", "生命体征评估");
    private List<String> keyContrasts = Lists.newArrayList(
            "输血诊断=病历内容",
            "评估时间=病历日期",
            "病历内容=临床诊断",
            "输血后检查|ABO血型=ABO",
            "输血前指征=输血前实验室检测指征",
            "输血后指征=输血后实验室检测指征",
            "生命体征评估=临床症状与生命体征",
            "生命体征评估|神志=神志状态",
            "生命体征评估|心肺功能=心肺功能评估",
            "科别=科室"
    );

}