|
1 | 1 | package app.banking.repository; |
2 | 2 |
|
| 3 | +import app.banking.DTO.AmountOnDate; |
3 | 4 | import app.banking.DTO.StateItem; |
| 5 | +import app.banking.DTO.SumDebitsAndCredits; |
4 | 6 | import app.banking.models.*; |
5 | 7 | import lombok.SneakyThrows; |
6 | 8 | import org.springframework.stereotype.Repository; |
7 | 9 | import postgres.addict.Queries; |
8 | 10 |
|
| 11 | +import java.sql.Date; |
9 | 12 | import java.sql.ResultSet; |
10 | 13 | import java.time.LocalDate; |
11 | 14 | import java.util.ArrayList; |
|
14 | 17 |
|
15 | 18 | import static app.banking.models.TransactionStatus.CANCELED; |
16 | 19 | import static app.banking.models.TransactionStatus.NO_PAYED; |
| 20 | +import static app.banking.models.TransactionType.CREDIT; |
| 21 | +import static app.banking.models.TransactionType.DEBIT; |
17 | 22 |
|
18 | 23 | @Repository |
19 | 24 | public class TransactionRepository extends CommonCrud<Transaction, Long> { |
@@ -176,4 +181,44 @@ public List<Transaction> findAllByIdAndType(Long accountId, TransactionType filt |
176 | 181 | .end() |
177 | 182 | ); |
178 | 183 | } |
| 184 | + |
| 185 | + private SumDebitsAndCredits mapResultSet(ResultSet resultSet){ |
| 186 | + List<AmountOnDate> incomes = new ArrayList<>(); |
| 187 | + List<AmountOnDate> outcomes = new ArrayList<>(); |
| 188 | + |
| 189 | + try { |
| 190 | + while (resultSet.next()) { |
| 191 | + Date date = resultSet.getDate("period_start"); |
| 192 | + double income = resultSet.getDouble("income"); |
| 193 | + double outcome = resultSet.getDouble("expense"); |
| 194 | + incomes.add(AmountOnDate |
| 195 | + .builder() |
| 196 | + .iteration(date.toLocalDate()) |
| 197 | + .amount(income) |
| 198 | + .build()); |
| 199 | + outcomes.add(AmountOnDate |
| 200 | + .builder() |
| 201 | + .iteration(date.toLocalDate()) |
| 202 | + .amount(outcome) |
| 203 | + .build()); |
| 204 | + } |
| 205 | + }catch (Exception ignored){} |
| 206 | + |
| 207 | + return SumDebitsAndCredits |
| 208 | + .builder() |
| 209 | + .listIncomes(incomes) |
| 210 | + .listOutComes(outcomes) |
| 211 | + .build(); |
| 212 | + } |
| 213 | + |
| 214 | + @SneakyThrows |
| 215 | + public SumDebitsAndCredits findSumDebitsAndCreditsByAccountId(Long accountId, String sort){ |
| 216 | + Queries queries = Queries |
| 217 | + .select() |
| 218 | + .from(Queries.calls("get_income_expense_data_custom_period", sort)) |
| 219 | + .where() |
| 220 | + .equals("id_account", accountId) |
| 221 | + .end(); |
| 222 | + return mapResultSet(executeQueries(queries)); |
| 223 | + } |
179 | 224 | } |
0 commit comments