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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
68
69 @XmlElements({ @XmlElement(name = "MsgId"), @XmlElement(name = "MsgID") })
70 private long msgId;
71
72
73
74 @XmlElements({ @XmlElement(name = "AgentId"), @XmlElement(name = "AgentID") })
75 private String agentId;
76
77 public WeixinMessage() {
78
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 }