View Javadoc
1   package com.foxinmy.weixin4j.request;
2   
3   import java.io.Serializable;
4   import java.util.Date;
5   
6   import javax.xml.bind.annotation.XmlElement;
7   import javax.xml.bind.annotation.XmlElements;
8   import javax.xml.bind.annotation.XmlTransient;
9   
10  import com.foxinmy.weixin4j.type.MessageType;
11  
12  /**
13   * 微信消息基类
14   * 
15   * @className WeixinMessage
16   * @author jinyu(foxinmy@gmail.com)
17   * @date 2015年5月6日
18   * @since JDK 1.6
19   * @see com.foxinmy.weixin4j.message.ImageMessage
20   * @see com.foxinmy.weixin4j.message.LinkMessage
21   * @see com.foxinmy.weixin4j.message.LocationMessage
22   * @see com.foxinmy.weixin4j.message.TextMessage
23   * @see com.foxinmy.weixin4j.message.VideoMessage
24   * @see com.foxinmy.weixin4j.message.VoiceMessage
25   * @see com.foxinmy.weixin4j.message.event.EventMessage
26   * @see com.foxinmy.weixin4j.message.event.LocationEventMessage
27   * @see com.foxinmy.weixin4j.message.event.MenuEventMessage
28   * @see com.foxinmy.weixin4j.message.event.MenuLocationEventMessage
29   * @see com.foxinmy.weixin4j.message.event.MenuPhotoEventMessage
30   * @see com.foxinmy.weixin4j.message.event.MenuScanEventMessage
31   * @see com.foxinmy.weixin4j.mp.event.KfCloseEventMessage
32   * @see com.foxinmy.weixin4j.mp.event.KfCreateEventMessage
33   * @see com.foxinmy.weixin4j.mp.event.KfSwitchEventMessage
34   * @see com.foxinmy.weixin4j.mp.event.MassEventMessage
35   * @see com.foxinmy.weixin4j.mp.event.ScanEventMessage
36   * @see com.foxinmy.weixin4j.mp.event.ScribeEventMessage
37   * @see com.foxinmy.weixin4j.mp.event.TemplatesendjobfinishMessage
38   * @see com.foxinmy.weixin4j.qy.event.BatchjobresultMessage
39   * @see com.foxinmy.weixin4j.qy.event.EnterAgentEventMessage
40   * @see com.foxinmy.weixin4j.qy.event.ScribeEventMessage
41   */
42  public class WeixinMessage implements Serializable {
43  
44  	private static final long serialVersionUID = 7761192742840031607L;
45  
46  	/**
47  	 * 开发者微信号
48  	 */
49  	@XmlElement(name = "ToUserName")
50  	private String toUserName;
51  	/**
52  	 * 发送方账号 即用户的openid
53  	 */
54  	@XmlElement(name = "FromUserName")
55  	private String fromUserName;
56  	/**
57  	 * 消息创建时间 系统毫秒数
58  	 */
59  	@XmlElement(name = "CreateTime")
60  	private long createTime;
61  	/**
62  	 * 消息类型
63  	 */
64  	@XmlElement(name = "MsgType")
65  	private String msgType;
66  	/**
67  	 * 消息ID 可用于排重
68  	 */
69  	@XmlElements({ @XmlElement(name = "MsgId"), @XmlElement(name = "MsgID") })
70  	private long msgId;
71  	/**
72  	 * 企业号独有的应用ID
73  	 */
74  	@XmlElements({ @XmlElement(name = "AgentId"), @XmlElement(name = "AgentID") })
75  	private String agentId;
76  
77  	public WeixinMessage() {
78  		// jaxb required
79  	}
80  
81  	public WeixinMessage(String msgType) {
82  		this.msgType = msgType;
83  	}
84  
85  	public String getToUserName() {
86  		return toUserName;
87  	}
88  
89  	public String getFromUserName() {
90  		return fromUserName;
91  	}
92  
93  	public long getCreateTime() {
94  		return createTime;
95  	}
96  
97  	@XmlTransient
98  	public Date getFormatCreateTime() {
99  		return new Date(createTime * 1000l);
100 	}
101 
102 	public String getMsgType() {
103 		return msgType;
104 	}
105 
106 	@XmlTransient
107 	public MessageType getFormatMsgType() {
108 		return MessageType.valueOf(msgType);
109 	}
110 
111 	public long getMsgId() {
112 		return msgId;
113 	}
114 
115 	public String getAgentId() {
116 		return agentId;
117 	}
118 
119 	@Override
120 	public int hashCode() {
121 		final int prime = 31;
122 		int result = 1;
123 		result = prime * result + ((agentId == null) ? 0 : agentId.hashCode());
124 		result = prime * result + (int) (createTime ^ (createTime >>> 32));
125 		result = prime * result
126 				+ ((fromUserName == null) ? 0 : fromUserName.hashCode());
127 		result = prime * result + (int) (msgId ^ (msgId >>> 32));
128 		result = prime * result + ((msgType == null) ? 0 : msgType.hashCode());
129 		result = prime * result
130 				+ ((toUserName == null) ? 0 : toUserName.hashCode());
131 		return result;
132 	}
133 
134 	@Override
135 	public boolean equals(Object obj) {
136 		if (this == obj)
137 			return true;
138 		if (obj == null)
139 			return false;
140 		if (getClass() != obj.getClass())
141 			return false;
142 		WeixinMessage other = (WeixinMessage) obj;
143 		if (msgId > 0l && other.getMsgId() > 0l) {
144 			return msgId == other.getMsgId();
145 		}
146 		return fromUserName.equals(other.getFromUserName())
147 				&& createTime == other.getCreateTime();
148 	}
149 
150 	@Override
151 	public String toString() {
152 		String toString = " toUserName=" + toUserName + ", fromUserName="
153 				+ fromUserName + ", createTime=" + createTime + ", msgType="
154 				+ msgType;
155 		if (msgId > 0l) {
156 			toString += ", msgId=" + msgId;
157 		}
158 		if (agentId != null) {
159 			toString += ", agentId=" + agentId;
160 		}
161 		return toString;
162 	}
163 }