修复问题
This commit is contained in:
@@ -4,6 +4,8 @@ import cn.wepact.dfm.training.dto.SyncRequest;
|
|||||||
import cn.wepact.dfm.training.dto.TaskScoreSummary;
|
import cn.wepact.dfm.training.dto.TaskScoreSummary;
|
||||||
import cn.wepact.dfm.training.service.SyncTaskService;
|
import cn.wepact.dfm.training.service.SyncTaskService;
|
||||||
import cn.wepact.dfm.training.service.SyncUserInfoService;
|
import cn.wepact.dfm.training.service.SyncUserInfoService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -17,6 +19,8 @@ import java.util.List;
|
|||||||
@RequestMapping("sync")
|
@RequestMapping("sync")
|
||||||
public class SyncController {
|
public class SyncController {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(SyncController.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SyncTaskService taskSyncService;
|
private SyncTaskService taskSyncService;
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -24,21 +28,31 @@ public class SyncController {
|
|||||||
|
|
||||||
@PostMapping("/syncTasks")
|
@PostMapping("/syncTasks")
|
||||||
public ResponseEntity<String> syncTasks(@RequestBody SyncRequest syncRequest) {
|
public ResponseEntity<String> syncTasks(@RequestBody SyncRequest syncRequest) {
|
||||||
|
log.info("【同步任务】请求参数 - syncType={}, offset={}, limit={}, duration={}, searchStartTime={}, searchEndTime={}",
|
||||||
// 调用 syncTasks 方法并传入请求数据
|
syncRequest.getSyncType(), syncRequest.getOffset(), syncRequest.getLimit(),
|
||||||
taskSyncService.syncTasks(syncRequest);
|
syncRequest.getDuration(), syncRequest.getSearchStartTime(), syncRequest.getSearchEndTime());
|
||||||
|
try {
|
||||||
// 返回成功响应
|
taskSyncService.syncTasks(syncRequest);
|
||||||
return ResponseEntity.ok("同步任务完成");
|
log.info("【同步任务】完成");
|
||||||
|
return ResponseEntity.ok("同步任务完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("【同步任务】失败", e);
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("同步任务失败: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@PostMapping("/syncUsers")
|
@PostMapping("/syncUsers")
|
||||||
public ResponseEntity<String> syncUsers(@RequestBody SyncRequest syncRequest) {
|
public ResponseEntity<String> syncUsers(@RequestBody SyncRequest syncRequest) {
|
||||||
|
log.info("【同步用户】请求参数 - syncType={}, offset={}, limit={}, duration={}, searchStartTime={}, searchEndTime={}",
|
||||||
// 调用 syncTasks 方法并传入请求数据
|
syncRequest.getSyncType(), syncRequest.getOffset(), syncRequest.getLimit(),
|
||||||
syncUserInfoService.syncUserInfos(syncRequest);
|
syncRequest.getDuration(), syncRequest.getSearchStartTime(), syncRequest.getSearchEndTime());
|
||||||
|
try {
|
||||||
// 返回成功响应
|
syncUserInfoService.syncUserInfos(syncRequest);
|
||||||
return ResponseEntity.ok("同步任务完成");
|
log.info("【同步用户】完成");
|
||||||
|
return ResponseEntity.ok("同步任务完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("【同步用户】失败", e);
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("同步任务失败: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
// /**
|
||||||
@@ -57,76 +71,87 @@ public class SyncController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/process-scores")
|
@PostMapping("/process-scores")
|
||||||
public ResponseEntity<String> processTaskScores() {
|
public ResponseEntity<String> processTaskScores() {
|
||||||
|
log.info("【处理分数】开始");
|
||||||
try {
|
try {
|
||||||
List<TaskScoreSummary> taskScoreSummaries = taskSyncService.getTaskScoreSummary();
|
List<TaskScoreSummary> taskScoreSummaries = taskSyncService.getTaskScoreSummary();
|
||||||
|
log.info("【处理分数】获取到 {} 条分数汇总", taskScoreSummaries.size());
|
||||||
taskSyncService.processTaskScores(taskScoreSummaries);
|
taskSyncService.processTaskScores(taskScoreSummaries);
|
||||||
|
log.info("【处理分数】完成");
|
||||||
return ResponseEntity.ok("任务分数处理成功");
|
return ResponseEntity.ok("任务分数处理成功");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
log.error("【处理分数】失败", e);
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("任务分数处理失败: " + e.getMessage());
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("任务分数处理失败: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 定时任务方法:每天某个时间点执行任务
|
// 定时任务方法:每10分钟执行一次
|
||||||
// @Scheduled(cron = "0 0 0 * * ?") // 每天0点执行,可以根据需求修改 cron 表达式
|
@Scheduled(cron = "0 */10 * * * ?")
|
||||||
@Scheduled(cron = "0 */10 * * * ?") // 每5分钟执行一次
|
|
||||||
@PostMapping("/executeTasks")
|
@PostMapping("/executeTasks")
|
||||||
@Transactional(rollbackOn = Exception.class)
|
@Transactional(rollbackOn = Exception.class)
|
||||||
public void executeTasks() {
|
public void executeTasks() {
|
||||||
|
log.info("【定时任务】开始执行 - 同步用户->同步任务->处理分数");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 第一个任务:同步用户
|
|
||||||
syncUsers();
|
syncUsers();
|
||||||
|
log.info("【定时任务】同步用户完成");
|
||||||
// 第二个任务:同步任务
|
|
||||||
syncTasks();
|
|
||||||
|
|
||||||
// 第三个任务:处理任务分数
|
|
||||||
processTaskScores();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 任务执行失败时的异常处理
|
log.error("【定时任务】同步用户异常,继续执行后续任务", e);
|
||||||
System.err.println("定时任务执行失败:" + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 同步用户信息
|
try {
|
||||||
|
syncTasks();
|
||||||
|
log.info("【定时任务】同步任务完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("【定时任务】同步任务异常,继续执行后续任务", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
processScores();
|
||||||
|
log.info("【定时任务】处理分数完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("【定时任务】处理分数异常", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("【定时任务】全部执行完毕");
|
||||||
|
}
|
||||||
|
|
||||||
private void syncUsers() throws Exception {
|
private void syncUsers() throws Exception {
|
||||||
try {
|
SyncRequest syncRequest = new SyncRequest();
|
||||||
SyncRequest syncRequest = new SyncRequest();
|
syncRequest.setDuration(0);
|
||||||
syncRequest.setDuration(0);
|
syncRequest.setSyncType("incremental");
|
||||||
syncRequest.setSyncType("incremental");
|
syncRequest.setOffset(0);
|
||||||
syncRequest.setOffset(0);
|
syncRequest.setLimit(20);
|
||||||
syncRequest.setLimit(20);
|
|
||||||
|
|
||||||
ResponseEntity<String> response = syncUserInfoService.syncUserInfos(syncRequest);
|
log.info("【定时-同步用户】请求参数: {}", syncRequest);
|
||||||
if (!response.getStatusCode().equals(HttpStatus.OK)) {
|
ResponseEntity<String> response = syncUserInfoService.syncUserInfos(syncRequest);
|
||||||
throw new Exception("同步用户失败: " + response.getBody());
|
log.info("【定时-同步用户】响应: {}", response.getBody());
|
||||||
}
|
if (!response.getStatusCode().equals(HttpStatus.OK)) {
|
||||||
} catch (Exception e) {
|
throw new Exception("同步用户失败: " + response.getBody());
|
||||||
System.err.println("同步用户失败: " + e.getMessage());
|
|
||||||
throw new Exception("同步用户执行异常: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 同步任务
|
|
||||||
private void syncTasks() throws Exception {
|
private void syncTasks() throws Exception {
|
||||||
try {
|
SyncRequest syncRequest = new SyncRequest();
|
||||||
SyncRequest syncRequest = new SyncRequest();
|
syncRequest.setDuration(0);
|
||||||
syncRequest.setDuration(0);
|
syncRequest.setSyncType("incremental");
|
||||||
syncRequest.setSyncType("incremental");
|
syncRequest.setOffset(0);
|
||||||
syncRequest.setOffset(0);
|
syncRequest.setLimit(20);
|
||||||
syncRequest.setLimit(20);
|
|
||||||
ResponseEntity<String> response = taskSyncService.syncTasks(syncRequest);
|
|
||||||
|
|
||||||
if (!response.getStatusCode().equals(HttpStatus.OK)) {
|
log.info("【定时-同步任务】请求参数: {}", syncRequest);
|
||||||
throw new Exception("同步任务失败: " + response.getBody());
|
ResponseEntity<String> response = taskSyncService.syncTasks(syncRequest);
|
||||||
}
|
log.info("【定时-同步任务】响应: {}", response.getBody());
|
||||||
} catch (Exception e) {
|
if (!response.getStatusCode().equals(HttpStatus.OK)) {
|
||||||
System.err.println("同步任务失败: " + e.getMessage());
|
throw new Exception("同步任务失败: " + response.getBody());
|
||||||
throw new Exception("同步任务执行异常: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void processScores() throws Exception {
|
||||||
|
log.info("【定时-处理分数】开始");
|
||||||
|
List<TaskScoreSummary> summaries = taskSyncService.getTaskScoreSummary();
|
||||||
|
log.info("【定时-处理分数】获取到 {} 条汇总", summaries.size());
|
||||||
|
taskSyncService.processTaskScores(summaries);
|
||||||
|
log.info("【定时-处理分数】完成");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import cn.wepact.dfm.training.dto.entity.TOrg;
|
|||||||
import cn.wepact.dfm.training.dto.entity.TUser;
|
import cn.wepact.dfm.training.dto.entity.TUser;
|
||||||
import cn.wepact.dfm.training.service.UserService;
|
import cn.wepact.dfm.training.service.UserService;
|
||||||
import cn.wepact.dfm.training.utils.EmailUtil;
|
import cn.wepact.dfm.training.utils.EmailUtil;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@@ -19,51 +21,60 @@ import java.util.List;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("users")
|
@RequestMapping("users")
|
||||||
public class UserController extends BaseController{
|
public class UserController extends BaseController{
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(UserController.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
// 登录
|
// 登录
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public GeneralRespBean<TUser> login(@RequestBody TUser loginRequest) {
|
public GeneralRespBean<TUser> login(@RequestBody TUser loginRequest) {
|
||||||
|
log.info("【登录】用户: {}", loginRequest.getUsername());
|
||||||
GeneralRespBean<TUser> respBean = new GeneralRespBean();
|
GeneralRespBean<TUser> respBean = new GeneralRespBean();
|
||||||
TUser user = userService.loginAndGetUser(loginRequest);
|
TUser user = userService.loginAndGetUser(loginRequest);
|
||||||
String jsonString=null;
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
respBean.setData(user);
|
respBean.setData(user);
|
||||||
respBean.setMsg(Constant.SUCCESS_MSG);
|
respBean.setMsg(Constant.SUCCESS_MSG);
|
||||||
respBean.setCode(Constant.SUCCESS_CODE);
|
respBean.setCode(Constant.SUCCESS_CODE);
|
||||||
|
log.info("【登录】用户: {} 登录成功", loginRequest.getUsername());
|
||||||
} else {
|
} else {
|
||||||
respBean.setData(null);
|
respBean.setData(null);
|
||||||
respBean.setMsg("登录失败,账号密码错误,或单位选择有误!");
|
respBean.setMsg("登录失败,账号密码错误,或单位选择有误!");
|
||||||
respBean.setCode(Constant.ERROR_CODE);
|
respBean.setCode(Constant.ERROR_CODE);
|
||||||
|
log.warn("【登录】用户: {} 登录失败", loginRequest.getUsername());
|
||||||
}
|
}
|
||||||
|
|
||||||
return respBean;
|
return respBean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注册
|
// 注册
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public GeneralRespBean<String> register(@RequestBody TUser registerRequest) {
|
public GeneralRespBean<String> register(@RequestBody TUser registerRequest) {
|
||||||
|
log.info("【注册】用户: {}", registerRequest.getUsername());
|
||||||
GeneralRespBean<String> respBean = new GeneralRespBean();
|
GeneralRespBean<String> respBean = new GeneralRespBean();
|
||||||
Boolean result = userService.register(registerRequest);
|
Boolean result = userService.register(registerRequest);
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
respBean.setMsg(Constant.SUCCESS_MSG);
|
respBean.setMsg(Constant.SUCCESS_MSG);
|
||||||
respBean.setCode(Constant.SUCCESS_CODE);
|
respBean.setCode(Constant.SUCCESS_CODE);
|
||||||
|
log.info("【注册】用户: {} 注册成功", registerRequest.getUsername());
|
||||||
} else {
|
} else {
|
||||||
respBean.setMsg("注册失败,账号已经存在!");
|
respBean.setMsg("注册失败,账号已经存在!");
|
||||||
respBean.setCode(Constant.ERROR_CODE);
|
respBean.setCode(Constant.ERROR_CODE);
|
||||||
|
log.warn("【注册】用户: {} 注册失败,账号已存在", registerRequest.getUsername());
|
||||||
}
|
}
|
||||||
return respBean;
|
return respBean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/getDeptList")
|
@GetMapping("/getDeptList")
|
||||||
public GeneralRespBean<List<TOrg>> getDeptList() {
|
public GeneralRespBean<List<TOrg>> getDeptList() {
|
||||||
|
log.info("【获取部门列表】开始");
|
||||||
GeneralRespBean<List<TOrg>> respBean = new GeneralRespBean();
|
GeneralRespBean<List<TOrg>> respBean = new GeneralRespBean();
|
||||||
List<TOrg> tOrgList = userService.getDeptList();
|
List<TOrg> tOrgList = userService.getDeptList();
|
||||||
respBean.setData(tOrgList);
|
respBean.setData(tOrgList);
|
||||||
respBean.setMsg(Constant.SUCCESS_MSG);
|
respBean.setMsg(Constant.SUCCESS_MSG);
|
||||||
respBean.setCode(Constant.SUCCESS_CODE);
|
respBean.setCode(Constant.SUCCESS_CODE);
|
||||||
|
log.info("【获取部门列表】共 {} 条", tOrgList.size());
|
||||||
return respBean;
|
return respBean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,8 +83,9 @@ public class UserController extends BaseController{
|
|||||||
*/
|
*/
|
||||||
@PostMapping(value = "/captcha")
|
@PostMapping(value = "/captcha")
|
||||||
public GeneralRespBean<Boolean> captchacode(HttpSession session) throws Exception {
|
public GeneralRespBean<Boolean> captchacode(HttpSession session) throws Exception {
|
||||||
GeneralRespBean<Boolean> respBean = new GeneralRespBean<>();
|
|
||||||
TUser user = getUserInfo();
|
TUser user = getUserInfo();
|
||||||
|
log.info("【验证码】用户: {} 请求发送验证码至邮箱: {}", user.getUsername(), user.getEmail());
|
||||||
|
GeneralRespBean<Boolean> respBean = new GeneralRespBean<>();
|
||||||
String randomCode = userService.generateRandomCode();
|
String randomCode = userService.generateRandomCode();
|
||||||
session.setAttribute("randomCode", randomCode);
|
session.setAttribute("randomCode", randomCode);
|
||||||
session.setAttribute("codeTime",System.currentTimeMillis());
|
session.setAttribute("codeTime",System.currentTimeMillis());
|
||||||
@@ -85,6 +97,7 @@ public class UserController extends BaseController{
|
|||||||
EmailUtil.sendMail(email);
|
EmailUtil.sendMail(email);
|
||||||
respBean.setMsg("验证码发送成功!");
|
respBean.setMsg("验证码发送成功!");
|
||||||
respBean.setCode("200");
|
respBean.setCode("200");
|
||||||
|
log.info("【验证码】用户: {} 验证码发送成功", user.getUsername());
|
||||||
|
|
||||||
return respBean;
|
return respBean;
|
||||||
}
|
}
|
||||||
@@ -98,20 +111,26 @@ public class UserController extends BaseController{
|
|||||||
GeneralRespBean<Boolean> respBean = new GeneralRespBean<>();
|
GeneralRespBean<Boolean> respBean = new GeneralRespBean<>();
|
||||||
TUser user = getUserInfo();
|
TUser user = getUserInfo();
|
||||||
tUser.setUserNo(user.getUserNo());
|
tUser.setUserNo(user.getUserNo());
|
||||||
|
log.info("【修改密码】用户: {} 请求修改密码", user.getUsername());
|
||||||
|
|
||||||
// 获得验证码对象
|
// 获得验证码对象
|
||||||
Object cko = session.getAttribute("randomCode");
|
Object cko = session.getAttribute("randomCode");
|
||||||
if (cko == null) {
|
if (cko == null) {
|
||||||
|
log.warn("【修改密码】用户: {} 验证码已失效(空)", user.getUsername());
|
||||||
return respBean.processErrorMsg("验证码已失效,请重新获取!");
|
return respBean.processErrorMsg("验证码已失效,请重新获取!");
|
||||||
}
|
}
|
||||||
String captcha = cko.toString();
|
String captcha = cko.toString();
|
||||||
// 判断验证码输入是否正确
|
// 判断验证码输入是否正确
|
||||||
if (StringUtils.isEmpty(tUser.getRandomCode()) || captcha == null || !(tUser.getRandomCode().equalsIgnoreCase(captcha))) {
|
if (StringUtils.isEmpty(tUser.getRandomCode()) || captcha == null || !(tUser.getRandomCode().equalsIgnoreCase(captcha))) {
|
||||||
|
log.warn("【修改密码】用户: {} 验证码错误", user.getUsername());
|
||||||
return respBean.processErrorMsg("验证码错误,请重新输入!");
|
return respBean.processErrorMsg("验证码错误,请重新输入!");
|
||||||
} else {
|
} else {
|
||||||
Long codeTime = Long.valueOf(session.getAttribute("codeTime") + "");
|
Long codeTime = Long.valueOf(session.getAttribute("codeTime") + "");
|
||||||
if ((System.currentTimeMillis() - codeTime) / 1000 / 60 > 4) {
|
if ((System.currentTimeMillis() - codeTime) / 1000 / 60 > 4) {
|
||||||
|
log.warn("【修改密码】用户: {} 验证码已超时", user.getUsername());
|
||||||
return respBean.processErrorMsg("验证码已失效,请重新获取!");
|
return respBean.processErrorMsg("验证码已失效,请重新获取!");
|
||||||
}
|
}
|
||||||
|
log.info("【修改密码】用户: {} 验证码校验通过,执行修改", user.getUsername());
|
||||||
return userService.updatePwd(tUser);
|
return userService.updatePwd(tUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,16 +138,19 @@ public class UserController extends BaseController{
|
|||||||
// 取消预览
|
// 取消预览
|
||||||
@PostMapping("/cancelPreview")
|
@PostMapping("/cancelPreview")
|
||||||
public GeneralRespBean<Boolean> cancelPreview() {
|
public GeneralRespBean<Boolean> cancelPreview() {
|
||||||
GeneralRespBean<Boolean> respBean = new GeneralRespBean();
|
|
||||||
TUser user = getUserInfo();
|
TUser user = getUserInfo();
|
||||||
|
log.info("【取消预览】用户: {} 请求取消预览", user.getUsername());
|
||||||
|
GeneralRespBean<Boolean> respBean = new GeneralRespBean();
|
||||||
Boolean result = userService.cancelPreview(user);
|
Boolean result = userService.cancelPreview(user);
|
||||||
respBean.setData(result);
|
respBean.setData(result);
|
||||||
if (result) {
|
if (result) {
|
||||||
respBean.setMsg(Constant.SUCCESS_MSG);
|
respBean.setMsg(Constant.SUCCESS_MSG);
|
||||||
respBean.setCode(Constant.SUCCESS_CODE);
|
respBean.setCode(Constant.SUCCESS_CODE);
|
||||||
|
log.info("【取消预览】用户: {} 取消成功", user.getUsername());
|
||||||
} else {
|
} else {
|
||||||
respBean.setMsg("取消预览失败");
|
respBean.setMsg("取消预览失败");
|
||||||
respBean.setCode(Constant.ERROR_CODE);
|
respBean.setCode(Constant.ERROR_CODE);
|
||||||
|
log.warn("【取消预览】用户: {} 取消失败", user.getUsername());
|
||||||
}
|
}
|
||||||
|
|
||||||
return respBean;
|
return respBean;
|
||||||
|
|||||||
@@ -79,6 +79,9 @@ public class SyncUserInfoService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void fullSync(int offset, int limit, String searchStartTime, String searchEndTime) {
|
private void fullSync(int offset, int limit, String searchStartTime, String searchEndTime) {
|
||||||
|
int consecutiveFailures = 0;
|
||||||
|
final int MAX_FAILURES = 3; // 最大连续失败次数
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// 请求数据
|
// 请求数据
|
||||||
Map<String, Object> requestParams = new HashMap<>();
|
Map<String, Object> requestParams = new HashMap<>();
|
||||||
@@ -92,8 +95,25 @@ public class SyncUserInfoService {
|
|||||||
|
|
||||||
// 如果返回数据为空,则结束同步
|
// 如果返回数据为空,则结束同步
|
||||||
if (response == null || response.isEmpty()) {
|
if (response == null || response.isEmpty()) {
|
||||||
break;
|
consecutiveFailures++;
|
||||||
|
logger.warn("全量同步API返回空响应 - offset: {}, 连续失败次数: {}", offset, consecutiveFailures);
|
||||||
|
|
||||||
|
if (consecutiveFailures >= MAX_FAILURES) {
|
||||||
|
logger.error("全量同步连续失败 {} 次,终止同步", MAX_FAILURES);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// 短暂等待后重试
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 重置失败计数器
|
||||||
|
consecutiveFailures = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 将返回的数据解析为 JSONObject
|
// 将返回的数据解析为 JSONObject
|
||||||
@@ -101,19 +121,16 @@ public class SyncUserInfoService {
|
|||||||
|
|
||||||
// 检查返回的 "data" 是否为空
|
// 检查返回的 "data" 是否为空
|
||||||
if (responseJson.containsKey("data") && responseJson.getJSONArray("data").size() == 0) {
|
if (responseJson.containsKey("data") && responseJson.getJSONArray("data").size() == 0) {
|
||||||
System.out.println("Data is empty, exiting the loop.");
|
logger.info("全量同步完成,没有更多数据 - offset: {}", offset);
|
||||||
break; // 如果 "data" 为空,跳出循环
|
break; // 如果 "data" 为空,跳出循环
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在这里处理 "data" 非空的情况
|
|
||||||
// 你可以继续进行数据处理
|
|
||||||
// ...
|
|
||||||
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// 如果 JSON 解析出错,打印异常并跳出循环
|
// 如果 JSON 解析出错,打印异常并跳出循环
|
||||||
System.err.println("Error parsing JSON response: " + e.getMessage());
|
logger.error("全量同步JSON解析失败 - offset: {}, 错误: {}", offset, e.getMessage());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析返回数据并保存到数据库
|
// 解析返回数据并保存到数据库
|
||||||
saveSyncUserData(offset, limit, requestParams, response);
|
saveSyncUserData(offset, limit, requestParams, response);
|
||||||
|
|
||||||
@@ -131,6 +148,9 @@ public class SyncUserInfoService {
|
|||||||
// 增量同步,使用上次同步的时间点作为查询的起始时间
|
// 增量同步,使用上次同步的时间点作为查询的起始时间
|
||||||
String searchStartTime = lastSyncTime;
|
String searchStartTime = lastSyncTime;
|
||||||
String searchEndTime = getCurrentTime();
|
String searchEndTime = getCurrentTime();
|
||||||
|
|
||||||
|
int consecutiveFailures = 0;
|
||||||
|
final int MAX_FAILURES = 3; // 最大连续失败次数
|
||||||
|
|
||||||
// 请求数据
|
// 请求数据
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -144,8 +164,25 @@ public class SyncUserInfoService {
|
|||||||
String response = callApi(Incr_API_USER_URL, requestParams);
|
String response = callApi(Incr_API_USER_URL, requestParams);
|
||||||
// 如果返回数据为空,则结束同步
|
// 如果返回数据为空,则结束同步
|
||||||
if (response == null || response.isEmpty()) {
|
if (response == null || response.isEmpty()) {
|
||||||
break;
|
consecutiveFailures++;
|
||||||
|
logger.warn("增量同步API返回空响应 - offset: {}, 连续失败次数: {}", offset, consecutiveFailures);
|
||||||
|
|
||||||
|
if (consecutiveFailures >= MAX_FAILURES) {
|
||||||
|
logger.error("增量同步连续失败 {} 次,终止同步", MAX_FAILURES);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// 短暂等待后重试
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 重置失败计数器
|
||||||
|
consecutiveFailures = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 将返回的数据解析为 JSONObject
|
// 将返回的数据解析为 JSONObject
|
||||||
@@ -153,17 +190,13 @@ public class SyncUserInfoService {
|
|||||||
|
|
||||||
// 检查返回的 "data" 是否为空
|
// 检查返回的 "data" 是否为空
|
||||||
if (responseJson.containsKey("data") && responseJson.getJSONArray("data").size() == 0) {
|
if (responseJson.containsKey("data") && responseJson.getJSONArray("data").size() == 0) {
|
||||||
System.out.println("Data is empty, exiting the loop.");
|
logger.info("增量同步完成,没有更多数据 - offset: {}", offset);
|
||||||
break; // 如果 "data" 为空,跳出循环
|
break; // 如果 "data" 为空,跳出循环
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在这里处理 "data" 非空的情况
|
|
||||||
// 你可以继续进行数据处理
|
|
||||||
// ...
|
|
||||||
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// 如果 JSON 解析出错,打印异常并跳出循环
|
// 如果 JSON 解析出错,打印异常并跳出循环
|
||||||
System.err.println("Error parsing JSON response: " + e.getMessage());
|
logger.error("增量同步JSON解析失败 - offset: {}, 错误: {}", offset, e.getMessage());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// 解析返回数据并保存到数据库
|
// 解析返回数据并保存到数据库
|
||||||
@@ -179,7 +212,7 @@ public class SyncUserInfoService {
|
|||||||
private void saveSyncUserData(int offset, int limit, Map<String, Object> params, String responseData) {
|
private void saveSyncUserData(int offset, int limit, Map<String, Object> params, String responseData) {
|
||||||
// 检查入参
|
// 检查入参
|
||||||
if (responseData == null || responseData.isEmpty()) {
|
if (responseData == null || responseData.isEmpty()) {
|
||||||
logger.error("响应数据为空");
|
logger.error("响应数据为空 - offset: {}, limit: {}", offset, limit);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +222,8 @@ public class SyncUserInfoService {
|
|||||||
responseJson = new JSONObject(responseData);
|
responseJson = new JSONObject(responseData);
|
||||||
// 检查data字段是否存在且不为空
|
// 检查data字段是否存在且不为空
|
||||||
if (!responseJson.containsKey("data")) {
|
if (!responseJson.containsKey("data")) {
|
||||||
logger.error("响应数据中没有data字段");
|
// 记录完整响应内容用于排查问题
|
||||||
|
logger.error("响应数据中没有data字段 - offset: {}, limit: {}, 完整响应: {}", offset, limit, responseData);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,19 +391,33 @@ public class SyncUserInfoService {
|
|||||||
}
|
}
|
||||||
// 调用接口
|
// 调用接口
|
||||||
private String callApi(String apiUrl, Map<String, Object> params) {
|
private String callApi(String apiUrl, Map<String, Object> params) {
|
||||||
// 假设 accessToken 是从某个地方获取的,比如从配置文件、Session 或者前端传递
|
// 获取 accessToken
|
||||||
String accessToken = getAccessToken(); // 你可以根据自己的需求获取 token
|
String accessToken = getAccessToken();
|
||||||
|
|
||||||
|
// 检查Token有效性
|
||||||
|
if (accessToken == null || accessToken.isEmpty()) {
|
||||||
|
logger.error("获取AccessToken失败,无法调用API: {}", apiUrl);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// 调用 HttpClientUtil 的 doPost 方法
|
// 调用 HttpClientUtil 的 doPost 方法
|
||||||
String response = HttpClientUtil.doPost(apiUrl, params, accessToken);
|
String response = HttpClientUtil.doPost(apiUrl, params, accessToken);
|
||||||
|
|
||||||
// 如果需要,你可以进一步处理响应结果
|
// 如果需要,你可以进一步处理响应结果
|
||||||
if (response != null && !response.isEmpty()) {
|
if (response != null && !response.isEmpty()) {
|
||||||
// 处理响应数据,比如解析 JSON 或者记录日志
|
// 记录响应日志用于调试
|
||||||
return response;
|
logger.debug("API请求成功 - URL: {}, 参数: {}, 响应长度: {} 字符", apiUrl, params, response.length());
|
||||||
|
|
||||||
|
// 检查响应是否为有效JSON
|
||||||
|
if (response.trim().startsWith("{")) {
|
||||||
|
return response;
|
||||||
|
} else {
|
||||||
|
logger.error("API响应不是有效的JSON格式 - URL: {}, 响应: {}", apiUrl, response);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// 错误处理
|
// 错误处理
|
||||||
System.out.println("API 请求失败,未收到响应");
|
logger.error("API请求失败,未收到响应 - URL: {}, 参数: {}", apiUrl, params);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,12 +31,17 @@ public class HttpClientUtil {
|
|||||||
post.setEntity(entity);
|
post.setEntity(entity);
|
||||||
|
|
||||||
try (CloseableHttpResponse response = client.execute(post)) {
|
try (CloseableHttpResponse response = client.execute(post)) {
|
||||||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
result = EntityUtils.toString(response.getEntity(), "UTF-8");
|
result = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||||
// Log response for debugging
|
|
||||||
log.debug("POST Response from {}: {}", pathUrl, result);
|
// 记录状态码和响应长度
|
||||||
|
if (statusCode >= 400) {
|
||||||
|
log.warn("POST request returned error status - URL: {}, Status: {}, Response: {}", pathUrl, statusCode, result);
|
||||||
|
} else {
|
||||||
|
log.debug("POST Response from {} - Status: {}, Length: {} chars", pathUrl, statusCode, result != null ? result.length() : 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
|
||||||
log.error("POST request failed - URL: {}, Data: {}, Error: {}", pathUrl, dataMap, e.getMessage(), e);
|
log.error("POST request failed - URL: {}, Data: {}, Error: {}", pathUrl, dataMap, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -56,7 +61,8 @@ public class HttpClientUtil {
|
|||||||
urlBuilder.deleteCharAt(urlBuilder.length() - 1);
|
urlBuilder.deleteCharAt(urlBuilder.length() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpGet get = new HttpGet(urlBuilder.toString());
|
String finalUrl = urlBuilder.toString();
|
||||||
|
HttpGet get = new HttpGet(finalUrl);
|
||||||
get.setHeader("Content-Type", "application/json;charset=utf-8");
|
get.setHeader("Content-Type", "application/json;charset=utf-8");
|
||||||
|
|
||||||
if (accessToken != null && !accessToken.isEmpty()) {
|
if (accessToken != null && !accessToken.isEmpty()) {
|
||||||
@@ -64,12 +70,17 @@ public class HttpClientUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try (CloseableHttpResponse response = client.execute(get)) {
|
try (CloseableHttpResponse response = client.execute(get)) {
|
||||||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
result = EntityUtils.toString(response.getEntity(), "UTF-8");
|
result = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||||
// Log response for debugging
|
|
||||||
log.debug("GET Response from {}: {}", pathUrl, result);
|
// 记录状态码和响应长度
|
||||||
|
if (statusCode >= 400) {
|
||||||
|
log.warn("GET request returned error status - URL: {}, Status: {}, Response: {}", finalUrl, statusCode, result);
|
||||||
|
} else {
|
||||||
|
log.debug("GET Response from {} - Status: {}, Length: {} chars", finalUrl, statusCode, result != null ? result.length() : 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
|
||||||
log.error("GET request failed - URL: {}, Parameters: {}, Error: {}", pathUrl, dataMap, e.getMessage(), e);
|
log.error("GET request failed - URL: {}, Parameters: {}, Error: {}", pathUrl, dataMap, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ spring:
|
|||||||
username: root
|
username: root
|
||||||
password: 1qaz!WSX
|
password: 1qaz!WSX
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
url: jdbc:mysql://127.0.0.1:3308/qualification_db?serverTimezone=Asia/Shanghai&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false
|
url: jdbc:mysql://10.100.5.9:3308/qualification_db?serverTimezone=Asia/Shanghai&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false
|
||||||
filter:
|
filter:
|
||||||
wall:
|
wall:
|
||||||
enabled: true
|
enabled: true
|
||||||
@@ -43,14 +43,13 @@ api:
|
|||||||
url: http://10.87.0.127/api/portal-service/resume/getJobApplicationResumeInformation/{employeeNo}
|
url: http://10.87.0.127/api/portal-service/resume/getJobApplicationResumeInformation/{employeeNo}
|
||||||
getPcToken:
|
getPcToken:
|
||||||
url: http://10.87.0.127/api/authority-management-service/recruit/getPcToken
|
url: http://10.87.0.127/api/authority-management-service/recruit/getPcToken
|
||||||
All_API_URL: https://openapi-tf-ali-01.yunxuetang.com.cn/v1/rpt2open/public/o2o/task/student/sync/all
|
All_API_URL: https://openapi-tf-tc.yunxuetang.com.cn/v1/rpt2open/public/o2o/task/student/sync/all
|
||||||
Incr_API_URL: https://openapi-tf-ali-01.yunxuetang.com.cn/v1/rpt2open/public/o2o/task/student/sync/incr
|
Incr_API_URL: https://openapi-tf-tc.yunxuetang.com.cn/v1/rpt2open/public/o2o/task/student/sync/incr
|
||||||
All_API_USER_URL: https://openapi-tf-ali-01.yunxuetang.com.cn/v1/rpt2open/public/user/sync/all
|
All_API_USER_URL: https://openapi-tf-tc.yunxuetang.com.cn/v1/rpt2open/public/user/sync/all
|
||||||
Incr_API_USER_URL: https://openapi-tf-ali-01.yunxuetang.com.cn/v1/rpt2open/public/user/sync/incr
|
Incr_API_USER_URL: https://openapi-tf-tc.yunxuetang.com.cn/v1/rpt2open/public/user/sync/incr
|
||||||
getAPITokenUrl: https://openapi-tf-ali-01.yunxuetang.com.cn/token?appId=4asv2fqldt7&appSecret=M2E3NzkzODA5Mzc3
|
getAPITokenUrl: https://openapi-tf-tc.yunxuetang.com.cn/token?appId=9a3t7br53c5&appSecret=ZWQ4OTJkYzQzMjcz
|
||||||
dongfeng_project_url: https://openapi-tf-ali-01.yunxuetang.com.cn/v1/mix2/dongfeng/project/get
|
dongfeng_project_url: https://openapi-tf-tc.yunxuetang.com.cn/v1/mix2/dongfeng/project/get
|
||||||
project_add_member_url: https://openapi-tf-ali-01.yunxuetang.com.cn/v1/mix2/dongfeng/project/add/member
|
project_add_member_url: https://openapi-tf-tc.yunxuetang.com.cn/v1/mix2/dongfeng/project/add/member
|
||||||
|
|
||||||
logging:
|
logging:
|
||||||
# logback.xml配置文件的位置
|
# logback.xml配置文件的位置
|
||||||
config: classpath:logback-spring.xml
|
config: classpath:logback-spring.xml
|
||||||
|
|||||||
Reference in New Issue
Block a user