package com.lantone.qc.trans.changx;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.BeHospitalizedDoc;
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.dbanaly.facade.changx.CxXmlUtil;
import com.lantone.qc.trans.comsis.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;

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

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

    @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 = CxXmlUtil.firstLevelNodeValue(content);
        String sex = "男";
        for (String key : sourceMap.keySet()) {
            if (key.indexOf("性别") > -1) {
                if (sourceMap.get(key).indexOf("男") > -1 || sourceMap.get(key).indexOf("女") > -1) {
                    sex = StringUtil.removeBlank(sourceMap.get(key));
                }
            }
        }
        sourceMap.put(sex, "");
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts);
        String specialCheck = structureMap.get("专科检查");
        if (StringUtil.isNotBlank(specialCheck)) {
            structureMap.put("专科体格检查", specialCheck);
        } else {
            String tgjc = concatSpecialCheck(sourceMap);
            if (StringUtil.isNotBlank(tgjc)) {
                structureMap.put("专科体格检查", tgjc);
            }
        }
        String marital = structureMap.get("婚育史");
        String marry = structureMap.get("婚姻");
        if (StringUtil.isBlank(marital) && StringUtil.isNotBlank(marry)) {
            structureMap.put("婚育史", marry);
        }
        BeHospitalizedDoc beHospitalizedDoc = ModelDocGenerate.beHospitalizedDocGen(structureMap);
        beHospitalizedDoc.setText(content);
        beHospitalizedDoc.setPageData((Map) structureMap);
        return beHospitalizedDoc;
    }


    private String concatSpecialCheck(Map<String, String> sourceMap) {
        return concatString(sourceMap.get("一般情况")) +
                concatString(sourceMap.get("皮肤情况")) +
                concatString(sourceMap.get("淋巴")) +
                concatString(sourceMap.get("头部检查")) +
                concatString(sourceMap.get("颈部")) +
                concatString(sourceMap.get("胸部检查")) +
                concatString(sourceMap.get("肺部检查")) +
                concatString(sourceMap.get("心脏检查")) +
                concatString(sourceMap.get("血管")) +
                concatString(sourceMap.get("腹部检查")) +
                concatString(sourceMap.get("外生殖器")) +
                concatString(sourceMap.get("直肠肛门")) +
                concatString(sourceMap.get("四肢脊柱检查")) +
                concatString(sourceMap.get("神经系统检查")) +
                concatString(sourceMap.get("其他说明"));
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "姓名++++患者姓名=姓名",
            "出生地++++出生地（住院）=出生地",
            "工作单位++++工作单位地址=工作单位",
            "婚姻状况++++婚姻=婚姻",
            "诊疗经过++++入院诊治经过=诊疗经过",
            "联系电话=电话",
            "供史者++++病史叙述者=病史陈述者",
            "++++病史叙述者=病史陈述者",
            "供史者++++供史者新=病史陈述者",
            "供史者=病史陈述者",
            "病史叙述者=病史陈述者",
            "病史提供者++++病史叙述者=病史陈述者",
            "婚育史:=婚育史",
            "外生殖器++++外生殖器文本框=",
            //            "户口地址=联系地址",
            "家庭住址++++现住址=现住址",
            "产次++++产=产次",
            "家长姓名++++家长=家长姓名",
            "职业++++首页职业新=职业",
            "签名时间++++修正签名时间=修正签名时间",
            "修正诊断++++初步诊断=修正诊断",
            "与患者关系=关系",
//            "出生日期=出生时间",
            "入院情况=入院病情",
            "++++药物过敏史=药物过敏史",
            "++++浮肿少尿史=浮肿少尿史",
            "++++出血史=出血史",
            "++++多饮多尿史=多饮多尿史",
            "++++传染病史=传染病史",
            "++++尿频尿痛史=尿频尿痛史",
            "++++外伤手术史=外伤手术史",
            "++++咳嗽气喘史=咳嗽气喘史",
            "++++抽搐史=抽搐史",
            "++++重大疾病史和治疗史=重大疾病史和治疗史",
            "++++腹痛腹泻史=腹痛腹泻史",
            "++++过敏史=过敏史",
            "++++输血史=输血史",
            "++++胸闷心悸史=胸闷心悸史",
            "实验室检查++++辅助检查=辅助检查",
            "签名++++=医师签名",
            "兄弟姐妹健康状况++++兄弟姐妹=兄弟姐妹健康状况",
            "++++（外）祖父母=外祖父母",
            "出生时间=出生日期"
    );

    private String concatString(String item) {
        if (StringUtil.isBlank(item)) {
            return "";
        } else {
            return item + "。";
        }
    }

}