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