package com.lantone.qc.trans.xiamen;

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

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

    private StagesSummaryDoc getStagesSummaryDoc(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, "28");
        }
        if (MapUtils.isNotEmpty(structureMap)) {
            StagesSummaryDoc stagesSummaryDoc = ModelDocGenerate.stagesSummaryDocGen(structureMap);
            stagesSummaryDoc.setPageData((Map) structureMap);
            return stagesSummaryDoc;
        } else {
            return null;
        }
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "病历标题=标题",
            "病情记录=病历内容"
    );

}