0

我的 mule 代码正在打两个表并获取一些详细信息。第一个是订单详细信息,我将其存储在流变量中,即订单,另一个数据库正在返回我存储在orderitem变量中的订单项目详细信息。

我想根据一个条件聚合两个有效负载。每个 orderId 都有订单项(存储在 flowVars.orderitem 中)并将这些订单项映射到各自的 orderID。

flowVars.order 值如下

[{partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=12345, orderDate=2017-02-28 16:41:41.0, id=22}, {partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=123456, orderDate=2017-02-28 16:41:41.0, id=23}, {partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=11111, orderDate=2017-02-28 16:41:41.0, id=24}, {partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=321123, orderDate=2017-05-19 15:25:41.0, id=26}]

flowVars.orderitem 值如下

 [{productCode=ELT-LP-ICND1-020067, orderId=12345, quantity=10, id=14}, {productCode=ELT-IP-ICND1-1.0, orderId=12345, quantity=11, id=15}, {productCode=ELT-LP-ICND1-020067, orderId=123456, quantity=12, id=16}, {productCode=ELT-IP-ICND1-1.0, orderId=123456, quantity=13, id=17}, {productCode=ELT-LP-ICND1-020067, orderId=11111, quantity=14, id=18}, {productCode=ELT-IP-ICND1-1.0, orderId=11111, quantity=15, id=19}, {productCode=ELT-LP-ICND2-020067, orderId=321123, quantity=5, id=20}]

预期产出

[
  {
    "orderId": "12345",
    "orderDate": "2017-02-28T16:41:41",
    "partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderItems": [
       {
          "productCode": "ELT-LP-ICND1-020067",
          "quantity": "10"
        },
        {
          "productCode": "ELT-IP-ICND1-1.0",
          "quantity": "11"
        }
    ]
  },
  {
    "orderId": "123456",
    "orderDate": "2017-02-28T16:41:41",
    "partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderItems": [
        {
          "productCode": "ELT-LP-ICND1-020067",
          "quantity": "12"
        },
        {
          "productCode": "ELT-IP-ICND1-1.0",
          "quantity": "13"
        }
    ]
  },
  {
    "orderId": "11111",
    "orderDate": "2017-02-28T16:41:41",
    "partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderItems": [
       {
          "productCode": "ELT-LP-ICND1-020067",
          "quantity": "14"
        },
        {
          "productCode": "ELT-IP-ICND1-1.0",
          "quantity": "15"
        }
    ]
  },
  {
    "orderId": "321123",
    "orderDate": "2017-05-19T15:25:41",
    "partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderItems": [
      {
          "productCode": "ELT-LP-ICND1-020067",
          "quantity": "5"
        }
    ]
  }
]

在这里,我需要显示订单的各个订单项目详细信息。所以基本上我需要结合两个有效载荷。

我尝试使用 dataweave 但没有运气。

数据编织代码:

%dw 1.0
%output application/json
%var mergeddata = flowVars.orderitem groupBy $.orderId  
---
 flowVars.order map ((data,index) ->
     {
    orderid: data.orderId,
    partnerid: data.partnerId,
    orderdate: data.orderDate,
    order: flowVars.orderitem default [] map ((data1 ,indexOf)  ->
           {
            (productcode: data1.productCode) when (data1.orderId == data.orderId),      
            (quantity: data1.quantity) when (data1.orderId == data.orderId) ,
            (id: data1.id) when (data1.orderId == data.orderId) 
            }

            )})  

以及转换后的输出:

{
    "orderid": "12345",
    "partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderdate": "2017-02-28T16:41:41",
    "order": [
      {
        "productcode": "ELT-LP-ICND1-020067",
        "quantity": 10,
        "id": 14
      },
      {
        "productcode": "ELT-IP-ICND1-1.0",
        "quantity": 11,
        "id": 15
      },
      {

      },
      {

      },
      {

      },
      {

      },
      {

      }
    ]
  },
  {
    "orderid": "123456",
    "partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderdate": "2017-02-28T16:41:41",
    "order": [
      {

      },
      {

      },
      {
        "productcode": "ELT-LP-ICND1-020067",
        "quantity": 12,
        "id": 16
      },
      {
        "productcode": "ELT-IP-ICND1-1.0",
        "quantity": 13,
        "id": 17
      },
      {

      },
      {

      },
      {

      }
    ]
  },
  {
    "orderid": "11111",
    "partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderdate": "2017-02-28T16:41:41",
    "order": [
      {

      },
      {

      },
      {

      },
      {

      },
      {
        "productcode": "ELT-LP-ICND1-020067",
        "quantity": 14,
        "id": 18
      },
      {
        "productcode": "ELT-IP-ICND1-1.0",
        "quantity": 15,
        "id": 19
      },
      {

      }
    ]
  },
  {
    "orderid": "321123",
    "partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderdate": "2017-05-19T15:25:41",
    "order": [
      {

      },
      {

      },
      {

      },
      {

      },
      {

      },
      {

      },
      {
        "productcode": "ELT-LP-ICND2-020067",
        "quantity": 5,
        "id": 20
      }
    ]
  }
]

如您所见,我几乎就在那里,并且能够将订单项目详细信息与相应的 orderId 映射,但在转换后我仍然得到一些空白值。

任何人都可以帮助我实现预期的输出。提前致谢!!!

4

1 回答 1

0

您需要过滤 flowVars.orderitem 映射,而不是完全迭代它并且仅在 orderId 匹配时打印值。

    order: ((flowVars.orderitem default []) filter (data.orderId == $.orderId)) map ((data1 ,indexOf) -> {
            productcode: data1.productCode,      
            quantity: data1.quantity
            id: data1.id
    })

然后,您也可以删除所有这些“何时”语句。

于 2017-05-26T02:08:29.820 回答