From 0e18d28ca93a02e65428695142ba38dcda26eb6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awek=20Zatorski?= Date: Fri, 15 Nov 2024 19:41:18 +0100 Subject: [PATCH] transactions matrix --- pom.xml | 12 +- .../sasiedzi/event/domain/Transaction.java | 2 +- .../sasiedzi/event/domain/TransactionDTO.java | 87 ++++++++++++++ .../sasiedzi/event/service/EventService.java | 6 +- .../event/web/rest/TransactionResource.java | 106 ++++++++++++++++-- .../webapp/app/core/jhi-navbar/jhi-navbar.vue | 6 + .../app/entities/transaction/transaction.vue | 5 + .../app/shared/model/transaction.model.ts | 21 ++++ 8 files changed, 230 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/sasiedzi/event/domain/TransactionDTO.java diff --git a/pom.xml b/pom.xml index 8ba330e..e0fbe4b 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,12 @@ - + + org.projectlombok + lombok + 1.18.34 + provided + tech.jhipster @@ -287,6 +292,11 @@ testcontainers test + + org.projectlombok + lombok + provided + diff --git a/src/main/java/com/sasiedzi/event/domain/Transaction.java b/src/main/java/com/sasiedzi/event/domain/Transaction.java index 0b45c85..364a754 100644 --- a/src/main/java/com/sasiedzi/event/domain/Transaction.java +++ b/src/main/java/com/sasiedzi/event/domain/Transaction.java @@ -34,7 +34,7 @@ public class Transaction implements Serializable { @Column(name = "comment") private String comment; - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.EAGER) @JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true) private Event event; diff --git a/src/main/java/com/sasiedzi/event/domain/TransactionDTO.java b/src/main/java/com/sasiedzi/event/domain/TransactionDTO.java new file mode 100644 index 0000000..4f62e72 --- /dev/null +++ b/src/main/java/com/sasiedzi/event/domain/TransactionDTO.java @@ -0,0 +1,87 @@ +package com.sasiedzi.event.domain; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.sasiedzi.event.domain.enumeration.TransactionType; +import jakarta.persistence.*; +import java.io.Serializable; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import lombok.Builder; +import lombok.Data; +import lombok.Getter; +import lombok.Setter; + +//@Data +//@Builder +public class TransactionDTO implements Serializable { + + private Long id; + + private TransactionType type; + + private LocalDate date; + + private String comment; + + private Event event; + + @JsonIgnoreProperties(value = { "transaction", "event" }, allowSetters = true) + private List items = new ArrayList<>(); + + public TransactionDTO(Long id, TransactionType type, LocalDate date, String comment, Event event) { + this.id = id; + this.type = type; + this.date = date; + this.comment = comment; + this.event = event; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public TransactionType getType() { + return type; + } + + public void setType(TransactionType type) { + this.type = type; + } + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } + + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + + public Event getEvent() { + return event; + } + + public void setEvent(Event event) { + this.event = event; + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/src/main/java/com/sasiedzi/event/service/EventService.java b/src/main/java/com/sasiedzi/event/service/EventService.java index 9da692f..8fa3b13 100644 --- a/src/main/java/com/sasiedzi/event/service/EventService.java +++ b/src/main/java/com/sasiedzi/event/service/EventService.java @@ -147,7 +147,7 @@ public class EventService { } else { transaction = allTransactions.get(0); if (transaction.getTransactionItems().isEmpty()) { - transaction.setTransactionItems(new HashSet<>()); + // transaction.setTransactionItems(new HashSet<>()); } transaction.getTransactionItems().clear(); transactionRepository.save(transaction); @@ -155,7 +155,7 @@ public class EventService { if (event.getRegistrations() != null && !event.getRegistrations().isEmpty()) { String fieldServiceAccountName = "Boisko"; String vaultAccountName = "Skarbiec"; - List accounts = userAccountRepository.findAllWithEagerRelationships(); + List accounts = userAccountRepository.findAll(); Map accountsByLogin = new HashMap<>(); accounts.forEach(acc -> { acc @@ -214,7 +214,7 @@ public class EventService { .map(TransactionItem::getAmount) .map(BigDecimal::negate) .reduce(BigDecimal.ZERO, BigDecimal::add); - if (!vaultValue.equals(BigDecimal.ZERO)) { + if (vaultValue.compareTo(BigDecimal.ZERO) != 0) { TransactionItem vaultItem = new TransactionItem(); vaultItem.setEvent(event); vaultItem.setAmount(vaultValue); diff --git a/src/main/java/com/sasiedzi/event/web/rest/TransactionResource.java b/src/main/java/com/sasiedzi/event/web/rest/TransactionResource.java index f32e9e3..05b0810 100644 --- a/src/main/java/com/sasiedzi/event/web/rest/TransactionResource.java +++ b/src/main/java/com/sasiedzi/event/web/rest/TransactionResource.java @@ -1,13 +1,13 @@ package com.sasiedzi.event.web.rest; -import com.sasiedzi.event.domain.Transaction; +import com.sasiedzi.event.domain.*; import com.sasiedzi.event.repository.TransactionRepository; import com.sasiedzi.event.web.rest.errors.BadRequestAlertException; +import java.math.BigDecimal; import java.net.URI; import java.net.URISyntaxException; -import java.util.List; -import java.util.Objects; -import java.util.Optional; +import java.util.*; +import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -148,17 +148,103 @@ public class TransactionResource { * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of transactions in body. */ @GetMapping("") - public List getAllTransactions( + public List getAllTransactions( @RequestParam(name = "eagerload", required = false, defaultValue = "true") boolean eagerload ) { - LOG.debug("REST request to get all Transactions"); - if (eagerload) { - return transactionRepository.findAllWithEagerRelationships(); - } else { - return transactionRepository.findAll(); + // LOG.debug("REST request to get all Transactions"); + // if (eagerload) { + // return transactionRepository.findAllWithEagerRelationships(); + // } else { + // return transactionRepository.findAll(); + // } + List all = transactionRepository.findAll(); + Map accounts = new HashMap<>(); + Map, BigDecimal> items = new HashMap<>(); + all.forEach(transaction -> { + transaction + .getTransactionItems() + .forEach(transactionItem -> { + accounts.put(transactionItem.getUserAccount(), transactionItem); + }); + }); + all.forEach(transaction -> { + transaction + .getTransactionItems() + .forEach(transactionItem -> { + items.put( + Pair.of(transactionItem.getUserAccount(), transaction), + add(items.get(Pair.of(transactionItem.getUserAccount(), transaction)), transactionItem.getAmount()) + ); + }); + }); + List accountsSorted = accounts + .keySet() + .stream() + .sorted(Comparator.comparing(UserAccount::getName).thenComparing(Comparator.comparing(UserAccount::getName))) + .toList(); + + List out = new ArrayList<>(); + for (Transaction transaction : all.stream().sorted(Comparator.comparing(Transaction::getDate)).toList()) { + TransactionDTO transactionDTO = new TransactionDTO( + transaction.getId(), + transaction.getType(), + transaction.getDate(), + transaction.getComment(), + transaction.getEvent() + ); + for (UserAccount userAccount : accountsSorted) { + BigDecimal amount = items.get(Pair.of(userAccount, transaction)); + + TransactionItem item = new TransactionItem(); + item.setUserAccount(userAccount); + transactionDTO.getItems().add(item); + item.setAmount(amount); + } + out.add(transactionDTO); } + + return out; } + BigDecimal add(BigDecimal val, BigDecimal val2) { + if (val == null) return val2; + return val.add(val2); + } + + // private Comparator getMyAccountFirstComparator (final String myAccountName) { + // + // Comparator customOrder = Comparator.comparing(s -> { + // if (s.getName().equals(myAccountName)) { + // return 1; + // } else if (s.getName().equals("Boisko")) { + // return s.getName(); + // } else if (s.getName().equals("Skarbiec")) { + // return 3; + // } + // return 4; + // }); + // + // return customOrder.thenComparing(UserAccount::getName); + // + // } + + // public class CustomComparator implements Comparator { + // + // @Override + // public int compare(UserAccount o1, UserAccount o2) { + // + // + // // Najpierw sortujemy według zdefiniowanego porządku + // int typeComparison = customOrder.compare(o1.getType(), o2.getType()); + // if (typeComparison != 0) { + // return typeComparison; + // } + // + // // Jeśli typy są takie same, sortujemy według nazwy + // return o1.getName().compareTo(o2.getName()); + // } + // } + /** * {@code GET /transactions/:id} : get the "id" transaction. * diff --git a/src/main/webapp/app/core/jhi-navbar/jhi-navbar.vue b/src/main/webapp/app/core/jhi-navbar/jhi-navbar.vue index 1108164..82ad2ed 100644 --- a/src/main/webapp/app/core/jhi-navbar/jhi-navbar.vue +++ b/src/main/webapp/app/core/jhi-navbar/jhi-navbar.vue @@ -30,6 +30,12 @@ Wszystkie wydarzenia + + + + Finanse + + diff --git a/src/main/webapp/app/entities/transaction/transaction.vue b/src/main/webapp/app/entities/transaction/transaction.vue index ef26313..870e1cd 100644 --- a/src/main/webapp/app/entities/transaction/transaction.vue +++ b/src/main/webapp/app/entities/transaction/transaction.vue @@ -31,7 +31,11 @@ Type Date Comment + Event + + {{ item.userAccount?.name }} + @@ -50,6 +54,7 @@ }} + {{ item.amount }}
diff --git a/src/main/webapp/app/shared/model/transaction.model.ts b/src/main/webapp/app/shared/model/transaction.model.ts index 23240cb..c21fba8 100644 --- a/src/main/webapp/app/shared/model/transaction.model.ts +++ b/src/main/webapp/app/shared/model/transaction.model.ts @@ -1,12 +1,14 @@ import { type IEvent } from '@/shared/model/event.model'; import { type TransactionType } from '@/shared/model/enumerations/transaction-type.model'; +import type { IUserAccount } from '@/shared/model/user-account.model'; export interface ITransaction { id?: number; type?: keyof typeof TransactionType | null; date?: Date | null; comment?: string | null; event?: IEvent | null; + items?: ITransactionItem[] | []; } export class Transaction implements ITransaction { @@ -16,5 +18,24 @@ export class Transaction implements ITransaction { public date?: Date | null, public comment?: string | null, public event?: IEvent | null, + public items?: ITransactionItem[] | [], + ) {} +} + +export interface ITransactionItem { + id?: number; + comment?: string | null; + event?: IEvent | null; + amount?: number | null; + userAccount?: IUserAccount | null; +} + +export class TransactionItem implements ITransactionItem { + constructor( + public id?: number, + public comment?: string | null, + public event?: IEvent | null, + public amount?: number | null, + public userAccount?: IUserAccount | null, ) {} }