package com.cyksj.model.request.corp;

import com.cyksj.common.exception.AesException;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.StringUtil;
import com.cyksj.common.util.Xmls;
import com.cyksj.common.util.corp.WxCryptUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author chan
 * @date 2022/3/14 上午10:38
 */
@Getter
@Setter
public class CorpXmlOutMessage implements Serializable {

	@JacksonXmlCData
	@JacksonXmlProperty(localName = "ToUserName")
	protected String toUserName;

	@JacksonXmlCData
	@JacksonXmlProperty(localName = "FromUserName")
	protected String fromUserName;

	@JacksonXmlCData
	@JacksonXmlProperty(localName = "CreateTime")
	protected Long createTime;

	@JacksonXmlCData
	@JacksonXmlProperty(localName = "MsgType")
	protected String msgType;

	protected String toXml() {
		try {
			return Xmls.toXml(this);
		} catch (JsonProcessingException e) {
			throw BusinessRuntimeException.getInstance("企业微信 xml转换error:" + StringUtil.getErrorText(e));
		}
	}

	/**
	 * 转换成加密的xml格式.
	 */
	public String toEncryptedXml(WxCryptUtil wxCryptUtil) throws AesException {
		String plainXml = toXml();
		return wxCryptUtil.encrypt(plainXml);
	}
}
