jiapeng 6 years ago
parent
commit
4e96b5df5b

+ 3 - 0
.gitignore

@ -1,3 +1,6 @@
1 1
# Created by .ignore support plugin (hsz.mobi)
2 2
.idea
3 3
target
4
/.settings/
5
/.classpath
6
/.project

+ 9 - 3
src/main/java/com/ekexiu/project/platform/organization/OrgDao.java

@ -4,6 +4,8 @@ import org.jfw.apt.annotation.Nullable;
4 4
import org.jfw.apt.orm.annotation.dao.DAO;
5 5
import org.jfw.apt.orm.annotation.dao.method.From;
6 6
import org.jfw.apt.orm.annotation.dao.method.OrderBy;
7
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
8
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
7 9
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
8 10
import org.jfw.apt.orm.annotation.dao.method.operator.SelectOne;
9 11
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
@ -13,9 +15,6 @@ import java.sql.Connection;
13 15
import java.sql.SQLException;
14 16
import java.util.List;
15 17
16
/**
17
 * Created by TT on 2018/11/22.
18
 */
19 18
@DAO
20 19
public interface OrgDao {
21 20
@ -30,4 +29,11 @@ public interface OrgDao {
30 29
    @SelectOne
31 30
    @Nullable
32 31
    Organization queryOne(Connection con, String id) throws SQLException;
32
    
33
    @DeleteWith
34
    @From(Organization.class)
35
    int delete(Connection con,String id)throws SQLException;
36
    
37
    @Insert
38
    int insert(Connection con,Organization org)throws SQLException;
33 39
}

+ 79 - 1
src/main/java/com/ekexiu/project/platform/organization/OrgService.java

@ -6,10 +6,19 @@ import org.jfw.apt.web.annotation.Path;
6 6
import org.jfw.apt.web.annotation.operate.Get;
7 7
import org.jfw.apt.web.annotation.operate.Post;
8 8
import org.jfw.apt.web.annotation.param.JdbcConn;
9
import org.jfw.util.DateUtil;
10
import org.jfw.util.exception.JfwBaseException;
11
import org.jfw.util.reflect.TypeReference;
9 12
13
import com.ekexiu.project.platform.professor.Professor;
14
import com.ekexiu.project.util.HttpService;
15
16
import java.io.IOException;
10 17
import java.sql.Connection;
11 18
import java.sql.SQLException;
19
import java.util.LinkedList;
12 20
import java.util.List;
21
import java.util.Map;
13 22
14 23
/**
15 24
 * Created by TT on 2018/11/22.
@ -17,6 +26,8 @@ import java.util.List;
17 26
@Path("/org")
18 27
public class OrgService {
19 28
29
	private String syncUrl = "http://www.ekexiu.com/ajax/platform/ids/buttedOrgs?pid=";
30
	private String infoUrl = "http://www.ekexiu.com/ajax/org/";
20 31
    @Autowrie
21 32
    private OrgDao orgDao;
22 33
@ -28,7 +39,23 @@ public class OrgService {
28 39
        this.orgDao = orgDao;
29 40
    }
30 41
31
    @Path("/setlevel")
42
    public String getSyncUrl() {
43
		return syncUrl;
44
	}
45
46
	public void setSyncUrl(String syncUrl) {
47
		this.syncUrl = syncUrl;
48
	}
49
50
	public String getInfoUrl() {
51
		return infoUrl;
52
	}
53
54
	public void setInfoUrl(String infoUrl) {
55
		this.infoUrl = infoUrl;
56
	}
57
58
	@Path("/setlevel")
32 59
    @Post
33 60
    public void setLevel(@JdbcConn(true) Connection con, String id, @Nullable Integer level) throws SQLException {
34 61
        orgDao.setlevel(con, id, level);
@ -45,4 +72,55 @@ public class OrgService {
45 72
    public Organization queryOne(@JdbcConn Connection con, String id) throws SQLException {
46 73
        return orgDao.queryOne(con, id);
47 74
    }
75
    
76
	@Get
77
    @Path("/sync")
78
   synchronized public void sync(@JdbcConn(true) Connection con,String pid)throws SQLException, JfwBaseException, IOException{
79
    	 List<Organization> existsList = this.orgDao.queryList(con);
80
    	 List<Organization> insertList = new LinkedList<Organization>();
81
    	 
82
    	 List<Map<String,String>> buttedProfessores = HttpService.get(this.syncUrl+pid, new TypeReference<List<Map<String,String>>>() {
83
		} .getType());
84
    	 
85
    	 if(buttedProfessores.size()>0){
86
    		 for(Map<String,String> map:buttedProfessores){
87
    			 String uid = map.get("uid");
88
    			 if(uid!= null){
89
    				 Organization p = null;
90
    				for(Organization pe:existsList){
91
    					if(uid.equals(pe.getId())){
92
    						p = pe;
93
    					}
94
    				}
95
    				if(p == null){
96
	    				 p = new Organization();
97
	    				 p.setId(uid);
98
	    				 p.setAssTime(map.get("createTime"));
99
	    				 insertList.add(p);
100
    				}else{
101
    					existsList.remove(p);    					
102
    				}
103
    			 }
104
    		 }
105
    		 
106
    		 for(Organization p:insertList){
107
    			 Map<String,Object> map = HttpService.get(this.infoUrl+p.getId(), new TypeReference<Map<String,Object>>() {
108
    				} .getType());
109
    			 p.setShareId(-1);
110
    			 if(map!=null){
111
    				 Object tmp = map.get("shareId");
112
    				 if(tmp!= null && tmp instanceof Double){
113
    					 p.setShareId(((Double)tmp).longValue());
114
    					 if(p.getAssTime()==null){
115
    						 p.setAssTime(DateUtil.formatDateTime(System.currentTimeMillis()));
116
    					 }
117
    					 orgDao.insert(con, p);
118
    				 }
119
    			 }
120
    		 }
121
    	 }
122
    	 for(Organization p:existsList){
123
			 this.orgDao.delete(con, p.getId());
124
		 }
125
    }
48 126
}

+ 9 - 0
src/main/java/com/ekexiu/project/platform/professor/ProfessorDao.java

@ -4,6 +4,8 @@ import org.jfw.apt.annotation.Nullable;
4 4
import org.jfw.apt.orm.annotation.dao.DAO;
5 5
import org.jfw.apt.orm.annotation.dao.method.From;
6 6
import org.jfw.apt.orm.annotation.dao.method.OrderBy;
7
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
8
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
7 9
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
8 10
import org.jfw.apt.orm.annotation.dao.method.operator.SelectOne;
9 11
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
@ -30,5 +32,12 @@ public interface ProfessorDao {
30 32
    @SelectOne
31 33
    @Nullable
32 34
    Professor queryOne(Connection con, String id) throws SQLException;
35
    
36
    @DeleteWith
37
    @From(Professor.class)
38
    int delete(Connection con,String id)throws SQLException;
39
    
40
    @Insert
41
    int insert(Connection con,Professor p)throws SQLException;
33 42
34 43
}

+ 114 - 22
src/main/java/com/ekexiu/project/platform/professor/ProfessorService.java

@ -6,10 +6,18 @@ import org.jfw.apt.web.annotation.Path;
6 6
import org.jfw.apt.web.annotation.operate.Get;
7 7
import org.jfw.apt.web.annotation.operate.Post;
8 8
import org.jfw.apt.web.annotation.param.JdbcConn;
9
import org.jfw.util.DateUtil;
10
import org.jfw.util.exception.JfwBaseException;
11
import org.jfw.util.reflect.TypeReference;
9 12
13
import com.ekexiu.project.util.HttpService;
14
15
import java.io.IOException;
10 16
import java.sql.Connection;
11 17
import java.sql.SQLException;
18
import java.util.LinkedList;
12 19
import java.util.List;
20
import java.util.Map;
13 21
14 22
/**
15 23
 * Created by TT on 2018/11/20.
@ -17,33 +25,117 @@ import java.util.List;
17 25
@Path("/professor")
18 26
public class ProfessorService {
19 27
20
    @Autowrie
21
    private ProfessorDao professorDao;
28
	private String syncUrl = "http://www.ekexiu.com/ajax/platform/ids/buttedProfessors?pid=";
29
	private String infoUrl = "http://www.ekexiu.com/ajax/professor/baseInfo/";
22 30
23
    public ProfessorDao getProfessorDao() {
24
        return professorDao;
25
    }
31
	@Autowrie
32
	private ProfessorDao professorDao;
26 33
27
    public void setProfessorDao(ProfessorDao professorDao) {
28
        this.professorDao = professorDao;
29
    }
34
	public String getInfoUrl() {
35
		return infoUrl;
36
	}
30 37
31
    @Path("/setlevel")
32
    @Post
33
    public void setLevel(@JdbcConn(true) Connection con, String id, @Nullable Integer level) throws SQLException {
34
        professorDao.setlevel(con, id, level);
35
    }
38
	public void setInfoUrl(String infoUrl) {
39
		this.infoUrl = infoUrl;
40
	}
36 41
37
    @Path("/list")
38
    @Get
39
    public List<Professor> queryList(@JdbcConn Connection con) throws SQLException {
40
        return professorDao.queryList(con);
41
    }
42
	public String getSyncUrl() {
43
		return syncUrl;
44
	}
45
46
	public void setSyncUrl(String syncUrl) {
47
		this.syncUrl = syncUrl;
48
	}
49
50
	public ProfessorDao getProfessorDao() {
51
		return professorDao;
52
	}
53
54
	public void setProfessorDao(ProfessorDao professorDao) {
55
		this.professorDao = professorDao;
56
	}
57
58
	@Path("/setlevel")
59
	@Post
60
	public void setLevel(@JdbcConn(true) Connection con, String id, @Nullable Integer level) throws SQLException {
61
		professorDao.setlevel(con, id, level);
62
	}
63
64
	@Path("/list")
65
	@Get
66
	public List<Professor> queryList(@JdbcConn Connection con) throws SQLException {
67
		return professorDao.queryList(con);
68
	}
69
70
	@Path("/qo")
71
	@Get
72
	public Professor queryOne(@JdbcConn Connection con, String id) throws SQLException {
73
		return professorDao.queryOne(con, id);
74
	}
42 75
43
    @Path("/qo")
44
    @Get
45
    public Professor queryOne(@JdbcConn Connection con, String id) throws SQLException {
46
        return professorDao.queryOne(con, id);
76
	@Get
77
    @Path("/sync")
78
   synchronized public void sync(@JdbcConn(true) Connection con,String pid)throws SQLException, JfwBaseException, IOException{
79
    	 List<Professor> existsList = this.professorDao.queryList(con);
80
    	 List<Professor> insertList = new LinkedList<Professor>();
81
    	 
82
    	 List<Map<String,String>> buttedProfessores = HttpService.get(this.syncUrl+pid, new TypeReference<List<Map<String,String>>>() {
83
		} .getType());
84
    	 
85
    	 if(buttedProfessores.size()>0){
86
    		 for(Map<String,String> map:buttedProfessores){
87
    			 String uid = map.get("uid");
88
    			 if(uid!= null){
89
    				Professor p = null;
90
    				for(Professor pe:existsList){
91
    					if(uid.equals(pe.getId())){
92
    						p = pe;
93
    					}
94
    				}
95
    				if(p == null){
96
	    				 p = new Professor();
97
	    				 p.setId(uid);
98
	    				 p.setAssTime(map.get("createTime"));
99
	    				 insertList.add(p);
100
    				}else{
101
    					existsList.remove(p);    					
102
    				}
103
    			 }
104
    		 }
105
    		 
106
    		 for(Professor p:insertList){
107
    			 Map<String,Object> map = HttpService.get(this.infoUrl+p.getId(), new TypeReference<Map<String,Object>>() {
108
    				} .getType());
109
    			 p.setShareId(-1);
110
    			 if(map!=null){
111
    				 Object tmp = map.get("shareId");
112
    				 if(tmp!= null && tmp instanceof Double){
113
    					 p.setShareId(((Double)tmp).longValue());
114
    					 if(p.getAssTime()==null){
115
    						 p.setAssTime(DateUtil.formatDateTime(System.currentTimeMillis()));
116
    					 }
117
    					 professorDao.insert(con, p);
118
    				 }
119
    			 }
120
    		 }
121
    	 }
122
    	 for(Professor p:existsList){
123
			 this.professorDao.delete(con, p.getId());
124
		 }
47 125
    }
126
	
127
	
128
//	public static void main(String[] args) throws Exception{
129
//		 Map<String,Object> map = HttpService.get("http://www.ekexiu.com/ajax/professor/baseInfo/FA08E12933D64D76999512DDDF3958F9", new TypeReference<Map<String,Object>>() {
130
//			} .getType());
131
//
132
//		 if(map!=null){
133
//			 Object tmp = map.get("shareId");
134
//			 if(tmp!= null){
135
//				 System.out.println(((Double)tmp).longValue());
136
//			 }
137
//		 }
138
//		
139
//	}
48 140
49 141
}

+ 216 - 0
src/main/java/com/ekexiu/project/util/HttpService.java

@ -0,0 +1,216 @@
1
package com.ekexiu.project.util;
2

3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.InputStreamReader;
6
import java.io.OutputStream;
7
import java.io.StringWriter;
8
import java.lang.reflect.ParameterizedType;
9
import java.lang.reflect.Type;
10
import java.net.HttpURLConnection;
11
import java.net.URL;
12

13
import org.jfw.util.ConstData;
14
import org.jfw.util.exception.JfwBaseException;
15
import org.jfw.util.io.IoUtil;
16
import org.jfw.util.json.JsonService;
17
import org.jfw.util.reflect.TypeReference;
18

19
public class HttpService {
20
	private static final byte[] SIMPLE_POST_DATA = "1=1".getBytes(ConstData.UTF8);
21

22
	private static String charset = "UTF-8";
23

24

25
	public static Type buildResultType(final Type t) {
26
		return new ParameterizedType() {
27

28
			@Override
29
			public Type getRawType() {
30
				return Result.class;
31
			}
32

33
			@Override
34
			public Type getOwnerType() {
35
				return HttpService.class;
36
			}
37

38
			@Override
39
			public Type[] getActualTypeArguments() {
40
				return new Type[] { t };
41
			}
42
		};
43
	}
44

45
	private static  void raise(Result<?> ret) throws JfwBaseException {
46
		throw new JfwBaseException(ret.getCode().intValue(), "remote calll error:" + ret.getMsg() + ";\r\n" + ret.getDetailMsg());
47
	}
48

49
	private static <T> T handResponse(InputStream in, Type typeOfT) throws JfwBaseException {
50
		if (null != typeOfT) {
51
			Result<T> ret = JsonService.fromJson(new InputStreamReader(in, ConstData.UTF8), buildResultType(typeOfT));
52
			if (ret.isSuccess()) {
53
				return ret.getData();
54
			} else {
55
				raise(ret);
56
			}
57

58
		} else {
59
			Result<String> ret = JsonService.fromJson(new InputStreamReader(in, ConstData.UTF8), new TypeReference<Result<String>>() {
60
			}.getType());
61
			if (ret.isSuccess())
62
				return null;
63
			else {
64
				raise(ret);
65
			}
66
		}
67
		return null;
68
	}
69

70

71
	public static <T> T get(String url, Type typeOfT) throws JfwBaseException, IOException {
72
		URL u = new URL(url);
73
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
74
		httpURLConnection.setRequestProperty("Accept-Charset", charset);
75
		httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
76
		if (httpURLConnection.getResponseCode() != 200) {
77
			throw new IOException("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
78
		}
79
		InputStream in = httpURLConnection.getInputStream();
80
		try {
81
			return handResponse(in, typeOfT);
82
		} finally {
83
			IoUtil.close(in);
84
		}
85
	}
86

87

88
	public static <T> T post(String url, String data, Type typeOfT) throws JfwBaseException, IOException {
89
		byte[] bs = SIMPLE_POST_DATA;
90
		if (data != null && data.length() > 0) {
91
			bs = data.getBytes(ConstData.UTF8);
92
		}
93
		URL u = new URL(url);
94
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
95
		httpURLConnection.setDoOutput(true);
96
		httpURLConnection.setRequestMethod("POST");
97
		httpURLConnection.setRequestProperty("Accept-Charset", charset);
98
		httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
99
		httpURLConnection.setRequestProperty("Content-Length", String.valueOf(bs.length));
100
		OutputStream outputStream = httpURLConnection.getOutputStream();
101
		try {
102
			outputStream.write(bs);
103
			outputStream.flush();
104
		} finally {
105
			IoUtil.close(outputStream);
106
		}
107
		if (httpURLConnection.getResponseCode() != 200) {
108
			throw new IOException("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
109
		}
110
		InputStream in = httpURLConnection.getInputStream();
111
		try {
112
			return handResponse(in, typeOfT);
113
		} finally {
114
			IoUtil.close(in);
115
		}
116
	}
117

118

119
	public static <T> T put(String url, Object data, Type typeOfT) throws JfwBaseException, IOException {
120
		StringWriter sw = new StringWriter();
121

122
		JsonService.toJson(data, sw);
123
		byte[] bs = sw.toString().getBytes(ConstData.UTF8);
124

125
		URL u = new URL(url);
126
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
127
		httpURLConnection.setDoOutput(true);
128
		httpURLConnection.setRequestMethod("PUT");
129
		httpURLConnection.setRequestProperty("Accept-Charset", charset);
130
		httpURLConnection.setRequestProperty("Content-Type", "application/json");
131
		httpURLConnection.setRequestProperty("Content-Length", String.valueOf(bs.length));
132
		OutputStream outputStream = httpURLConnection.getOutputStream();
133
		try {
134
			outputStream.write(bs);
135
			outputStream.flush();
136
		} finally {
137
			IoUtil.close(outputStream);
138
		}
139
		if (httpURLConnection.getResponseCode() != 200) {
140
			throw new IOException("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
141
		}
142
		InputStream in = httpURLConnection.getInputStream();
143
		try {
144
			return handResponse(in, typeOfT);
145
		} finally {
146
			IoUtil.close(in);
147
		}
148
	}
149

150

151
	public static <T> T delete(String url, Type typeOfT) throws JfwBaseException, IOException {
152
		URL u = new URL(url);
153
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
154
		httpURLConnection.setRequestProperty("Accept-Charset", charset);
155
		httpURLConnection.setRequestMethod("DELETE");
156
		httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
157
		if (httpURLConnection.getResponseCode() != 200) {
158
			throw new IOException("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
159
		}
160
		InputStream in = httpURLConnection.getInputStream();
161
		try {
162
			return handResponse(in, typeOfT);
163
		} finally {
164
			IoUtil.close(in);
165
		}
166
	}
167

168
	public static class Result<T> {
169
		private boolean success;
170
		private Integer code;
171
		private String msg;
172
		private String detailMsg;
173
		private T data;
174

175
		public boolean isSuccess() {
176
			return success;
177
		}
178

179
		public void setSuccess(boolean success) {
180
			this.success = success;
181
		}
182

183
		public Integer getCode() {
184
			return code;
185
		}
186

187
		public void setCode(Integer code) {
188
			this.code = code;
189
		}
190

191
		public String getMsg() {
192
			return msg;
193
		}
194

195
		public void setMsg(String msg) {
196
			this.msg = msg;
197
		}
198

199
		public String getDetailMsg() {
200
			return detailMsg;
201
		}
202

203
		public void setDetailMsg(String detailMsg) {
204
			this.detailMsg = detailMsg;
205
		}
206

207
		public T getData() {
208
			return data;
209
		}
210

211
		public void setData(T data) {
212
			this.data = data;
213
		}
214
	}
215

216
}