2025-06-05 08:47:33 +00:00
|
|
|
|
package com.ruoyi.webApi;
|
|
|
|
|
|
2025-06-13 06:44:33 +00:00
|
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
2025-06-05 08:47:33 +00:00
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
2025-06-13 06:44:33 +00:00
|
|
|
|
import com.ruoyi.bill.domain.Poundappli;
|
|
|
|
|
import com.ruoyi.bill.service.IPoundappliService;
|
2025-06-13 02:46:08 +00:00
|
|
|
|
import com.ruoyi.common.core.domain.entity.SysDictData;
|
2025-06-05 08:47:33 +00:00
|
|
|
|
import com.ruoyi.system.service.ISysConfigService;
|
2025-06-13 02:46:08 +00:00
|
|
|
|
import com.ruoyi.system.service.ISysDictTypeService;
|
2025-06-05 08:47:33 +00:00
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.OutputStream;
|
2025-06-13 06:44:33 +00:00
|
|
|
|
import java.math.BigDecimal;
|
2025-06-05 08:47:33 +00:00
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
2025-06-13 06:44:33 +00:00
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.Date;
|
2025-06-13 02:46:08 +00:00
|
|
|
|
import java.util.List;
|
2025-06-13 09:29:37 +00:00
|
|
|
|
import java.util.UUID;
|
2025-06-05 08:47:33 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author 16358
|
|
|
|
|
* @date 2025/6/5
|
|
|
|
|
*/
|
|
|
|
|
@Component("apiTask")
|
|
|
|
|
public class ApiTask {
|
|
|
|
|
@Autowired
|
|
|
|
|
private ISysConfigService sysConfigService;
|
2025-06-13 02:46:08 +00:00
|
|
|
|
@Autowired
|
|
|
|
|
private ISysDictTypeService dictTypeService;
|
2025-06-13 06:44:33 +00:00
|
|
|
|
@Autowired
|
|
|
|
|
private IPoundappliService poundappliService;
|
2025-06-05 08:47:33 +00:00
|
|
|
|
|
|
|
|
|
//登录星空接口
|
|
|
|
|
public String OAlogin(){
|
|
|
|
|
// 目标URL
|
|
|
|
|
String url = sysConfigService.selectConfigByKey("OA_Url")+"/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";
|
|
|
|
|
// 请求体,JSON格式
|
|
|
|
|
String jsonInputString = sysConfigService.selectConfigByKey("login_JSON");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 创建URL对象
|
|
|
|
|
URL apiUrl = new URL(url);
|
|
|
|
|
// 打开连接
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
|
|
|
|
|
|
|
|
|
|
// 设置请求方法为POST
|
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
|
|
|
|
|
|
// 设置请求头
|
|
|
|
|
// Content-Type 指定发送的数据格式是JSON,并且字符集为UTF-8
|
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json; utf-8");
|
|
|
|
|
// Accept 指定期望接收的数据格式是JSON
|
|
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
|
|
|
|
|
|
|
// 允许写入请求体
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
// 获取输出流,发送请求体数据
|
|
|
|
|
try (OutputStream os = connection.getOutputStream()) {
|
|
|
|
|
byte[] input = jsonInputString.getBytes("utf-8");
|
|
|
|
|
os.write(input, 0, input.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取响应码
|
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
|
|
// 读取响应内容
|
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
|
|
|
try (BufferedReader br = new BufferedReader(
|
|
|
|
|
new InputStreamReader(connection.getInputStream(), "utf-8"))) {
|
|
|
|
|
String responseLine = null;
|
|
|
|
|
while ((responseLine = br.readLine()) != null) {
|
|
|
|
|
response.append(responseLine.trim());
|
|
|
|
|
}
|
|
|
|
|
return response.toString();
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
throw new RuntimeException("因未知原因导致登录OA系统失败!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判断是否登录成功并返回token
|
|
|
|
|
public String getToken(){
|
|
|
|
|
//触发登录接口
|
|
|
|
|
String response = OAlogin();
|
|
|
|
|
|
|
|
|
|
// 解析响应体为 JSONObject
|
|
|
|
|
JSONObject jsonResponse = JSONObject.parseObject(response.toString());
|
|
|
|
|
// 提取 "Message" 字段
|
|
|
|
|
if (jsonResponse != null && !jsonResponse.containsKey("")) {
|
|
|
|
|
String loginResultType = jsonResponse.getString("LoginResultType");
|
|
|
|
|
if("1".equals(loginResultType)){
|
|
|
|
|
return jsonResponse.getString("KDSVCSessionId");
|
|
|
|
|
}else {
|
|
|
|
|
throw new RuntimeException(jsonResponse.getString("Message"));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("返回参数解析错误!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//测试任务
|
|
|
|
|
public void testTAS(){
|
|
|
|
|
String token = getToken();
|
|
|
|
|
System.out.println(token);
|
|
|
|
|
}
|
2025-06-13 02:46:08 +00:00
|
|
|
|
|
|
|
|
|
//定时任务拉取星空数据(发货通知单,收料通知单,调拨申请单,简单生产入库)
|
|
|
|
|
public void getOAData(){
|
|
|
|
|
//获取各个单据的单据id
|
|
|
|
|
List<SysDictData> oaFormid = dictTypeService.selectDictDataByType("oa_formid");
|
|
|
|
|
|
|
|
|
|
//记录错误日志
|
|
|
|
|
int errorCount = 1;
|
|
|
|
|
StringBuilder errorLog = new StringBuilder();
|
|
|
|
|
//循环调用接口 获取数据
|
|
|
|
|
try {
|
|
|
|
|
for (SysDictData sysDictData : oaFormid) {
|
|
|
|
|
String dictValue = sysDictData.getDictValue();
|
|
|
|
|
String oaData = getOAData(dictValue);
|
|
|
|
|
if (oaData != null && !oaData.isEmpty()){
|
|
|
|
|
if (oaData.contains("ErrorCode")){
|
|
|
|
|
errorLog.append(errorCount++ + "、"+ "获取"+ sysDictData.getDictLabel() + "数据失败!:" + oaData + "\n");
|
|
|
|
|
}else{
|
|
|
|
|
//保存数据
|
|
|
|
|
switch (dictValue){
|
|
|
|
|
case "SAL_DELIVERYNOTICE":
|
|
|
|
|
//保存数据
|
2025-06-13 06:44:33 +00:00
|
|
|
|
makePoundAppliFormData(dictValue,oaData);
|
2025-06-13 02:46:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case "PUR_ReceiveBill":
|
|
|
|
|
//保存数据
|
2025-06-13 06:44:33 +00:00
|
|
|
|
makePoundAppliFormData(dictValue,oaData);
|
2025-06-13 02:46:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case "STK_TRANSFERAPPLY":
|
|
|
|
|
//保存数据
|
2025-06-13 06:44:33 +00:00
|
|
|
|
makeMainPoundFormData(dictValue,oaData);
|
2025-06-13 02:46:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case "SP_InStock":
|
|
|
|
|
//保存数据
|
2025-06-13 06:44:33 +00:00
|
|
|
|
makeMainPoundFormData(dictValue,oaData);
|
2025-06-13 02:46:08 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
errorLog.append(errorCount++ + "、"+ "获取数据失败!: 因未知错误导致返回参数为空!" + "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
errorLog.append(errorCount++ + "、"+ "获取"+ sysDictData.getDictLabel() + "数据失败!: 因未知错误导致返回参数为空!" + "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
errorLog.append(errorCount++ + "、"+ "获取数据失败!: " + e.getMessage() + "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errorCount > 1){
|
|
|
|
|
throw new RuntimeException(errorLog.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//调用OA系统接口拉取各个单据的数据
|
|
|
|
|
public String getOAData(String dictValue){
|
|
|
|
|
// 目标URL
|
|
|
|
|
String url = sysConfigService.selectConfigByKey("OA_Url")+"/k3cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc";
|
|
|
|
|
|
|
|
|
|
//记录错误日志
|
|
|
|
|
StringBuilder errorLog = new StringBuilder();
|
|
|
|
|
try {
|
|
|
|
|
// 构建请求体,包含参数
|
|
|
|
|
String jsonInputString = sysConfigService.selectConfigByKey(dictValue+"_raw");
|
|
|
|
|
// 创建URL对象
|
|
|
|
|
URL apiUrl = new URL(url);
|
|
|
|
|
// 打开连接
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
|
|
|
|
|
|
|
|
|
|
// 设置请求方法为POST
|
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
|
|
|
|
|
|
// 设置请求头
|
|
|
|
|
// Content-Type 指定发送的数据格式是JSON,并且字符集为UTF-8
|
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json; utf-8");
|
|
|
|
|
// Accept 指定期望接收的数据格式是JSON
|
|
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
|
|
// 设置请求头,包含token
|
|
|
|
|
String token = getToken();
|
|
|
|
|
if (token != null && !token.isEmpty()) {
|
|
|
|
|
connection.setRequestProperty("kdservice-sessionid", token);
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("获取到的Token为空,无法设置请求头。");
|
|
|
|
|
}
|
|
|
|
|
// 允许写入请求体
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
// 获取输出流,发送请求体数据
|
|
|
|
|
try (OutputStream os = connection.getOutputStream()) {
|
|
|
|
|
byte[] input = jsonInputString.getBytes("utf-8");
|
|
|
|
|
os.write(input, 0, input.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取响应码
|
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
|
|
// 读取响应内容
|
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
|
|
|
try (BufferedReader br = new BufferedReader(
|
|
|
|
|
new InputStreamReader(connection.getInputStream(), "utf-8"))) {
|
|
|
|
|
String responseLine = null;
|
|
|
|
|
while ((responseLine = br.readLine()) != null) {
|
|
|
|
|
response.append(responseLine.trim());
|
|
|
|
|
}
|
|
|
|
|
return response.toString();
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
// throw new RuntimeException("因未知原因导致获取数据失败!");
|
|
|
|
|
errorLog.append("因未知原因导致获取数据失败!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// throw new RuntimeException(e);
|
|
|
|
|
errorLog.append("获取数据失败!: " + e.getMessage()+ "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errorLog.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 06:44:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* 保存星空数据为中台<过磅申请>数据
|
|
|
|
|
* @param formid 星空单据id
|
|
|
|
|
* @param oaData 星空数据
|
|
|
|
|
*/
|
|
|
|
|
public void makePoundAppliFormData(String formid,String oaData){
|
|
|
|
|
// 1. 将 oaData 解析为 JSONArray(二维数组)
|
|
|
|
|
JSONArray dataList = JSONArray.parseArray(oaData);
|
|
|
|
|
|
|
|
|
|
// 2. 遍历每一行数据
|
|
|
|
|
for (int i = 0; i < dataList.size(); i++) {
|
|
|
|
|
JSONArray row = dataList.getJSONArray(i);
|
|
|
|
|
String dateStr = row.getString(0); // 申请日期
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
|
|
|
|
LocalDateTime localDateTime = LocalDateTime.parse(dateStr, formatter);
|
|
|
|
|
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
|
|
String billNo = row.getString(1); // 申请单号
|
|
|
|
|
String carno = row.getString(2); // 车号
|
|
|
|
|
String company = row.getString(3); // 发货单位
|
|
|
|
|
String materialName = row.getString(4); // 物料名称
|
|
|
|
|
String specification = row.getString(5); // 物料规格
|
|
|
|
|
Double weight = row.getDouble(6); // 计划重量
|
|
|
|
|
// Double F_JLHCSL = row.getDouble(7); // 计划回传重量(中台回传)
|
|
|
|
|
// String F_YMZ = row.getString(8); // 原毛重(中台回传)
|
|
|
|
|
// String F_YPZ = row.getString(9); // 原皮重(中台回传)
|
|
|
|
|
// Integer F_JS = row.getInteger(10); // 件数(中台回传)
|
|
|
|
|
// Integer F_GMSHDW = row.getInteger(11); // 国贸收货单位(中台回传)
|
|
|
|
|
// Integer F_CYDW = row.getInteger(12); // 承运单位(中台回传)
|
|
|
|
|
// Integer F_BDMC = row.getInteger(13); // 磅点名称(中台回传)
|
|
|
|
|
String materialNumber = row.getString(14); // 物料编码
|
|
|
|
|
String fid = row.getString(15); // 单据头内码
|
|
|
|
|
String fentryid = row.getString(16); // 单据体内码
|
|
|
|
|
String sourceBillNo = row.getString(17); // 来源单号
|
|
|
|
|
|
|
|
|
|
// 3. 保存过磅申请
|
|
|
|
|
Poundappli poundappli = new Poundappli();
|
2025-06-13 09:29:37 +00:00
|
|
|
|
poundappli.setId(UUID.randomUUID().toString());
|
2025-06-13 08:53:45 +00:00
|
|
|
|
poundappli.setBllstate("1");//过磅状态
|
2025-06-13 06:44:33 +00:00
|
|
|
|
poundappli.setBsndt(date);
|
|
|
|
|
poundappli.setUsrcode(billNo);
|
|
|
|
|
poundappli.setCarno(carno);
|
2025-06-13 08:53:45 +00:00
|
|
|
|
poundappli.setSendunitname(company);
|
2025-06-13 06:44:33 +00:00
|
|
|
|
poundappli.setItmname(materialName);
|
|
|
|
|
poundappli.setSpecification(specification);
|
|
|
|
|
poundappli.setSrccleanmqty(new BigDecimal(weight));
|
|
|
|
|
poundappli.setItmno(materialNumber);
|
|
|
|
|
poundappli.setFid(fid);
|
|
|
|
|
poundappli.setFentity_fentryid(fentryid);
|
|
|
|
|
poundappli.setSrcbillusrcode(sourceBillNo);
|
|
|
|
|
poundappli.setOabilltype(formid);//星空单据类型
|
|
|
|
|
poundappli.setSrcbilltype("发货通知单");//来源单据类型
|
2025-06-13 08:53:45 +00:00
|
|
|
|
|
2025-06-13 09:29:37 +00:00
|
|
|
|
poundappliService.insertPoundappliDpi(poundappli);
|
2025-06-13 06:44:33 +00:00
|
|
|
|
}
|
2025-06-13 02:46:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 06:44:33 +00:00
|
|
|
|
/**保存星空数据为中台<主榜单>数据
|
|
|
|
|
* @param formid 星空单据id
|
|
|
|
|
* @param oaData 星空数据
|
|
|
|
|
*/
|
|
|
|
|
public void makeMainPoundFormData(String formid,String oaData){
|
2025-06-13 02:46:08 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-05 08:47:33 +00:00
|
|
|
|
}
|