package com.docus.server.message.converters;

import com.docus.core.util.DateUtil;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.server.message.dto.RecordSubmitDTO;
import com.docus.server.message.util.XmlUtil;
import org.w3c.dom.Node;

import java.util.Date;

public class WsRecordSubmitMessageConvert {
    public static RecordSubmitDTO convert(String str) {
        String inHospNo = null;
        String inHospIndexNo = null;
        Integer visitNo = null;
        String name = null;
        Date submitTime = null;
        Integer inHospState = null;
        String submitDoctorCode = null;
        String submitDoctorName = null;
        XmlUtil xmlUtil = XmlUtil.of(str);

        try {
            // 住院流水号
            Node inHospNoNode = xmlUtil.getNode("/Request/Msg/INHOSP_NO");
            inHospNo = inHospNoNode.getTextContent();
        } catch (Exception ex) {
            throw new BaseException("解析 住院流水号 出错！");
        }

        try {
            // 住院号
            Node inHospIndexNoNode = xmlUtil.getNode("/Request/Msg/INHOSP_INDEX_NO");
            inHospIndexNo = inHospIndexNoNode.getTextContent();
        } catch (Exception ex) {
            throw new BaseException("解析 住院号 出错！");
        }
        try {
            // 住院次数
            Node visitNoNode = xmlUtil.getNode("/Request/Msg/VISIT_NO");
            visitNo = Integer.parseInt(visitNoNode.getTextContent());
        } catch (Exception ex) {
            throw new BaseException("解析 住院次数 出错！");
        }
        try {
            // 患者姓名
            Node nameNode = xmlUtil.getNode("/Request/Msg/NAME");
            name = nameNode.getTextContent();
        } catch (Exception ex) {
            throw new BaseException("解析 姓名 出错！");
        }
        try {
            // 提交时间
            Node submitTimeNode = xmlUtil.getNode("/Request/Msg/SUBMIT_TIME");
            submitTime = DateUtil.parse(submitTimeNode.getTextContent(), DateUtil.PATTERN_DATETIME);
        } catch (Exception ex) {
            throw new BaseException("解析 提交时间 出错！");
        }
        try {
            // 提交时间
            Node submitDoctorCodeNode = xmlUtil.getNode("/Request/Msg/SUBMIT_DOCTOR_CODE");
            submitDoctorCode = submitDoctorCodeNode.getTextContent();
        } catch (Exception ex) {
            throw new BaseException("解析 提交医生工号 出错！");
        }
        try {
            // 提交时间
            Node submitDoctorNameNode = xmlUtil.getNode("/Request/Msg/SUBMIT_DOCTOR_NAME");
            submitDoctorName = submitDoctorNameNode.getTextContent();
        } catch (Exception ex) {
            throw new BaseException("解析 提交医生姓名 出错！");
        }

        RecordSubmitDTO recordSubmitDTO = new RecordSubmitDTO();
        recordSubmitDTO.setInHospNo(inHospNo);
        recordSubmitDTO.setVisitNo(visitNo);
        recordSubmitDTO.setInHospState(inHospState);
        recordSubmitDTO.setName(name);
        recordSubmitDTO.setInHospIndexNo(inHospIndexNo);
        recordSubmitDTO.setSubmitTime(submitTime);
        recordSubmitDTO.setSubmitDoctorCode(submitDoctorCode);
        recordSubmitDTO.setSubmitDoctorName(submitDoctorName);
        return recordSubmitDTO;
    }
}
