package com.lantone.qc.trans.ninghaiyiyi;

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

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

    private RescueDoc getRescueDoc(Map<String, Object> contentMap) {
        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 NingHaiYiYiRescueHtmlAnalysis();
            Map<String, String> sourceMap = beiLunHtmlAnalysis.analysis(content, recTitle, recTypeId);
            if (MapUtils.isNotEmpty(sourceMap)) {
                if (sourceMap.containsKey("抢救时间") && sourceMap.get("抢救时间").contains("-")) {
                    String[] rescueDates = sourceMap.get("抢救时间").split("-");
                    if (rescueDates.length > 1) {
                        sourceMap.put("开始时间", rescueDates[0]);
                        sourceMap.put("结束时间", rescueDates[1]);
                    }
                }
                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)) {
            RescueDoc rescueDoc = ModelDocGenerate.rescueDocGen(structureMap);
            rescueDoc.setPageData((Map) structureMap);
            return rescueDoc;
        } else {
            return null;
        }
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "病历标题=标题",
            "参加抢救人员及职称=参加抢救的医务人员姓名及专业技术职称",
            "病情变化及抢救过程=抢救内容"
    );

}