From d11418d8672e330df50f0aecd365ee50cbd1dfea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=B4=B5=E5=BC=BA?= Date: Tue, 21 Oct 2025 17:41:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E4=BB=98=E6=AC=BE=E6=8E=A8SAP?= =?UTF-8?q?=E3=80=81=E8=B4=B9=E6=8E=A7=E6=8A=A5=E9=94=99=E5=8F=8D=E5=86=99?= =?UTF-8?q?=E5=89=8D=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fi/plugin/operate/PaybillOperation.java | 52 +++++++++++++++---- .../operate/PaybillPushSapOperation.java | 8 +-- .../operate/RecPushVoucherOperation.java | 5 ++ .../java/shjh/jhzj7/fi/fi/utils/SapUtils.java | 25 +++++++++ 4 files changed, 76 insertions(+), 14 deletions(-) diff --git a/main/java/shjh/jhzj7/fi/fi/plugin/operate/PaybillOperation.java b/main/java/shjh/jhzj7/fi/fi/plugin/operate/PaybillOperation.java index 451bc76..789f769 100644 --- a/main/java/shjh/jhzj7/fi/fi/plugin/operate/PaybillOperation.java +++ b/main/java/shjh/jhzj7/fi/fi/plugin/operate/PaybillOperation.java @@ -8,17 +8,25 @@ import kd.bos.dataentity.entity.DynamicObjectCollection; import kd.bos.entity.operate.result.OperateErrorInfo; import kd.bos.entity.plugin.AbstractOperationServicePlugIn; import kd.bos.entity.plugin.args.AfterOperationArgs; +import kd.bos.fileservice.FileServiceFactory; +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.AttachmentDto; import kd.bos.servicehelper.AttachmentServiceHelper; import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.bos.util.StringUtils; import kd.sdk.plugin.Plugin; +import shjh.jhzj7.fi.fi.plugin.task.RecPushSapTask; +import shjh.jhzj7.fi.fi.utils.SapUtils; +import java.io.ByteArrayOutputStream; import java.time.LocalDate; import java.time.ZoneId; import java.time.format.DateTimeFormatter; +import java.util.Base64; import java.util.Date; import java.util.List; import java.util.Map; @@ -32,6 +40,9 @@ import static shjh.jhzj7.fi.fi.utils.SapUtils.withholding_billing; public class PaybillOperation extends AbstractOperationServicePlugIn implements Plugin { + + private final static Log logger = LogFactory.getLog(PaybillOperation.class); + @Override public void afterExecuteOperationTransaction(AfterOperationArgs e) { super.afterExecuteOperationTransaction(e); @@ -86,7 +97,10 @@ public class PaybillOperation extends AbstractOperationServicePlugIn implements String code = (String) jsonObject.get("code"); if (!"0".equals(code)) { addErrorInfo(bill, ""+jsonObject.get("msg")); + //更新推送报错 + SapUtils.updateBillErrorMsg(bill,jsonObject,"shjh_errormsg"); } else { + bill.set("shjh_errormsg",""); bill.set("shjh_ispushfk", true); SaveServiceHelper.save(new DynamicObject[]{bill}); this.operationResult.addSuccessPkId(bill.getPkValue()); @@ -171,22 +185,40 @@ public class PaybillOperation extends AbstractOperationServicePlugIn implements header.put("FM_ExpenseTypeCode", FM_ExpenseTypeCode);// 业务大类编码(EQ49,EQ44,EQ1101) header.put("FM_CurrencyCode", "RMB");// 币种编码,默认:RMB header.put("FM_RequestName", bill.getString("description"));// 单据主题_拼接 -// List> attachments = AttachmentServiceHelper.getAttachments(bill.getDataEntityType().getName(), bill.getLong("id"), "attachmentpanel"); -// if (attachments.size()!=0){ -// JSONArray attachUrls = new JSONArray(); -// for (Map attachment : attachments) { -// String url = (String)attachment.get("url"); -// attachUrls.add(url); -// } -// header.put("FM_AttachUrl", attachUrls);//附件URL地址:array -// } - header.put("FM_AttachUrl", null);//附件URL地址:array + List> attachments = AttachmentServiceHelper.getAttachments(bill.getDataEntityType().getName(), bill.getLong("id"), "attachmentpanel"); + if (attachments.size()!=0){ + JSONArray attachUrls = new JSONArray(); + for (Map attachment : attachments) { + JSONObject jsonObject = new JSONObject(); + String fileBase64 = getFileBase64StrByUrl(attachment);//转换为Base64数据 + jsonObject.put("attachData",fileBase64); + byte[] decodedBytes = Base64.getDecoder().decode(fileBase64); + int byteLength = decodedBytes.length; + jsonObject.put("attachDataLength",byteLength); + jsonObject.put("attachName",attachment.get("name")); + jsonObject.put("attachType",attachment.get("type")); + attachUrls.add(jsonObject); + } + header.put("FM_AttachUrl", attachUrls);//附件URL地址:array + } + //header.put("FM_AttachUrl", null);//附件URL地址:array header.put("FM_BudType", 0);//预算类别0-组织预算,1-全年预算,当前只处理0 int header.put("Remark", bill.getString("description"));// 事项描述_摘要 return header; } + public static String getFileBase64StrByUrl(Map attachment){ + Object attPKId = attachment.get("attPkId"); + AttachmentDto attachmentDto = AttachmentServiceHelper.getAttachmentInfoByAttPk(attPKId); + String resourcePath = attachmentDto.getResourcePath(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileServiceFactory.getAttachmentFileService().download(resourcePath,out,null); + byte[] bytes = out.toByteArray(); + String baseStr = new String(Base64.getEncoder().encode(bytes)); + return baseStr; + } + // 构建body方法 public static JSONArray buildBody(DynamicObject bill) { bill = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), "cas_paybill"); diff --git a/main/java/shjh/jhzj7/fi/fi/plugin/operate/PaybillPushSapOperation.java b/main/java/shjh/jhzj7/fi/fi/plugin/operate/PaybillPushSapOperation.java index b448ed0..a512452 100644 --- a/main/java/shjh/jhzj7/fi/fi/plugin/operate/PaybillPushSapOperation.java +++ b/main/java/shjh/jhzj7/fi/fi/plugin/operate/PaybillPushSapOperation.java @@ -21,10 +21,7 @@ import kd.bos.servicehelper.QueryServiceHelper; import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.bos.util.StringUtils; import kd.sdk.plugin.Plugin; -import shjh.jhzj7.fi.fi.utils.ApiUtils; -import shjh.jhzj7.fi.fi.utils.EsbUtils; -import shjh.jhzj7.fi.fi.utils.JhzjUtils; -import shjh.jhzj7.fi.fi.utils.SysUtils; +import shjh.jhzj7.fi.fi.utils.*; import shjh.jhzj7.fi.fi.utils.domin.ResponseData; import java.io.IOException; @@ -819,6 +816,7 @@ public class PaybillPushSapOperation extends AbstractOperationServicePlugIn impl bill.set("shjh_credentialnum",responseData.getNumber()); bill.set("shjh_sapfiscalyear",responseData.getYear()); bill.set("shjh_ispushsap", true); + bill.set("shjh_errormsg",""); SaveServiceHelper.save(new DynamicObject[]{bill}); //处理sap主动付款时,更新日记账的sap凭证号 //处理被动付款时,更新流水和日记账的sap凭证号 @@ -856,6 +854,8 @@ public class PaybillPushSapOperation extends AbstractOperationServicePlugIn impl this.operationResult.addErrorInfo(operateErrorInfo); } }else { + //更新推送报错 + SapUtils.updateBillErrorMsg(bill,jsonObject,"shjh_errormsg"); OperateErrorInfo operateErrorInfo = new OperateErrorInfo(); operateErrorInfo.setMessage("推送SAP凭证接口失败,原因:"+jsonObject.get("msg")); operateErrorInfo.setErrorLevel(ErrorLevel.Error.name()); diff --git a/main/java/shjh/jhzj7/fi/fi/plugin/operate/RecPushVoucherOperation.java b/main/java/shjh/jhzj7/fi/fi/plugin/operate/RecPushVoucherOperation.java index aa1480a..96f6a80 100644 --- a/main/java/shjh/jhzj7/fi/fi/plugin/operate/RecPushVoucherOperation.java +++ b/main/java/shjh/jhzj7/fi/fi/plugin/operate/RecPushVoucherOperation.java @@ -26,6 +26,7 @@ import kd.sdk.plugin.Plugin; import shjh.jhzj7.fi.fi.plugin.form.info.RecFieldsInfo; import shjh.jhzj7.fi.fi.utils.ApiUtils; import shjh.jhzj7.fi.fi.utils.JhzjUtils; +import shjh.jhzj7.fi.fi.utils.SapUtils; import shjh.jhzj7.fi.fi.utils.SysUtils; import shjh.jhzj7.fi.fi.utils.domin.ResponseData; @@ -234,6 +235,8 @@ public class RecPushVoucherOperation extends AbstractOperationServicePlugIn impl recBill.set("shjh_vouchernum",responseData.getNumber()); recBill.set("shjh_sapfiscalyear",responseData.getYear()); recBill.set("shjh_ispushsap", true); + recBill.set("shjh_errormsg",""); + SaveServiceHelper.update(recBill); DynamicObject bankjournal = BusinessDataServiceHelper.loadSingle("cas_bankjournal", new QFilter("billno", QCP.equals, recBill.getString("billno")).toArray()); @@ -265,6 +268,8 @@ public class RecPushVoucherOperation extends AbstractOperationServicePlugIn impl } }else { + //更新推送报错 + SapUtils.updateBillErrorMsg(recBill,response,"shjh_errormsg"); OperateErrorInfo operateErrorInfo = new OperateErrorInfo(); operateErrorInfo.setMessage((String) response.get("msg")); operateErrorInfo.setErrorLevel(ErrorLevel.Error.name()); diff --git a/main/java/shjh/jhzj7/fi/fi/utils/SapUtils.java b/main/java/shjh/jhzj7/fi/fi/utils/SapUtils.java index c17b2b4..6feb41f 100644 --- a/main/java/shjh/jhzj7/fi/fi/utils/SapUtils.java +++ b/main/java/shjh/jhzj7/fi/fi/utils/SapUtils.java @@ -12,6 +12,7 @@ import kd.bos.orm.query.QCP; import kd.bos.orm.query.QFilter; import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.bos.servicehelper.QueryServiceHelper; +import kd.bos.servicehelper.operation.SaveServiceHelper; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; @@ -758,4 +759,28 @@ public class SapUtils { return sapMap; } + + /** + * 更新单据错误信息 + * @param bill 表单实体 + * @param response 返回参数 + * @param filedName 字段标识 + */ + public static void updateBillErrorMsg(DynamicObject bill,JSONObject response,String filedName){ + try { + String errorMsg = response.getString("msg"); + + if (errorMsg != null && errorMsg.length() > 2000) { + // 记录完整错误信息到日志 + logger.warn("错误信息过长,已截断。完整信息: {}", errorMsg); + // 截取前2000个字符 + errorMsg = errorMsg.substring(0, 2000); + } + + bill.set(filedName, errorMsg); + SaveServiceHelper.update(bill); + } catch (Exception e) { + logger.error("更新单据错误信息报错: {}", e.getMessage()); + } + } }