parent
							
								
									9343a1a821
								
							
						
					
					
						commit
						1e68b8d405
					
				|  | @ -165,7 +165,6 @@ public class ApiService { | |||
|      * @return | ||||
|      */ | ||||
|     public static String paymentSlipsJson(DynamicObject dynamic, String systemName) { | ||||
|         String requestBody; | ||||
|         // 获取推送的单据标识 | ||||
|         String billMark = dynamic.getDataEntityType().getName(); | ||||
| 
 | ||||
|  | @ -177,8 +176,22 @@ public class ApiService { | |||
|                 new QFilter("shkd_sourcebill", QCP.equals, billMark).and( | ||||
|                         new QFilter("shkd_targetsystem", QCP.equals, systemName)).toArray()); | ||||
| 
 | ||||
|         // 组装请求体数据 | ||||
|         String requestBody = assembleRequestBody(objects[0]); | ||||
|         logger.info("请求URL:{}\n组装请求body:{}", objects[0].getString("shkd_url"), requestBody); | ||||
| 
 | ||||
|         // 调用接口,获取响应数据 | ||||
|         return pushBill(objects[0], requestBody); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 组装请求体数据 | ||||
|      * @param dynamic API映射单据数据对象 | ||||
|      * @return | ||||
|      */ | ||||
|     public static String assembleRequestBody(DynamicObject dynamic) { | ||||
|         // 获取数据表信息 | ||||
|         DynamicObjectCollection dynamicObjectCollection = objects[0].getDynamicObjectCollection("entryentity"); | ||||
|         DynamicObjectCollection dynamicObjectCollection = dynamic.getDynamicObjectCollection("entryentity"); | ||||
| 
 | ||||
|         // 提取所有层级并存储在 Set 中 | ||||
|         Set<Integer> tiers = new HashSet<>(); | ||||
|  | @ -201,7 +214,7 @@ public class ApiService { | |||
|         } | ||||
| 
 | ||||
|         // 获取组装body类型 | ||||
|         String shkd_bodytype = objects[0].getString("shkd_bodytype"); | ||||
|         String shkd_bodytype = dynamic.getString("shkd_bodytype"); | ||||
|         if ("数组".equals(shkd_bodytype)) { | ||||
|             JSONArray jsonArray = new JSONArray(); | ||||
|             JSONObject json = new JSONObject(); | ||||
|  | @ -209,17 +222,13 @@ public class ApiService { | |||
|                 valueAssignment(dynamicObject, dynamic, json, floors); | ||||
|             } | ||||
|             jsonArray.add(json); | ||||
|             requestBody = jsonArray.toJSONString(); | ||||
|             return jsonArray.toJSONString(); | ||||
|         } else { | ||||
|             JSONObject resultJson = new JSONObject(); | ||||
|             processFloor(resultJson, "data", floors.get(0), floors, dynamic); | ||||
|             requestBody = resultJson.toJSONString(); | ||||
|             return resultJson.toJSONString(); | ||||
|         } | ||||
|         logger.info("请求URL:{}\n组装请求body:{}", objects[0].getString("shkd_url"), requestBody); | ||||
|         String responseBody = pushBill(objects[0], requestBody); | ||||
|         return responseBody; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 处理楼层 | ||||
|      * | ||||
|  |  | |||
|  | @ -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,22 +10,18 @@ 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.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.text.SimpleDateFormat; | ||||
| import java.util.*; | ||||
| import java.util.ArrayList; | ||||
| import java.util.EventObject; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| /** | ||||
|  * 动态表单插件 | ||||
|  | @ -52,9 +46,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); | ||||
|  | @ -96,46 +87,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(dataEntity); | ||||
|             codeEdit.setText(format(requestBody)); | ||||
|         } | ||||
| 
 | ||||
|         if ("shkd_analyzejson".equals(itemKey)) { | ||||
|  | @ -151,54 +108,8 @@ 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("\\", ""); | ||||
|                 } else { | ||||
|                     formattedContent = "响应失败"; | ||||
|                 } | ||||
|             } catch (Exception e) { | ||||
|                 formattedContent = "请求失败," + e.getMessage(); | ||||
|             } | ||||
|             this.getView().showTipNotification("返回结果:" + formattedContent); | ||||
|             String responseBody = ApiService.paymentSlipsJson(billObject, dataEntity.getString("shkd_systemname")); | ||||
|             this.getView().showTipNotification("返回结果:" + responseBody); | ||||
|         } | ||||
| 
 | ||||
|         if ("shkd_gettoken".equals(itemKey)) { | ||||
|  | @ -209,45 +120,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); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取子对象集合 | ||||
|  | @ -301,129 +173,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 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); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 格式化JSON字符串 | ||||
|      * | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue