package com.lantone.qc.trans.taizhou;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.model.doc.ward.AttendingDoctorWardDoc;
import com.lantone.qc.pub.model.doc.ward.DirectorDoctorWardDoc;
import com.lantone.qc.pub.model.doc.ward.GeneralDoctorWardDoc;
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.trans.taizhou.util.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/17 13:27
 */
public class TaiZhouThreeLevelWardDocTrans extends ModelDocTrans {

    @Override
    public List<ThreeLevelWardDoc> extract(MedrecVo medrecVo) {
        List<ThreeLevelWardDoc> retList = Lists.newArrayList();
        List<String> contents = (List) medrecVo.getContent().get("content");

        ThreeLevelWardDoc result = new ThreeLevelWardDoc();
        contents.forEach(content -> {
            classifyThreeLevelWardDoc(result, content);
        });
        retList.add(result);
        return retList;
    }

    private void classifyThreeLevelWardDoc(ThreeLevelWardDoc result, String content) {
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZWithReplace(content); // xml原始数据给华卓
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts);

        if (StringUtils.isEmpty(structureMap.get("查房日期"))) {
            return;
        }

        //不能简单的标准转化，如果有查房标题，优先使用
        if (StringUtils.isEmpty(structureMap.get("查房标题")) && StringUtils.isNotEmpty(structureMap.get("查房备注"))) {
            structureMap.put("查房标题", structureMap.get("查房备注"));
        }
        if (StringUtils.isEmpty(structureMap.get("查房标题")) && StringUtils.isNotEmpty(structureMap.get("上级查房医生职称"))) {
            structureMap.put("查房标题", structureMap.get("上级查房医生职称"));
        }
        if (StringUtils.isEmpty(structureMap.get("查房标题")) && StringUtils.isNotEmpty(structureMap.get("查房类别"))) {
            structureMap.put("查房标题", structureMap.get("查房类别"));
        }
        //总的查房记录 汇总
        ThreeLevelWardDoc allDoctorWradDoc = new ThreeLevelWardDoc();
        allDoctorWradDoc.setStructureMap(structureMap);
        allDoctorWradDoc.setPageData((Map)sourceMap);
        result.addAllDoctorWradDoc(allDoctorWradDoc);

        //主任医师查房
        DirectorDoctorWardDoc directorDoctorWardDoc = findDirectorDoctorWardDoc(sourceMap, structureMap);
        if (directorDoctorWardDoc != null) {
            result.addDirectorDoctorWardDoc(findDirectorDoctorWardDoc(sourceMap, structureMap));
        }
        //主治医师查房
        AttendingDoctorWardDoc attendingDoctorWardDoc = findAttendingDoctorWardDoc(sourceMap, structureMap);
        if (attendingDoctorWardDoc != null) {
            result.addAttendingDoctorWardDoc(attendingDoctorWardDoc);
        }
        //普通医师查房
        if (directorDoctorWardDoc == null && attendingDoctorWardDoc == null) {
            GeneralDoctorWardDoc generalDoctorWardDoc = new GeneralDoctorWardDoc();
            generalDoctorWardDoc.setStructureMap(structureMap);
            generalDoctorWardDoc.setPageData((Map)sourceMap);
            result.addGeneralDoctorWardDoc(generalDoctorWardDoc);
        }
    }

    /**
     * 主任医师查房
     * @param sourceMap
     * @param structureMap
     */
    private DirectorDoctorWardDoc findDirectorDoctorWardDoc(Map<String, String> sourceMap, Map<String, String> structureMap) {
        String title = structureMap.get("查房标题");
        title = subTitle(title);    //标题有代字

        DirectorDoctorWardDoc directorDoctorWardDoc = null;
        if (StringUtils.isNotEmpty(title) && title.contains("主任") ) {
            directorDoctorWardDoc = new DirectorDoctorWardDoc();
            directorDoctorWardDoc.setStructureMap(structureMap);
            directorDoctorWardDoc.setPageData((Map)sourceMap);
        }
        return directorDoctorWardDoc;
    }

    /**
     * 主治医师查房
     * @param sourceMap
     * @param structureMap
     */
    private AttendingDoctorWardDoc findAttendingDoctorWardDoc(Map<String, String> sourceMap, Map<String, String> structureMap) {
        String title = structureMap.get("查房标题");
        title = subTitle(title);    //标题有代字
        AttendingDoctorWardDoc attendingDoctorWardDoc = null;
        if (StringUtils.isNotEmpty(title) && title.contains("主治")) {
            attendingDoctorWardDoc = new AttendingDoctorWardDoc();
            attendingDoctorWardDoc.setStructureMap(structureMap);
            attendingDoctorWardDoc.setPageData((Map)sourceMap);
        }
        return attendingDoctorWardDoc;
    }

    /**
     * 普通医师查房
     * @param threeLevelWardDoc
     */
    private GeneralDoctorWardDoc findGeneralDoctorWardDoc(ThreeLevelWardDoc threeLevelWardDoc) {
        GeneralDoctorWardDoc generalDoctorWardDoc = new GeneralDoctorWardDoc();
        generalDoctorWardDoc.setStructureMap(threeLevelWardDoc.getStructureMap());
        generalDoctorWardDoc.setText(threeLevelWardDoc.getText());
        generalDoctorWardDoc.setPageData(threeLevelWardDoc.getPageData());
        return generalDoctorWardDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "事件日期=记录时间",
            "事件日期=查房日期",
            "简要病情=病情记录",
            "查房记录=病情记录",
            "医生=记录医师"
    );

    public static String subTitle(String srcText) {
        if (StringUtil.isNotBlank(srcText) && srcText.contains("代") && srcText.indexOf("代") != srcText.length() - 1) {
            srcText = srcText.substring(srcText.indexOf("代") + 1);
        }
        return srcText;
    }

}