jiapeng 7 years ago
parent
commit
e98be7ecea

+ 42 - 19
src/main/java/com/ekexiu/portal/msg/Service.java

@ -19,6 +19,9 @@ import org.jfw.apt.web.annotation.param.JdbcConn;
19 19
import org.jfw.util.DateUtil;
20 20
import org.jfw.util.PageQueryResult;
21 21
import org.jfw.util.StringUtil;
22
import org.jfw.util.json.JsonService;
23

24
import com.ekexiu.portal.util.WebMsgSocketServlet;
22 25

23 26
@Path("/webMsg")
24 27
public class Service {
@ -56,7 +59,7 @@ public class Service {
56 59
		if (list == null || list.isEmpty())
57 60
			return EMPTY_ACTORS;
58 61

59
		SimpleDateFormat sf = new SimpleDateFormat(DateUtil.DATETIME_FORMAT);
62
		SimpleDateFormat sf = new SimpleDateFormat(DateUtil.TIMESTAMP_FORMAT);
60 63
		List<ActorItem> ret = new ArrayList<ActorItem>(list.size());
61 64
		for (ListIterator<WebMsgIdx> it = list.listIterator(); it.hasNext();) {
62 65
			ret.add(build(it.next(), owner, sf));
@ -65,11 +68,29 @@ public class Service {
65 68
	}
66 69

67 70
	@Get
68
	@Path("/idx")
71
	@Path("/idx/qm")
69 72
	public List<ActorItem> query(@JdbcConn Connection con, String id) throws SQLException {
70 73
		List<WebMsgIdx> list = webMsgDao.query(con, id);
71 74
		return build(list, id);
72 75
	}
76
	@Get
77
	@Path("/idx/qo")
78
	public ActorItem query(@JdbcConn Connection con,String id,String oid)throws SQLException{
79
		WebMsgIdx wmi = null;
80
		if(id.compareTo(oid)<0){
81
			wmi = webMsgDao.query(con,id, oid);
82
		}else{
83
			wmi = webMsgDao.query(con,oid, id);
84
		}
85
		SimpleDateFormat sf = new SimpleDateFormat(DateUtil.TIMESTAMP_FORMAT);
86
		if(wmi!=null){
87
			return build(wmi,id, sf);
88
		}
89
		return null;
90
		
91
	}
92
	
93
	
73 94

74 95
	@Get
75 96
	@Path("/idx/pq")
@ -108,7 +129,7 @@ public class Service {
108 129
		while (next) {
109 130
			try {
110 131
				long time = System.currentTimeMillis();
111
				wmc.setSendTime(DateUtil.formatDateTime(time));
132
				wmc.setSendTime(DateUtil.formatTimeStamp(time));
112 133
				webMsgDao.insert(con, wmc);
113 134
				int numOfUpdate = 0;
114 135
				if (ownerSend) {
@ -121,7 +142,7 @@ public class Service {
121 142
						wmi = new WebMsgIdx();
122 143
						wmi.setActor(actor);
123 144
						wmi.setLastCnt(cnt);
124
					
145

125 146
						wmi.setNumOfActor(ownerSend ? 1 : 0);
126 147
						wmi.setNumOfOwner(ownerSend ? 0 : 1);
127 148
						wmi.setOwner(owner);
@ -129,10 +150,11 @@ public class Service {
129 150
						wmi.setShowOfOwner(true);
130 151
					}
131 152
					wmi.setLastTime(time);
132
					webMsgDao.insert(con,wmi);
153
					webMsgDao.insert(con, wmi);
133 154
				}
134 155
				con.commit();
135
				next=false;
156
				next = false;
157
				WebMsgSocketServlet.sendMessage(reciver, JsonService.toJson(wmc));
136 158
			} catch (SQLException e) {
137 159
				if ("23505".equals(e.getSQLState())) {
138 160
					con.rollback();
@ -142,35 +164,36 @@ public class Service {
142 164
		}
143 165
	}
144 166

145

146 167
	@Get
147 168
	@Path("/cnt/load")
148
	public List<WebMsgCnt> load(@JdbcConn Connection con,String sender,String reciver,@Nullable String bt,@DefaultValue("true") boolean all)throws SQLException{
149
		if(all){
150
			return webMsgDao.queryAll(con,sender, reciver, bt);
151
		}else{
169
	public List<WebMsgCnt> load(@JdbcConn Connection con, String sender, String reciver, @Nullable String bt, @DefaultValue("true") boolean all)
170
			throws SQLException {
171
		if (all) {
172
			return webMsgDao.queryAll(con, sender, reciver, bt);
173
		} else {
152 174
			return webMsgDao.query(con, sender, reciver, bt);
153 175
		}
154 176
	}
155
	
177

156 178
	@Post
157 179
	@Path("/readed")
158
	public void readed(@JdbcConn(true) Connection con,String sender,String reciver,String time)throws SQLException{
180
	public void readed(@JdbcConn(true) Connection con, String sender, String reciver, String time) throws SQLException {
159 181
		int num = webMsgDao.readed(con, sender, reciver, time);
160
		if(num>0){
182
		if (num > 0) {
161 183
			String owner = sender;
162 184
			String actor = reciver;
163
			boolean 	ownerSend = true;
164
			if(sender.compareTo(reciver)>0){
185
			boolean ownerSend = true;
186
			if (sender.compareTo(reciver) > 0) {
165 187
				owner = reciver;
166 188
				actor = sender;
167 189
				ownerSend = false;
168 190
			}
169
			if(ownerSend){
170
				webMsgDao.decActorNum(con, owner, actor,num);
171
			}else{
191
			if (ownerSend) {
192
				webMsgDao.decActorNum(con, owner, actor, num);
193
			} else {
172 194
				webMsgDao.decOwnerNum(con, owner, actor, num);
173 195
			}
174 196
		}
175 197
	}
198

176 199
}

+ 1 - 1
src/main/java/com/ekexiu/portal/msg/WebMsgCnt.java

@ -38,7 +38,7 @@ public class WebMsgCnt {
38 38
		this.cnt = cnt;
39 39
	}
40 40
	
41
	@Column(DE.dateTime_de)
41
	@Column(DE.text_de)
42 42
	public String getSendTime() {
43 43
		return sendTime;
44 44
	}

+ 5 - 3
src/main/java/com/ekexiu/portal/msg/WebMsgDao.java

@ -2,7 +2,7 @@ package com.ekexiu.portal.msg;
2 2

3 3
import java.sql.Connection;
4 4
import java.sql.SQLException;
5
import java.util.List;
5
import java.util.List; 
6 6

7 7
import org.jfw.apt.annotation.Nullable;
8 8
import org.jfw.apt.orm.annotation.dao.DAO;
@ -13,6 +13,7 @@ import org.jfw.apt.orm.annotation.dao.method.SetSentence;
13 13
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
14 14
import org.jfw.apt.orm.annotation.dao.method.operator.PageSelect;
15 15
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
16
import org.jfw.apt.orm.annotation.dao.method.operator.SelectOne;
16 17
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
17 18
import org.jfw.apt.orm.annotation.dao.param.GroupSqlColumn;
18 19
import org.jfw.apt.orm.annotation.dao.param.GtEq;
@ -30,6 +31,9 @@ public interface WebMsgDao {
30 31
	List<WebMsgIdx> query(Connection con, @GroupSqlColumn(handlerClass = StringHandler.class, isAnd = false, value = { "OWNER=? AND SHOW_OF_OWNER='1'",
31 32
			"ACTOR=? AND SHOW_OF_ACTOR='1'" },force=false) String id) throws SQLException;
32 33
	
34
	@SelectOne
35
	@Nullable
36
	WebMsgIdx query(Connection con,String owner,String actor)throws SQLException;
33 37
	
34 38
	@Exclude({"showOfOwner","showOfActor"})
35 39
	@PageSelect
@ -85,6 +89,4 @@ public interface WebMsgDao {
85 89
	@From(WebMsgCnt.class)
86 90
	@SetSentence("READED='1'")
87 91
	int readed(Connection con,String sender,String reciver,@LtEq String sendTime)throws SQLException;
88
	
89
	
90 92
}

+ 8 - 7
src/main/java/com/ekexiu/portal/util/WebMsgSocketServlet.java

@ -20,15 +20,12 @@ public class WebMsgSocketServlet {
20 20
	private static final Map<String, Session> sessions = new ConcurrentHashMap<String, Session>();
21 21
	private static final String[] TYPE_LIST = new String[]{"www","app"};
22 22
	
23
	
24 23
	public static void sendMessage(String id ,String cnt){
25
		
26 24
		for(String type:TYPE_LIST){
27 25
			String key = id+"~"+type;
28 26
			Session session = sessions.get(key);
29 27
			if(session!=null && session.isOpen()){
30 28
				try {
31
					System.out.println("SEND:"+key+"           "+cnt);
32 29
					session.getBasicRemote().sendText(cnt);
33 30
				} catch (IOException e) {
34 31
				}
@ -36,6 +33,13 @@ public class WebMsgSocketServlet {
36 33
			
37 34
		}
38 35
	}
36
	public static boolean invalidType(String type){
37
		for(String s:TYPE_LIST){
38
			if(s.equals(type))
39
				return false;
40
		}
41
		return true;
42
	}
39 43

40 44
	@OnMessage
41 45
	public void echoTextMessage(Session session, String msg, boolean last) {
@ -75,7 +79,6 @@ public class WebMsgSocketServlet {
75 79
	@OnOpen
76 80
	public void open(Session session) {
77 81
		String qs = session.getQueryString();
78
		System.out.println("onOpen:"+qs);
79 82
		String[] ps = qs.split("&");
80 83
		String id = null;
81 84
		String type = null;
@ -89,7 +92,7 @@ public class WebMsgSocketServlet {
89 92
			}
90 93
		}
91 94

92
		if (id == null || type == null) {
95
		if (id == null || type == null || invalidType(type) ) {
93 96
			try {
94 97
				session.close();
95 98
			} catch (IOException e) {
@ -97,7 +100,6 @@ public class WebMsgSocketServlet {
97 100
			return;
98 101
		}
99 102
		String sessionFlag = id + "~" + type;
100

101 103
		Session old = null;
102 104
		synchronized (lock) {
103 105
			old = sessions.get(sessionFlag);
@ -114,7 +116,6 @@ public class WebMsgSocketServlet {
114 116
	@OnClose
115 117
	public void close(Session session) {
116 118
		String qs = session.getQueryString();
117
		System.out.println("OnClose:"+qs);
118 119
		String[] ps = qs.split("&");
119 120
		String id = null;
120 121
		String type = null;