This commit is contained in:
2025-11-05 14:43:35 +08:00
parent aa94662db0
commit f79cdcaec7
5 changed files with 109 additions and 38 deletions
@@ -13,6 +13,7 @@ import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.models.Sys_user_history;
import com.budwk.app.sys.param.SysDataUserUpdatePageForm;
import com.budwk.app.sys.param.SysDataUserUpdateParam;
import com.budwk.app.sys.services.SysDataUnitPullService;
import com.budwk.app.sys.services.SysDataUserPullService;
import com.budwk.app.sys.services.SysDataUserUpdateService;
import com.budwk.app.sys.services.SysUserService;
@@ -49,6 +50,8 @@ public class SysDataUserUpdateController {
@Inject
private SysDataUserPullService sysDataUserPullService;
@Inject
private SysDataUnitPullService sysDataUnitPullService;
@Inject
private SysUserService sysUserService;
@At("")
@@ -115,5 +118,14 @@ public class SysDataUserUpdateController {
return Result.success(sysDataUserUpdateService.update(updateParam));
}
@At
@SaCheckPermission("sys.data.user.pull")
@SLog(tag = "更新单位数据", msg = "更新单位数据")
@ApiOperation("更新单位数据")
public Result updateUnits(){
sysDataUnitPullService.updateUnits();
return Result.success();
}
}
@@ -13,7 +13,15 @@ import com.budwk.app.sys.enums.Api2UnitFiledMap;
import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.services.SysDataUnitPullService;
import com.budwk.app.sys.utils.DataCenterUtil;
import com.tencentcloudapi.apigateway.v20180808.models.RequestConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
@@ -21,7 +29,10 @@ 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 java.net.http.HttpClient;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -63,9 +74,83 @@ public class SysDataUnitPullServiceImpl extends BaseServiceImpl<Sys_unit> implem
@Override
@Aop(TransAop.READ_COMMITTED)
public void updateUnits(List<String> 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<Sys_unit> insertList = new ArrayList<>();
// 需要更新的单位
List<Sys_unit> updateList = new ArrayList<>();
JSONObject dataJson = jsonBody.getJSONObject("data");
JSONArray rows = dataJson.get("Rows", JSONArray.class);
List<JSONObject> data = rows.stream().map(v -> (JSONObject) v).toList();
Map<String, String> 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);
}
// 请求数据
String tokenUrl = "https://dcs.hgu.edu.cn:8888/openApi/API/zzjg";
HttpRequest httpRequest = HttpUtil.createPost(tokenUrl);
/* 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");
@@ -74,39 +159,8 @@ public class SysDataUnitPullServiceImpl extends BaseServiceImpl<Sys_unit> implem
log.info("请求人员数据: {}", httpRequest);
String resBody = httpRequest.execute().body();
log.info("请求人员数据结果: {}", resBody);
JSONObject jsonBody = JSONUtil.parseObj(resBody);
JSONObject jsonBody = JSONUtil.parseObj(resBody);*/
if (jsonBody.getInt("code") != 200) {
throw new BaseException("获取单位数据失败,错误码: " + jsonBody.getInt("code") + ";错误原因:" + jsonBody.getStr("msg"));
}
// 需要新增的单位
List<Sys_unit> insertList = new ArrayList<>();
// 需要更新的单位
List<Sys_unit> updateList = new ArrayList<>();
JSONObject dataJson = jsonBody.getJSONObject("data");
JSONArray rows = dataJson.get("Rows", JSONArray.class);
List<JSONObject> data = rows.stream().map(v -> (JSONObject) v).toList();
Map<String, String> 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);
}
}
@@ -114,12 +114,11 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
Map<String, List<Sys_data_dict>> dataDictMap = dataDictList.stream().collect(Collectors.groupingBy(Sys_data_dict::getParentCode));
// 请求数据
String tokenUrl = "https://dcs.hgu.edu.cn:8888/openApi/API/jzgjbxx";
HttpRequest httpRequest = HttpUtil.createPost(tokenUrl);
HttpRequest httpRequest = HttpRequest.post("https://dcs.hgu.edu.cn:8888/openApi/API/jzgjbxx");
httpRequest.header("Content-Type", "application/json");
httpRequest.header("applyId", "39923603855883264");
httpRequest.header("secretKey", "169a960048124f4483e9637807afc9f6");
httpRequest.body(JSONUtil.toJsonStr(Map.of("page", 1, "pagesize", 3000)));
httpRequest.body(JSONUtil.toJsonStr(Map.of("page", 1, "pagesize", 900)));
log.info("请求人员数据: {}", httpRequest);
String resBody = httpRequest.execute().body();