package com.lantone.qc.trans;

import com.google.common.collect.Maps;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.model.vo.QueryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;

import java.util.Map;

/**
 * @Description:
 * @author: rengb
 * @time: 2020/3/5 19:49
 */
@Slf4j
public class TransDispatch {

    public static InputInfo trans(QueryVo queryVo) {
        try {
            String classPath = "com.lantone.qc.trans."
                    + queryVo.getCid()
                    + "."
                    + queryVo.getCid().substring(0, 1).toUpperCase()
                    + queryVo.getCid().substring(1)
                    + "DocTrans";
            DocTrans docTrans = (DocTrans) Class.forName(classPath).newInstance();
            queryDeal(queryVo, "会诊", "会诊申请单", "会诊结果单", "会诊记录");
            queryDeal(queryVo, "手术", "术前讨论、术前小结", "手术记录", "术后首次病程及谈话记录");
            queryDeal(queryVo, "转科", "转入记录", "转出记录", "转科记录");
            return docTrans.extract(queryVo);
        } catch (Exception e) {
            log.info(e.getMessage(), e);
            return null;
        }
    }

    private static void queryDeal(QueryVo queryVo, String bigTitle, String... smallTitles) {
        MedrecVo medrecVo = new MedrecVo();
        medrecVo.setTitle(bigTitle);
        Map<String, Object> content = Maps.newHashMap();
        Map<String, Object> contentDetail = Maps.newHashMap();
        for (MedrecVo i : queryVo.getMedrec()) {
            for (String smallTitle : smallTitles) {
                if (i.getTitle() != null && i.getTitle().equals(smallTitle)) {
                    contentDetail.put(smallTitle, i.getContent().get("content"));
                }
            }
        }
        if (MapUtils.isEmpty(contentDetail)) {
            return;
        }
        content.put("content", contentDetail);
        medrecVo.setContent(content);
        queryVo.getMedrec().add(medrecVo);
    }

}