package com.lantone.qc.trans.taizhou;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.BeHospitalizedDoc;
import com.lantone.qc.pub.model.label.ChiefLabel;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.model.label.FamilyLabel;
import com.lantone.qc.pub.model.label.MaritalLabel;
import com.lantone.qc.pub.model.label.MenstrualLabel;
import com.lantone.qc.pub.model.label.PacsLabel;
import com.lantone.qc.pub.model.label.PastLabel;
import com.lantone.qc.pub.model.label.PersonalLabel;
import com.lantone.qc.pub.model.label.PresentLabel;
import com.lantone.qc.pub.model.label.VitalLabel;
import com.lantone.qc.pub.model.label.VitalLabelSpecial;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.dbanaly.facade.taizhou.TzXmlUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @Description: 入院记录文档生成
 * @author: rengb
 * @time: 2020/3/5 15:47
 */
public class TaiZhouBeHospitalizedDocTrans extends ModelDocTrans {

    /**
     * @param medrecVo
     * @return
     */
    @Override
    public BeHospitalizedDoc extract(MedrecVo medrecVo) {
        Map<String, Object> contentMap = ((List<Map>) medrecVo.getContent().get("content")).get(0);
        String content = contentMap.get("xmlText").toString();

        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content);
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts);
        OrdinaryAssistant.removeBlank(structureMap);
        /*if (StringUtils.isEmpty(structureMap.get("入院时间")) && StringUtils.isNotEmpty(structureMap.get("入病房时间"))) {
            structureMap.put("入院时间", structureMap.get("入病房时间"));
        }
         */
        //如果有入院时间，优先使用
        addKeyMapping(structureMap,"入院时间","入病房时间");
        //如果有职业，优先使用
        addKeyMapping(structureMap,"职业","工作性质");
        BeHospitalizedDoc beHospitalizedDoc = beHospitalizedDocGen(structureMap); // 不走共用
        beHospitalizedDoc.setPageData((Map) structureMap);
        return beHospitalizedDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "本人姓名=姓名",
            "性别=性别",
            "年龄=年龄",
            "民族=民族",
            "职业=职业",
            "出生地址=出生地",
            "出生、生长史=出生生长史",
            "婚姻状况=婚姻",
            "地址=联系地址",
            "工作场所=工作单位",
            "信息来源=病史陈述者",
            "生日=出生日期",
            "出生时间=出生日期",
            "病人出生日期=出生日期",
            "新生儿出生日期=出生日期",
//            "出生地址=户口地址",
            "本人电话=电话",
            "联系电话=联系人电话",
            "入院日期=入院日期",
            "入病房时间=入院日期",
            "病历日期=记录日期",
            "主诉=主诉",
            "现病史=现病史",
            "内科疾病史(其它)=内科疾病史其它",
            "一般健康状况=既往健康状况",
            "婚姻家庭关系和睦=夫妻关系",
            "体格检查=体格检查",
            "专科检查=专科体格检查",
            "初步诊断=初步诊断",
            "修正诊断=修正诊断",
            "医生=医师签名",
            "补充诊断=补充诊断",
//            "户口地址=联系地址",
            "家属提供的信息可信吗=病史可靠性",
            "入院诊断(中医疾病)=中医诊断",
            "长期用药情况(包括可能成瘾的药物)=长期用药",
            "体温(口)=体温"
    );


    /**
     * 入院记录
     *
     * @param structureMap
     * @return
     */
    public static BeHospitalizedDoc beHospitalizedDocGen(Map<String, String> structureMap) {
        BeHospitalizedDoc beHospitalizedDoc = new BeHospitalizedDoc();

        // 走模型
        ChiefLabel chiefLabel = new ChiefLabel();
        chiefLabel.setText(structureMap.get("主诉"));
        beHospitalizedDoc.setChiefLabel(chiefLabel);
        if (StringUtil.isBlank(structureMap.get("主诉"))) {
            beHospitalizedDoc.setChiefLabel(null);
        }
        //        structureMap.remove("主诉");

        // 走模型
        PresentLabel presentLabel = new PresentLabel();
        presentLabel.setText(structureMap.get("现病史"));
        beHospitalizedDoc.setPresentLabel(presentLabel);
        if (StringUtil.isBlank(structureMap.get("现病史"))) {
            beHospitalizedDoc.setPresentLabel(null);
        }
        //        structureMap.remove("现病史");

        PastLabel pastLabel = new PastLabel();
        pastLabel.setCrfLabel(false);
        pastLabel.setText(structureMap.get("既往史"));
        beHospitalizedDoc.setPastLabel(pastLabel);
        if (StringUtil.isBlank(structureMap.get("既往史"))) {
            beHospitalizedDoc.setPastLabel(null);
        }
        //        structureMap.remove("既往史");

        PersonalLabel personalLabel = new PersonalLabel();
        personalLabel.setCrfLabel(false);
        personalLabel.setText(structureMap.get("个人史"));
        beHospitalizedDoc.setPersonalLabel(personalLabel);
        if (StringUtil.isBlank(structureMap.get("个人史"))) {
            beHospitalizedDoc.setPersonalLabel(null);
        }
        //        structureMap.remove("个人史");

        MaritalLabel maritalLabel = new MaritalLabel();
        maritalLabel.setCrfLabel(false);
        maritalLabel.setText("婚育史: " + structureMap.get("婚育史"));
        beHospitalizedDoc.setMaritalLabel(maritalLabel);
        if (StringUtil.isBlank(structureMap.get("婚育史"))) {
            beHospitalizedDoc.setMaritalLabel(null);
        }
        //        structureMap.remove("婚育史");

        MenstrualLabel menstrualLabel = new MenstrualLabel();
        menstrualLabel.setCrfLabel(false);
        menstrualLabel.setText("月经史:" + structureMap.get("月经史"));
        beHospitalizedDoc.setMenstrualLabel(menstrualLabel);
        if (StringUtil.isBlank(structureMap.get("月经史"))) {
            beHospitalizedDoc.setMenstrualLabel(null);
        }
        //        structureMap.remove("月经史");

        FamilyLabel familyLabel = new FamilyLabel();
        familyLabel.setCrfLabel(false);
        familyLabel.setText(structureMap.get("家族史"));
        beHospitalizedDoc.setFamilyLabel(familyLabel);
        if (StringUtil.isBlank(structureMap.get("家族史"))) {
            beHospitalizedDoc.setFamilyLabel(null);
        }
        //        structureMap.remove("家族史");

        VitalLabel vitalLabel = new VitalLabel();
        vitalLabel.setCrfLabel(false);
        vitalLabel.setText(structureMap.get("体格检查"));
        beHospitalizedDoc.setVitalLabel(vitalLabel);
        if (StringUtil.isBlank(structureMap.get("体格检查"))) {
            beHospitalizedDoc.setVitalLabel(null);
        }
        //        structureMap.remove("体格检查");

        VitalLabelSpecial vitalLabelSpecial = new VitalLabelSpecial();
        vitalLabelSpecial.setCrfLabel(false);
        vitalLabelSpecial.setText(structureMap.get("专科体格检查"));
        beHospitalizedDoc.setVitalLabelSpecial(vitalLabelSpecial);
        if (StringUtil.isBlank(structureMap.get("专科体格检查"))) {
            beHospitalizedDoc.setVitalLabelSpecial(null);
        }
        //        structureMap.remove("专科体格检查");

        // 走模型
        PacsLabel pacsLabel = new PacsLabel();
        pacsLabel.setCrfLabel(false);
        pacsLabel.setText(structureMap.get("辅助检查"));
        beHospitalizedDoc.setPacsLabel(pacsLabel);
        if (StringUtil.isBlank(structureMap.get("辅助检查"))) {
            beHospitalizedDoc.setPacsLabel(null);
        }
        //        structureMap.remove("辅助检查");

        // 走模型
        DiagLabel initialDiagLabel = new DiagLabel();
        initialDiagLabel.setText(structureMap.get("初步诊断"));
        beHospitalizedDoc.setInitialDiagLabel(initialDiagLabel);
        if (StringUtil.isBlank(structureMap.get("初步诊断"))) {
            beHospitalizedDoc.setInitialDiagLabel(null);
        }
        //        structureMap.remove("初步诊断");

        // 走模型
        DiagLabel revisedDiagLabel = new DiagLabel();
        revisedDiagLabel.setText(structureMap.get("修正诊断"));
        beHospitalizedDoc.setRevisedDiagLabel(revisedDiagLabel);
        if (StringUtil.isBlank(structureMap.get("修正诊断"))) {
            beHospitalizedDoc.setRevisedDiagLabel(null);
        }
        //        structureMap.remove("修正诊断");

        // 走模型
        DiagLabel suppleDiagLabel = new DiagLabel();
        suppleDiagLabel.setText(structureMap.get("补充诊断"));
        beHospitalizedDoc.setSuppleDiagLabel(suppleDiagLabel);
        if (StringUtil.isBlank(structureMap.get("补充诊断"))) {
            beHospitalizedDoc.setSuppleDiagLabel(null);
        }
        //        structureMap.remove("补充诊断");

        beHospitalizedDoc.setStructureMap(structureMap);

        return beHospitalizedDoc;
    }

    /**
     * 给structureMap添加一个key（映射）
     * @param structureMap
     * @param target    需要用到的key
     * @param source    structureMap中含有的key
     */
    protected void addKeyMapping(Map<String, String> structureMap, String target, String source) {
        if (StringUtils.isEmpty(structureMap.get(target)) && StringUtils.isNotEmpty(structureMap.get(source))) {
            structureMap.put(target, structureMap.get(source));
        }
    }

}