yixindibang/measurement/src/main/java/com/ruoyi/webApi/ApiPostBack.java

233 lines
11 KiB
Java
Raw Normal View History

2025-06-17 09:33:30 +00:00
package com.ruoyi.webApi;
import com.ruoyi.bill.domain.PoundBill;
import com.ruoyi.bill.domain.Poundappli;
import com.ruoyi.bill.service.IPoundBillService;
import com.ruoyi.bill.service.IPoundappliService;
import com.ruoyi.system.service.ISysConfigService;
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;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 各个单据回传星空接口单据更新单据下推
* @author 16358
* @date 2025/6/16
*/
@Component("apiPostBack")
public class ApiPostBack {
@Autowired
private ISysConfigService sysConfigService;
private ApiTask apiTask;
private IPoundBillService poundBillService;
/**
* 磅单信息对象更新回星空
* @return 回传成功报文/错误日志信息
*/
public String makePoundBillFormData(String id){
PoundBill poundBill = poundBillService.selectPoundBillById(id);
if(poundBill == null) throw new RuntimeException("磅单不存在!");
// 目标URL
String url = sysConfigService.selectConfigByKey("OA_Url")+"/k3cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.save.common.kdsvc";
String jsonInputString = "";
String oabilltype = poundBill.getSrcblltype();
if("SAL_DELIVERYNOTICE".equals(oabilltype)){
//发货通知单
jsonInputString = "{\n" +
" \"Formid\": \"SAL_DELIVERYNOTICE\",\n" +
" \"data\": {\n" +
" \"NeedUpDateFields\": [],\n" +
" \"NeedReturnFields\": [],\n" +
" \"IsDeleteEntry\": \"true\",\n" +
" \"IsAutoAdjustField\": \"true\",\n" +
" \"Model\": {\n" +
" \"FID\": \""+poundBill.getFid()+"\",\n" +
" \"F_GMSHDW\": \""+poundBill.getRecunitid()+"\",\n" +
" \"F_CYDW\": \""+poundBill.getTrnunitid()+"\",\n" +
" \"F_BDMC\":\""+poundBill.getPoundname()+" \",\n" +
" \"FEntity\": [\n" +
" {\n" +
" \"FEntryID\": "+poundBill.getFentity_fentryid()+",\n" +
" \"F_JLHCSL\": \""+poundBill.getNetmqty()+"\",\n" +
" \"F_YMZ\": \""+poundBill.getSrcwghmqty()+"\",\n" +
" \"F_YPZ\": \""+poundBill.getSrcempmqty()+"\",\n" +
" \"F_JS\": \""+poundBill.getNetmqty()+"\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"}";
}else if("PUR_ReceiveBill".equals(oabilltype)){
//收料通知单
jsonInputString = "{\n" +
" \"Formid\": \"PUR_ReceiveBill\",\n" +
" \"data\": {\n" +
" \"NeedUpDateFields\": [],\n" +
" \"NeedReturnFields\": [],\n" +
" \"IsDeleteEntry\": \"true\",\n" +
" \"IsAutoAdjustField\": \"true\",\n" +
" \"Model\": {\n" +
" \"FID\": \""+poundBill.getFid()+"\",\n" +
" \"F_GMSHDW\": \""+poundBill.getRecunitid()+"\",\n" +
" \"F_CYDW\": \""+poundBill.getTrnunitid()+"\",\n" +
" \"F_BDMC\":\""+poundBill.getPoundname()+" \",\n" +
" \"FDetailEntity\": [\n" +
" {\n" +
" \"FEntryID\": "+poundBill.getFentity_fentryid()+",\n" +
" \"F_JLHCSL\": \""+poundBill.getNetmqty()+"\",\n" +
" \"F_YMZ\": \""+poundBill.getSrcwghmqty()+"\",\n" +
" \"F_YPZ\": \""+poundBill.getSrcempmqty()+"\",\n" +
" \"F_JS\": \""+poundBill.getNetmqty()+"\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"}";
}else if("STK_TRANSFERAPPLY".equals(oabilltype)){
//调拨申请单
jsonInputString = "{\n" +
" \"Formid\": \"STK_TRANSFERAPPLY\",\n" +
" \"data\": {\n" +
" \"NeedUpDateFields\": [],\n" +
" \"NeedReturnFields\": [],\n" +
" \"IsDeleteEntry\": \"true\",\n" +
" \"IsAutoAdjustField\": \"true\",\n" +
" \"Model\": {\n" +
" \"FID\": \""+poundBill.getFid()+"\",\n" +
" \"F_DYCS\": "+poundBill.getPrintcount()+", \n" +
" \"F_CYDW\": \""+poundBill.getTrnunitid()+"\", \n" +
" \"F_FHDW\": \""+poundBill.getSendunitid()+"\", \n" +
" \"F_SHDW\": \""+poundBill.getRecunitid()+"\", \n" +
" \"F_ZGS\": "+poundBill.getZgsId()+", \n" +
" \"FEntity\": [\n" +
" {\n" +
" \"FEntryID\": "+poundBill.getFentity_fentryid()+",\n" +
" \"F_JLHCSL\": \""+poundBill.getNetmqty()+"\" \n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"}";
}else if("SP_InStock".equals(oabilltype)){
//简单生产入库单
jsonInputString = "{\n" +
" \"Formid\": \"SP_InStock\",\n" +
" \"data\": {\n" +
" \"NeedUpDateFields\": [],\n" +
" \"NeedReturnFields\": [],\n" +
" \"IsDeleteEntry\": \"true\",\n" +
" \"IsAutoAdjustField\": \"true\",\n" +
" \"Model\": {\n" +
" \"FID\": \""+poundBill.getFid()+"\",\n" +
" \"F_YWBD\": \""+poundBill.getPoundid()+"\",\n" +
" \"F_SJYWBD\": \""+poundBill.getPoundid()+"\",\n" +
" \"F_ZGS\": "+poundBill.getZgsId()+", \n" +
" \"F_FHDW\": \""+poundBill.getSendunitid()+"\", \n" +
" \"F_SHDW\": \""+poundBill.getRecunitid()+"\", \n" +
" \"FEntity\": [\n" +
" {\n" +
" \"FEntryID\": "+poundBill.getFentity_fentryid()+",\n" +
" \"FMaterialId\": {\n" +
" \"FNumber\": \"\"\n" +
" },\n" +
" \"FInStockType\": \"1\",\n" +
" \"FMustQty\": "+poundBill.getNetmqty()+",\n" +
" \"FRealQty\": "+poundBill.getNetmqty()+",\n" +
" \"FStockId\": {\n" +
" \"FNumber\": \"\"\n" +
" },\n" +
" \"FWorkShopId1\": {\n" +
" \"FNumber\": \"\"\n" +
" },\n" +
" \"FShiftGroupId\": {\n" +
" \"FNumber\": \"\"\n" +
" },\n" +
" \"F_CZ\": 0,\n" +
" \"FMemo\": \""+poundBill.getRemark()+"\",\n" +
" \"F_LC\": \""+poundBill.getBoilerno()+"\",\n" +
" \"F_GH\": 0,\n" +
" \"F_GANGH\": \""+poundBill.getSteeno()+"\",\n" +
" \"F_ZKS\": 0,\n" +
" \"F_KH\": 0,\n" +
" \"F_PJZL\": 0,\n" +
" \"F_XCLX\": \""+poundBill.getLinetype()+"\",\n" +
" \"F_PH\": \""+poundBill.getBatno()+"\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"}";
}else {
throw new RuntimeException("单据类型错误!");
}
//记录错误日志
StringBuilder errorLog = new StringBuilder();
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");
// 设置请求头包含token
String token = apiTask.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();
}
}