package com.lantone.qc.trans.shaoyf;

import com.google.common.collect.Lists;
import com.lantone.qc.dbanaly.facade.shaoyf.ShaoyfXmlUtil;
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.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.shaoyf.util.ShaoyfOrdinaryAssistant;
import org.apache.commons.lang3.StringUtils;

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

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

/**
 * @Description: 入院记录文档生成
 * @author: rengb
 * @time: 2020/3/5 15:47
 */
public class ShaoyfBeHospitalizedDocTrans 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 = ShaoyfXmlUtil.getXmlToMap(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("入病房时间"));
        }

        structureMap.put("签名", structureMap.get("医师签名"));
        ShaoyfOrdinaryAssistant.techTitleDocRemove(structureMap, "签名");
        ShaoyfOrdinaryAssistant.techTitleDocRemove(structureMap, "审核人");

        BeHospitalizedDoc beHospitalizedDoc = beHospitalizedDocGen(structureMap); // 不走共用
        beHospitalizedDoc.setPageData((Map) structureMap);
        return beHospitalizedDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "本人姓名=姓名",
            "性别=性别",
            "年龄=年龄",
            "民族=民族",
            "职业=职业",
            "出生地址=出生地",
            "出生、生长史=出生生长史",
            "地址=联系地址",
            "现住址=联系地址",
            "工作场所=工作单位",
            "信息来源=病史陈述者",
            "病史可靠程度=病史可靠性",
            "生日=出生日期",
            "出生时间=出生日期",
            "病人出生日期=出生日期",
            "新生儿出生日期=出生日期",
            "出生地址=户口地址",
            "本人电话=电话",
            "联系电话=联系人电话",
            "入院日期=入院日期",
            "入病房时间=入院日期",
            "入院日期时间=入院日期",
            "病历日期=记录日期",
            "主诉=主诉",
            "现病史=现病史",
            "一般健康状况=既往健康状况",
            "婚姻家庭关系和睦=夫妻关系",
            "体格检查=体格检查",
            "专科检查=专科体格检查",
            "初步诊断=初步诊断",
            "修正诊断=修正诊断",
            "当前诊断=死亡诊断",
            "喂养间隔=间隔时间",
            "出院状态=出院情况",
            "医生=医师签名",
            "补充诊断=补充诊断",
            "户口地址=联系地址",
            "内科疾病史(其它)=内科疾病史其它",
            "家属提供的信息可信吗=病史可靠性",
            "入院诊断(中医疾病)=中医诊断",
            "长期用药情况(包括可能成瘾的药物)=长期用药",
            "体温(口)=体温",
            "常规产前检查=产前检查",
            "呼吸频率/节律=呼吸节律",
            "肺下界(肩胛下)=肺下界",
            "第I/II/III测量线上脾脏大小=脾脏大小",
            "肌力(左上肢)=肌力左上肢",
            "肌力(左下肢)=肌力左下肢",
            "肌力(右上肢)=肌力右上肢",
            "肌力(右下肢)=肌力右下肢",
            "运动系统：步态=运动系统步态",
            "运动系统：共济失调=运动系统共济失调",
            "复诊科室=科别"
    );


    /**
     * 入院记录
     *
     * @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);
        //        structureMap.remove("主诉");

        // 走模型
        PresentLabel presentLabel = new PresentLabel();
        presentLabel.setText(structureMap.get("现病史"));
        beHospitalizedDoc.setPresentLabel(presentLabel);
        //        structureMap.remove("现病史");

        PastLabel pastLabel = new PastLabel();
        pastLabel.setCrfLabel(false);
        pastLabel.setText(structureMap.get("既往史"));
        beHospitalizedDoc.setPastLabel(pastLabel);
        //        structureMap.remove("既往史");

        PersonalLabel personalLabel = new PersonalLabel();
        personalLabel.setCrfLabel(false);
        personalLabel.setText(structureMap.get("个人史"));
        beHospitalizedDoc.setPersonalLabel(personalLabel);
        //        structureMap.remove("个人史");

        MaritalLabel maritalLabel = new MaritalLabel();
        maritalLabel.setCrfLabel(false);
        maritalLabel.setText("婚育史: " + structureMap.get("婚育史"));
        beHospitalizedDoc.setMaritalLabel(maritalLabel);
        //        structureMap.remove("婚育史");

        MenstrualLabel menstrualLabel = new MenstrualLabel();
        menstrualLabel.setCrfLabel(false);
        menstrualLabel.setText("月经史:" + structureMap.get("月经史"));
        beHospitalizedDoc.setMenstrualLabel(menstrualLabel);
        //        structureMap.remove("月经史");

        FamilyLabel familyLabel = new FamilyLabel();
        familyLabel.setCrfLabel(false);
        familyLabel.setText(structureMap.get("家族史"));
        beHospitalizedDoc.setFamilyLabel(familyLabel);
        //        structureMap.remove("家族史");

        VitalLabel vitalLabel = new VitalLabel();
        vitalLabel.setCrfLabel(false);
        vitalLabel.setText(structureMap.get("体格检查"));
        beHospitalizedDoc.setVitalLabel(vitalLabel);
        //        structureMap.remove("体格检查");

        VitalLabelSpecial vitalLabelSpecial = new VitalLabelSpecial();
        vitalLabelSpecial.setCrfLabel(true);
        vitalLabelSpecial.setText(structureMap.get("专科体格检查"));
        beHospitalizedDoc.setVitalLabelSpecial(vitalLabelSpecial);
        //        structureMap.remove("专科体格检查");

        // 走模型
        PacsLabel pacsLabel = new PacsLabel();
        pacsLabel.setCrfLabel(true);
        List<String> keys = Lists.newArrayList("实验室检查", "影像学检查");
        String pacsText = structureMapJoin(structureMap, keys);
        pacsLabel.setText(pacsText);
        beHospitalizedDoc.setPacsLabel(pacsLabel);
        //        structureMap.remove("辅助检查");

        // 走模型
        DiagLabel initialDiagLabel = new DiagLabel();
        initialDiagLabel.setText(structureMap.get("初步诊断"));
        beHospitalizedDoc.setInitialDiagLabel(initialDiagLabel);
        //        structureMap.remove("初步诊断");

        // 走模型
        DiagLabel revisedDiagLabel = new DiagLabel();
        revisedDiagLabel.setText(structureMap.get("修正诊断"));
        beHospitalizedDoc.setRevisedDiagLabel(revisedDiagLabel);
        //        structureMap.remove("修正诊断");

        // 走模型
        DiagLabel suppleDiagLabel = new DiagLabel();
        suppleDiagLabel.setText(structureMap.get("补充诊断"));
        beHospitalizedDoc.setSuppleDiagLabel(suppleDiagLabel);
        //        structureMap.remove("补充诊断");

        beHospitalizedDoc.setStructureMap(structureMap);

        return beHospitalizedDoc;
    }

}