编辑代码

def list = [
  [id: 1, name: 'John'],
  [id: 2, name: 'Jane'],
  [id: 3, name: 'Alice'],
  [id: 1, name: 'Bob'],
  [id: 1, name: 'Hank'],
  [id: 2, name: 'Mary']
]
//按机构分组 it.id + "_"+ it.name
def groupedMap = list.groupBy { it.id }
//按每个请求100size的detail组装数据
def pcList = new ArrayList()
groupedMap.each { key, value ->
  //按100
  def chunkedList = value.collate(2)
  chunkedList.eachWithIndex { sublist, index ->
    def mp =  new HashMap()
    mp.put("method", "insertDrugInfo")
    mp.put("opter_id", sublist.get(0).get("id"))
    mp.put("opter_name", sublist.get(0).get("name"))
    mp.put("fixmedins_code","P00000000001")
    //map key的转换
    def newSubList = new ArrayList()
    for(mmp in sublist){
        mmp = mmp.collectEntries { k, v ->
            [(k == 'id' ? 'phone' : k): v]
        }
        newSubList.add(mmp)
    }
    //detail节点
    mp.put("detail", newSubList);
    pcList.add(mp)
  }
}

pcList.each { map ->
    println map
}