package com.docus.server.message.rpc.impl;

import com.docus.server.message.config.MeiZhouSyQueryConfig;
import com.docus.server.message.rpc.MeiZhouSyRpc;
import com.docus.server.message.rpc.request.MzSyArchiveRecallRequest;
import com.docus.server.message.util.JaxWsDynamicClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 梅州三院远程调用
 *
 * @author YongBin Wen
 * @date 2025/8/29 0029 9:52
 */
@Slf4j
@Service
public class MeiZhouSyRpcImpl implements MeiZhouSyRpc {
    @Resource
    private MeiZhouSyQueryConfig config;

    @Override
    public String archiveAndRecall(MzSyArchiveRecallRequest request) {
        String requestId = UUID.randomUUID().toString();
        request.setId(requestId);
        String param = toXml(request);
        String[] params = {param};

        log.info("[{}]梅州三院解档/归档调用，地址：{},命名空间：{},方法：{},参数：{}",requestId,
                config.getMedicalArchiveAndRecallWsdlAddr(),
                config.getMedicalArchiveAndRecallWsdlNamespaceUri(),
                config.getMedicalArchiveAndRecallWsdlOperationName(),
                params);
        String result = JaxWsDynamicClient.send(
                config.getMedicalArchiveAndRecallWsdlAddr(),
                config.getMedicalArchiveAndRecallWsdlNamespaceUri(),
                config.getMedicalArchiveAndRecallWsdlOperationName(),
                params);
        log.info("[{}]梅州三院解档/归档调用，返回结果：{}",requestId,result);
        return result;
    }


    private String toXml(MzSyArchiveRecallRequest request) {
        return "<Request><Msg>" +
                "<!--归档/解档ID-->" +
                "<Id>" + request.getId() + "</Id>" +
                "<!--住院号-->" +
                "<inHospIndexNo>" + request.getInHospIndexNo() + "</inHospIndexNo>" +
                "<!--住院次数-->" +
                "<visitNo>" + request.getVisitNo() + "</visitNo>" +
                "<!--患者姓名-->" +
                "<patientName>" + request.getPatientName() + "</patientName>" +
                "<!--住院就诊流水号-->" +
                "<inHospNo>" + request.getInHospNo() + "</inHospNo>" +
                "<!--入院时间yyyy-MM-dd HH:mm:ss-->" +
                "<admitTime>" + request.getAdmitTime() + "</admitTime>" +
                "<!--出院时间yyyy-MM-dd HH:mm:ss-->" +
                "<disTime>" + request.getDisTime() + "</disTime>" +
                "<!--归档时间yyyy-MM-dd HH:mm:ss-->" +
                "<archiveTime>" + request.getArchiveTime() + "</archiveTime>" +
                "<!--解档时间yyyy-MM-dd HH:mm:ss-->" +
                "<unarchiveTime>" + request.getUnarchiveTime() + "</unarchiveTime>" +
                "<!--出院科室代码-->" +
                "<dischargeDeptCode>" + request.getDischargeDeptCode() + "</dischargeDeptCode>" +
                "<!--出院科室-->" +
                "<dischargeDeptName>" + request.getDischargeDeptName() + "</dischargeDeptName>" +
                "<!--操作人工号-->" +
                "<controlUserName>" + request.getControlUserName() + "</controlUserName>" +
                "<!--操作人姓名-->" +
                "<controlName>" + request.getControlName() + "</controlName>" +
                "<!--解档/归档状态 0:解档 1:归档-->" +
                "<archiveState>" + request.getArchiveState() + "</archiveState>" +
                "<!--推送类型 EMR:电子病例，NURSE:护理-->" +
                "<type>" + request.getType() + "</type>" +
                "</Msg></Request>";
    }
}
