diff --git a/src/main/java/com/budwk/app/sys/controller/SysUnitController.java b/src/main/java/com/budwk/app/sys/controller/SysUnitController.java index 0bfadc4..aed4ebb 100644 --- a/src/main/java/com/budwk/app/sys/controller/SysUnitController.java +++ b/src/main/java/com/budwk/app/sys/controller/SysUnitController.java @@ -295,12 +295,11 @@ public class SysUnitController { nodeList.add(new TreeNode<>(list.get(i).getId(), list.get(i).getParentId(), list.get(i).getName(), i) .setExtra( Map.of( - "unitType", list.get(i).getUnitType(), "unitTypeCode", list.get(i).getUnitTypeCode() ) )); } - List> treeList = TreeUtil.build(nodeList, StrUtil.blankToDefault(pid, "0000")); + List> treeList = TreeUtil.build(nodeList, StrUtil.blankToDefault(pid, "10")); // NutMap menuMap = NutMap.NEW(); // for (Sys_unit unit : list) { diff --git a/src/main/java/com/budwk/app/sys/services/impl/SysDataUnitPullServiceImpl.java b/src/main/java/com/budwk/app/sys/services/impl/SysDataUnitPullServiceImpl.java index 9de6346..f39a8b4 100644 --- a/src/main/java/com/budwk/app/sys/services/impl/SysDataUnitPullServiceImpl.java +++ b/src/main/java/com/budwk/app/sys/services/impl/SysDataUnitPullServiceImpl.java @@ -1,5 +1,6 @@ package com.budwk.app.sys.services.impl; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpUtil; @@ -74,92 +75,56 @@ public class SysDataUnitPullServiceImpl extends BaseServiceImpl implem @Override @Aop(TransAop.READ_COMMITTED) public void updateUnits(List unitIds) { - String url = "https://dcs.hgu.edu.cn:8888/openApi/API/jzgjbxx"; // 确保无空格! - try (CloseableHttpClient httpClient = HttpClients.custom() - .build()) { - - // 构建请求体 JSON - JSONObject body = new JSONObject(); - body.put("page", 1); - body.put("pagesize", 1000); // ⚠️ 建议 ≤1000,此处设为 100 更安全 - // 创建 POST 请求 - HttpPost post = new HttpPost(url); - post.setHeader("Content-Type", "application/json"); - post.setHeader("applyId", "39923603855883264"); // 替换为你的实际 applyId - post.setHeader("secretKey", "169a960048124f4483e9637807afc9f6"); // 替换为你的实际 secretKey - post.setHeader("User-Agent", "ApiClient/1.0 (Java)"); - post.setHeader("Accept", "*/*"); - post.setHeader("Connection", "close"); - - // 设置请求体 - StringEntity entity = new StringEntity( - JSONUtil.toJsonStr(body), - StandardCharsets.UTF_8 - ); - post.setEntity(entity); - - // 执行请求 - HttpResponse response = httpClient.execute(post); - HttpEntity responseEntity = response.getEntity(); - String responseBody = responseEntity != null - ? EntityUtils.toString(responseEntity, StandardCharsets.UTF_8) - : ""; - - System.out.println("响应状态码: " + response.getStatusLine().getStatusCode()); - System.out.println("响应内容: " + responseBody); - - JSONObject jsonBody = JSONUtil.parseObj(responseBody); - - if (jsonBody.getInt("code") != 200) { - throw new BaseException("获取单位数据失败,错误码: " + jsonBody.getInt("code") + ";错误原因:" + jsonBody.getStr("msg")); - } - - // 需要新增的单位 - List insertList = new ArrayList<>(); - // 需要更新的单位 - List updateList = new ArrayList<>(); - - JSONObject dataJson = jsonBody.getJSONObject("data"); - JSONArray rows = dataJson.get("Rows", JSONArray.class); - - List data = rows.stream().map(v -> (JSONObject) v).toList(); - - Map api2db = Arrays.stream(Api2UnitFiledMap.values()) - .collect(Collectors.toMap(e -> e.apiField, e -> e.dbColumn)); - for (JSONObject row : data) { - Sys_unit unit = DataCenterUtil.mapJsonToBean(row, Sys_unit.class, api2db); - unit.setUnitcode(unit.getId()); - unit.setUnitTypeCode(unit.getParentId().length() == 2 ? 0 : 1); - - if (unitIds.contains(unit.getId())) { - // 存在则更新 - updateList.add(unit); - } else { - // 不存在则新增 - insertList.add(unit); - } - } - log.info("本次拉取单位数据,新增{}条,更新{}条", insertList.size(), updateList.size()); - dao().insert(insertList); - dao().update(updateList); - - } catch (Exception e) { - e.printStackTrace(); - // 实际项目中:log.error("拉取人员数据失败", e); - } // 请求数据 - /* HttpRequest httpRequest = HttpRequest.post("https://dcs.hgu.edu.cn:8888/openApi/API/zzjg"); + HttpRequest httpRequest = HttpRequest.post("https://dcs.hgu.edu.cn:8888/openApi/API/zzjg"); httpRequest.header("Content-Type", "application/json"); httpRequest.header("applyId", "39923603855883264"); httpRequest.header("secretKey", "169a960048124f4483e9637807afc9f6"); httpRequest.body(JSONUtil.toJsonStr(Map.of("page", 1, "pagesize", 1000))); - log.info("请求人员数据: {}", httpRequest); + log.info("请求单位数据: {}", httpRequest); String resBody = httpRequest.execute().body(); - log.info("请求人员数据结果: {}", resBody); - JSONObject jsonBody = JSONUtil.parseObj(resBody);*/ + log.info("请求单位数据结果: {}", resBody); + JSONObject jsonBody = JSONUtil.parseObj(resBody); + + if (jsonBody.getInt("status") != 200) { + throw new BaseException("获取单位数据失败,错误码: " + jsonBody.getInt("status") + ";错误原因:" + jsonBody.getStr("msg")); + } + + // 需要新增的单位 + List insertList = new ArrayList<>(); + // 需要更新的单位 + List updateList = new ArrayList<>(); + + JSONObject dataJson = jsonBody.getJSONObject("data"); + JSONArray rows = dataJson.get("Rows", JSONArray.class); + + List data = rows.stream().map(v -> (JSONObject) v).toList(); + + Map api2db = Arrays.stream(Api2UnitFiledMap.values()) + .collect(Collectors.toMap(e -> e.apiField, e -> e.dbColumn)); + for (JSONObject row : data) { + Sys_unit unit = DataCenterUtil.mapJsonToBean(row, Sys_unit.class, api2db); + unit.setUnitcode(unit.getId()); + if (ObjectUtil.isNotEmpty(unit.getParentId())) { + unit.setUnitTypeCode(unit.getParentId().length() == 2 ? 0 : 1); + } else { + unit.setUnitTypeCode(0); + } + + if (unitIds.contains(unit.getId())) { + // 存在则更新 + updateList.add(unit); + } else { + // 不存在则新增 + insertList.add(unit); + } + } + log.info("本次拉取单位数据,新增{}条,更新{}条", insertList.size(), updateList.size()); + dao().insert(insertList); + dao().update(updateList); } diff --git a/src/main/java/com/budwk/app/sys/services/impl/SysDataUserPullServiceImpl.java b/src/main/java/com/budwk/app/sys/services/impl/SysDataUserPullServiceImpl.java index ca83749..42502b6 100644 --- a/src/main/java/com/budwk/app/sys/services/impl/SysDataUserPullServiceImpl.java +++ b/src/main/java/com/budwk/app/sys/services/impl/SysDataUserPullServiceImpl.java @@ -13,6 +13,7 @@ import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; +import com.alibaba.fastjson.JSON; import com.budwk.app.base.config.DataCenterProperties; import com.budwk.app.base.exception.BaseException; import com.budwk.app.base.service.impl.BaseServiceImpl; @@ -36,6 +37,7 @@ import org.nutz.dao.sql.Sql; import org.nutz.ioc.aop.Aop; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.IocBean; +import org.nutz.json.Json; import org.nutz.lang.util.NutMap; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @@ -118,15 +120,17 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl httpRequest.header("Content-Type", "application/json"); httpRequest.header("applyId", "39923603855883264"); httpRequest.header("secretKey", "169a960048124f4483e9637807afc9f6"); - httpRequest.body(JSONUtil.toJsonStr(Map.of("page", 1, "pagesize", 900))); + httpRequest.body(JSONUtil.toJsonStr(Map.of("page", 1, "pagesize", 5000))); log.info("请求人员数据: {}", httpRequest); String resBody = httpRequest.execute().body(); log.info("请求人员数据结果: {}", resBody); + JSONObject jsonBody = JSONUtil.parseObj(resBody); + System.out.println(jsonBody); if (jsonBody.getInt("status") != 200) { - throw new BaseException("获取人员数据失败,错误码: " + jsonBody.getInt("code") + ";错误原因:" + jsonBody.getStr("msg")); + throw new BaseException("获取人员数据失败,错误码: " + jsonBody.getInt("status") + ";错误原因:" + jsonBody.getStr("msg")); } List rawDataList = new ArrayList<>(); @@ -194,6 +198,7 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl } return nowDate; } catch (Exception e) { + e.printStackTrace(); throw new BaseException("数据拉取失败,{}", e.getMessage()); } }