package com.lantone.qc.trans.xiamen;

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.pub.util.StringUtil;
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/17 13:23
 */
@Slf4j
public class XiamenDifficultCaseDiscussDocTrans 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);
            structureMap = OrdinaryAssistant.mapKeyContrast(structureMap, keyContrasts, "19");
        }
        if (MapUtils.isNotEmpty(structureMap)) {
            String speakMsg = "";
            String speakKey = null,speakVal = null;
            for (int i = 1; i < 20; i++) {
                speakKey = "讨论发言|" + i;
                speakVal = structureMap.get(speakKey);
                if (StringUtil.isBlank(speakVal)) {
                    break;
                }
                speakMsg += i + "、" + speakVal;
            }
            structureMap.put("讨论发言", speakMsg);

            DifficultCaseDiscussDoc difficultCaseDiscussDoc = ModelDocGenerate.difficultCaseDiscussDocGen(structureMap);
            difficultCaseDiscussDoc.setPageData((Map) structureMap);
            return difficultCaseDiscussDoc;
        } else {
            return null;
        }
    }

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

}