Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
fang 2024-12-16 18:06:14 +08:00
commit d8b51ce65f
4 changed files with 512 additions and 524 deletions

View File

@ -16,10 +16,17 @@ import okhttp3.Response;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.*;
/**
@ -154,22 +161,38 @@ public class ApiService {
* 组装推送BIP付款单JSON
*
* @param dynamic 某个单据的 某条需要推送的数据 映射字段必须包含在其中缺失字段会报错
* @param systemName 推送系统名称
* @param systemName 推送系统标识
* @return
*/
public static JSONObject paymentSlipsJson(DynamicObject dynamic, String systemName) {
public static String paymentSlipsJson(DynamicObject dynamic, String systemName) {
// 获取推送的单据标识
String billMark = dynamic.getDataEntityType().getName();
DynamicObject[] objects = BusinessDataServiceHelper.load("shkd_apimapping", "id,billno," +
"shkd_name,shkd_sourcenumber,shkd_domainname,shkd_token,shkd_url,shkd_bodytype," +
"shkd_sourcebill,shkd_targetsystem,entryentity,entryentity.shkd_tarfield,entryentity.shkd_tartier," +
"entryentity.shkd_tartype,entryentity.shkd_parentfield,entryentity.shkd_soufield,entryentity.shkd_defaultdata," +
"entryentity.shkd_required,entryentity.shkd_remarks",
new QFilter("shkd_sourcebill", QCP.equals, billMark).and(
new QFilter("shkd_targetsystem", QCP.equals, systemName)).toArray());
// 组装请求体数据
String requestBody = assembleRequestBody(dynamic, objects[0]);
logger.info("请求URL{}\n组装请求body{}", objects[0].getString("shkd_url"), requestBody);
// 调用接口获取响应数据
return pushBill(objects[0], requestBody);
}
/**
* 组装请求体数据
* @param dynamic 推送数据对象
* @param mapping API映射对象数据
* @return
*/
public static String assembleRequestBody(DynamicObject dynamic, DynamicObject mapping) {
// 获取数据表信息
DynamicObjectCollection dynamicObjectCollection = objects[0].getDynamicObjectCollection("entryentity");
DynamicObjectCollection dynamicObjectCollection = mapping.getDynamicObjectCollection("entryentity");
// 提取所有层级并存储在 Set
Set<Integer> tiers = new HashSet<>();
@ -181,7 +204,6 @@ public class ApiService {
});
List<List<DynamicObject>> floors = new ArrayList<>();
// 目前定的4层之后需要弄成动态的
for (int i = 1; i <= tiers.size(); i++) {
floors.add(new ArrayList<>());
}
@ -191,68 +213,36 @@ public class ApiService {
int tier = Integer.parseInt(dynamicObject.getString("shkd_tartier"));
floors.get(tier - 1).add(dynamicObject);
}
// 获取组装body类型
String shkd_bodytype = mapping.getString("shkd_bodytype");
if ("数组".equals(shkd_bodytype)) {
JSONArray jsonArray = new JSONArray();
JSONObject json = new JSONObject();
for (DynamicObject dynamicObject : dynamicObjectCollection) {
valueAssignment(dynamicObject, dynamic, json, floors);
}
jsonArray.add(json);
return jsonArray.toJSONString();
} else {
JSONObject resultJson = new JSONObject();
processFloor(resultJson, "data", floors.get(0), floors, dynamic);
logger.info("调用接口系统:{}\n调用接口单据{}\n调用JSON{}", systemName, dynamic, resultJson);
return resultJson;
}
public static void traverseJson(JSONObject jsonObject, List<String> path, String level) {
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
String fullPath = buildPath(path, key, level);
System.out.println(fullPath + ": " + value);
if (value instanceof JSONObject) {
path.add(key);
traverseJson((JSONObject) value, path, level + "." + (path.size() + 1));
path.remove(path.size() - 1);
} else if (value instanceof JSONArray) {
path.add(key);
traverseJsonArray((JSONArray) value, path, level + "." + (path.size() + 1));
path.remove(path.size() - 1);
return resultJson.toJSONString();
}
}
}
public static void traverseJsonArray(JSONArray jsonArray, List<String> path, String level) {
for (int i = 0; i < jsonArray.size(); i++) {
Object value = jsonArray.get(i);
if (value instanceof JSONObject) {
path.add("[" + i + "]");
traverseJson((JSONObject) value, path, level + "." + (i + 1));
path.remove(path.size() - 1);
} else if (value instanceof JSONArray) {
path.add("[" + i + "]");
traverseJsonArray((JSONArray) value, path, level + "." + (i + 1));
path.remove(path.size() - 1);
} else {
String fullPath = buildPath(path, "[" + i + "]", level);
System.out.println(fullPath + ": " + value);
}
}
}
public static String buildPath(List<String> path, String key, String level) {
StringBuilder sb = new StringBuilder();
sb.append("层级 ").append(level).append(": ");
for (String s : path) {
sb.append(s).append(".");
}
sb.append(key);
return sb.toString();
}
/**
* 遍历JSON对象
* 处理楼层
*
* @param parentJson
* @param parentKey
* @param currentFloor
* @param floors
* @param parentJson 父JSON对象
* @param parentKey 父字段标识
* @param currentFloor 当前层级对象List
* @param floors 所有楼层对象List<List>
* @param dynamic 源单据对象
*/
public static void processFloor(JSONObject parentJson, String parentKey, List<DynamicObject> currentFloor, List<List<DynamicObject>> floors, DynamicObject dynamic) {
private static void processFloor(JSONObject parentJson, String parentKey, List<DynamicObject> currentFloor, List<List<DynamicObject>> floors, DynamicObject dynamic) {
logger.info("进入processFloor方法");
if (currentFloor.isEmpty()) {
logger.info("currentFloor.isEmpty()");
return;
}
@ -263,114 +253,153 @@ public class ApiService {
}
if ("arrayList".equals(currentFloor.get(0).getString("shkd_tartype"))) {
logger.info("进入数组");
JSONArray jsonArray = new JSONArray();
//目前JSONArray都是一层
JSONObject json = new JSONObject();
for (DynamicObject dynamicObject : currentFloor) {
String key = dynamicObject.getString("shkd_tarfield");
Object shkd_soufield = dynamicObject.get("shkd_soufield");
Object value = null;
if (shkd_soufield != null) {
String[] parts = shkd_soufield.toString().split("\\.");
if (parts.length == 1) {
value = dynamic.get(parts[0]);
} else if (parts.length == 2) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection = dynamic.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection.get(0).get(parts[1]);
} else {
value = dynamic.getDynamicObject(parts[0]).get(parts[1]);
}
} else if (parts.length == 3) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection = dynamic.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection.get(0).getDynamicObject(parts[1]).get(parts[2]);
} else {
value = dynamic.getDynamicObject(parts[0]).getDynamicObject(parts[1]).get(parts[2]);
}
}
} else {
value = dynamicObject.get("shkd_defaultdata");
}
String tartype = dynamicObject.getString("shkd_tartype");
if ("string".equals(tartype) || "date".equals(tartype)) {
json.put(key, value);
} else if ("int".equals(tartype)) {
json.put(key, Integer.parseInt(value.toString()));
} else if ("object".equals(tartype)) {
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic);
json.put(key, childJson);
} else if ("arrayList".equals(tartype)) {
JSONArray childJsonArray = new JSONArray();
for (DynamicObject childDynamicObject : getChildren(floors, dynamicObject.getString("shkd_tarfield"))) {
JSONObject childJson = new JSONObject();
processFloor(childJson, key, Collections.singletonList(childDynamicObject), floors, dynamic);
childJsonArray.add(childJson);
}
json.put(key, childJsonArray);
}
valueAssignment(dynamicObject, dynamic, json, floors);
}
jsonArray.add(json);
parentJson.put(parentKey, jsonArray);
} else {
logger.info("进入对象");
for (DynamicObject dynamicObject : currentFloor) {
String key = dynamicObject.getString("shkd_tarfield");
Object shkd_soufield = dynamicObject.get("shkd_soufield");
Object value = null;
if (shkd_soufield != null) {
String[] parts = shkd_soufield.toString().split("\\.");
if (parts.length == 1) {
value = dynamic.get(parts[0]);
} else if (parts.length == 2) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection = dynamic.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection.get(0).get(parts[1]);
} else {
value = dynamic.getDynamicObject(parts[0]).get(parts[1]);
}
} else if (parts.length == 3) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection = dynamic.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection.get(0).getDynamicObject(parts[1]).get(parts[2]);
} else {
value = dynamic.getDynamicObject(parts[0]).getDynamicObject(parts[1]).get(parts[2]);
}
}
} else {
value = dynamicObject.get("shkd_defaultdata");
}
String tartype = dynamicObject.getString("shkd_tartype");
if ("string".equals(tartype) || "date".equals(tartype)) {
parentJson.put(key, value);
} else if ("int".equals(tartype)) {
parentJson.put(key, Integer.parseInt(value.toString()));
} else if ("object".equals(tartype)) {
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic);
parentJson.put(key, childJson);
} else if ("arrayList".equals(tartype)) {
JSONArray childJsonArray = new JSONArray();
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic);
childJsonArray.add(childJson);
parentJson.put(key, childJsonArray);
}
valueAssignment(dynamicObject, dynamic, parentJson, floors);
}
}
}
/**
* 获取子节点
* 给value赋值
*
* @param floors
* @param parentKey
* @return
* @param dynamicObject 映射分录的单行数据
* @param billObject 源单数据
* @param jsonObject JSON对象
* @param floors 层集合
*/
public static List<DynamicObject> getChildren(List<List<DynamicObject>> floors, String parentKey) {
private static void valueAssignment(DynamicObject dynamicObject, DynamicObject billObject, JSONObject jsonObject, List<List<DynamicObject>> floors) {
String key = dynamicObject.getString("shkd_tarfield");
Object value = null;
// 将映射字段塞入对应JSON中
Object shkd_soufield = dynamicObject.get("shkd_soufield");
logger.info("shkd_soufield: {}", shkd_soufield);
if (shkd_soufield != null && !"".equals(shkd_soufield)) {
String[] parts = shkd_soufield.toString().split("\\.");
logger.info("billObject{}\nparts: {}", billObject, Arrays.toString(parts));
if (parts.length == 1) {
if ("payeetype".equals(parts[0]) || "payertype".equals(parts[0])) {
String objectType = billObject.getString(parts[0]);
switch (objectType) {
case "bos_org":
value = "4";
break;
case "bd_supplier":
value = "2";
break;
case "bd_customer":
value = "1";
break;
case "bos_user":
value = "3";
break;
case "other":
value = "4";
break;
}
} else if ("debitamount".equals(parts[0])) {
// 付款金额
BigDecimal debitamount = billObject.getBigDecimal("debitamount");
// 收款金额
BigDecimal creditamount = billObject.getBigDecimal("creditamount");
if (debitamount.compareTo(BigDecimal.ZERO) != 0) {
value = billObject.get("debitamount");
} else {
value = billObject.get("creditamount");
}
} else if ("direction".equals(parts[0])) {
// 付款金额
BigDecimal debitamount = billObject.getBigDecimal("debitamount");
// 收款金额
BigDecimal creditamount = billObject.getBigDecimal("creditamount");
if (debitamount.compareTo(BigDecimal.ZERO) != 0) {
value = "1";//支出
} else {
value = "2";//收入
}
} else if ("bizdate".equals(parts[0])) {
Date bizdate = billObject.getDate("bizdate");
// 创建一个SimpleDateFormat对象指定格式为"yyyy-MM-dd"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 格式化Date对象为字符串
value = sdf.format(bizdate);
} else {
value = billObject.get(parts[0]);
}
} else if (parts.length == 2) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection1 = billObject.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection1.get(0).get(parts[1]);
} else {
DynamicObject object = billObject.getDynamicObject(parts[0]);
if (object != null) {
value = object.get(parts[1]);
}
}
} else if (parts.length == 3) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection1 = billObject.getDynamicObjectCollection(parts[0]);
DynamicObject object = dynamicObjectCollection1.get(0).getDynamicObject(parts[1]);
if (object != null) {
value = object.get(parts[2]);
}
} else {
DynamicObject object = billObject.getDynamicObject(parts[0]);
if (object != null) {
DynamicObject object1 = object.getDynamicObject(parts[1]);
if (object1 != null) {
value = object1.get(parts[2]);
}
}
}
}
} else {
value = dynamicObject.get("shkd_defaultdata");
}
String tartype = dynamicObject.getString("shkd_tartype");
if ("String".equals(tartype) || "Date".equals(tartype)) {
jsonObject.put(key, value);
} else if ("Integer".equals(tartype)) {
jsonObject.put(key, Integer.parseInt(value.toString()));
} else if ("BigDecimal".equals(tartype)) {
jsonObject.put(key, new BigDecimal(value.toString()));
} else if ("对象".equals(tartype)) {
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, billObject);
jsonObject.put(key, childJson);
} else if ("数组".equals(tartype)) {
JSONArray childJsonArray = new JSONArray();
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, billObject);
childJsonArray.add(childJson);
jsonObject.put(key, childJsonArray);
}
}
/**
* 获取子对象集合
*
* @param floors 所有层级的所有对象
* @param parentKey 父节点的shkd_tarfield
* @return 子对象集合
*/
private static List<DynamicObject> getChildren(List<List<DynamicObject>> floors, String parentKey) {
List<DynamicObject> children = new ArrayList<>();
for (List<DynamicObject> floor : floors) {
for (DynamicObject dynamicObject : floor) {
@ -382,5 +411,68 @@ public class ApiService {
return children;
}
/**
* 推送单条数据
* @param dataEntity
* @param requestBody
* @return
*/
private static String pushBill(DynamicObject dataEntity, String requestBody) {
// 响应数据
String formattedContent;
try {
// 域名
String domainName = dataEntity.getString("shkd_domainname");
Map<String, Object> resultMap = ApiService.getBIPToken(domainName);
Object token = resultMap.get("token");
logger.info("获取token{}", token);
// 请求URL
URL url = new URL(dataEntity.getString("shkd_url") + "?access_token=" + token);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为POST
connection.setRequestMethod("POST");
// 设置请求头
connection.setRequestProperty("Content-Type", "application/json");
// 允许输出
connection.setDoOutput(true);
// 写入请求体
try (OutputStream os = connection.getOutputStream()) {
byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// 获取响应码
int responseCode = connection.getResponseCode();
// 读取响应内容
if (responseCode == HttpURLConnection.HTTP_OK) { // 成功响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
// 替换 \n 为实际的换行符
formattedContent = content.toString().replace("\\n", "\n");
} else {
// 读取错误流
BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8));
String errorLine;
StringBuilder errorContent = new StringBuilder();
while ((errorLine = errorReader.readLine()) != null) {
errorContent.append(errorLine);
}
errorReader.close();
formattedContent = "响应失败: " + errorContent.toString();
}
} catch (Exception e) {
formattedContent = "请求失败," + e.getMessage();
}
return formattedContent;
}
}

View File

@ -29,8 +29,6 @@ import java.util.Map;
public class PayBillApiSavePlugin implements ApiSavePlugin {
private static final Log logger = LogFactory.getLog(PayBillApiSavePlugin.class);
private static final long serialVersionUID = 7055073356277386444L;
private String shkd_businessid;
private String shkd_businessnumber;
@Override
public List<Map<String, Object>> preHandleRequestData(List<Map<String, Object>> reqData) {
@ -58,6 +56,7 @@ public class PayBillApiSavePlugin implements ApiSavePlugin {
logger.info("进入付款处理单API");
//收款人类型
objectType = payeetype.toString();
if (!"other".equals(objectType)) {
//收款人编码
objectNumber = map.get("payeenumber").toString();
//收款人ID
@ -66,15 +65,16 @@ public class PayBillApiSavePlugin implements ApiSavePlugin {
Map<String, Object> payeebankObj = (Map<String, Object>) map.get("payeebank");
bankNumber = (String) payeebankObj.get("number");
logger.info("付款处理 → \nobjectType{}\nobjectNumber{}\nbankNumber:{}", objectType, objectNumber, bankNumber);
fieldName2 = "payerbank";
DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load(objectType, "id,number", new QFilter("number", QCP.equals, objectNumber).toArray());
logger.info("dynamicObjects.length{}", dynamicObjects.length);
if (dynamicObjects.length > 0) {
map.put(fieldName1, dynamicObjects[0].getPkValue());
logger.info("dynamicObjects[0]{}", dynamicObjects[0]);
}
}
if ("bos_org".equals(objectType)) {
fieldName2 = "payerbank";
DynamicObject[] amAccountbanks = BusinessDataServiceHelper.load("am_accountbank", "id,bank,bank.number", new QFilter("number", QCP.equals, bankNumber).toArray());
logger.info("amAccountbanks.length:{}", amAccountbanks.length);
Map<String, Object> payerbank = new HashMap<>();
@ -85,9 +85,11 @@ public class PayBillApiSavePlugin implements ApiSavePlugin {
logger.info("payerbank:{}", payerbank);
map.put(fieldName2, payerbank);
}
}
if (payertype != null) {
objectType = payertype.toString();
if (!"other".equals(objectType)) {
objectNumber = map.get("payernumber").toString();
fieldName1 = "payer";
@ -95,14 +97,17 @@ public class PayBillApiSavePlugin implements ApiSavePlugin {
Map<String, Object> accountbank = (Map<String, Object>) map.get("accountbank");
bankNumber = (String) accountbank.get("number");
logger.info("收款处理 → \nobjectType{}\nobjectNumber{}\nbankNumber:{}", objectType, objectNumber, bankNumber);
fieldName2 = "payeebank";
DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load(objectType, "id,number", new QFilter("number", QCP.equals, objectNumber).toArray());
logger.info("dynamicObjects.length{}", dynamicObjects.length);
if (dynamicObjects.length > 0) {
map.put(fieldName1, dynamicObjects[0].getPkValue());
logger.info("dynamicObjects[0]{}", dynamicObjects[0]);
}
}
if ("bos_org".equals(objectType)) {
fieldName2 = "payeebank";
DynamicObject[] amAccountbanks = BusinessDataServiceHelper.load("am_accountbank", "id,bank,bank.number", new QFilter("number", QCP.equals, bankNumber).toArray());
logger.info("amAccountbanks.length:{}", amAccountbanks.length);
Map<String, Object> payeebank = new HashMap<>();
@ -112,45 +117,9 @@ public class PayBillApiSavePlugin implements ApiSavePlugin {
logger.info("payerbank:{}", payeebank);
map.put(fieldName2, payeebank);
}
shkd_businessid = map.get("shkd_businessid").toString();
shkd_businessnumber = map.get("shkd_businessnumber").toString();
// shkd_businessid
// shkd_businessnumber
// shkd_businessname
}
logger.info("最终处理 → 调用接口参数:{}", reqData);
}
return reqData;
}
/*@Override
public SerializerResult serialize(Object response, String accept, String contentType) {
logger.info("进入serialize方法");
try {
if (contentType.contains(MediaType.APPLICATION_JSON)) {
logger.info("进入if");
//返回text文本
String responseStr = new ObjectMapper().writeValueAsString(response);
JSONObject jsonObject = JSON.parseObject(responseStr);
// 获取 data 节点
JSONObject data = jsonObject.getJSONObject("data");
// 获取 result 节点
JSONArray result = data.getJSONArray("result");
result.getJSONObject(0).put("shkd_businessid", shkd_businessid);
result.getJSONObject(0).put("shkd_businessnumber", shkd_businessnumber);
return new SerializerResult(MediaType.TEXT_PLAIN, jsonObject.toJSONString());
} else {
logger.info("进入else");
//其他类型的出参序列化
String responseStr = new ObjectMapper().writeValueAsString(response);
JSONObject jsonObject = JSON.parseObject(responseStr);
return new SerializerResult(MediaType.TEXT_PLAIN, jsonObject.toJSONString());
}
} catch (JsonProcessingException e) {
logger.info("catch");
//处理异常时严禁抛出异常可以定义自己的错误返回信息
String result = "...";
return new SerializerResult(MediaType.TEXT_PLAIN, result);
}
}*/
}

View File

@ -1,7 +1,5 @@
package shkd.sys.sys.plugin.form;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@ -12,21 +10,19 @@ import kd.bos.form.control.CodeEdit;
import kd.bos.form.control.Toolbar;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import shkd.sys.sys.common.ApiEntity;
import shkd.sys.sys.mservice.ApiService;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.Map;
/**
* 动态表单插件
@ -51,9 +47,6 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin {
public void itemClick(ItemClickEvent evt) {
String itemKey = evt.getItemKey();
DynamicObject dataEntity = this.getModel().getDataEntity(true);
// 获取代码编辑器控件
CodeEdit codeEdit = this.getView().getControl("shkd_codeeditap");
if ("shkd_generatejson".equals(itemKey)) {
// 获取想要推送单据编码
String billNumber = dataEntity.getString("shkd_sourcenumber");
logger.info("获取推送单据编码 → billNumber{}", billNumber);
@ -68,7 +61,8 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin {
case "cas_paybill":
DynamicObject[] objects1 = BusinessDataServiceHelper.load("cas_paybill",
"id,billno,actpayamt,entry,entry.e_expenseitem,entry.e_remark,settletype,payeebanknum," +
"payeetype,payeenumber,payeracctbank,payeebank,payeebankname,paymenttype,org,bizdate,description"
"payeetype,payeenumber,payeracctbank,payeebank,payeebankname,paymenttype,org,bizdate,description," +
"shkd_pushstatus,shkd_businessnumber,shkd_businessid,shkd_businessname,billstatus,bankpaystatus"
, new QFilter("billno", QCP.equals, billNumber).toArray());
billObject = objects1[0];
logger.info("获取付款处理推送对象 → billObject{}", billObject);
@ -77,7 +71,8 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin {
case "cas_recbill":
DynamicObject[] objects2 = BusinessDataServiceHelper.load("cas_recbill",
"id,billno,entry,entry.e_expenseitem,settletype,entry.e_remark,receivingtype," +
"payertype,org,bizdate,accountbank,payernumber,actrecamt,txt_description"
"payertype,org,bizdate,accountbank,payernumber,actrecamt,txt_description,shkd_pushstatus," +
"shkd_businessnumber,shkd_businessid,shkd_businessname,billstatus"
, new QFilter("billno", QCP.equals, billNumber).toArray());
billObject = objects2[0];
logger.info("获取收款处理推送对象 → billObject{}", billObject);
@ -86,7 +81,8 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin {
case "bei_transdetail_cas":
DynamicObject[] objects3 = BusinessDataServiceHelper.load("bei_transdetail_cas",
"id,bizdate,oppbank,oppunit,accountbank,description,company,oppbanknumber," +
"bankdetailno,transbalance,description,debitamount,creditamount"
"bankdetailno,transbalance,description,debitamount,creditamount,shkd_pushstatus," +
"shkd_businessnumber,shkd_businessid,shkd_businessname"
, new QFilter("billno", QCP.equals, billNumber).toArray());
billObject = objects3[0];
logger.info("获取银企交易明细查询推送对象 → billObject{}", billObject);
@ -95,46 +91,12 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin {
return;
}
// 生成JSON
DynamicObjectCollection dynamicObjectCollection = dataEntity.getDynamicObjectCollection("entryentity");
// 获取代码编辑器控件
CodeEdit codeEdit = this.getView().getControl("shkd_codeeditap");
// 提取所有层级并存储在 Set
Set<Integer> tiers = new HashSet<>();
dynamicObjectCollection.forEach(dynamicObject -> {
String shkd_tartier = dynamicObject.getString("shkd_tartier");
if (shkd_tartier != null && !shkd_tartier.isEmpty()) {
tiers.add(Integer.parseInt(shkd_tartier));
}
});
List<List<DynamicObject>> floors = new ArrayList<>();
for (int i = 1; i <= tiers.size(); i++) {
floors.add(new ArrayList<>());
}
// 分层
for (DynamicObject dynamicObject : dynamicObjectCollection) {
int tier = Integer.parseInt(dynamicObject.getString("shkd_tartier"));
floors.get(tier - 1).add(dynamicObject);
}
// 获取组装body类型
String shkd_bodytype = dataEntity.getString("shkd_bodytype");
if ("数组".equals(shkd_bodytype)) {
JSONArray jsonArray = new JSONArray();
JSONObject json = new JSONObject();
for (DynamicObject dynamicObject : dynamicObjectCollection) {
valueAssignment(dynamicObject, billObject, json, floors);
}
jsonArray.add(json);
codeEdit.setText(format(jsonArray.toJSONString()));
logger.info("数组 → JSON{}", jsonArray.toJSONString());
} else {
JSONObject resultJson = new JSONObject();
processFloor(resultJson, "data", floors.get(0), floors, billObject);
codeEdit.setText(format(resultJson.toJSONString()));
logger.info("对象 → JSON{}", resultJson.toJSONString());
}
if ("shkd_generatejson".equals(itemKey)) {
String requestBody = ApiService.assembleRequestBody(billObject, dataEntity);
codeEdit.setText(format(requestBody));
}
if ("shkd_analyzejson".equals(itemKey)) {
@ -150,54 +112,33 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin {
}
if ("shkd_testapi".equals(itemKey)) {
//响应数据
String formattedContent;
try {
//域名
String domainName = dataEntity.getString("shkd_domainname");
Map<String, Object> resultMap = ApiService.getBIPToken(domainName);
// 请求URL
URL url = new URL(dataEntity.getString("shkd_url") + "?access_token=" + resultMap.get("token"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为POST
connection.setRequestMethod("POST");
// 设置请求头
connection.setRequestProperty("Content-Type", "application/json");
// 允许输出
connection.setDoOutput(true);
// 写入请求体
try (OutputStream os = connection.getOutputStream()) {
byte[] input = codeEdit.getText().getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// 获取响应码
int responseCode = connection.getResponseCode();
// 这里可以进一步读取响应内容根据实际情况处理
// 读取响应内容
if (responseCode == HttpURLConnection.HTTP_OK) { // 成功响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
// 替换 \n 为实际的换行符
formattedContent = content.toString().replace("\\n", "\n").replace("\\","");
String responseBody = ApiService.paymentSlipsJson(billObject, "BIP");
JSONObject jsonObject = JSON.parseObject(responseBody);
String code = jsonObject.getString("code");
if ("200".equals(code)) {
JSONObject data = jsonObject.getJSONObject("data");
billObject.set("shkd_businessnumber", data.getString("code"));
billObject.set("shkd_businessid", data.getString("id"));
billObject.set("shkd_businessname", "BIP");
billObject.set("shkd_pushstatus", "已推送");
SaveServiceHelper.save(new DynamicObject[]{billObject});
} else {
formattedContent = "响应失败";
logger.info("推送 → 失败\n失败单据编号{}\n推送失败接口返回数据{}", billObject.getString("billno"), responseBody);
}
} catch (Exception e) {
formattedContent = "请求失败," + e.getMessage();
this.getView().showTipNotification("返回结果:" + responseBody);
}
this.getView().showTipNotification("返回结果:" + formattedContent);
if ("shkd_hitback".equals(itemKey)) {
String responseBody = ApiService.paymentSlipsJson(billObject, "BIPNO");
JSONObject jsonObject = JSON.parseObject(responseBody);
String code = jsonObject.getString("code");
if ("200".equals(code)) {
billObject.set("billstatus", "A");
SaveServiceHelper.save(new DynamicObject[]{billObject});
} else {
logger.info("推送 → 失败\n失败单据编号{}\n推送失败接口返回数据{}", billObject.getString("billno"), responseBody);
}
this.getView().showTipNotification("返回结果:" + responseBody);
}
if ("shkd_gettoken".equals(itemKey)) {
@ -208,45 +149,6 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin {
}
}
/**
* 处理楼层
*
* @param parentJson 父JSON对象
* @param parentKey 父字段标识
* @param currentFloor 当前层级对象List
* @param floors 所有楼层对象List<List>
* @param dynamic 源单据对象
*/
private void processFloor(JSONObject parentJson, String parentKey, List<DynamicObject> currentFloor, List<List<DynamicObject>> floors, DynamicObject dynamic) {
logger.info("进入processFloor方法");
if (currentFloor.isEmpty()) {
logger.info("currentFloor.isEmpty()");
return;
}
if (currentFloor.size() == 1 && "object".equals(currentFloor.get(0).getString("shkd_tartype"))) {
parentJson.put(parentKey, new JSONObject());
processFloor(parentJson.getJSONObject(parentKey), parentKey, getChildren(floors, currentFloor.get(0).getString("shkd_tarfield")), floors, dynamic);
return;
}
if ("arrayList".equals(currentFloor.get(0).getString("shkd_tartype"))) {
logger.info("进入数组");
JSONArray jsonArray = new JSONArray();
//目前JSONArray都是一层
JSONObject json = new JSONObject();
for (DynamicObject dynamicObject : currentFloor) {
valueAssignment(dynamicObject, dynamic, json, floors);
}
jsonArray.add(json);
parentJson.put(parentKey, jsonArray);
} else {
logger.info("进入对象");
for (DynamicObject dynamicObject : currentFloor) {
valueAssignment(dynamicObject, dynamic, parentJson, floors);
}
}
}
/**
* 获取子对象集合
@ -300,123 +202,6 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin {
this.getModel().setValue("shkd_parentfield", pKey, dynamicObjectCollection.size() - 1);
}
/**
* 给value赋值
*
* @param dynamicObject 映射分录的单行数据
* @param billObject 源单数据
* @param jsonObject JSON对象
* @param floors 层集合
*/
private void valueAssignment(DynamicObject dynamicObject, DynamicObject billObject, JSONObject jsonObject, List<List<DynamicObject>> floors) {
String key = dynamicObject.getString("shkd_tarfield");
Object value = null;
// 将映射字段塞入对应JSON中
Object shkd_soufield = dynamicObject.get("shkd_soufield");
logger.info("shkd_soufield: {}", shkd_soufield);
if (shkd_soufield != null && !"".equals(shkd_soufield)) {
String[] parts = shkd_soufield.toString().split("\\.");
logger.info("billObject{}\nparts: {}", billObject, Arrays.toString(parts));
if (parts.length == 1) {
if ("payeetype".equals(parts[0]) || "payertype".equals(parts[0])) {
String objectType = billObject.getString(parts[0]);
switch (objectType) {
case "bos_org":
value = "4";
break;
case "bd_supplier":
value = "2";
break;
case "bd_customer":
value = "1";
break;
case "bos_user":
value = "3";
break;
case "other":
value = "4";
break;
}
} else if ("debitamount".equals(parts[0])) {
// 付款金额
BigDecimal debitamount = billObject.getBigDecimal("debitamount");
// 收款金额
BigDecimal creditamount = billObject.getBigDecimal("creditamount");
if (debitamount.compareTo(BigDecimal.ZERO) != 0) {
value = billObject.get("debitamount");
} else {
value = billObject.get("creditamount");
}
} else if ("direction".equals(parts[0])) {
// 付款金额
BigDecimal debitamount = billObject.getBigDecimal("debitamount");
// 收款金额
BigDecimal creditamount = billObject.getBigDecimal("creditamount");
if (debitamount.compareTo(BigDecimal.ZERO) != 0) {
value = "1";//支出
} else {
value = "2";//收入
}
} else {
value = billObject.get(parts[0]);
}
} else if (parts.length == 2) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection1 = billObject.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection1.get(0).get(parts[1]);
} else {
DynamicObject object = billObject.getDynamicObject(parts[0]);
if (object != null) {
value = object.get(parts[1]);
}
}
} else if (parts.length == 3) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection1 = billObject.getDynamicObjectCollection(parts[0]);
DynamicObject object = dynamicObjectCollection1.get(0).getDynamicObject(parts[1]);
if (object != null) {
value = object.get(parts[2]);
}
} else {
DynamicObject object = billObject.getDynamicObject(parts[0]);
if (object != null) {
DynamicObject object1 = object.getDynamicObject(parts[1]);
if (object1 != null) {
value = object1.get(parts[2]);
}
}
}
}
} else {
value = dynamicObject.get("shkd_defaultdata");
}
String tartype = dynamicObject.getString("shkd_tartype");
if ("String".equals(tartype) || "Date".equals(tartype)) {
jsonObject.put(key, value);
} else if ("Integer".equals(tartype)) {
jsonObject.put(key, Integer.parseInt(value.toString()));
} else if ("BigDecimal".equals(tartype)) {
jsonObject.put(key, new BigDecimal(value.toString()));
} else if ("对象".equals(tartype)) {
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, billObject);
jsonObject.put(key, childJson);
} else if ("数组".equals(tartype)) {
JSONArray childJsonArray = new JSONArray();
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, billObject);
childJsonArray.add(childJson);
jsonObject.put(key, childJsonArray);
}
}
/**
* 格式化JSON字符串
*

View File

@ -0,0 +1,142 @@
package shkd.sys.sys.plugin.task;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.qing.util.DateUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import shkd.sys.sys.mservice.ApiService;
import shkd.sys.sys.plugin.form.ApiMappingBillPlugin;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
* 后台任务插件
*/
public class PushTaskPlugin extends AbstractTask implements Plugin {
private static final Log logger = LogFactory.getLog(PushTaskPlugin.class);
@Override
public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
Object billMark = map.get("billMark");
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 获取当前日期的前两天
LocalDate twoDaysAgo = currentDate.minusDays(2);
// 格式化日期为 "yyyy-MM-dd"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = twoDaysAgo.format(formatter);
Date date;
try {
date = DateUtils.stringToDate(formattedDate, "yyyy-MM-dd");
} catch (ParseException e) {
throw new RuntimeException(e);
}
String billName = null;
List<DynamicObject> dynamicObjects = new ArrayList<>();
List<DynamicObject> objects = new ArrayList<>();
String finalBillName;
if (billMark != null) {
switch (billMark.toString()) {
// 付款处理
case "cas_paybill":
DynamicObject[] objects1 = BusinessDataServiceHelper.load("cas_paybill",
"id,billno,actpayamt,entry,entry.e_expenseitem,entry.e_remark,settletype,payeebanknum," +
"payeetype,payeenumber,payeracctbank,payeebank,payeebankname,paymenttype,org,bizdate,description," +
"shkd_pushstatus,shkd_businessnumber,shkd_businessid,shkd_businessname,billstatus,bankpaystatus"
, new QFilter("bizdate", QCP.large_equals, date).and("shkd_pushstatus", QCP.not_equals, "已推送")
.and("billstatus", QCP.equals, "D").toArray());
dynamicObjects = Arrays.asList(objects1);
billName = "付款处理";
finalBillName = billName;
dynamicObjects.forEach(dynamicObject -> {
String responseBody = ApiService.paymentSlipsJson(dynamicObject, "BIP");
logger.info("推送 → {} → 开始\n推送单据编号{}\n接口响应数据{}", finalBillName, dynamicObject.getString("billno"), responseBody);
if (!responseBody.contains("失败")) {
JSONObject jsonObject = JSON.parseObject(responseBody);
String code = jsonObject.getString("code");
if ("200".equals(code)) {
JSONObject data = jsonObject.getJSONObject("data");
dynamicObject.set("shkd_businessnumber", data.getString("code"));
dynamicObject.set("shkd_businessid", data.getString("id"));
dynamicObject.set("shkd_businessname", "BIP");
dynamicObject.set("shkd_pushstatus", "已推送");
objects.add(dynamicObject);
} else {
logger.info("推送 → {} → 失败\n失败单据编号{}\n推送失败接口返回数据{}", finalBillName, dynamicObject.getString("billno"), responseBody);
}
} else {
logger.info("推送 → {} → 失败,失败单据编号:{}", finalBillName, dynamicObject.getString("billno"));
}
});
SaveServiceHelper.save(objects.toArray(new DynamicObject[0]));
break;
// 收款处理
case "cas_recbill":
DynamicObject[] objects2 = BusinessDataServiceHelper.load("cas_recbill",
"id,billno,entry,entry.e_expenseitem,settletype,entry.e_remark,receivingtype," +
"payertype,org,bizdate,accountbank,payernumber,actrecamt,txt_description,shkd_pushstatus," +
"shkd_businessnumber,shkd_businessid,shkd_businessname,billstatus"
, new QFilter("bizdate", QCP.large_equals, date).and("shkd_pushstatus", QCP.not_equals, "已推送").toArray());
dynamicObjects = Arrays.asList(objects2);
billName = "收款处理";
break;
// 银行收付处理
case "bei_transdetail_cas":
DynamicObject[] objects3 = BusinessDataServiceHelper.load("bei_transdetail_cas",
"id,bizdate,oppbank,oppunit,accountbank,description,company,oppbanknumber," +
"bankdetailno,transbalance,description,debitamount,creditamount,shkd_pushstatus," +
"shkd_businessnumber,shkd_businessid,shkd_businessname"
, new QFilter("bizdate", QCP.large_equals, date).and("shkd_pushstatus", QCP.not_equals, "已推送").toArray());
dynamicObjects = Arrays.asList(objects3);
billName = "银行收付处理";
break;
// 支付失败付款处理支付结果定时推送
case "cas_paybill_result":
DynamicObject[] objects4 = BusinessDataServiceHelper.load("cas_paybill",
"id,billno,actpayamt,entry,entry.e_expenseitem,entry.e_remark,settletype,payeebanknum," +
"payeetype,payeenumber,payeracctbank,payeebank,payeebankname,paymenttype,org,bizdate,description," +
"shkd_pushstatus,shkd_businessnumber,shkd_businessid,shkd_businessname,billstatus,bankpaystatus"
, new QFilter("bizdate", QCP.large_equals, date).and("bankpaystatus", QCP.in, new String[]{"TF", "NC", "OF"}).toArray());
dynamicObjects = Arrays.asList(objects4);
finalBillName = billName;
dynamicObjects.forEach(dynamicObject -> {
String responseBody = ApiService.paymentSlipsJson(dynamicObject, "BIPNO");
logger.info("推送 → {} → 开始\n推送单据编号{}\n接口响应数据{}", finalBillName, dynamicObject.getString("billno"), responseBody);
if (!responseBody.contains("失败")) {
JSONObject jsonObject = JSON.parseObject(responseBody);
String code = jsonObject.getString("code");
if ("200".equals(code)) {
dynamicObject.set("billstatus", "A");
objects.add(dynamicObject);
} else {
logger.info("推送 → {} → 失败\n失败单据编号{}\n推送失败接口返回数据{}", finalBillName, dynamicObject.getString("billno"), responseBody);
}
} else {
logger.info("推送 → {} → 失败,失败单据编号:{}", finalBillName, dynamicObject.getString("billno"));
}
});
SaveServiceHelper.save(objects.toArray(new DynamicObject[0]));
default:
return;
}
}
}
}