package com.cyksj.model.request.corp;

import com.cyksj.common.util.Xmls;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * @author chan
 * @date 2022/3/14 上午10:57
 */
@Slf4j
@Getter
@Setter
@ToString
public class CorpXmlMessage implements Serializable {

	///////////////////////
	// 以下都是微信推送过来的消息的xml的element所对应的属性
	///////////////////////

	@JacksonXmlProperty(localName = "AgentID")
	private String agentId;

	@JacksonXmlProperty(localName = "ToUserName")
	private String toUserName;

	@JacksonXmlProperty(localName = "FromUserName")
	private String fromUserName;

	@JacksonXmlProperty(localName = "CreateTime")
	private Long createTime;

	/**
	 * <pre>
	 * 当接受用户消息时，可能会获得以下值：
	 * {@link WxConsts.XmlMsgType#TEXT}
	 * {@link WxConsts.XmlMsgType#IMAGE}
	 * {@link WxConsts.XmlMsgType#VOICE}
	 * {@link WxConsts.XmlMsgType#VIDEO}
	 * {@link WxConsts.XmlMsgType#LOCATION}
	 * {@link WxConsts.XmlMsgType#LINK}
	 * {@link WxConsts.XmlMsgType#EVENT}
	 * 当发送消息的时候使用：
	 * {@link WxConsts.XmlMsgType#TEXT}
	 * {@link WxConsts.XmlMsgType#IMAGE}
	 * {@link WxConsts.XmlMsgType#VOICE}
	 * {@link WxConsts.XmlMsgType#VIDEO}
	 * {@link WxConsts.XmlMsgType#NEWS}
	 * </pre>
	 */
	@JacksonXmlProperty(localName = "MsgType")
	private String msgType;

	@JacksonXmlProperty(localName = "Content")
	private String content;

	@JacksonXmlProperty(localName = "MsgId")
	private Long msgId;

	@JacksonXmlProperty(localName = "PicUrl")
	private String picUrl;

	@JacksonXmlProperty(localName = "MediaId")
	private String mediaId;

	@JacksonXmlProperty(localName = "Format")
	private String format;

	@JacksonXmlProperty(localName = "ThumbMediaId")
	private String thumbMediaId;

	@JacksonXmlProperty(localName = "Location_X")
	private Double locationX;

	@JacksonXmlProperty(localName = "Location_Y")
	private Double locationY;

	@JacksonXmlProperty(localName = "Scale")
	private Double scale;

	@JacksonXmlProperty(localName = "Label")
	private String label;

	@JacksonXmlProperty(localName = "Title")
	private String title;

	@JacksonXmlProperty(localName = "Description")
	private String description;

	@JacksonXmlProperty(localName = "Url")
	private String url;

	@JacksonXmlProperty(localName = "Event")
	private String event;

	@JacksonXmlProperty(localName = "UpdateDetail")
	private String updateDetail;

	@JacksonXmlProperty(localName = "JoinScene")
	private String joinScene;

	@JacksonXmlProperty(localName = "QuitScene")
	private String quitScene;

	@JacksonXmlProperty(localName = "MemChangeCnt")
	private String memChangeCnt;

	@JacksonXmlProperty(localName = "Source")
	private String source;

	@JacksonXmlProperty(localName = "StrategyId")
	private String strategyId;

	@JacksonXmlProperty(localName = "EventKey")
	private String eventKey;

	@JacksonXmlProperty(localName = "Ticket")
	private String ticket;

	@JacksonXmlProperty(localName = "Latitude")
	private Double latitude;

	@JacksonXmlProperty(localName = "Longitude")
	private Double longitude;

	@JacksonXmlProperty(localName = "Precision")
	private Double precision;

	@JacksonXmlProperty(localName = "Recognition")
	private String recognition;

	@JacksonXmlProperty(localName = "TaskId")
	private String taskId;

	/**
	 * 通讯录变更事件.
	 * 请参考常量 me.chanjar.weixin.cp.constant.WxCpConsts.ContactChangeType
	 */
	@JacksonXmlProperty(localName = "ChangeType")
	private String changeType;

	/**
	 * 变更信息的成员UserID.
	 */
	@JacksonXmlProperty(localName = "UserID")
	private String userId;

	/**
	 * 变更信息的外部联系人的userid，注意不是企业成员的帐号.
	 */
	@JacksonXmlProperty(localName = "ExternalUserID")
	private String externalUserId;

	/**
	 * 添加此用户的「联系我」方式配置的state参数，可用于识别添加此用户的渠道.
	 */
	@JacksonXmlProperty(localName = "State")
	private String state;

	/**
	 * 欢迎语code，可用于发送欢迎语.
	 */
	@JacksonXmlProperty(localName = "WelcomeCode")
	private String welcomeCode;

	/**
	 * 新的UserID，变更时推送（userid由系统生成时可更改一次）.
	 */
	@JacksonXmlProperty(localName = "NewUserID")
	private String newUserId;

	/**
	 * 客户联系回调: 客户接替失败的原因
	 */
	@JacksonXmlProperty(localName = "FailReason")
	private String failReason;

	/**
	 * 客户联系回调: 标签类型
	 */
	@JacksonXmlProperty(localName = "TagType")
	private String tagType;

	/**
	 * 客户联系回调 标签id
	 */
	@JacksonXmlProperty(localName = "Id")
	private String id;


	///////////////////////////////////////
	// 群发消息返回的结果
	///////////////////////////////////////
	/**
	 * 多个时间共用字段.
	 * 1. 群发的结果.
	 * 2. 通讯录变更事件
	 * 激活状态：1=已激活 2=已禁用 4=未激活 已激活代表已激活企业微信或已关注微工作台（原企业号）.
	 */
	@JacksonXmlProperty(localName = "Status")
	private String status;

	/**
	 * group_id下粉丝数；或者openid_list中的粉丝数.
	 */
	@JacksonXmlProperty(localName = "TotalCount")
	private Integer totalCount;

	/**
	 * 过滤.
	 * （过滤是指特定地区、性别的过滤、用户设置拒收的过滤，用户接收已超4条的过滤）后，准备发送的粉丝数，原则上，filterCount = sentCount + errorCount
	 */
	@JacksonXmlProperty(localName = "FilterCount")
	private Integer filterCount;

	/**
	 * 发送成功的粉丝数.
	 */
	@JacksonXmlProperty(localName = "SentCount")
	private Integer sentCount;

	/**
	 * 发送失败的粉丝数.
	 */
	@JacksonXmlProperty(localName = "ErrorCount")
	private Integer errorCount;

	/**
	 * 客户群id
	 */
	@JacksonXmlProperty(localName = "ChatId")
	private String chatId;

	/**
	 * 获客链接ID
	 */
	@JacksonXmlProperty(localName = "LinkId")
	private String LinkId;

	// kf
	/**
	 * 调用拉取消息接口时，需要传此token，用于校验请求的合法性
	 */
	@JacksonXmlProperty(localName = "Token")
	private String token;

	/**
	 * 有新消息的客服帐号。可通过sync_msg接口指定open_kfid获取此客服帐号的消息
	 */
	@JacksonXmlProperty(localName = "OpenKfId")
	private String openKfId;

	public static CorpXmlMessage fromXml(String xml) {
		//修改微信变态的消息内容格式，方便解析
		xml = xml.replace("</PicList><PicList>", "");
		final CorpXmlMessage xmlMessage;
		try {
			xmlMessage = Xmls.parseObject(xml, CorpXmlMessage.class);
			return xmlMessage;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static CorpXmlMessage fromXml(String xml, String agentId) {
		//修改微信变态的消息内容格式，方便解析
		xml = xml.replace("</PicList><PicList>", "");
		final CorpXmlMessage xmlMessage;
		try {
			xmlMessage = Xmls.parseObject(xml, CorpXmlMessage.class);
			xmlMessage.setAgentId(agentId);
			return xmlMessage;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
