How to join and group a list of repeated objects in java

Viewed 54

I need help. I'm trying to get the java return to validate if the name (bankAccount) attribute of the "History" query bank" are the same. If yes then group the day and total attributes. If you have a way to adjust the SQL query so that it makes the correct return even better.

Observation: The idea of ​​the query is to return the last account balance record by day of the month.

SpringBoot Java Backend return:

    [
    {
        "bankAccount": "Intermedium",
        "day": "2021-12-01T12:55:43.559742",
        "total": 2000.00
    },
    {
        "bankAccount": "Intermedium",
        "day": "2021-12-02T08:37:18.316628",
        "total": 1000.00
    },
    {
        "bankAccount": "Intermedium",
        "day": "2021-12-03T08:20:52.86353",
        "total": 2300.00
    },
    {
        "bankAccount": "Santander",
        "day": "2021-12-01T12:55:54.783701",
        "total": 3000.00
    },
    {
        "bankAccount": "Santander",
        "day": "2021-12-02T08:37:03.832318",
        "total": 2000.00
    },
    {
        "bankAccount": "Santander",
        "day": "2021-12-03T08:21:08.954686",
        "total": 1500.00
    },
    {
        "bankAccount": "Nubank",
        "day": "2021-12-01T12:55:25.98958",
        "total": 1000.00
    },
    {
       "bankAccount": "Nubank",
        "day": "2021-12-02T08:36:54.141208",
        "total": 500.00
    },
    {
       "bankAccount": "Nubank",
        "day": "2021-12-03T08:20:33.213685",
        "total": 700.00
    }
]

What am i trying:

//The idea is to be a single object, being able to have more days and more totals.
[

    {
    "bankAccount": "Intermedium",
    "day": ["2021-12-01T12:55:43.559742","2021-12-02T08:37:18.316628","2021-12- 
           03T08:20:52.86353",]
    "total": [2000.00, 1000.00, 2300.00]
    },

    {
        "bankAccount": "Santander",
        "day": ["2021-12-01T12:55:54.783701","2021-12-02T08:37:03.832318","2021-12- 
               03T08:21:08.954686",]
        "total": [3000.00, 2000.00, 1500.00]
    },

    {
        "bankAccount": "Nubank",
        "day": ["2021-12-01T12:55:25.98958","2021-12-02T08:36:54.141208","2021-12- 
               03T08:20:33.213685",]
        "total": [1000.00, 500.00, 700.00]
    },
   
]

My DTO:

    @AllArgsConstructor
    @Getter
    @Setter
    public class BankAccountHistoryStatisticByDayDTO {

    private String bankAccount;

    private LocalDateTime day;

    private BigDecimal total;


}

My Class:

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
public class BankAccountHistory extends BaseEntity {


    @NotNull
    private LocalDateTime date;

    @ManyToOne
    @JoinColumn(name = "bank_account_id")
    private BankAccount bankAccount;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @NotNull
    private String name;

    @NotNull
    private BigDecimal balance;


   //EqualsandHash...
}

My Service:

   public List<BankAccountHistoryStatisticByDayDTO> byDay(LocalDateTime monthReference, Long userId) {
        LocalDateTime startDate = monthReference
                .withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endDate = monthReference.withDayOfMonth(monthReference.getMonth().maxLength())
                .withHour(23).withMinute(59).withSecond(59);

        List<BankAccountHistory> list = historyRepository.byDay(startDate, endDate, userId);


        List<BankAccountHistoryStatisticByDayDTO> listDto = list.stream()
                .map(obj -> new BankAccountHistoryStatisticByDayDTO(obj.getName(), obj.getDate(), obj.getBalance()))
                .collect(Collectors.toList());

     


        return listDto;
    }

My Repository:

    public interface HistoryRepository extends JpaRepository<BankAccountHistory, Long>,
        JpaSpecificationExecutor<BankAccountHistory> , BankAccountRepositoryQueries {

    @Query(nativeQuery = true, value = "SELECT * " +
            "FROM bank_account_history b1 " +
            "INNER JOIN (SELECT b.name,max(b.date) as maxdate,b.user_id " +
            "FROM bank_account_history b " +
            "GROUP BY b.user_id,b.name, CAST(b.date as date)) as b2 ON b2.user_id = b1.user_id AND b2.name = b1.name AND b2.maxdate = b1.date " +
            "where b1.user_id = :userId " +`enter code here`
            "and b1.date >= :startDate " +
            "AND b1.date <= :endDate " +
            "order by b1.name, b1.date asc ")
    List<BankAccountHistory> byDay(@Param("startDate") LocalDateTime startDate,
                                              @Param("endDate") LocalDateTime endDate, Long userId);
}
0 Answers
Related