package com.docus.server.collect.dept;

import com.docus.core.util.XmlUtil;
import com.docus.server.collect.mq.dept.AbstractDeptMqCollectService;
import com.docus.server.sys.common.pojo.dto.DeptDTO;
import org.springframework.stereotype.Service;
import org.w3c.dom.Node;

@Service
public class JmsIbmMqDeptCollectServiceImpl extends AbstractDeptMqCollectService {

    /**
     * 监听注解订阅，自己编写
     *
     * @param message 科室信息
     */
    //@JmsIbmListener(destination = "TJ_createDepartment")
    @Override
    public void addDept(String message) {
        super.addDept(message);
    }

    /**
     * 监听注解订阅，自己编写
     *
     * @param message 科室信息
     */
    //@JmsIbmListener(destination = "TJ_updateDepartment")
    @Override
    public void updateDept(String message) {
        super.updateDept(message);
    }

    /**
     * 解析新增的消息, 下面是个例子
     *
     * @param xml
     * @return
     */
    @Override
    public DeptDTO parseAddDeptXml(String xml) {
        XmlUtil xmlParseUtil = XmlUtil.of(xml);
        Node deptCodeNode = xmlParseUtil.getNode("/PRPM_IN401030UV01/controlActProcess/subject/registrationRequest/subject1/assignedEntity/id/item/@extension");
        Node deptNameNode = xmlParseUtil.getNode("/PRPM_IN401030UV01/controlActProcess/subject/registrationRequest/subject1/assignedEntity/assignedPrincipalOrganization/name/item[@use='OR']/part/@value");
        Node authorIdNode = xmlParseUtil.getNode("/PRPM_IN401030UV01/controlActProcess/subject/registrationRequest/author/assignedEntity/id/item/@extension");
        Node authorNameNode = xmlParseUtil.getNode("/PRPM_IN401030UV01/controlActProcess/subject/registrationRequest/author/assignedEntity/assignedPerson/name/item/part/@value");

        DeptDTO deptDto = new DeptDTO();
        deptDto.setDeptCode(deptCodeNode.getNodeValue());
        deptDto.setDeptName(deptNameNode.getNodeValue());
        deptDto.setAuthorId(authorIdNode.getNodeValue());
        deptDto.setAuthorName(authorNameNode.getNodeValue());
        return deptDto;
    }

    /**
     * 解析更新的消息, 下面是个例子
     *
     * @param xml
     * @return
     */
    @Override
    public DeptDTO parseUpdateDeptXml(String xml) {
        XmlUtil xmlParseUtil = XmlUtil.of(xml);
        Node deptCodeNode = xmlParseUtil.getNode("/PRPM_IN403010UV01/controlActProcess/subject/registrationRequest/subject1/assignedEntity/id/item/@extension");
        Node deptNameNode = xmlParseUtil.getNode("/PRPM_IN403010UV01/controlActProcess/subject/registrationRequest/subject1/assignedEntity/assignedPrincipalOrganization/name/item[@use='OR']/part/@value");
        Node authorIdNode = xmlParseUtil.getNode("/PRPM_IN403010UV01/controlActProcess/subject/registrationRequest/author/assignedEntity/id/item/@extension");
        Node authorNameNode = xmlParseUtil.getNode("/PRPM_IN403010UV01/controlActProcess/subject/registrationRequest/author/assignedEntity/assignedPerson/name/item/part/@value");

        DeptDTO deptDto = new DeptDTO();
        deptDto.setDeptCode(deptCodeNode.getNodeValue());
        deptDto.setDeptName(deptNameNode.getNodeValue());
        deptDto.setAuthorId(authorIdNode.getNodeValue());
        deptDto.setAuthorName(authorNameNode.getNodeValue());
        return deptDto;
    }

}
