package com.lantone.qc.trans.beilun;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.DifficultCaseDiscussDoc;
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.beilun.util.BeiLunDifficultCaseDiscussHtmlAnalysis;
import com.lantone.qc.trans.beilun.util.BeiLunHtmlAnalysis;
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/17 13:23
 */
@Slf4j
public class BeiLunDifficultCaseDiscussDocTrans extends ModelDocTrans {

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

    private DifficultCaseDiscussDoc getDifficultCaseDiscussDoc(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();
            BeiLunHtmlAnalysis beiLunHtmlAnalysis = new BeiLunDifficultCaseDiscussHtmlAnalysis();
            Map<String, String> sourceMap = beiLunHtmlAnalysis.analysis(content, recTitle, recTypeId);
            if (!sourceMap.containsKey("具体讨论意见") && sourceMap.containsKey("参加人员")) {
                String participant = sourceMap.get("参加人员");
                if (participant.contains("医师汇报病史")) {
                    String parCrew = "";
                    //参加人员
                    if (participant.indexOf("医师汇报病史") > 2) {
                        parCrew = participant.substring(0, participant.indexOf("医师汇报病史") - 2);
                    }
                    //具体讨论意见
                    String SpeDiscussion = participant.substring(Math.max(0, participant.indexOf("医师汇报病史") - 2));
                    sourceMap.put("参加人员", parCrew);
                    sourceMap.put("具体讨论意见", SpeDiscussion);
                }
            }


            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)) {
            DifficultCaseDiscussDoc difficultCaseDiscussDoc = ModelDocGenerate.difficultCaseDiscussDocGen(structureMap);
            difficultCaseDiscussDoc.setPageData((Map) structureMap);
            return difficultCaseDiscussDoc;
        } else {
            return null;
        }
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "主持人=主持人姓名及专业技术职务",
            "参加人员=参加人员的姓名及专业技术职务",
            "主持人小结意见/讨论结论=主持人小结"
    );

}