package com.lantone.qc.trans.xiamen;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.FirstCourseRecordDoc;
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.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;

import static com.lantone.qc.trans.comsis.ModelDocGenerate.structureMapJoin;

/**
 * @Description: 首次病程录文档生成
 * @author: rengb
 * @time: 2020/3/17 13:25
 */
@Slf4j
public class XiamenFirstCourseRecordDocTrans extends ModelDocTrans {

    @Override
    public FirstCourseRecordDoc extract(MedrecVo medrecVo) {
        FirstCourseRecordDoc firstCourseRecordDoc = null;
        try {
            Map<String, Object> contentMap = ((List<Map>) medrecVo.getContent().get("content")).get(0);
            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, "2");

            }
            if (MapUtils.isNotEmpty(structureMap)) {
                firstCourseRecordDoc = ModelDocGenerate.firstCourseRecordDocGen(structureMap);
                firstCourseRecordDoc.setText(CommonAnalysisUtil.html2String(content));
                firstCourseRecordDoc.setPageData((Map) structureMap);
                List<String> keys = Lists.newArrayList("需求评估", "预期目标", "诊疗计划", "治疗监测计划");
                String treatPlanJoin = structureMapJoin(structureMap, keys);
                if (StringUtil.isNotBlank(treatPlanJoin)) {
                    firstCourseRecordDoc.getTreatPlanLabel().setAiText(treatPlanJoin);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return firstCourseRecordDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "时间=病历日期",
            "病历特点=病例特点",
            "入院查体=查体",
            "辅助检查=辅检",
            "诊疗计划(方案)=诊疗计划"
    );

}