package com.docus.server.message.converters;

import com.docus.core.util.DateUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.server.message.dto.NkRecordSubmitDTO;
import com.docus.server.message.dto.TbasicQrPreciseCondition;
import com.docus.server.message.feign.vo.ArchiveAndRecallPushVo;
import com.docus.server.message.util.XmlUtil;
import org.w3c.dom.Node;

import java.util.Date;
import java.util.UUID;

public class NkWsRecordSubmitMessageConvert {
    public static NkRecordSubmitDTO convert(String str) {
        String jzh = null;
        String inpatientNo = null;
        String name = null;
        Integer admissTimes = null;
        String submitNo=null;
        String submitName=null;
        String submitDate = null;

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

        try {
            // 住院号
            Node inHospIndexNoNode = xmlUtil.getNode("/Request/Msg/INHOSP_INDEX_NO");
            inpatientNo = inHospIndexNoNode.getTextContent();
        } catch (Exception ex) {
            throw new BaseException("解析 住院号 出错！");
        }
        try {
            // 住院次数
            Node visitNoNode = xmlUtil.getNode("/Request/Msg/VISIT_NO");
            String admissTimesStr = visitNoNode.getTextContent();
            if (Func.isNotBlank(admissTimesStr)) {
                admissTimes = Integer.parseInt(admissTimesStr.trim());
            }
        } 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 submitNoNode = xmlUtil.getNode("/Request/Msg/SUBMIT_NO");
            submitNo = submitNoNode.getTextContent();
        } catch (Exception ex) {
            throw new BaseException("解析 提交人工号 出错！");
        }
        try {
            // 提交人姓名
            Node submitNameNode = xmlUtil.getNode("/Request/Msg/SUBMIT_NAME");
            submitName = submitNameNode.getTextContent();
        } catch (Exception ex) {
            throw new BaseException("解析 提交人姓名 出错！");
        }

        try {
            // 提交时间
            Node submitTimeNode = xmlUtil.getNode("/Request/Msg/SUBMIT_TIME");
            String submitTime = submitTimeNode.getTextContent();
            Date submitDateTime = Func.parseDate(submitTime.trim(), DateUtil.PATTERN_DATETIME);
            submitDate = Func.formatDateTime(submitDateTime);
        } catch (Exception ex) {
            throw new BaseException("解析 提交时间 出错！");
        }

        NkRecordSubmitDTO recordSubmitDTO = new NkRecordSubmitDTO();
        recordSubmitDTO.setJzh(jzh);
        recordSubmitDTO.setInpatientNo(inpatientNo);
        recordSubmitDTO.setAdmissTimes(admissTimes);
        recordSubmitDTO.setName(name);
        recordSubmitDTO.setSubmitNo(submitNo);
        recordSubmitDTO.setSubmitName(submitName);
        recordSubmitDTO.setSubmitDate(submitDate);
        return recordSubmitDTO;
    }

    public static TbasicQrPreciseCondition convert(NkRecordSubmitDTO dto) {
        if (dto == null) {
            return null;
        }
        TbasicQrPreciseCondition condition = new TbasicQrPreciseCondition();

        condition.setInpatientNo(dto.getInpatientNo());
        condition.setAdmissTimes(dto.getAdmissTimes());
        condition.setName(dto.getName());
        return condition;
    }
    public static String getPushXml(ArchiveAndRecallPushVo vo) {
        String result = "<Msg>\n" +
                "\t<!--归档/解档ID-->\n" +
                "\t<Id>"+ UUID.randomUUID()+"</Id>\n" +
                "\t<!--住院号-->\n" +
                "\t<inHospIndexNo>"+vo.getInpatientNo()+"</inHospIndexNo>\n" +
                "\t<!--住院次数-->\n" +
                "\t<visitNo>"+vo.getAdmissTimes()+"</visitNo>\n" +
                "\t<!--患者姓名-->\n" +
                "\t<patientName>"+vo.getName()+"</patientName>\n" +
                "\t<!--住院就诊流水号-->\n" +
                "\t<inHospNo>"+vo.getExternalId()+"</inHospNo>\n" +
                "\t<!--入院时间yyyy-MM-dd HH:mm:ss-->\n" +
                "\t<admitTime>"+ DateUtil.format(vo.getAdmissDate(),DateUtil.PATTERN_DATETIME)+"</admitTime>\n" +
                "\t<!--出院时间yyyy-MM-dd HH:mm:ss-->\n" +
                "\t<disTime>"+DateUtil.format(vo.getDisDate(),DateUtil.PATTERN_DATETIME)+"</disTime>\n" +
                "\t<!--归档时间yyyy-MM-dd HH:mm:ss-->\n" +
                "\t<archiveTime>"+vo.getArchiveTime()+"</archiveTime>\n" +
                "\t<!--解档时间yyyy-MM-dd HH:mm:ss-->\n" +
                "\t<unarchiveTime></unarchiveTime>\n" +
                "\t<!--出院科室代码-->\n" +
                "\t<dischargeDeptCode>"+vo.getDisDept()+"</dischargeDeptCode>\n" +
                "\t<!--出院科室-->\n" +
                "\t<dischargeDeptName>"+vo.getDisDeptName()+"</dischargeDeptName>\n" +
                "\t<!--操作人工号-->\n" +
                "\t<controlUserName>"+vo.getControlUserName()+"</controlUserName>\n" +
                "\t<!--操作人姓名-->\n" +
                "\t<controlName>"+vo.getControlName()+"</controlName>\n" +
                "\t<!--解档/归档状态 0:解档 1:归档-->\n" +
                "\t<archiveState>"+vo.getIsArchive()+"</archiveState>\n" +
                "</Msg>";
        return result;
    }
}
