ScheduleTest.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. package com.xjrsoft.xjrsoftboot;
  2. import cn.dev33.satoken.secure.SaSecureUtil;
  3. import cn.hutool.db.Db;
  4. import com.google.gson.JsonArray;
  5. import com.google.gson.JsonElement;
  6. import com.google.gson.JsonObject;
  7. import com.google.gson.JsonParser;
  8. import com.xjrsoft.XjrSoftApplication;
  9. import com.xjrsoft.common.constant.GlobalConstant;
  10. import com.xjrsoft.common.utils.DatasourceUtil;
  11. import com.xjrsoft.module.courseTable.service.ICourseTableService;
  12. import com.xjrsoft.module.schedule.entity.JianyueData;
  13. import com.xjrsoft.module.schedule.util.DataUtil;
  14. import com.xjrsoft.module.schedule.util.ScheduleUtil;
  15. import org.junit.jupiter.api.Test;
  16. import org.junit.runner.RunWith;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.boot.test.context.SpringBootTest;
  19. import org.springframework.test.context.junit4.SpringRunner;
  20. import javax.crypto.Mac;
  21. import javax.crypto.spec.SecretKeySpec;
  22. import javax.sql.DataSource;
  23. import java.io.*;
  24. import java.net.HttpURLConnection;
  25. import java.net.MalformedURLException;
  26. import java.net.URL;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.HashSet;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.Set;
  33. // 简约课表测试
  34. @RunWith(SpringRunner.class)
  35. @SpringBootTest(classes = XjrSoftApplication.class)
  36. public class ScheduleTest {
  37. public static final String ALGORITHM = "HmacSHA256";
  38. public static final String apiUrl = "http://219.153.208.33:20000/api/v1/ScheduleFlowV2/OpenApi/";
  39. @Autowired
  40. private ICourseTableService courseTableService;
  41. public static String calculateHMac(String key, String data) throws Exception {
  42. Mac sha256_HMAC = Mac.getInstance(ALGORITHM);
  43. SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), ALGORITHM);
  44. sha256_HMAC.init(secret_key);
  45. return byteArrayToHex(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
  46. }
  47. public static String byteArrayToHex(byte[] a) {
  48. StringBuilder sb = new StringBuilder(a.length * 2);
  49. for (byte b : a)
  50. sb.append(String.format("%02x", b));
  51. return sb.toString();
  52. }
  53. /**
  54. * 将加密后的字节数组转换成字符串
  55. *
  56. * @param b 字节数组
  57. * @return 字符串
  58. */
  59. private static String byteArrayToHexString(byte[] b) {
  60. StringBuilder hs = new StringBuilder();
  61. String stmp;
  62. for (int n = 0; b != null && n < b.length; n++) {
  63. stmp = Integer.toHexString(b[n] & 0XFF);
  64. if (stmp.length() == 1)
  65. hs.append('0');
  66. hs.append(stmp);
  67. }
  68. return hs.toString().toLowerCase();
  69. }
  70. /**
  71. * https://live.jianyuekb.com/api/v1/ScheduleFlowV2/OpenApi/
  72. * schoolId:UUFM5TID45X
  73. * secert:UUFM5TID45X
  74. * password:Jh&NAbn6Rm#p@6ZZ
  75. */
  76. @Test
  77. public void ScheduleFlowTest() throws Exception {
  78. long timestamp = System.currentTimeMillis();
  79. System.out.println("timestamp:" + timestamp);
  80. String md5Str = SaSecureUtil.md5("@ak8To$xSHFoT6FoqsqYb3" + timestamp);
  81. String sign = calculateHMac("UUXQ8G4W9IU", md5Str);
  82. System.out.println("sign:" + sign);
  83. // JsonObject paramJson = new JsonObject();
  84. // paramJson.addProperty("pageIndex", 1);
  85. // paramJson.addProperty("pageSize", 200);
  86. //获取时间戳
  87. // long timestamp = System.currentTimeMillis();
  88. //生成签名
  89. // String sign = ScheduleUtil.createSign(timestamp);
  90. // String result = ScheduleUtil.doPost("http://219.153.208.33:20000/api/v1/ScheduleFlowV2/OpenApi/SchoolDistrict/page", paramJson.toString(), sign, timestamp);
  91. // System.out.println(result);
  92. DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
  93. try {
  94. Db use = Db.use(datasource);
  95. String sql = "SELECT distinct table_name FROM jianyue_data WHERE 1 = 1";
  96. List<JianyueData> query = use.query(sql, JianyueData.class);
  97. Set<String> tables = new HashSet<>();
  98. for (JianyueData jianyueData : query) {
  99. tables.add(jianyueData.getTableName());
  100. }
  101. sql = "SELECT * FROM jianyue_data WHERE 0 = 0";
  102. List<JianyueData> list = use.query(sql, JianyueData.class);
  103. Map<String, Map<Long, String>> dataMap = new HashMap<>();
  104. for (String table : tables) {
  105. Map<Long, String> tableData = new HashMap<>();
  106. for (JianyueData jianyueData : list) {
  107. if(!table.equals(jianyueData.getTableName())){
  108. continue;
  109. }
  110. tableData.put(jianyueData.getSourceId(), jianyueData.getJianyueId());
  111. }
  112. dataMap.put(table, tableData);
  113. }
  114. DataUtil dataUtil = new DataUtil();
  115. //查询校区
  116. JsonArray schoolDistrictData = dataUtil.getSchoolDistrictData();
  117. //推送年级
  118. String tableName = "base_grade";
  119. Map<Long, String> grade = dataUtil.insertGrade(use, tableName, schoolDistrictData.get(0).getAsString(), dataMap.get(tableName));
  120. if(grade.isEmpty() && dataMap.get(tableName) != null){
  121. grade = dataMap.get(tableName);
  122. }
  123. //推送学期
  124. tableName = "base_semester";
  125. dataUtil.insertSemester(use, tableName, dataMap.get(tableName));
  126. //推送标签
  127. tableName = "base_label";
  128. Map<Long, String> tagMap = dataUtil.insertCourseTag(use, tableName, dataMap.get(tableName));
  129. if(tagMap.isEmpty() && dataMap.get(tableName) != null){
  130. tagMap = dataMap.get(tableName);
  131. }
  132. //推送课程
  133. tableName = "base_course_subject";
  134. dataUtil.insertCourse(use, tableName, dataMap.get(tableName), tagMap);
  135. //推送教职工
  136. tableName = "base_teacher";
  137. Map<Long, String> teacherMap = dataUtil.insertTeacher(use, tableName, dataMap.get(tableName));
  138. if(teacherMap.isEmpty() && dataMap.get(tableName) != null){
  139. teacherMap = dataMap.get(tableName);
  140. }
  141. //推送学生
  142. tableName = "base_student";
  143. dataUtil.insertStudent(use, tableName, grade, dataMap.get(tableName));
  144. //推送教室
  145. tableName = "base_classroom";
  146. Map<Long, String> classroomMap = dataUtil.insertClassRoom(use, tableName, schoolDistrictData.get(0).getAsString(), dataMap.get(tableName));
  147. if(classroomMap.isEmpty() && dataMap.get(tableName) != null){
  148. classroomMap = dataMap.get(tableName);
  149. }
  150. //推送行政班,先查询当前学期id
  151. String currenSemeter = dataUtil.getCurrenSemeter();
  152. tableName = "base_class";
  153. dataUtil.insertClass(use, tableName, grade, teacherMap, currenSemeter, dataMap.get(tableName), classroomMap);
  154. } catch (Exception e) {
  155. }
  156. }
  157. void SubstitutePreTestin(String sign, Long timestamp){
  158. JsonParser jsonParser = new JsonParser();
  159. String url = apiUrl + "RescheduleApply/Extend/Substitute/PreTesting";
  160. JsonObject jsonObject = new JsonObject();
  161. jsonObject.addProperty("timetableId", "4deba192-0b39-9821-5eac-3a1079ba6fbf");
  162. jsonObject.addProperty("isCycles", Boolean.FALSE);
  163. JsonArray extendIds = new JsonArray();
  164. extendIds.add(14954799374790L);
  165. jsonObject.add("extendIds", extendIds);
  166. String result = doPost(url, jsonObject.toString(), sign, timestamp);
  167. System.out.println(result);
  168. }
  169. void getPreCheck(String sign, Long timestamp){
  170. JsonParser jsonParser = new JsonParser();
  171. String url = apiUrl + "RescheduleApply/Extend/PreTesting";
  172. JsonObject jsonObject = new JsonObject();
  173. jsonObject.addProperty("timetableId", "1e77cf3e-a027-1665-8100-3a106a58b8ab");
  174. jsonObject.addProperty("isCycles", Boolean.FALSE);
  175. // jsonObject.addProperty("startDate", "2024-01-01");
  176. // jsonObject.addProperty("endDate", "2024-01-31");
  177. // jsonObject.addProperty("dayOfweek", 5);
  178. jsonObject.addProperty("numberOfday", 3);
  179. jsonObject.addProperty("date", "2024-01-31");
  180. jsonObject.addProperty("reschduleId", "4e2fc331-ce9d-f512-13fb-3a106a58bf82");
  181. String result = doPost(url, jsonObject.toString(), sign, timestamp);
  182. System.out.println(result);
  183. }
  184. void getTimetable(String sign, Long timestamp){
  185. JsonParser jsonParser = new JsonParser();
  186. String url = apiUrl + "RescheduleApply/Can/Reschedule/Timetable/List";
  187. JsonObject jsonObject = new JsonObject();
  188. jsonObject.addProperty("timetableId", "1e77cf3e-a027-1665-8100-3a106a58b8ab");
  189. jsonObject.addProperty("isCycles", Boolean.TRUE);
  190. JsonObject cycles = new JsonObject();
  191. cycles.addProperty("startDate", "2024-01-25");
  192. cycles.addProperty("endDate", "2024-01-30");
  193. cycles.addProperty("dayOfweek", 5);
  194. cycles.addProperty("numberOfday", 3);
  195. jsonObject.add("cycles", cycles);
  196. jsonObject.addProperty("reschduleDate", "2024-02-02");
  197. String result = doPost(url, jsonObject.toString(), sign, timestamp);
  198. System.out.println(result);
  199. }
  200. List<String> getClassInfo(String sign, Long timestamp){
  201. JsonParser jsonParser = new JsonParser();
  202. String url = apiUrl + "Class/page";
  203. JsonObject jsonObject = new JsonObject();
  204. jsonObject.addProperty("pageSize", "200");
  205. jsonObject.addProperty("pageIndex", "1");
  206. String result = doPost(url, jsonObject.toString(), sign, timestamp);
  207. System.out.println("ClassInfo:" + result);
  208. JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  209. JsonArray jsonArray = resultJson.get("data").getAsJsonObject().get("dataList").getAsJsonArray();
  210. List<String> resultArray = new ArrayList<>();
  211. for (JsonElement jsonElement : jsonArray) {
  212. JsonObject asJsonObject = jsonElement.getAsJsonObject();
  213. resultArray.add(asJsonObject.get("serialNo").getAsString());
  214. }
  215. return resultArray;
  216. }
  217. JsonArray getSchoolDistrictData(String sign, Long timestamp){
  218. JsonParser jsonParser = new JsonParser();
  219. // String url = apiUrl + "SchoolDistrict/Create";
  220. // JsonObject jsonObject = new JsonObject();
  221. // jsonObject.addProperty("name", "test1");
  222. // jsonObject.addProperty("shortName", "本部112");
  223. // jsonObject.addProperty("note", "本部211");
  224. // jsonObject.addProperty("sort", "2");
  225. // String result = doPost(url, jsonObject.toString(), sign, timestamp);
  226. // JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  227. String url = apiUrl + "SchoolDistrict/page";
  228. JsonObject jsonObject = new JsonObject();
  229. jsonObject.addProperty("pageSize", "15");
  230. jsonObject.addProperty("pageIndex", "1");
  231. String result = doPost(url, jsonObject.toString(), sign, timestamp);
  232. JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  233. JsonArray jsonArray = resultJson.get("data").getAsJsonObject().get("dataList").getAsJsonArray();
  234. JsonArray resultArray = new JsonArray();
  235. for (JsonElement jsonElement : jsonArray) {
  236. JsonObject asJsonObject = jsonElement.getAsJsonObject();
  237. resultArray.add(asJsonObject.get("id").getAsString());
  238. }
  239. return resultArray;
  240. }
  241. List<String> getGradeInfo(String sign, Long timestamp, JsonArray schoolDistrictIds){
  242. JsonParser jsonParser = new JsonParser();
  243. String url = apiUrl + "TimeTable/List";
  244. JsonObject jsonObject = new JsonObject();
  245. // jsonObject.addProperty("schoolDistrictIds", schoolDistrictIds.toString());
  246. jsonObject.addProperty("pageSize", "15");
  247. jsonObject.addProperty("pageIndex", "1");
  248. String result = doPost(url, jsonObject.toString(), sign, timestamp);
  249. System.out.println("grade:" + result);
  250. JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  251. JsonArray data = resultJson.get("data").getAsJsonArray();
  252. if(data.size() == 0){
  253. return new ArrayList<>();
  254. }
  255. JsonArray gradeArray = resultJson.get("data").getAsJsonObject().get("dataList").getAsJsonArray();
  256. List<String> gradeIds = new ArrayList<>();
  257. for (JsonElement jsonElement : gradeArray) {
  258. JsonObject asJsonObject = jsonElement.getAsJsonObject();
  259. gradeIds.add(asJsonObject.get("serialNo").getAsString());
  260. }
  261. return gradeIds;
  262. }
  263. JsonArray getScheduleInfo(String sign, Long timestamp, List<String> ids) throws IOException {
  264. JsonParser jsonParser = new JsonParser();
  265. String url = apiUrl + "TimeTable/List";
  266. if(ids.isEmpty()){
  267. JsonObject jsonObject = new JsonObject();
  268. jsonObject.addProperty("startDate", "2024-01-01 00:00:00");
  269. jsonObject.addProperty("endDate", "2024-02-21 23:59:59");
  270. String result = doPost(url, jsonObject.toString(), sign, timestamp);
  271. File file = new File("D:\\workspace\\其他\\课表\\file.txt");
  272. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  273. writer.write(result); // 写入字符串内容
  274. writer.close();
  275. return null;
  276. }
  277. for (String id : ids) {
  278. JsonObject jsonObject = new JsonObject();
  279. jsonObject.addProperty("classSerialNo", id);
  280. jsonObject.addProperty("startDate", "2024-01-01 00:00:00");
  281. jsonObject.addProperty("endDate", "2024-02-21 23:59:59");
  282. String result = doPost(url, jsonObject.toString(), sign, timestamp);
  283. System.out.println(id + ":" + result);
  284. }
  285. return null;
  286. }
  287. // List<String> insertClass(String sign, Long timestamp, String schoolDistrictId) throws InterruptedException {
  288. // List<ClassroomJianyuekbVo> dataList = baseClassroomMapper.getJianyueList();
  289. // String url = apiUrl + "class/create";
  290. // List<String> idList = new ArrayList<>();
  291. // JsonParser jsonParser = new JsonParser();
  292. //
  293. // String findUrl = apiUrl + "class/page";
  294. //
  295. // for (ClassroomJianyuekbVo classroom : dataList) {
  296. // JsonObject paramJson = new JsonObject();
  297. // paramJson.addProperty("schoolDistrictId", schoolDistrictId);
  298. // paramJson.addProperty("name", classroom.getName());
  299. //
  300. //// paramJson.addProperty("peopleCount", classroom.getPeopleCount());
  301. //// paramJson.addProperty("location", classroom.getLocation());
  302. // paramJson.addProperty("peopleCount", 50);
  303. // paramJson.addProperty("location", "无");
  304. // paramJson.addProperty("extendId", classroom.getExtendId());
  305. // paramJson.addProperty("floor", classroom.getFloor());
  306. //// String findResult = doPost(findUrl, paramJson.toString(), sign, timestamp);
  307. //// if(findResult != null){
  308. //// JsonObject resultJson = jsonParser.parse(findResult).getAsJsonObject();
  309. //// JsonObject data = resultJson.get("data").getAsJsonObject();
  310. //// if(data.get("total").getAsInt() != 0){
  311. //// //调用update
  312. //// }
  313. //// }
  314. //
  315. // String result = doPost(url, paramJson.toString(), sign, timestamp);
  316. // if(result == null){
  317. // System.out.println(paramJson.toString());
  318. // continue;
  319. // }
  320. // JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  321. // idList.add(resultJson.get("data").getAsString());
  322. // }
  323. // return idList;
  324. // }
  325. //
  326. // List<String> insertClassRoom(String sign, Long timestamp, String schoolDistrictId) throws InterruptedException {
  327. // List<ClassroomJianyuekbVo> dataList = baseClassroomMapper.getJianyueList();
  328. // String url = apiUrl + "classroom/create";
  329. // List<String> idList = new ArrayList<>();
  330. // JsonParser jsonParser = new JsonParser();
  331. //
  332. // String findUrl = apiUrl + "ClassRoom/page";
  333. //
  334. // for (ClassroomJianyuekbVo classroom : dataList) {
  335. // JsonObject paramJson = new JsonObject();
  336. // paramJson.addProperty("schoolDistrictId", schoolDistrictId);
  337. // paramJson.addProperty("name", classroom.getName());
  338. //
  339. //// paramJson.addProperty("peopleCount", classroom.getPeopleCount());
  340. //// paramJson.addProperty("location", classroom.getLocation());
  341. // paramJson.addProperty("peopleCount", 50);
  342. // paramJson.addProperty("location", "无");
  343. // paramJson.addProperty("extendId", classroom.getExtendId());
  344. // paramJson.addProperty("floor", classroom.getFloor());
  345. //// String findResult = doPost(findUrl, paramJson.toString(), sign, timestamp);
  346. //// if(findResult != null){
  347. //// JsonObject resultJson = jsonParser.parse(findResult).getAsJsonObject();
  348. //// JsonObject data = resultJson.get("data").getAsJsonObject();
  349. //// if(data.get("total").getAsInt() != 0){
  350. //// //调用update
  351. //// }
  352. //// }
  353. //
  354. // String result = doPost(url, paramJson.toString(), sign, timestamp);
  355. // if(result == null){
  356. // System.out.println(paramJson.toString());
  357. // continue;
  358. // }
  359. // JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  360. // idList.add(resultJson.get("data").getAsString());
  361. // }
  362. // return idList;
  363. // }
  364. //
  365. // List<String> insertStudent(String sign, Long timestamp, Map<Long, String> gradeMap) throws Exception {
  366. //
  367. // List<StudentJianyuekbVo> dataList = baseStudentMapper.getJianyueStudentList();
  368. // String url = apiUrl + "student/create";
  369. // List<String> idList = new ArrayList<>();
  370. // JsonParser jsonParser = new JsonParser();
  371. //
  372. // String findUrl = apiUrl + "Student/page";
  373. //
  374. // for (StudentJianyuekbVo student : dataList) {
  375. // JsonObject paramJson = new JsonObject();
  376. // paramJson.addProperty("registerNo", student.getRegisterNo());
  377. // paramJson.addProperty("gender", student.getGender());
  378. //
  379. // paramJson.addProperty("realName", student.getRealName());
  380. // paramJson.addProperty("alias", student.getAlias());
  381. // paramJson.addProperty("extendId", student.getExtendId());
  382. // paramJson.addProperty("gradeSerialNo", gradeMap.get(student.getGradeSerialNo()));
  383. //// String findResult = doPost(findUrl, paramJson.toString(), sign, timestamp);
  384. //// if(findResult != null){
  385. //// JsonObject resultJson = jsonParser.parse(findResult).getAsJsonObject();
  386. //// JsonObject data = resultJson.get("data").getAsJsonObject();
  387. //// if(data.get("total").getAsInt() != 0){
  388. //// //调用update
  389. //// }
  390. //// }
  391. // timestamp = System.currentTimeMillis();
  392. // System.out.println("timestamp:" + timestamp);
  393. // String md5Str = SaSecureUtil.md5("Jh&NAbn6Rm#p@6ZZ" + timestamp);
  394. //
  395. //
  396. // sign = calculateHMac("UUFM5TID45X", md5Str);
  397. // System.out.println("sign:" + sign);
  398. // String result = doPost(url, paramJson.toString(), sign, timestamp);
  399. // if(result == null){
  400. // System.out.println(paramJson.toString());
  401. // continue;
  402. // }
  403. // JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  404. // idList.add(resultJson.get("data").getAsString());
  405. // }
  406. // return idList;
  407. // }
  408. //
  409. // List<String> insertTeacher(String sign, Long timestamp) throws InterruptedException {
  410. // List<XjrUser> xjrUsers = xjrUserMapper.selectJoinList(
  411. // XjrUser.class, new MPJLambdaWrapper<XjrUser>()
  412. // .innerJoin("xjr_user_role_relation t2 on t.id = t2.user_id")
  413. // .eq("t2.role_id", 2)
  414. // );
  415. // String url = apiUrl + "teacher/create";
  416. // List<String> idList = new ArrayList<>();
  417. // JsonParser jsonParser = new JsonParser();
  418. //
  419. // String findUrl = apiUrl + "/Teacher/Page";
  420. //
  421. // for (XjrUser user : xjrUsers) {
  422. // Thread.sleep(1000);
  423. // JsonObject paramJson = new JsonObject();
  424. // paramJson.addProperty("jobNumber", user.getUserName());
  425. // paramJson.addProperty("gender", user.getGender());
  426. //
  427. // String name = user.getName().substring(0, 1) + user.getUserName();
  428. // paramJson.addProperty("name", name);
  429. // paramJson.addProperty("alias", name);
  430. // paramJson.addProperty("extendId", user.getId().toString());
  431. // String findResult = doPost(findUrl, paramJson.toString(), sign, timestamp);
  432. // if(findResult != null){
  433. // JsonObject resultJson = jsonParser.parse(findResult).getAsJsonObject();
  434. // JsonObject data = resultJson.get("data").getAsJsonObject();
  435. // if(data.get("total").getAsInt() != 0){
  436. // //调用update
  437. // }
  438. // }
  439. //
  440. // String result = doPost(url, paramJson.toString(), sign, timestamp);
  441. // if(result == null){
  442. // System.out.println(paramJson.toString());
  443. // continue;
  444. // }
  445. // JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  446. // idList.add(resultJson.get("data").getAsString());
  447. // }
  448. // return idList;
  449. // }
  450. //
  451. // List<String> insertCourse(String sign, Long timestamp){
  452. // List<BaseCourseSubject> list = baseCourseSubjectService.list(
  453. // new QueryWrapper<BaseCourseSubject>().lambda()
  454. // .eq(BaseCourseSubject::getDeleteMark, DeleteMark.NODELETE.getCode())
  455. // );
  456. // String url = apiUrl + "courseclass/Create";
  457. // List<String> idList = new ArrayList<>();
  458. // JsonParser jsonParser = new JsonParser();
  459. // for (BaseCourseSubject courseSubject : list) {
  460. // JsonObject paramJson = new JsonObject();
  461. // paramJson.addProperty("name", courseSubject.getName() + courseSubject.getCode());
  462. // paramJson.addProperty("code", courseSubject.getCode());
  463. // paramJson.addProperty("extendId", courseSubject.getId().toString());
  464. // String result = doPost(url, paramJson.toString(), sign, timestamp);
  465. // if(result == null){
  466. // System.out.println("result is null, paramJson: " + paramJson.toString());
  467. // continue;
  468. // }
  469. // JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  470. // if(resultJson.get("code").getAsInt() == -1){
  471. // System.out.println("paramJson: " + paramJson.toString() + ", 返回结果:" + result);
  472. // }
  473. // idList.add(resultJson.get("data").getAsString());
  474. // }
  475. //
  476. // return idList;
  477. // }
  478. //
  479. //
  480. //
  481. // List<String> insertSemester(String sign, Long timestamp){
  482. // List<BaseSemester> list = baseSemesterService.list(
  483. // new QueryWrapper<BaseSemester>().lambda()
  484. // .eq(BaseSemester::getDeleteMark, DeleteMark.NODELETE.getCode())
  485. // );
  486. // String url = apiUrl + "semester/Create";
  487. // List<String> idList = new ArrayList<>();
  488. // JsonParser jsonParser = new JsonParser();
  489. // SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
  490. // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  491. // String spring = "春";
  492. // String autumn = "秋";
  493. //
  494. // for (BaseSemester semester : list) {
  495. // JsonObject paramJson = new JsonObject();
  496. //
  497. // paramJson.addProperty("schoolYear", Integer.parseInt(sdfYear.format(semester.getStartDate())));
  498. // Integer period = null;
  499. // if(semester.getName().contains(String.valueOf(spring))){
  500. // period = 1;
  501. // }else if(semester.getName().contains(autumn)){
  502. // period = 2;
  503. // }
  504. // paramJson.addProperty("period", period);
  505. // paramJson.addProperty("startDate", sdf.format(semester.getStartDate()));
  506. // paramJson.addProperty("endDate", sdf.format(semester.getEndDate()));
  507. // paramJson.addProperty("extendId", semester.getId().toString());
  508. //
  509. // if(semester.getStartDate() != null && semester.getEndDate() != null && semester.getEndDate().getTime() > timestamp && semester.getStartDate().getTime() < timestamp){
  510. // paramJson.addProperty("isCurrent", Boolean.TRUE);
  511. // }else{
  512. // paramJson.addProperty("isCurrent", Boolean.FALSE);
  513. // }
  514. // System.out.println(paramJson.toString());
  515. // String result = doPost(url, paramJson.toString(), sign, timestamp);
  516. //
  517. // JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  518. // idList.add(resultJson.get("data").getAsString());
  519. // }
  520. //
  521. // return idList;
  522. // }
  523. /**
  524. * 由于简约中校区中的本部不能删除,如果没有多的校区,就只查询本部的id即可
  525. * @param sign 签名
  526. * @param timestamp 时间戳
  527. * @return 返回添加好的校区id
  528. */
  529. String insertSchoolDistrictData(String sign, Long timestamp){
  530. JsonParser jsonParser = new JsonParser();
  531. // String url = apiUrl + "SchoolDistrict/Create";
  532. // JsonObject jsonObject = new JsonObject();
  533. // jsonObject.addProperty("name", "test1");
  534. // jsonObject.addProperty("shortName", "本部112");
  535. // jsonObject.addProperty("note", "本部211");
  536. // jsonObject.addProperty("sort", "2");
  537. // String result = doPost(url, jsonObject.toString(), sign, timestamp);
  538. // JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  539. String url = apiUrl + "SchoolDistrict/page";
  540. JsonObject jsonObject = new JsonObject();
  541. jsonObject.addProperty("keyword", "本部");
  542. jsonObject.addProperty("pageSize", "15");
  543. jsonObject.addProperty("pageIndex", "1");
  544. String result = doPost(url, jsonObject.toString(), sign, timestamp);
  545. JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  546. String id = resultJson.get("data").getAsJsonObject().get("dataList").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsString();
  547. return id;
  548. }
  549. // Map<Long, String> insertGrade(String sign, Long timestamp, String schoolDistrictId){
  550. // List<BaseGrade> list = baseGradeService.list(
  551. // new QueryWrapper<BaseGrade>().lambda()
  552. // .eq(BaseGrade::getDeleteMark, DeleteMark.NODELETE.getCode())
  553. // );
  554. // String url = apiUrl + "eduyear/create";
  555. // JsonParser jsonParser = new JsonParser();
  556. // Map<Long, String> idMap = new HashMap<>();
  557. // for (BaseGrade baseGrade : list) {
  558. // JsonObject paramJson = new JsonObject();
  559. // paramJson.addProperty("schoolDistrictId", schoolDistrictId);
  560. // paramJson.addProperty("period", 4);
  561. // paramJson.addProperty("startYear", baseGrade.getTitle().replaceAll("年", ""));
  562. // paramJson.addProperty("extendId", baseGrade.getId().toString());
  563. // paramJson.addProperty("year", 3);
  564. //
  565. // String result = doPost(url, paramJson.toString(), sign, timestamp);
  566. //
  567. // JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  568. //
  569. // idMap.put(baseGrade.getId(), resultJson.get("data").getAsString());
  570. // }
  571. //
  572. // return idMap;
  573. // }
  574. public static String doGet(String httpurl) throws Exception {
  575. long timestamp = System.currentTimeMillis();
  576. String password = "";
  577. String md5Str = SaSecureUtil.md5(String.format("Jh&NAbn6Rm#p@6ZZ%d", timestamp));
  578. System.out.println(md5Str);
  579. String sign = calculateHMac("a7dbed0dfc70ebd92934cf9292de8749e9af01a2405217be881f9e77f6ba5320", md5Str);
  580. System.out.println(sign);
  581. HttpURLConnection connection = null;
  582. InputStream is = null;
  583. BufferedReader br = null;
  584. String result = null;// 返回结果字符串
  585. try {
  586. // 创建远程url连接对象
  587. URL url = new URL(httpurl);
  588. // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
  589. connection = (HttpURLConnection) url.openConnection();
  590. connection.setRequestProperty("schoolId","UUFM5TID45X");
  591. connection.setRequestProperty("sign",sign);
  592. connection.setRequestProperty("timestamp",String.format("%d",timestamp));
  593. // 设置连接方式:get
  594. connection.setRequestMethod("GET");
  595. // 设置连接主机服务器的超时时间:15000毫秒
  596. connection.setConnectTimeout(15000);
  597. // 设置读取远程返回的数据时间:60000毫秒
  598. connection.setReadTimeout(60000);
  599. // 发送请求
  600. connection.connect();
  601. // 通过connection连接,获取输入流
  602. if (connection.getResponseCode() == 200) {
  603. is = connection.getInputStream();
  604. // 封装输入流is,并指定字符集
  605. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  606. // 存放数据
  607. StringBuffer sbf = new StringBuffer();
  608. String temp = null;
  609. while ((temp = br.readLine()) != null) {
  610. sbf.append(temp);
  611. sbf.append("\r\n");
  612. }
  613. result = sbf.toString();
  614. }
  615. } catch (MalformedURLException e) {
  616. e.printStackTrace();
  617. } catch (IOException e) {
  618. e.printStackTrace();
  619. } finally {
  620. // 关闭资源
  621. if (null != br) {
  622. try {
  623. br.close();
  624. } catch (IOException e) {
  625. e.printStackTrace();
  626. }
  627. }
  628. if (null != is) {
  629. try {
  630. is.close();
  631. } catch (IOException e) {
  632. e.printStackTrace();
  633. }
  634. }
  635. connection.disconnect();// 关闭远程连接
  636. }
  637. return result;
  638. }
  639. public static String doPost(String httpUrl, String param, String sign, Long timestamp) {
  640. HttpURLConnection connection = null;
  641. InputStream is = null;
  642. OutputStream os = null;
  643. BufferedReader br = null;
  644. String result = null;
  645. OutputStreamWriter writer = null;
  646. try {
  647. URL url = new URL(httpUrl);
  648. // 通过远程url连接对象打开连接
  649. connection = (HttpURLConnection) url.openConnection();
  650. connection.setRequestProperty("schoolId","UUFM5TID45X");
  651. connection.setRequestProperty("sign",sign);
  652. connection.setRequestProperty("timestamp",String.format("%d",timestamp));
  653. // 设置连接请求方式
  654. connection.setRequestMethod("POST");
  655. // 设置连接主机服务器超时时间:15000毫秒
  656. connection.setConnectTimeout(15000);
  657. // 设置读取主机服务器返回数据超时时间:60000毫秒
  658. connection.setReadTimeout(60000);
  659. // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
  660. connection.setDoOutput(true);
  661. // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
  662. connection.setDoInput(true);
  663. // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
  664. connection.setRequestProperty("Content-Type", "application/json");
  665. // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
  666. // connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  667. writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
  668. //body参数放这里
  669. writer.write(param);
  670. writer.flush();
  671. writer.close();
  672. // 通过连接对象获取一个输出流
  673. // os = connection.getOutputStream();
  674. // // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
  675. // os.write(param.getBytes());
  676. // 通过连接对象获取一个输入流,向远程读取
  677. if (connection.getResponseCode() == 200) {
  678. is = connection.getInputStream();
  679. // 对输入流对象进行包装:charset根据工作项目组的要求来设置
  680. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  681. StringBuffer sbf = new StringBuffer();
  682. String temp = null;
  683. // 循环遍历一行一行读取数据
  684. while ((temp = br.readLine()) != null) {
  685. sbf.append(temp);
  686. sbf.append("\r\n");
  687. }
  688. result = sbf.toString();
  689. }else{
  690. is = connection.getInputStream();
  691. // 对输入流对象进行包装:charset根据工作项目组的要求来设置
  692. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  693. StringBuffer sbf = new StringBuffer();
  694. String temp = null;
  695. // 循环遍历一行一行读取数据
  696. while ((temp = br.readLine()) != null) {
  697. sbf.append(temp);
  698. sbf.append("\r\n");
  699. }
  700. System.out.println(sbf.toString());
  701. }
  702. } catch (MalformedURLException e) {
  703. e.printStackTrace();
  704. } catch (IOException e) {
  705. e.printStackTrace();
  706. } finally {
  707. // 关闭资源
  708. if (null != br) {
  709. try {
  710. br.close();
  711. } catch (IOException e) {
  712. e.printStackTrace();
  713. }
  714. }
  715. if (null != os) {
  716. try {
  717. os.close();
  718. } catch (IOException e) {
  719. e.printStackTrace();
  720. }
  721. }
  722. if (null != is) {
  723. try {
  724. is.close();
  725. } catch (IOException e) {
  726. e.printStackTrace();
  727. }
  728. }
  729. // 断开与远程地址url的连接
  730. connection.disconnect();
  731. }
  732. return result;
  733. }
  734. public static String doPost(String httpUrl) {
  735. HttpURLConnection connection = null;
  736. InputStream is = null;
  737. OutputStream os = null;
  738. BufferedReader br = null;
  739. String result = null;
  740. OutputStreamWriter writer = null;
  741. try {
  742. URL url = new URL(httpUrl);
  743. // 通过远程url连接对象打开连接
  744. connection = (HttpURLConnection) url.openConnection();
  745. connection.setRequestProperty("schoolId","UUFM5TID45X");
  746. // 设置连接请求方式
  747. connection.setRequestMethod("POST");
  748. // 设置连接主机服务器超时时间:15000毫秒
  749. connection.setConnectTimeout(15000);
  750. // 设置读取主机服务器返回数据超时时间:60000毫秒
  751. connection.setReadTimeout(60000);
  752. // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
  753. connection.setDoOutput(true);
  754. // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
  755. connection.setDoInput(true);
  756. // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
  757. connection.setRequestProperty("Content-Type", "application/json");
  758. // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
  759. // connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  760. writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
  761. //body参数放这里
  762. writer.flush();
  763. writer.close();
  764. // 通过连接对象获取一个输出流
  765. // os = connection.getOutputStream();
  766. // // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
  767. // os.write(param.getBytes());
  768. // 通过连接对象获取一个输入流,向远程读取
  769. if (connection.getResponseCode() == 200) {
  770. is = connection.getInputStream();
  771. // 对输入流对象进行包装:charset根据工作项目组的要求来设置
  772. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  773. StringBuffer sbf = new StringBuffer();
  774. String temp = null;
  775. // 循环遍历一行一行读取数据
  776. while ((temp = br.readLine()) != null) {
  777. sbf.append(temp);
  778. sbf.append("\r\n");
  779. }
  780. result = sbf.toString();
  781. }
  782. } catch (MalformedURLException e) {
  783. e.printStackTrace();
  784. } catch (IOException e) {
  785. e.printStackTrace();
  786. } finally {
  787. // 关闭资源
  788. if (null != br) {
  789. try {
  790. br.close();
  791. } catch (IOException e) {
  792. e.printStackTrace();
  793. }
  794. }
  795. if (null != os) {
  796. try {
  797. os.close();
  798. } catch (IOException e) {
  799. e.printStackTrace();
  800. }
  801. }
  802. if (null != is) {
  803. try {
  804. is.close();
  805. } catch (IOException e) {
  806. e.printStackTrace();
  807. }
  808. }
  809. // 断开与远程地址url的连接
  810. connection.disconnect();
  811. }
  812. return result;
  813. }
  814. }