ScheduleTest.java 38 KB

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