Groovy web console

subscribe to the feed Subscribe
to this
site

loopsAndGroupBy

Published 1 month ago by aeischeid with tags groupBy each
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def rows = [
      [account_key:5781, account_name:"Swine Vet Center", name: "Henry Johnson", issueing_vet_origin_stateprovince_license: "8460 - IA", species: "Swine", count: 1],
      [account_key:5781, account_name:"Swine Vet Center", name: "Chris Sievers DVM", issueing_vet_origin_stateprovince_license: "8321 - IA", species: "Swine", count: 6],
      [account_key:5781, account_name:"Swine Vet Center", name: "Jacob Schwartz", issueing_vet_origin_stateprovince_license: "8161 - IA", species: "Swine", count: 1],
      [account_key:5781, account_name:"Swine Vet Center", name: "Jeff Feder DVM", issueing_vet_origin_stateprovince_license: "6562 - IA", species: "Swine", count: 1],
      [account_key:5781, account_name:"Swine Vet Center", name: "Jordan Graham DVM", issueing_vet_origin_stateprovince_license: "8079 - IA", species: "Swine", count: 10],
      [account_key:5781, account_name:"Swine Vet Center", name: "Paul E. Yeske DVM", issueing_vet_origin_stateprovince_license: "5458 - IA", species: "Swine", count: 13],
      [account_key:5781, account_name:"Swine Vet Center", name: "Ross Kiehne DVM", issueing_vet_origin_stateprovince_license: "6712 - IA", species: "Swine", count: 36],
      [account_key:5781, account_name:"Swine Vet Center", name: "Ryan Strobel DVM",, issueing_vet_origin_stateprovince_license: "8425 - IA", species: "Swine", count: 11],
      [account_key:7501, account_name:"Audubon-Manning Veterinary Clinic", name: "Gavin Yager DVM", issueing_vet_origin_stateprovince_license: "7629 - IA", species: "Bovine", count:6],
      [account_key:7501, account_name:"Audubon-Manning Veterinary Clinic", name: "Gavin Yager DVM", issueing_vet_origin_stateprovince_license: "7629 - IA", species: "Caprine", count:3],
      [account_key:7501, account_name:"Audubon-Manning Veterinary Clinic", name: "Gavin Yager DVM", issueing_vet_origin_stateprovince_license: "7629 - IA", species: "Swine", count:2],
      [account_key:7501, account_name:"Audubon-Manning Veterinary Clinic", name: "Jodie Pettit DVM", issueing_vet_origin_stateprovince_license: "6828 - IA", species: "Bovine", count:2],
      [account_key:7501, account_name:"Audubon-Manning Veterinary Clinic", name: "Jodie Pettit DVM", issueing_vet_origin_stateprovince_license: "6828 - IA", species: "Equine", count:4],
      [account_key:7501, account_name:"Audubon-Manning Veterinary Clinic", name: "Jodie Pettit DVM", issueing_vet_origin_stateprovince_license: "6828 - IA", species: "Swine", count:2],
      [account_key:7501, account_name:"Audubon-Manning Veterinary Clinic", name: "John A. Greving DVM", issueing_vet_origin_stateprovince_license: "7984 - IA", species: "Bovine", count:2],
      [account_key:7501, account_name:"Audubon-Manning Veterinary Clinic", name: "Josh Ellingson DVM", issueing_vet_origin_stateprovince_license: "7728 - IA", species: "Swine", count:3],
      [account_key:7501, account_name:"Audubon-Manning Veterinary Clinic", name: "Paul Thomas DVM", issueing_vet_origin_stateprovince_license: "7995 - IA", species: "Swine", count:3]
    ]

    
    def grouped = rows.groupBy({ it.account_key }, { it.name })

//println grouped["7501"]["Gavin Yager DVM"]

Integer count = 0
def newRows = [:]

grouped.each{k,v->
    //println v
    println "accountId:" + k
    v.each{name, speciesEntries ->
        println name
        def newRou = [
            speciesEntries[0].account_key,
            speciesEntries[0].account_name,
            name,
            speciesEntries[0].issueing_vet_origin_stateprovince_license,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
        ]
        speciesEntries.each{
            switch(it.species) {
                case "Equine":
                    newRou[4] = it.count
                case "Swine":
                    newRou[5] = it.count
                case "Bovine":
                    newRou[6] = it.count
                case "Camelid":
                    newRou[7] = it.count
                case "Caprine":
                    newRou[8] = it.count
                case "Cervidae":
                    newRou[9] = it.count
                case "Ovine":
                    newRou[10] = it.count
                case "Poultry":
                    newRou[11] = it.count
            }
        }
        newRows[count] = newRou    
    }
    
    count++
}

return newRows