package com.lantone.qc.trans.xiamen;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.CrisisValueReportDoc;
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;

/**
 * @Description: 危急值记录文档生成
 * @author: rengb
 * @time: 2020/3/19 19:41
 */
@Slf4j
public class XiamenCrisisValueReportDocTrans extends ModelDocTrans {

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

    private CrisisValueReportDoc getCrisisValueReportDoc(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);
            structureMap = OrdinaryAssistant.mapKeyContrast(structureMap, keyContrasts, "23");
        }
        if (MapUtils.isNotEmpty(structureMap)) {
            CrisisValueReportDoc crisisValueReportDoc = ModelDocGenerate.crisisValueReportDocGen(structureMap);
            crisisValueReportDoc.setPageData((Map) structureMap);
            return crisisValueReportDoc;
        } else {
            return null;
        }
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "病情记录=病情分析及处理"
    );

}