package com.lantone.qc.trans.ywzxyy;

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 com.lantone.qc.trans.ywzxyy.util.YwzxyyClinicBloodEffectXmlAnalysis;
import com.lantone.qc.trans.ywzxyy.util.YwzxyyXmlAnalysis;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;

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

/**
 * @ClassName : TaiZhouClinicBloodEffectDocTrans
 * @Description : 输血后效果评价
 * @Author : 楼辉荣
 * @Date: 2020-04-19 17:53
 */
@Slf4j
public class YwzxyyClinicBloodEffectDocTrans 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);
        } else {
            String recTitle = contentMap.get("recTitle").toString();
            String recTypeId = contentMap.get("recTypeId").toString();
            YwzxyyXmlAnalysis ywzxyyXmlAnalysis = new YwzxyyClinicBloodEffectXmlAnalysis();
            Map<String, String> sourceMap = ywzxyyXmlAnalysis.analysis(content, recTitle, recTypeId);
            if (MapUtils.isNotEmpty(sourceMap)) {
                structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts, "32");
                structureMap.put("记录编号", contentMap.get("recId").toString());
                structureMap.put("病历号", contentMap.get("behospitalCode") == null ? null : contentMap.get("behospitalCode").toString());
            }
        }
        if (MapUtils.isNotEmpty(structureMap)) {
            ClinicBloodEffectDoc clinicBloodEffectDoc = ModelDocGenerate.clinicBloodEffectDoc(structureMap);
            clinicBloodEffectDoc.setPageData((Map) structureMap);
            return clinicBloodEffectDoc;
        } else {
            return null;
        }
    }

    private List<String> keyContrasts = Lists.newArrayList();

}