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.TbasicQrPreciseCondition;
import com.docus.server.message.dto.ZqRecordSubmitDTO;
import com.docus.server.message.util.XmlUtil;
import org.w3c.dom.Node;

import java.util.Date;
import java.util.Objects;

public class ZqWsRecordSubmitMessageConvert {
    public static ZqRecordSubmitDTO convert(String str) {
        String jzh = null;
        String inpatientNo = null;
        String name = null;
        String submitDate = null;
        Integer admissTimes = null;
        Date admissDate = null;
        Date disDate = 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 admissTimeNode = xmlUtil.getNode("/Request/Msg/ADMISS_TIME");
            String admissTime = admissTimeNode.getTextContent();
            if (Func.isNotBlank(admissTime)) {
                admissDate = Func.parseDate(admissTime.trim(), DateUtil.PATTERN_DATETIME);
            }

        } catch (Exception ex) {
            throw new BaseException("解析 入院时间 出错！");
        }
        try {
            // 提交时间
            Node disTimeNode = xmlUtil.getNode("/Request/Msg/DIS_TIME");
            String disTime = disTimeNode.getTextContent();
            if (Func.isNotBlank(disTime)) {
                disDate = Func.parseDate(disTime.trim(), DateUtil.PATTERN_DATETIME);
            }

        } 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("解析 提交时间 出错！");
        }

        ZqRecordSubmitDTO recordSubmitDTO = new ZqRecordSubmitDTO();
        recordSubmitDTO.setJzh(jzh);
        recordSubmitDTO.setInpatientNo(inpatientNo);
        recordSubmitDTO.setAdmissTimes(admissTimes);
        recordSubmitDTO.setName(name);
        recordSubmitDTO.setAdmissDate(admissDate);
        recordSubmitDTO.setDisDate(disDate);
        recordSubmitDTO.setSubmitDate(submitDate);
        return recordSubmitDTO;
    }

    public static TbasicQrPreciseCondition convert(ZqRecordSubmitDTO dto) {
        if (dto == null) {
            return null;
        }
        Date dtoAdmissDate = dto.getAdmissDate();
        Date dtoDisDate = dto.getDisDate();

        String admissDateBegin = null;
        String admissDateEnd = null;
        String disDateBegin = null;
        String disDateEnd = null;
        if (!Objects.isNull(dtoAdmissDate)) {
            String admissDateFormatDate = Func.formatDate(dtoAdmissDate);
            admissDateBegin = admissDateFormatDate + " 00:00:00";
            admissDateEnd = admissDateFormatDate + " 23:59:59";
        }
        if (!Objects.isNull(dtoDisDate)) {
            String disDateFormatDate = Func.formatDate(dtoDisDate);
            disDateBegin = disDateFormatDate + " 00:00:00";
            disDateEnd = disDateFormatDate + " 23:59:59";
        }

        TbasicQrPreciseCondition condition = new TbasicQrPreciseCondition();
//        condition.setJzh(dto.getJzh());
        condition.setInpatientNo(dto.getInpatientNo());
        condition.setAdmissTimes(dto.getAdmissTimes());
        condition.setAdmissDateBegin(admissDateBegin);
        condition.setAdmissDateEnd(admissDateEnd);
        condition.setDisDateBegin(disDateBegin);
        condition.setDisDateEnd(disDateEnd);
        condition.setName(dto.getName());
        return condition;
    }
}
