This commit is contained in:
hongqiwei
2026-01-15 11:49:51 +08:00
parent c86372e447
commit a56afdd4f1
11 changed files with 192 additions and 354 deletions
@@ -36,16 +36,16 @@ public class DataCenterProperties {
@Inject
private RedisService redisService;
private String appId;
private String key;
private String secret;
private String tokenUrl;
private Map<String, String> codes = new HashMap<>();
private Map<String, String> urls = new HashMap<>();
public void init() {
Map<String, String> all = conf.toMap();
appId = all.get("data-center.app-id");
key = all.get("data-center.key");
secret = all.get("data-center.secret");
tokenUrl = all.get("data-center.token-url");
extract(all, "data-center.codes.", codes);
extract(all, "data-center.urls.", urls);
}
@@ -68,12 +68,8 @@ public class DataCenterProperties {
* @return 相关配置
*/
public Credential credential(String key) {
String code = codes.get(key);
String url = urls.get(key);
if (code == null || url == null) {
throw new BaseException("未知业务: " + key);
}
String token = getToken(Integer.parseInt(code), key);
String token = getToken(key);
return new Credential(url, token);
}
@@ -87,11 +83,10 @@ public class DataCenterProperties {
/**
* 获取token
* @param code
* @param keyPrefix
* @return token
*/
private String getToken(Integer code, String keyPrefix) {
private String getToken(String keyPrefix) {
String dateCenterPrefix = RedisConstant.DATE_CENTER_PREFIX + keyPrefix;
String token = redisService.get(dateCenterPrefix);
if (StrUtil.isNotBlank(token)) {
@@ -99,18 +94,18 @@ public class DataCenterProperties {
} else {
HttpRequest httpRequest = HttpUtil.createPost(tokenUrl);
Map<String, Object> reqBody = Map.of(
"appId", appId,
"code", code
"key", key,
"secret", secret
);
httpRequest.body(JSONUtil.toJsonStr(reqBody));
JSONObject resp = JSONUtil.parseObj(httpRequest.execute().body());
if (resp.getInt("code") != 200) {
throw new BaseException("获取token失败: " + resp.getStr("msg"));
if (!"ok".equals(resp.getStr("message"))) {
throw new BaseException("获取token失败: " + resp.getStr("description"));
} else {
JSONObject data = resp.getJSONObject("data");
String tokenStr = data.getStr("token");
Integer expire = data.getInt("expire");
JSONObject data = resp.getJSONObject("result");
String tokenStr = data.getStr("access_token");
Integer expire = data.getInt("expires_in");
redisService.setex(dateCenterPrefix, expire, tokenStr);
return tokenStr;
}
@@ -1,4 +1,4 @@
package com.budwk.app.base.sms.impl.jshvc;
package com.budwk.app.base.sms.impl.ypi;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
@@ -17,7 +17,7 @@ import java.util.List;
import java.util.Map;
/**
* @ClassName SmsJshvcServiceImpl
* @ClassName SmsYpiServiceImpl
* @Author JyuHsin
* @Date 2025/11/26 19:32
* @Version 1.0
@@ -25,12 +25,12 @@ import java.util.Map;
*/
@Slf4j
@IocBean
public class SmsJshvcServiceImpl implements SmsService {
public class SmsYpiServiceImpl implements SmsService {
private static final String APPID = "1430576717768122368";
private static final String APP_SECRET = "19A0ACB03FBQHL4R4XQD";
private static final String TOKEN_URL = "https://gateway.jshvc.edu.cn/token/gateway/accessToken";
private static final String MSG_URL = "https://gateway.jshvc.edu.cn/mp/restful/v2/message/send";
private static final String TOKEN_URL = "https://gateway.ypi.edu.cn/token/gateway/accessToken";
private static final String MSG_URL = "https://gateway.ypi.edu.cn/mp/restful/v2/message/send";
private static final String REDIS_KEY_MSG_ACCESS_TOKEN = "msg:token:";
@Inject
@@ -38,7 +38,7 @@ public class SmsJshvcServiceImpl implements SmsService {
@Override
public void send(String loginName, String content) {
// send(loginName, "智慧工会", content);
send(loginName, "智慧工会", content);
}
@Override
@@ -48,16 +48,16 @@ public class SmsJshvcServiceImpl implements SmsService {
@Override
public void send(String loginName, String title, String content, String link) {
// Map<String, String> paramMap = Map.of("userId", loginName);
// doSend(title, content, List.of(paramMap), link);
Map<String, String> paramMap = Map.of("userId", loginName);
doSend(title, content, List.of(paramMap), link);
}
@Override
public void massSend(List<String> loginNames, String title, String content, String link) {
// List<Map<String, String>> receivers = loginNames.stream()
// .map(name -> Map.of("userId", name))
// .toList();
// doSend(title, content, receivers, link);
List<Map<String, String>> receivers = loginNames.stream()
.map(name -> Map.of("userId", name))
.toList();
doSend(title, content, receivers, link);
}
/**
@@ -208,10 +208,10 @@ public class SysHomeController {
// if (!DateUtil.isIn(today, startDate, endDate)) {
// continue;
// }
// 只过滤结束的
if (DateUtil.compare(today, endDate) > 0) {
continue;
}
// 只过滤结束的
if (DateUtil.compare(today, endDate) > 0) {
continue;
}
Integer allowUserGroupId = activity.getAllowUserGroupId();
String allowUserSql = activity.getAllowUserSql();
activity.setAllowUserSql(null);
@@ -264,71 +264,46 @@ public class SysHomeController {
// @CacheResult(cacheKey = "news", ignoreNull = true, cacheLiveTime = 60 * 60 * 24)
public Result getNews() {
// 定义要爬取的URL
String domain = "https://gh.whcp.edu.cn";
String domain = "https://gonghui.ypi.edu.cn";
try {
// 使用Jsoup连接到URL并获取页面内容
Document doc = Jsoup.connect(domain).get();
JSONObject root = new JSONObject();
// 1. 新闻快讯(来自 .list1
/* List<JSONObject> newsArray = new ArrayList<>();
Elements newsItems = doc.select(".ml .main_page2 .news_list li");
for (Element item : newsItems) {
String date = item.select(".date").text().trim();
Element link = item.select("a.text").first();
String title = link.select(".title").text().trim();
String url = link.attr("href").trim();
JSONObject node = new JSONObject();
node.set("title", title);
node.set("date", date);
node.set("url", domain + url);
newsArray.add(node);
}
root.set("news", newsArray);*/
// 2. 通知公告(来自 .list2
List<JSONObject> noticesArray = new ArrayList<>();
Elements noticeItems = doc.select(".tu_text .news_list li");
for (Element item : noticeItems) {
String date = item.select(".news_meta").text().trim();
Element link = item.select(".news_title a").first();
String title = link.text().trim();
String url = link.attr("href").trim();
JSONObject node = new JSONObject();
node.set("title", title);
node.set("date", date);
node.set("url", domain + url);
noticesArray.add(node);
}
root.set("notices", noticesArray);
// 3. 基层风采(来自 .pic-list1
/* List<JSONObject> grassrootsArray = new ArrayList<>();
Elements grassrootsItems = doc.select(".group3 .pic-list1 li");
List<JSONObject> grassrootsArray = new ArrayList<>();
Elements grassrootsItems = doc.select(".news #wp_news_w21 ul li");
for (Element item : grassrootsItems) {
Element link = item.select("a.img-scale").first();
String date = link.select(".date").text().trim();
String title = link.select(".title").text().trim();
String url = link.attr("href").trim();
String summary = link.select(".info").text().trim();
String imageUrl = "";
Element img = link.select("img").first();
if (img != null) {
imageUrl = img.attr("src").trim();
}
Element aTitle = item.select("a").first();
String title = aTitle.attr("title").trim();
String url = aTitle.attr("href").trim();
Element meta = item.select(".Article_PublishDate").first();
String date = meta.text().trim();
/*Element link = item.select(".nr").first();
String date = link.select(".date").text().trim();
String title = link.select(".news_title a").text().trim();
String url = link.select(".news_title a").attr("href").trim();
String summary = link.select(".news_text a").text().trim();
String imageUrl = "";
Element img = link.select(".news_imgs").first();
if (img != null) {
imageUrl = img.attr("src").trim();
}*/
JSONObject node = new JSONObject();
node.set("title", title);
node.set("date", date);
node.set("url", domain + url);
node.set("summary", summary);
node.set("image", domain + imageUrl);
node.set("url", "https://gonghui.ypi.edu.cn/" + url);
//node.set("summary", summary);
//node.set("image", StrUtil.isNotBlank(imageUrl) ? (domain + imageUrl) : "");
grassrootsArray.add(node);
}
root.set("grassroots", grassrootsArray);*/
root.set("grassroots", grassrootsArray);
return Result.success(root);
} catch (Exception e) {
log.error(e);
@@ -12,9 +12,10 @@ import lombok.Getter;
@Getter
public enum Api2UnitFiledMap {
ZZJGDM("dwdm", "id"),
ZZJGMC("dwmc", "name"),
BMLB("bmflmc", "unitType");
ZZJGDM("DWDM", "id"),
ZZJGMC("DWMC", "name"),
DWCC("DWCC", "unitLevel"),
LSDWH("LSDWH", "parentId");
public final String apiField;
public final String dbColumn;
@@ -36,7 +36,7 @@ public class Sys_user extends BaseModel implements Serializable {
@Column
@Comment("工号")
@ColDefine(type = ColType.VARCHAR, width = 120)
@DataCenterColumn(name = "工号", key = "JGH")
@DataCenterColumn(name = "工号", key = "ZGH")
private String loginname;
@Column
@@ -48,7 +48,7 @@ public class Sys_user extends BaseModel implements Serializable {
@Column
@Comment("性别")
@ColDefine(type = ColType.VARCHAR, width = 10)
@DataCenterColumn(name = "", key = "XBM", dict = "USER_SEX")
@DataCenterColumn(name = "", key = "XBMC")
private String sex;
@Column
@@ -64,12 +64,13 @@ public class Sys_user extends BaseModel implements Serializable {
@Column
@Comment("出生日期")
@ColDefine(type = ColType.DATE)
@DataCenterColumn(name = "出生日期", key = "CSRQ")
private Date birthday;
@Column
@Comment("政治面貌")
@ColDefine(type = ColType.VARCHAR, width = 100)
@DataCenterColumn(name = "政治面貌", key = "ZZMMM", dict = "USER_POLITICAL")
@DataCenterColumn(name = "政治面貌", key = "ZZMM")
private String political;
@Column
@@ -80,7 +81,7 @@ public class Sys_user extends BaseModel implements Serializable {
@Column
@Comment("民族")
@ColDefine(type = ColType.VARCHAR, width = 20)
@DataCenterColumn(name = "民族", key = "MZM", dict = "USER_NATION")
@DataCenterColumn(name = "民族", key = "MZMC")
private String nation;
@Column
@@ -96,6 +97,7 @@ public class Sys_user extends BaseModel implements Serializable {
@Column
@Comment("证件类型")
@ColDefine(type = ColType.VARCHAR, width = 30)
@DataCenterColumn(name = "证件类型", key = "SFZJLXDM")
private String idCardType;
@Column
@@ -107,72 +109,70 @@ public class Sys_user extends BaseModel implements Serializable {
@Column
@Comment("手机号码")
@ColDefine(type = ColType.VARCHAR, width = 32)
@DataCenterColumn(name = "手机号码", key = "SJHM")
@DataCenterColumn(name = "手机号码", key = "SJH")
private String mobile;
@Column
@Comment("学历")
@ColDefine(type = ColType.VARCHAR, width = 20)
@DataCenterColumn(name = "学历", key = "ZGXLM", dict = "USER_EDUCATION")
@DataCenterColumn(name = "学历", key = "ZGXLMC")
private String education;
@Column
@Comment("婚姻状况")
@ColDefine(type = ColType.VARCHAR, width = 20)
@DataCenterColumn(name = "婚姻状况", key = "HYZKMC")
private String marriage;
@Column
@Comment("学位")
@ColDefine(type = ColType.VARCHAR, width = 50)
// @DataCenterColumn(name = "学位", key = "zgxwmc")
@DataCenterColumn(name = "学位", key = "ZGXWMC")
private String academicDegree;
@Column
@Comment("岗位类别")
@ColDefine(type = ColType.VARCHAR, width = 50)
@DataCenterColumn(name = "岗位类别", key = "JRGWDJM",dict = "USER_JOB_CATEGORY")
@DataCenterColumn(name = "岗位类别", key = "GWLBMC")
private String jobCategory;
@Column
@Comment("职称")
@ColDefine(type = ColType.VARCHAR, width = 50)
@DataCenterColumn(name = "职称", key = "DQZC", dict = "USER_PROFESSIONAL_TITLE")
@DataCenterColumn(name = "职称", key = "PRZYJSZWMC")
private String professionalTitle;
@Column
@Comment("职级")
@ColDefine(type = ColType.VARCHAR, width = 50)
@DataCenterColumn(name = "取得职称等级码", key = "QDZCDJM", dict = "USER_PROFESSIONAL_LEVEL")
@DataCenterColumn(name = "职级", key = "ZYJSZWJB")
private String professionalLevel;
@Column
@Comment("职工来源")
@ColDefine(type = ColType.VARCHAR, width = 50)
// @DataCenterColumn(name = "职工来源", key = "zgly")
@DataCenterColumn(name = "职工来源", key = "JZGLYMC")
private String employeeSource;
@Column
@Comment("行政级别(用于管理岗位)")
@ColDefine(type = ColType.VARCHAR, width = 50)
// @DataCenterColumn(name = "行政级别(用于管理岗位)", key = "xzjb")
private String administrativeLevel;
@Column
@Comment("岗位系列")
@ColDefine(type = ColType.VARCHAR, width = 50)
// @DataCenterColumn(name = "岗位系列", key = "gwxl")
private String positionSeries;
@Column
@Comment("岗级")
@ColDefine(type = ColType.VARCHAR, width = 50)
// @DataCenterColumn(name = "岗级", key = "gj")
@DataCenterColumn(name = "岗级", key = "GWDJMC")
private String positionLevel;
@Column
@Comment("行政岗级")
@ColDefine(type = ColType.VARCHAR, width = 50)
// @DataCenterColumn(name = "行政岗级", key = "xzgj")
private String administrativePositionLevel;
@Column
@@ -189,25 +189,19 @@ public class Sys_user extends BaseModel implements Serializable {
@Column
@Comment("在职状态")
@ColDefine(type = ColType.VARCHAR, width = 30)
@DataCenterColumn(name = "当前状态", key = "DQZTM", dict = "USER_STATE")
@DataCenterColumn(name = "在岗状态", key = "ZGZTMC")
private String userState;
@Column
@Comment("在岗情况")
@ColDefine(type = ColType.VARCHAR, width = 30)
@DataCenterColumn(name = "在岗情况码", key = "ZGQKM", dict = "USER_DUTY_SITUATION")
private String dutySituation;
@Column
@Comment("教职工类别码")
@ColDefine(type = ColType.VARCHAR, width = 30)
@DataCenterColumn(name = "教职工类别码", key = "JZGLBM", dict = "USER_PERSON_TYPE")
@DataCenterColumn(name = "教职工类别码", key = "JZGLBMC")
private String personType;
@Column
@Comment("编制类别码")
@ColDefine(type = ColType.VARCHAR, width = 30)
@DataCenterColumn(name = "编制类别码", key = "BZLBM", dict = "USER_PREPARED_BY_TYPE")
@DataCenterColumn(name = "编制", key = "ZBZTMC")
private String preparedBy;
@Column
@@ -218,6 +212,7 @@ public class Sys_user extends BaseModel implements Serializable {
@Column
@Comment("预计离校时间/最后离校时间")
@ColDefine(type = ColType.DATE)
@DataCenterColumn(name = "预计离校时间/最后离校时间", key = "LXRQ")
private Date expectedLeaveSchoolDate;
@Column
@@ -257,7 +252,7 @@ public class Sys_user extends BaseModel implements Serializable {
@Column
@ColDefine(type = ColType.VARCHAR, width = 32)
@DataCenterColumn(name = "单位", key = "DWH")
@DataCenterColumn(name = "单位代码", key = "SZDWDM")
private String unitId;
@Column
@@ -268,6 +263,7 @@ public class Sys_user extends BaseModel implements Serializable {
@Comment("是否双肩挑")
@ColDefine(type = ColType.BOOLEAN)
@Default(value = "0")
@DataCenterColumn(name = "是否双肩挑", key = "SFSJT")
private Boolean manyUnit;
@Column
@@ -319,17 +315,6 @@ public class Sys_user extends BaseModel implements Serializable {
@ColDefine(type = ColType.VARCHAR, customType = "text")
private String personalData;
@Column
@Comment("特长及获奖情况")
@ColDefine(type = ColType.VARCHAR, customType = "text")
private String specialty;
@Column
@Comment("家庭住址")
@ColDefine(type = ColType.VARCHAR, width = 100)
private String homeAddress;
@Column
@Comment("常用审批意见")
@ColDefine(type = ColType.MYSQL_JSON)
@@ -394,4 +379,10 @@ public class Sys_user extends BaseModel implements Serializable {
@Comment("openId")
@ColDefine(type = ColType.VARCHAR, width = 50)
private String openId;
@Column
@Comment("党政职务")
@ColDefine(type = ColType.VARCHAR, width = 50)
@DataCenterColumn(name = "党政职务", key = "DZZW")
private String position;
}
@@ -1,7 +1,6 @@
package com.budwk.app.sys.services.impl;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
@@ -13,16 +12,18 @@ 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.budwk.app.web.commons.base.Globals;
import lombok.extern.slf4j.Slf4j;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
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 java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -64,36 +65,37 @@ public class SysDataUnitPullServiceImpl extends BaseServiceImpl<Sys_unit> implem
@Override
@Aop(TransAop.READ_COMMITTED)
public void updateUnits(List<String> unitIds) {
// token url参数
Map<String, String> tokenParams = Map.of(
"grant_type", "password",
"scope", "read",
"username", "3848A3C738EFBB2C2E76E0D59B419DACA16A80F3AB0354CD",
"password", "3848A3C738EFBB2C2E76E0D59B419DACA16A80F3AB0354CD"
);
// 判断有没有父节点
/*Sys_unit parentUnit = dao().fetch(Sys_unit.class, Cnd.where(Sys_unit::getUnitcode, "=", "0000"));
if(parentUnit == null) {
Sys_unit pu = new Sys_unit();
pu.setId("0000");
pu.setName(Globals.AppName.replace("智慧工会", ""));
pu.setUnitcode("0000");
pu.setHasChildren(true);
pu.setUnitLevel(1);
pu.setUnitType("根目录");
pu.setUnitTypeCode(0);
dao().insert(pu);
}*/
String params = URLUtil.buildQuery(tokenParams, StandardCharsets.UTF_8);
String tokenUrl = "https://bd.whcp.edu.cn/sjjkfwpt/api/bd-api/oauth/token" + (StrUtil.isBlank(params) ? "" : "?" + params);
HttpRequest tokenHttpRequest = HttpUtil.createPost(tokenUrl);
tokenHttpRequest.header("Content-Type", "application/json");
tokenHttpRequest.header("Authorization", "Basic MTE0OjU5M2UwNGI4YTE4NWRlMzkyNjY4ZmY0ZDJhZGMyZTEw");
log.info("请求token: {}", tokenHttpRequest);
String tokenResBody = tokenHttpRequest.execute().body();
log.info("请求token结果: {}", tokenResBody);
JSONObject tokenJsonBody = JSONUtil.parseObj(tokenResBody);
// 获取配置
DataCenterProperties.Credential credential = dcPro.credential("unit");
String url = credential.getUrl();
String token = credential.getToken();
// 请求数据
HttpRequest httpRequest = HttpUtil.createGet("https://bd.whcp.edu.cn/sjjkfwpt/api/bd-api/get/YXSDWJBSJZLB");
httpRequest.form(Map.of("access_token", tokenJsonBody.getStr("access_token"), "pageSize", 100, "page", 1));
log.info("请求单位数据: {}", httpRequest);
HttpRequest httpRequest = HttpUtil.createPost(url);
httpRequest.header("Content-Type", "application/json");
httpRequest.header("Authorization", "Bearer " + token);
httpRequest.body(JSONUtil.toJsonStr(Map.of("per_page", 1000)));
String resBody = httpRequest.execute().body();
log.info("请求单位数据结果: {}", resBody);
JSONObject jsonBody = JSONUtil.parseObj(resBody);
if (jsonBody.getInt("code") != 200) {
throw new BaseException("获取单位数据失败,错误码: " + jsonBody.getInt("code") + ";错误原因:" + jsonBody.getStr("msg"));
if (!"ok".equals(jsonBody.getStr("message"))) {
throw new BaseException("获取单位数据失败,错误码: " + jsonBody.getInt("code") + ";错误原因:" + jsonBody.getStr("description"));
}
// 需要新增的单位
@@ -101,17 +103,28 @@ public class SysDataUnitPullServiceImpl extends BaseServiceImpl<Sys_unit> implem
// 需要更新的单位
List<Sys_unit> updateList = new ArrayList<>();
// 拉取过来的数据,全部的单位,一万四千多条,这里只保留 “部门类”
List<JSONObject> data = jsonBody.getJSONArray("data").stream()
JSONObject result = jsonBody.getJSONObject("result");
List<JSONObject> data = result.getJSONArray("data").stream()
.map(o -> (JSONObject) o)
.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);
JSONObject jsonRow = JSONUtil.parseObj(JSONUtil.toJsonStr(row));
Sys_unit unit = DataCenterUtil.mapJsonToBean(jsonRow, Sys_unit.class, api2db);
unit.setUnitcode(unit.getId());
unit.setUnitTypeCode(1);
if(unit.getUnitLevel() == 1 || "0000".equals(unit.getUnitcode())) {
unit.setUnitType("根目录");
unit.setUnitTypeCode(0);
unit.setParentId("0");
} else {
unit.setUnitType("子部门");
unit.setUnitTypeCode(1);
}
if(unit.getUnitLevel() == 2) {
unit.setParentId("0000");
}
if (unitIds.contains(unit.getId())) {
// 存在则更新
@@ -6,7 +6,6 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
@@ -33,6 +32,7 @@ import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
@@ -42,7 +42,6 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
@@ -110,54 +109,43 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
@Aop(TransAop.READ_COMMITTED)
public Date pull() {
try {
// 获取数据中心的字典
List<Sys_data_dict> dataDictList = dao().query(Sys_data_dict.class, Cnd.NEW());
// 字典码表Map,key为父级编码,value为子级字典列表
Map<String, List<Sys_data_dict>> dataDictMap = dataDictList.stream().collect(Collectors.groupingBy(Sys_data_dict::getParentCode));
// token url参数
Map<String, String> tokenParams = Map.of(
"grant_type", "password",
"scope", "read",
"username", "3848A3C738EFBB2C2E76E0D59B419DACA16A80F3AB0354CD",
"password", "3848A3C738EFBB2C2E76E0D59B419DACA16A80F3AB0354CD"
);
String params = URLUtil.buildQuery(tokenParams, StandardCharsets.UTF_8);
String tokenUrl = "https://bd.whcp.edu.cn/sjjkfwpt/api/bd-api/oauth/token" + (StrUtil.isBlank(params) ? "" : "?" + params);
HttpRequest tokenHttpRequest = HttpUtil.createPost(tokenUrl);
tokenHttpRequest.header("Content-Type", "application/json");
tokenHttpRequest.header("Authorization", "Basic MTE0OjU5M2UwNGI4YTE4NWRlMzkyNjY4ZmY0ZDJhZGMyZTEw");
log.info("请求token: {}", tokenHttpRequest);
String tokenResBody = tokenHttpRequest.execute().body();
log.info("请求token结果: {}", tokenResBody);
JSONObject tokenJsonBody = JSONUtil.parseObj(tokenResBody);
// 请求数据
HttpRequest httpRequest = HttpUtil.createGet("https://bd.whcp.edu.cn/sjjkfwpt/api/bd-api/get/JZGJCSJZLB");
httpRequest.form(Map.of("access_token", tokenJsonBody.getStr("access_token"), "pageSize", 5000, "page", 1));
log.info("请求人员数据: {}", httpRequest);
String resBody = httpRequest.execute().body();
log.info("请求人员数据结果: {}", resBody);
JSONObject jsonBody = JSONUtil.parseObj(resBody);
if (jsonBody.getInt("code") != 200) {
throw new BaseException("获取人员数据失败,错误码: " + jsonBody.getInt("code") + ";错误原因:" + jsonBody.getStr("msg"));
}
// 获取配置文件 接口地址 和 token
DataCenterProperties.Credential credential = dataCenterProperties.credential("teacher");
String url = credential.getUrl();
String token = credential.getToken();
List<JSONObject> rawDataList = new ArrayList<>();
JSONArray data = jsonBody.getJSONArray("data");
int page = 1;
int maxPage;
do {
// 请求数据
HttpRequest httpRequest = HttpUtil.createPost(url);
httpRequest.header("Content-Type", "application/json");
httpRequest.header("Authorization", "Bearer " + token);
httpRequest.body(JSONUtil.toJsonStr(Map.of("page", page, "per_page", 500)));
if (!data.isEmpty()) {
// 先收集所有原始数据
rawDataList = data.stream().map(v -> (JSONObject) v).toList();
log.info("数据拉取进度: {}/{}", rawDataList.size(), jsonBody.getInt("total"));
} else {
log.warn("当前未获取到数据");
}
String resBody = httpRequest.execute().body();
JSONObject jsonBody = JSONUtil.parseObj(resBody);
if (!"ok".equals(jsonBody.getStr("message"))) {
throw new BaseException("获取人员数据失败,错误码: " + jsonBody.getInt("code") + ";错误原因:" + jsonBody.getStr("description"));
}
JSONObject result = jsonBody.getJSONObject("result");
maxPage = result.getInt("max_page");
int total = result.getInt("total");
JSONArray data = result.getJSONArray("data");
if (!data.isEmpty()) {
// 先收集所有原始数据
rawDataList.addAll(data.stream().map(v -> JSONUtil.parseObj(JSONUtil.toJsonStr(v))).toList());
log.info("数据拉取进度: 总数据量:{},已经拉取{},本次拉取:{}", total, rawDataList.size(), data.size());
} else {
log.warn("当前未获取到数据");
}
page ++;
} while (page <= maxPage);
Date nowDate = new Date();
@@ -170,25 +158,9 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
try {
// 根据字段类型设置值
if (mapping.field.getType() == String.class) {
if (StrUtil.isNotBlank(mapping.dict)) {
Sys_data_dict sysDataDict = dataDictMap.get(mapping.dict).stream()
.filter(v -> raw.getStr(mapping.key)
.equals(v.getCode())).findFirst().orElse(new Sys_data_dict());
mapping.field.set(sysUser, sysDataDict.getName());
} else {
// 特殊处理 根据身份证号获取性别和出生年月
if ("SFZJH".equals(mapping.field.getName())) {
String idCard = raw.getStr(mapping.key);
// 设置出生年月
try {
sysUser.setBirthday(IdcardUtil.getBirthDate(idCard));
} catch (Exception e) {
sysUser.setBirthday(null);
}
} else {
mapping.field.set(sysUser, raw.getStr(mapping.key));
}
mapping.field.set(sysUser, raw.getStr(mapping.key));
if("XBMC".equals(mapping.key) && StrUtil.isNotBlank(raw.getStr(mapping.key))) {
mapping.field.set(sysUser, raw.getStr(mapping.key).replace("", ""));
}
} else if (mapping.field.getType() == Date.class) {
mapping.field.set(sysUser, raw.getDate(mapping.key));
@@ -198,15 +170,15 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
}
}
// 设置拉取时间
sysUser.setPullTime(nowDate);
return sysUser;
}).toList();
log.info("用户数据初始化完成,等待插入,当前数据条数{}", latestSourceList.size());
// 插入到数据库
dao().insert(latestSourceList);
// 更新字典数据
this.updateDict(latestSourceList);
// 人员的单位数据和数据库的单位数据比较,如果人员里面有单位不存在,去更新单位数据
List<String> sourceUnitIds = latestSourceList.stream().map(Sys_user_source::getUnitId).distinct().toList();
@@ -225,6 +197,11 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
}
}
@Override
public Map<String, Date> pullPostDoctoral() {
return null;
}
@Async
private void updateDict(List<Sys_user_source> userSources) {
//判断是否要更新在职状态字典
@@ -234,9 +211,7 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
//判断是否要更新学位字典
List<String> sourceAcademicDegrees = userSources.stream().map(Sys_user::getAcademicDegree).filter(StrUtil::isNotBlank).distinct().toList();
//判断是否要更新教职工类别字典
List<String> personTypes = userSources.stream().map(Sys_user::getPersonType).filter(StrUtil::isNotBlank).distinct().toList();
//判断是否要更新身份类型字典
List<String> sourceIdentityTypes = userSources.stream().map(Sys_user::getIdentityType).filter(StrUtil::isNotBlank).distinct().toList();
List<String> sourcePersonTypes = userSources.stream().map(Sys_user::getPersonType).filter(StrUtil::isNotBlank).distinct().toList();
//系统在职状态字典
List<Sys_dict> sysUserStates = sysDictService.getSubListByCode("USER_STATE");
@@ -244,10 +219,7 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
List<Sys_dict> sysEducations = sysDictService.getSubListByCode("USER_EDUCATION");
//系统学位字典
List<Sys_dict> sysAcademicDegrees = sysDictService.getSubListByCode("USER_ACADEMIC_DEGREE");
//系统教职工类别字典
List<Sys_dict> sysStaffTypes = sysDictService.getSubListByCode("USER_STAFF_TYPE");
//系统身份类型字典
List<Sys_dict> sysIdentityTypes = sysDictService.getSubListByCode("USER_IDENTITY_TYPE");
List<Sys_dict> sysPersonTypes = sysDictService.getSubListByCode("USER_PERSON_TYPE");
//系统中不存在的就插入
List<Sys_dict> insertUserStates = sourceUserStates.stream().filter(state -> sysUserStates.stream().noneMatch(dict -> dict.getCode().equals(state))).map(state -> {
@@ -271,6 +243,13 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
return dict;
}).toList();
List<Sys_dict> insertPersonTypes = sourcePersonTypes.stream().filter(personType -> sysPersonTypes.stream().noneMatch(dict -> dict.getCode().equals(personType))).map(personType -> {
Sys_dict dict = new Sys_dict();
dict.setCode(personType);
dict.setName(personType);
return dict;
}).toList();
for (Sys_dict dict : insertUserStates) {
sysDictService.saveByParentCode(dict, "USER_STATE");
}
@@ -280,63 +259,12 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
for (Sys_dict dict : insertAcademicDegrees) {
sysDictService.saveByParentCode(dict, "USER_ACADEMIC_DEGREE");
}
for (Sys_dict dict : insertPersonTypes) {
sysDictService.saveByParentCode(dict, "USER_PERSON_TYPE");
}
sysDictService.clearCache();
}
@Override
public Map<String, Date> pullPostDoctoral() {
String appId = "1926887238437818370";
String secret = "32df31fcf4fc43f9a647ee1f47134f36";
List<JSONObject> allUsers = new ArrayList<>();
int skip = 0;
int totalCount = 0;
boolean firstRequest = true;
do {
long ts = System.currentTimeMillis();
String sign = Base64.encode(DigestUtil.md5Hex(appId + secret + ts, CharsetUtil.CHARSET_UTF_8));
HttpRequest httpRequest = HttpUtil.createPost("https://sjzcpt.nnu.edu.cn/cdsp/data-api/v2/DS0040");
httpRequest.header("Content-Type", "application/json");
httpRequest.header("appId", appId);
httpRequest.header("timestamp", String.valueOf(ts));
httpRequest.header("sign", sign);
HashMap<String, Object> reqBody = new HashMap<>();
reqBody.put("$count", true);
reqBody.put("$skip", skip);
reqBody.put("$top", 1000);
httpRequest.body(JSONUtil.toJsonStr(reqBody));
String resBody = httpRequest.execute().body();
JSONObject entries = JSONUtil.parseObj(resBody);
if (firstRequest) {
totalCount = entries.getInt("@odata.count", 0);
firstRequest = false;
}
JSONArray value = entries.getJSONArray("value");
if (!value.isEmpty()) {
List<JSONObject> list = value.stream().map(item -> (JSONObject) item).toList();
allUsers.addAll(list);
skip += list.size();
} else {
break;
}
} while (skip < totalCount);
Map<String, Date> data = allUsers.stream().filter(v -> v.getStr("进站日期") != null).collect(Collectors.toMap(v -> v.getStr("教职工号"), v -> v.getDate("进站日期")));
return data;
}
@Override
public NutMap searchOptions() {
Sql sql = Sqls.create("""
@@ -385,73 +313,8 @@ public class SysDataUserPullServiceImpl extends BaseServiceImpl<Sys_user_source>
return listMap(sql);
}
@Override
public Map<String, NutMap> pullFinance() {
// 获取配置文件 接口地址 和 token
DataCenterProperties.Credential credential = dataCenterProperties.credential("finance");
String url = credential.getUrl();
String token = credential.getToken();
List<JSONObject> rawDataList = new ArrayList<>();
String year = String.valueOf(DateUtil.thisYear());
String month = String.valueOf(DateUtil.thisMonth() + 1);
int pageNum = 1;
int pageSize = 1000;
int totalPages = 1;
int totalCount = 0;
do {
HttpRequest httpRequest = HttpUtil.createPost(url);
httpRequest.header(Header.CONTENT_TYPE, "application/json");
httpRequest.header("Accept-Encoding", "gzip, deflate, br");
httpRequest.header("X-H3C-TOKEN", token);
Map<String, Object> reqBody = Map.of(
"nf", year,
"yf", month,
"pageNum", pageNum,
"pageSize", pageSize
);
httpRequest.body(JSONUtil.toJsonStr(reqBody));
JSONObject resp = JSONUtil.parseObj(httpRequest.execute().body());
if (resp.getInt("code") != 0) {
throw new BaseException("获取财务数据失败,错误码: " + resp.getInt("code") + ";错误原因:" + resp.getStr("msg"));
}
JSONObject data = resp.getJSONObject("data");
if (pageNum == 1) {
totalCount = data.getInt("total", 0);
// totalPages = data.getInt("pages", 0);
totalPages = (int) Math.ceil((double) totalCount / pageSize);
}
JSONArray records = data.getJSONArray("records");
if (CollUtil.isNotEmpty(records)) {
rawDataList.addAll(records.stream().map(v -> (JSONObject) v).toList());
log.info("数据拉取进度: {}/{}", rawDataList.size(), totalCount);
}
pageNum++; // 只加页码
} while (pageNum <= totalPages);
// 接口数据输出
log.info("数据拉取结果: {}", rawDataList);
// 最后结果数据
Map<String, NutMap> result = new HashMap<>();
Map<String, String> api2db = Arrays.stream(Api2FinanceFiledMap.values())
.collect(Collectors.toMap(e -> e.apiField, e -> e.dbColumn));
for (JSONObject row : rawDataList) {
NutMap nutMap = DataCenterUtil.mapJsonToBean(row, NutMap.class, api2db);
result.put(nutMap.getString("loginname"), nutMap);
}
// 输出结果
log.info("财务数据拉取转换结果: {}", result);
log.info("数据拉取完成,共拉取 {} 条数据", result.size());
return result;
return null;
}
}