package com.lantone.qc.trans.ninghaiyiyi;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.DeathCaseDiscussDoc;
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.ninghaiyiyi.util.NingHaiYiYiDeathCaseDiscussHtmlAnalysis;
import com.lantone.qc.trans.ninghaiyiyi.util.NingHaiYiYiHtmlAnalysis;
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/17 13:22
 */
@Slf4j
public class NingHaiYiYiDeathCaseDiscussDocTrans extends ModelDocTrans {

    @Override
    public DeathCaseDiscussDoc extract(MedrecVo medrecVo) {
        DeathCaseDiscussDoc deathCaseDiscussDoc = null;
        try {
            Map<String, Object> contentMap = ((List<Map>) medrecVo.getContent().get("content")).get(0);
            String content = contentMap.get("htmlText").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();
                NingHaiYiYiHtmlAnalysis beiLunHtmlAnalysis = new NingHaiYiYiDeathCaseDiscussHtmlAnalysis();
                Map<String, String> sourceMap = beiLunHtmlAnalysis.analysis(content, recTitle, recTypeId);
                if (MapUtils.isNotEmpty(sourceMap)) {
                    structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts);
                    structureMap.put("记录编号", contentMap.get("recId").toString());
                    structureMap.put("病历号", contentMap.get("behospitalCode") == null ? null : contentMap.get("behospitalCode").toString());
                }
            }
            if (MapUtils.isNotEmpty(structureMap)) {
                deathCaseDiscussDoc = ModelDocGenerate.deathCaseDiscussDocGen(structureMap);
                deathCaseDiscussDoc.setPageData((Map) structureMap);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return deathCaseDiscussDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "医生签名时间=记录时间",
            "主持人小结意见=主持人小结",
            "具体讨论意见=讨论内容",
            "记录者签名=记录医生",
            "主持人（签名）=主持人签名"
    );

}