package com.lantone.qc.trans.yiwu;

import com.google.common.collect.Lists;
import com.lantone.qc.dbanaly.facade.yiwu.YiWuXmlUtil;
import com.lantone.qc.dbanaly.util.ModuleMappingUtil;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.model.label.LeaveHospitalDoctorAdviceLabel;
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.OrdinaryAssistant;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @Description: 出院小结文档生成
 * @author: rengb
 * @time: 2020/3/17 13:25
 */
public class YiWuLeaveHospitalDocTrans extends ModelDocTrans {
    private String modeId = "5";

    @Override
    public LeaveHospitalDoc extract(MedrecVo medrecVo) {
        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);
        } else {
            Map<String, String> sourceMap = YiWuXmlUtil.xmlToMap(content);
            List<String> titles = Lists.newArrayList("入院时间", "出院时间", "住院数", "住院天数", "入院诊断", "出院诊断", "出院诊断"
                    , "入院情况", "住院经过", "出院情况", "出院医嘱", "医师签名", "签名时间", "主诉", "诊疗过程", "医师签名时间");
            String text = sourceMap.get("原始文本");
            List<String> sortTitles = CommonAnalysisUtil.sortTitles(titles, text);
            CommonAnalysisUtil.cutByTitles(text, sortTitles, 0, sourceMap);
            sourceMap.put("mode_id", ModuleMappingUtil.getDeptModuleId(modeId));
            sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
            sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
            structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts, modeId);
        }
        LeaveHospitalDoc leaveHospitalDoc = new LeaveHospitalDoc();
        leaveHospitalDoc.setStructureMap(structureMap);
        leaveHospitalDoc.setPageData((Map) structureMap);
        if (StringUtil.isNotEmpty(structureMap.get("原始文本"))) {
            leaveHospitalDoc.setText(structureMap.get("原始文本"));
        }
        if (structureMap.containsKey("记录医生") && "记录医师".equals(structureMap.get("记录医生"))) {
            structureMap.put("记录医生", "");
        }
        if (StringUtils.isNotEmpty(structureMap.get("出院医嘱"))) {
            LeaveHospitalDoctorAdviceLabel leaveHospitalDoctorAdviceLabel = new LeaveHospitalDoctorAdviceLabel();
            leaveHospitalDoctorAdviceLabel.setText(structureMap.get("出院医嘱"));
            leaveHospitalDoctorAdviceLabel.setAiText(structureMap.get("出院医嘱"));
            leaveHospitalDoc.setLeaveHospitalDoctorAdviceLabel(leaveHospitalDoctorAdviceLabel);
        }

        return leaveHospitalDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "本人姓名=姓名",
            "实际住院天数=住院天数",
            "出院时情况=出院情况",
            //            "注意事项=健康教育",
            "复诊时间=随访计划",
            "出院指导=健康教育",
            "医生=记录医生",
            "医师签名=记录医生",
            "病历日期=记录时间",
            "体温(耳)=体温耳",
            "主诉=入院情况",
            "诊疗过程=诊治经过",
            "医师签名时间=记录时间",
            "出院时间=出院日期",
            "初步诊断=入院诊断",
            "住院经过=诊治经过",
            "患者签名日期=记录时间",
            "医师签名=记录医生",
            "签名时间=记录时间"
    );

}