diff --git a/nifi-hzyadev-bundle/hzya-nifi-Zjnx-czb-processors/src/main/resources/js/SearchCriteria.js b/nifi-hzyadev-bundle/hzya-nifi-Zjnx-czb-processors/src/main/resources/js/SearchCriteria.js new file mode 100644 index 0000000..64595ad --- /dev/null +++ b/nifi-hzyadev-bundle/hzya-nifi-Zjnx-czb-processors/src/main/resources/js/SearchCriteria.js @@ -0,0 +1,76 @@ +var relationships = context.getAvailableRelationships(); +var REL_SUCCESS = null; +var REL_FAILURE = null; + +relationships.forEach(function(rel) { + if (rel.getName() === 'success') { + REL_SUCCESS = rel; + } else if (rel.getName() === 'failure') { + REL_FAILURE = rel; + } +}); + +/** + * 辅助函数:获取并格式化当前日期为 'YYYY-MM-DD' 格式 + */ +function getCurrentDateFormatted() { + var today = new Date(); + var year = today.getFullYear(); + var month = today.getMonth() + 1; + if (month < 10) { + month = '0' + month; + } + var day = today.getDate(); + if (day < 10) { + day = '0' + day; + } + return year + '-' + month + '-' + day; +} + +var flowFile = session.get(); +if (flowFile === null) { + flowFile = session.create(); +} + +try { + if (REL_SUCCESS === null) { + throw new Error("在处理器的可用关系中找不到 'success' 关系。"); + } + + var jsonData = { + "erpSysFlag": "0000", + "bizContent": { + "bankAccNo": "201000270605501", + "queryBeginDate": "", + "queryEndDate": "", + "current": 1, + "size": 100 + } + }; + + var currentDate = getCurrentDateFormatted(); + //测试 + jsonData.bizContent.queryBeginDate = "2021-01-01"; + jsonData.bizContent.queryEndDate = "2021-01-31"; + + var jsonString = JSON.stringify(jsonData); + + // 关键修正:为了解决Nashorn引擎对 session.write 的方法签名歧义问题, + // 我们必须显式地创建一个 org.apache.nifi.processor.io.OutputStreamCallback 的实例。 + flowFile = session.write(flowFile, new (Java.type('org.apache.nifi.processor.io.OutputStreamCallback'))({ + process: function(outputStream) { + outputStream.write(jsonString.getBytes("UTF-8")); + } + })); + + session.transfer(flowFile, REL_SUCCESS); + +} catch (e) { + log.error('处理 JSON 并创建 FlowFile 时出错。', e); + + if (REL_FAILURE !== null) { + session.transfer(flowFile, REL_FAILURE); + } else { + session.remove(flowFile); + } +} \ No newline at end of file diff --git a/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-nar/pom.xml b/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-nar/pom.xml index b76d4e0..1321d62 100644 --- a/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-nar/pom.xml +++ b/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-nar/pom.xml @@ -21,7 +21,7 @@ com.hzya - hzya-nifi-Zjnx-czb-processors + hzya-nifi-Zsyh-cbs-processors 1.0 diff --git a/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-processors/pom.xml b/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-processors/pom.xml index 1f1c168..8b208e4 100644 --- a/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-processors/pom.xml +++ b/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-processors/pom.xml @@ -95,11 +95,11 @@ slf4j-simple 1.7.36 - - commons-logging - commons-logging - 1.2 - + + + + + commons-codec diff --git a/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-processors/src/main/resources/js/SearchCriteria.js b/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-processors/src/main/resources/js/SearchCriteria.js new file mode 100644 index 0000000..1fa8c07 --- /dev/null +++ b/nifi-hzyadev-bundle/hzya-nifi-Zsyh-cbs-processors/src/main/resources/js/SearchCriteria.js @@ -0,0 +1,48 @@ +//2025-09-10 10:19:05 动态计算查询结果脚本 + +// 从上下文中获取所有可用的关系 +var relationships = context.getAvailableRelationships(); +var REL_SUCCESS = null; +var REL_FAILURE = null; + +relationships.forEach(function (rel) { + if (rel.getName() === 'success') { + REL_SUCCESS = rel; + } else if (rel.getName() === 'failure') { + REL_FAILURE = rel; + } +}); + +var flowFile = session.get(); +if (flowFile === null) { + flowFile = session.create(); +} + +try { + if (REL_SUCCESS === null) { + throw new Error("在处理器的可用关系中找不到 'success' 关系。"); + } + + var currentDate = java.time.LocalDate.now().toString(); + + var jsonData = { + "applyDateStart": currentDate, "applyDateEnd": currentDate, "initAccountList": "591915131310106" + }; + + var jsonString = JSON.stringify(jsonData); + + flowFile = session.write(flowFile, new (Java.type('org.apache.nifi.processor.io.OutputStreamCallback'))({ + process: function (outputStream) { + outputStream.write(jsonString.getBytes("UTF-8")); + } + })); + + session.transfer(flowFile, REL_SUCCESS); +} catch (e) { + log.error('处理 JSON 并创建 FlowFile 时出错。', e); + if (REL_FAILURE !== null) { + session.transfer(flowFile, REL_FAILURE); + } else { + session.remove(flowFile); + } +} \ No newline at end of file