package com.lantone.qc.trans.xiamen;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.SeriouslyIllNoticeDoc;
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.CommonAnalysisUtil;
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 XiamenSeriouslyIllNoticeDocTrans extends ModelDocTrans {

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

    private SeriouslyIllNoticeDoc getSeriouslyIllNoticeDoc(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, "29");
        }
        if (MapUtils.isNotEmpty(structureMap)) {
            SeriouslyIllNoticeDoc seriouslyIllNoticeDoc = ModelDocGenerate.seriouslyIllNoticeDocGen(structureMap);
            seriouslyIllNoticeDoc.setText(CommonAnalysisUtil.html2String(content));
            seriouslyIllNoticeDoc.setPageData((Map) structureMap);
            return seriouslyIllNoticeDoc;
        } else {
            return null;
        }
    }

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

}