transactions matrix
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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<TransactionItem> 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<TransactionItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<TransactionItem> items) {
|
||||
this.items = items;
|
||||
}
|
||||
}
|
||||
@@ -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<UserAccount> accounts = userAccountRepository.findAllWithEagerRelationships();
|
||||
List<UserAccount> accounts = userAccountRepository.findAll();
|
||||
Map<String, UserAccount> 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);
|
||||
|
||||
@@ -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<Transaction> getAllTransactions(
|
||||
public List<TransactionDTO> 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<Transaction> all = transactionRepository.findAll();
|
||||
Map<UserAccount, TransactionItem> accounts = new HashMap<>();
|
||||
Map<Pair<UserAccount, Transaction>, 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<UserAccount> accountsSorted = accounts
|
||||
.keySet()
|
||||
.stream()
|
||||
.sorted(Comparator.comparing(UserAccount::getName).thenComparing(Comparator.comparing(UserAccount::getName)))
|
||||
.toList();
|
||||
|
||||
List<TransactionDTO> 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<UserAccount> getMyAccountFirstComparator (final String myAccountName) {
|
||||
//
|
||||
// Comparator<UserAccount> 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<UserAccount> {
|
||||
//
|
||||
// @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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user