AdvisorServer/src/main/java/com/syzb/business/service/BusinessApiService.java

216 lines
9.1 KiB
Java
Raw Normal View History

2025-02-20 14:14:51 +08:00
package com.syzb.business.service;
2025-02-15 17:29:47 +08:00
2025-02-21 15:30:13 +08:00
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
2025-02-15 17:29:47 +08:00
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
2025-02-20 14:14:51 +08:00
import com.syzb.business.vo.*;
import com.syzb.common.handler.BizException;
import com.syzb.common.result.ResponseStatus;
import com.syzb.common.util.logger.LoggerUtil;
2025-02-15 17:29:47 +08:00
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
2025-02-21 15:30:13 +08:00
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
2025-02-15 17:29:47 +08:00
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// business信息Service
@Service
public class BusinessApiService {
private String host = "https://webdzapi.ceniu.sztg.com";
private String loginUrl = "/common/loginByUserName";
private String getUserUrl = "/common/getUserByToken";
2025-02-21 15:30:13 +08:00
private String getModuleListUrl = "/user/getUserModuleList";
private String getOrderListUrl = "/order/getAllBatchOrderListByPage";
2025-02-19 16:05:27 +08:00
2025-02-21 20:05:47 +08:00
private String getModuleUserListUrl = "/order/getUserModuleListByPage";
2025-02-15 17:29:47 +08:00
private String authorizationDeptId = "16";
private String authorizationKey = "56e670eea5ff3a1aebbc02820e908ceb56e670eea5ff3a1aebbc02820e908ceb";
// private String authorizationDeptId = "33";
//
// private String authorizationKey = "90804bd6089d950e8804c6d7cad7558a90804bd6089d950e8804c6d7cad7558a";
private static final int timeout = 10000;
private static final int SUCCESS_CODE = 200;
2025-02-19 16:05:27 +08:00
private static final int TOKEN_ERROR_CODE = 401;
2025-02-21 15:30:13 +08:00
public BusinessLoginVO login(String userName, String password) {
2025-02-15 17:29:47 +08:00
Map<String, Object> params = new HashMap<>();
params.put("username", userName);
params.put("password", sha512(password));
BusinessLoginRspVO vo = post(loginUrl, params, BusinessLoginRspVO.class);
return vo.getData();
}
2025-02-21 15:30:13 +08:00
public BusinessUserVO getUser(String token) {
2025-02-15 17:29:47 +08:00
Map<String, Object> params = new HashMap<>();
params.put("token", token);
BusinessUserRspVO vo = post(getUserUrl, params, BusinessUserRspVO.class);
return vo.getData();
}
2025-02-21 15:30:13 +08:00
public List<BusinessModuleVO> getModuleList(String userId) {
2025-02-19 16:05:27 +08:00
Map<String, Object> params = new HashMap<>();
params.put("userId", userId);
2025-02-21 15:30:13 +08:00
BusinessModuleRspVO vo = post(getModuleListUrl, params, BusinessModuleRspVO.class);
return vo.getData();
}
public List<BusinessOrderVO> getOrderList(LocalDateTime startTime, LocalDateTime endTime) {
List<BusinessOrderVO> result = new ArrayList<>();
2025-02-21 20:05:47 +08:00
String startTimeStr = LocalDateTimeUtil.format(startTime, DatePattern.NORM_DATETIME_FORMATTER);
String endTimeStr = LocalDateTimeUtil.format(endTime, DatePattern.NORM_DATETIME_FORMATTER);
2025-02-21 15:30:13 +08:00
Integer currentPage = 1;
Integer pageSize = 100;
while (true) {
BusinessOrderExtVO extVO = getOrderList(startTimeStr, endTimeStr, currentPage, pageSize);
2025-02-21 20:05:47 +08:00
if (extVO == null || extVO.getData() == null) {
break;
}
2025-02-21 15:30:13 +08:00
result.addAll(extVO.getData());
if (extVO.getData().size() < pageSize || result.size() >= extVO.getTotal()) {
break;
}
currentPage++;
}
return result;
}
2025-02-21 20:05:47 +08:00
private BusinessOrderExtVO getOrderList(String startTime, String endTime, Integer currentPage, Integer pageSize) {
2025-02-21 15:30:13 +08:00
Map<String, Object> params = new HashMap<>();
params.put("currentPage", currentPage);
params.put("pageSize", pageSize);
params.put("updateStartTime", startTime);
params.put("updateEndTime", endTime);
BusinessOrderRspVO vo = post(getOrderListUrl, params, BusinessOrderRspVO.class);
2025-02-19 16:05:27 +08:00
return vo.getData();
}
2025-02-21 20:05:47 +08:00
public List<BusinessModuleUserVO> getModuleUserList(Integer moduleId, LocalDateTime startTime, LocalDateTime endTime) {
List<BusinessModuleUserVO> result = new ArrayList<>();
String startTimeStr = LocalDateTimeUtil.format(startTime, DatePattern.NORM_DATETIME_FORMATTER);
String endTimeStr = LocalDateTimeUtil.format(endTime, DatePattern.NORM_DATETIME_FORMATTER);
Integer currentPage = 1;
Integer pageSize = 100;
while (true) {
BusinessModuleUserExtVO extVO = getModuleUserList(moduleId, startTimeStr, endTimeStr, currentPage, pageSize);
if (extVO == null || extVO.getData() == null) {
break;
}
result.addAll(extVO.getData());
if (extVO.getData().size() < pageSize || result.size() >= extVO.getTotal()) {
break;
}
currentPage++;
}
return result;
}
private BusinessModuleUserExtVO getModuleUserList(Integer moduleId, String startTime, String endTime, Integer currentPage, Integer pageSize) {
Map<String, Object> params = new HashMap<>();
params.put("moduleId", moduleId);
params.put("currentPage", currentPage);
params.put("pageSize", pageSize);
params.put("updateStartTime", startTime);
params.put("updateEndTime", endTime);
BusinessModuleUserRspVO vo = post(getModuleUserListUrl, params, BusinessModuleUserRspVO.class);
return vo.getData();
}
2025-02-15 17:29:47 +08:00
public static void main(String[] args) {
BusinessApiService service = new BusinessApiService();
2025-02-19 17:53:25 +08:00
// BusinessLoginVO loginVO = service.loginByUserName("sz545138", "Abc@123");
// String token = loginVO.getToken();
2025-02-21 15:30:13 +08:00
// String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1NDUxMzgiLCJpYXQiOjE3Mzk5NTcxMjYsImV4cCI6MTczOTk2MDcyNn0.02FnOeW7RnIeI7sqwTTw0H9NPj03iFRAkr9PPXwpaDMgRTK2v7krDqAYa6dSIkuGx8wnKWPp-kcA4Ajr_MLjdw";
// BusinessUserVO userVO = service.getUser(token);
// System.out.println(JSONUtil.toJsonStr(userVO));
// service.getModuleList(userVO.getUserId().toString());
2025-02-21 20:05:47 +08:00
// List<BusinessOrderVO> orderList = service.getOrderList(LocalDate.now().withDayOfYear(1).atStartOfDay(), LocalDateTime.now());
// System.out.println(JSONUtil.toJsonStr(orderList));
List<BusinessModuleUserVO> moduleUserList = service.getModuleUserList(1000, LocalDate.now().withDayOfYear(1).atStartOfDay(), LocalDateTime.now());
System.out.println(JSONUtil.toJsonStr(moduleUserList));
2025-02-15 17:29:47 +08:00
}
private <T extends BusinessVO> T post(String url, Map<String, Object> params, Class<T> type) {
T vo;
try {
if (params == null) {
params = new HashMap<>();
}
String body = JSONUtil.toJsonStr(params);
Map<String, List<String>> header = buildHeader();
LoggerUtil.api.info("url:" + url + ",body:" + body, ",header:" + JSONUtil.toJsonStr(header));
String response = HttpRequest.post(host + url)
.body(body, "application/json")
.header(header, true)
.timeout(timeout)//超时,毫秒
.execute().body();
LoggerUtil.api.info("response:" + response);
vo = JSONUtil.toBean(response, type);
LoggerUtil.api.info("vo:" + JSONUtil.toJsonStr(vo));
2025-02-19 16:05:27 +08:00
if (vo.getCode() == null) {
throw new BizException(ResponseStatus.OUTSYS_ERROR, "协议异常:" + vo.getMessage());
} else if (vo.getCode() == SUCCESS_CODE) {
return vo;
} else if (vo.getCode() == TOKEN_ERROR_CODE) {
throw new BizException(ResponseStatus.SESSION_EXPIRY);
} else {
2025-02-15 17:29:47 +08:00
throw new BizException(ResponseStatus.OUTSYS_ERROR, vo.getCode() + ":" + vo.getMessage());
}
} catch (BizException e) {
throw e;
} catch (Exception e) {
throw new BizException(ResponseStatus.OUTSYS_ERROR, e);
}
}
private String encodeSign(String time) {
return SecureUtil.md5(authorizationKey + authorizationDeptId + time);
}
private static String sha512(String str) {
try {
// 获取SHA-512算法的MessageDigest实例
MessageDigest digest = MessageDigest.getInstance("SHA-512");
// 将输入字符串转换为字节数组并计算哈希值
byte[] hashBytes = digest.digest(str.getBytes(StandardCharsets.UTF_8));
// 将字节数组转换为十六进制字符串
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
private Map<String, List<String>> buildHeader() {
Map<String, List<String>> header = new HashMap<>();
String time = String.valueOf(System.currentTimeMillis());
header.put("Authorization-Dept-Id", List.of(authorizationDeptId));
header.put("Authorization-Sign-Time", List.of(time));
header.put("Authorization-Sign", List.of(encodeSign(time)));
return header;
}
}