This commit is contained in:
2025-03-09 16:22:54 +08:00
parent d335612b2f
commit 82f85233aa
13 changed files with 285 additions and 11 deletions

View File

@@ -0,0 +1,167 @@
package com.ruoyi.oa.utils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.oa.domain.vo.CalcResultVo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class OwnHttpUtils {
public static String getJsonName(String monthStr, String filePath) {
// 目标接口地址
String jsonBody = String.format("{\"month_str\":\"%s\",\"file_path\":\"%s\"}", monthStr, filePath);
return getResult("dakaji_fenxi",jsonBody);
}
public static CalcResultVo getBaseCalc(String fileName, String personName) {
// 目标接口地址
String jsonBody = String.format("{\"file_name\":\"%s\",\"person_name\":\"%s\"}", fileName, personName);
jsonBody = getResult("get_person_info",jsonBody);
System.out.println(jsonBody);
// 1. 将 JSON 字符串解析成 JSONObject
JSONObject jsonObject = JSON.parseObject(jsonBody);
CalcResultVo calcResultVo = new CalcResultVo();
// 2. 获取数值类型
Long lateCount = jsonObject.getLong("迟到次数");
calcResultVo.setLateCount(lateCount);
Long leaveEarly10Count = jsonObject.getLong("早退10分钟次数");
Long leaveEarly30Count = jsonObject.getLong("早退30分钟次数");
Long absentCount = jsonObject.getLong("旷工次数");
Long absent05Count = jsonObject.getLong("旷工半天次数");
Long notAttendance = jsonObject.getLong("未打卡次数");
// 3. 获取数组类型(列表),使用 getJSONArray 再转成 Java 的 List 或数组
JSONArray lateDatesArr = jsonObject.getJSONArray("迟到日期");
if (lateDatesArr != null) {
// 如果想要 List<String> 形式,可以这样:
List<String> lateDates = lateDatesArr.toJavaList(String.class);
calcResultVo.setLateDates(lateDates);
}
// 3. 获取数组类型(列表),使用 getJSONArray 再转成 Java 的 List 或数组
JSONArray leaveEarly10Dates = jsonObject.getJSONArray("早退10分钟日期");
if (leaveEarly10Dates != null) {
// 如果想要 List<String> 形式,可以这样:
List<String> lateDates = leaveEarly10Dates.toJavaList(String.class);
calcResultVo.setLeaveEarly10Dates(lateDates);
}
// 3. 获取数组类型(列表),使用 getJSONArray 再转成 Java 的 List 或数组
JSONArray leaveEarly30Dates = jsonObject.getJSONArray("早退30分钟日期");
if (leaveEarly30Dates != null) {
// 如果想要 List<String> 形式,可以这样:
List<String> lateDates = leaveEarly30Dates.toJavaList(String.class);
calcResultVo.setLeaveEarly30Dates(lateDates);
}
// 3. 获取数组类型(列表),使用 getJSONArray 再转成 Java 的 List 或数组
JSONArray absentDates = jsonObject.getJSONArray("旷工日期");
if (absentDates != null) {
// 如果想要 List<String> 形式,可以这样:
List<String> lateDates = absentDates.toJavaList(String.class);
calcResultVo.setAbsentDates(lateDates);
}
// 3. 获取数组类型(列表),使用 getJSONArray 再转成 Java 的 List 或数组
JSONArray absent05Dates = jsonObject.getJSONArray("旷工半天日期");
if (absent05Dates != null) {
// 如果想要 List<String> 形式,可以这样:
List<String> lateDates = absent05Dates.toJavaList(String.class);
calcResultVo.setAbsent05Dates(lateDates);
}
// 3. 获取数组类型(列表),使用 getJSONArray 再转成 Java 的 List 或数组
JSONArray notAttendanceDates = jsonObject.getJSONArray("未打卡日期");
if (notAttendanceDates != null) {
// 如果想要 List<String> 形式,可以这样:
List<String> lateDates = notAttendanceDates.toJavaList(String.class);
calcResultVo.setNotAttendanceDates(lateDates);
}
// 例如获取 迟到罚金
Long lateFee = jsonObject.getLong("迟到罚金");
Long leaveEarlyFee = jsonObject.getLong("早退罚金");
Long notAttendanceFee = jsonObject.getLong("打卡罚金");
calcResultVo.setLateCount(lateCount);
calcResultVo.setLeaveEarly10Count(leaveEarly10Count);
calcResultVo.setLeaveEarly30Count(leaveEarly30Count);
calcResultVo.setAbsentCount(absentCount);
calcResultVo.setAbsent05Count(absent05Count);
calcResultVo.setNotAttendance(notAttendance);
calcResultVo.setLateFee(lateFee);
calcResultVo.setLeaveEarlyFee(leaveEarlyFee);
calcResultVo.setNotAttendanceFee(notAttendanceFee);
return calcResultVo;
}
public static void main(String[] args) {
String fileName = "/home/ubuntu/lyq/fad_oa_kqdeal/202411.json";
String personName = "丁苗松";
String jsonBody = String.format("{\"file_name\":\"%s\",\"person_name\":\"%s\"}", fileName, personName);
}
private static String getResult(String apiName, String jsonBody) {
try {
String baseUrl = "http://49.232.154.205:23108/";
// 3. 打开连接
URL url = new URL(baseUrl+apiName);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 4. 设置请求方式为 POST
connection.setRequestMethod("POST");
// 5. 设置请求头 Content-Type: application/json
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
// 允许写输出流
connection.setDoOutput(true);
// 6. 通过输出流发送 JSON 请求体
try (OutputStream os = connection.getOutputStream()) {
byte[] input = jsonBody.getBytes("UTF-8");
os.write(input, 0, input.length);
}
// 7. 获取响应码
int responseCode = connection.getResponseCode();
System.out.println("Response Code : " + responseCode);
// 8. 如果响应成功(200),读取响应体
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine);
}
connection.disconnect();
return response.toString();
}
} else {
System.out.println("Request failed. Response code: " + responseCode);
connection.disconnect();
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}