Skip to content

Commit 7ff352e

Browse files
Merge pull request #122 from phuuthanh-dev/develop
dashboard
2 parents 7dbba12 + 079d605 commit 7ff352e

File tree

7 files changed

+51
-41
lines changed

7 files changed

+51
-41
lines changed

src/main/java/vn/webapp/backend/auction/controller/DashBoardController.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ public class DashBoardController {
1818
public ResponseEntity<DashBoardResponse> getDashBoardTotal(
1919
@RequestParam("yearGetRegisterAccount") Integer yearGetRegisterAccount,
2020
@RequestParam("yearGetAuction") Integer yearGetAuction,
21-
@RequestParam("yearGetRevenue") Integer yearGetRevenue
21+
@RequestParam("yearGetRevenue") Integer yearGetRevenue,
22+
@RequestParam("yearGetAuctionFailedAndSuccess") Integer yearGetAuctionFailedAndSuccess,
23+
@RequestParam("monthGetAuctionFailedAndSuccess") Integer monthGetAuctionFailedAndSuccess,
24+
@RequestParam("yearGetUserJoinAuction") Integer yearGetUserJoinAuction
2225
) {
23-
return ResponseEntity.ok(dashBoardService.getInformation(yearGetRegisterAccount, yearGetAuction, yearGetRevenue));
26+
return ResponseEntity.ok(dashBoardService.getInformation(yearGetRegisterAccount, yearGetAuction, yearGetRevenue,
27+
yearGetAuctionFailedAndSuccess, monthGetAuctionFailedAndSuccess,
28+
yearGetUserJoinAuction));
2429
}
2530
}

src/main/java/vn/webapp/backend/auction/controller/RequestApprovalController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public ResponseEntity<RequestApproval> getRequestById(@PathVariable Integer id)
3030

3131
@PutMapping("/set-state/{id}")
3232
public ResponseEntity<RequestApproval> setState(@PathVariable Integer id, @RequestParam Integer responderId, @RequestParam String state) {
33-
requestApprovalService.setRequestState(id,responderId, state);
33+
requestApprovalService.setRequestState(id, responderId, state);
3434
return ResponseEntity.ok().build();
3535
}
3636

@@ -57,7 +57,7 @@ public ResponseEntity<Page<RequestApproval>> getRequestByRoleOfSender(
5757
@RequestParam(defaultValue = "asc") String sortOrder) {
5858
Sort.Direction direction = (sortOrder.equalsIgnoreCase("asc")) ? Sort.Direction.ASC : Sort.Direction.DESC;
5959
Pageable pageable = PageRequest.of(page, size, direction, sortBy);
60-
return ResponseEntity.ok(requestApprovalService.getRequestBySenderRole(role,jewelryName,category,pageable));
60+
return ResponseEntity.ok(requestApprovalService.getRequestBySenderRole(role, jewelryName, category, pageable));
6161
}
6262

6363

@@ -99,7 +99,7 @@ public ResponseEntity<Page<RequestApproval>> getRequestApprovalByUserId(
9999
@RequestParam(defaultValue = "desc") String sortOrder) {
100100
Sort.Direction direction = (sortOrder.equalsIgnoreCase("asc")) ? Sort.Direction.ASC : Sort.Direction.DESC;
101101
Pageable pageable = PageRequest.of(page, size, direction, sortBy);
102-
return ResponseEntity.ok(requestApprovalService.getRequestApprovalByUserId(id, jewelryName,pageable));
102+
return ResponseEntity.ok(requestApprovalService.getRequestApprovalByUserId(id, jewelryName, pageable));
103103
}
104104

105105
@GetMapping("/request-passed")

src/main/java/vn/webapp/backend/auction/dto/DashBoardResponse.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
@AllArgsConstructor
1111
@NoArgsConstructor
1212
public class DashBoardResponse {
13-
private Integer totalAuctions;
1413
private Double totalRevenueToday;
1514
private Integer totalUser;
1615
private Integer totalJewelryActive;
@@ -23,12 +22,11 @@ public class DashBoardResponse {
2322
private Integer totalStaffs;
2423
private Integer totalManagers;
2524
private Integer totalAdmins;
26-
private double percentAuctionFailed;
27-
private double percentAuctionSuccess;
28-
private double participationRate;
29-
private double notParticipationRate;
25+
private double auctionFailed;
26+
private double auctionSuccess;
27+
private Double[] totalParticipationByMonth;
3028
private Integer[] totalUsersByMonth;
3129
private Integer[] totalAuctionByMonth;
32-
private Double totalRevenue;
30+
private Double[] totalRevenueNear10Year;
3331
private Double[] totalRevenueByMonth;
3432
}

src/main/java/vn/webapp/backend/auction/repository/AuctionRegistrationRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public interface AuctionRegistrationRepository extends JpaRepository<AuctionRegi
2828
@Query("SELECT SUM(ar.registrationFee) FROM AuctionRegistration ar")
2929
Double sumTotalRegistrationFee();
3030

31-
@Query("SELECT COUNT(DISTINCT ar.user.id) FROM AuctionRegistration ar WHERE ar.auctionRegistrationState = 'VALID'")
32-
Long countDistinctUsersRegistered();
31+
@Query("SELECT COUNT(DISTINCT ar.user.id) FROM AuctionRegistration ar WHERE ar.auctionRegistrationState = 'VALID' AND MONTH(ar.registrationDate) = :month AND YEAR(ar.registrationDate) = :year")
32+
Long countDistinctUsersRegistered(@Param("month") Integer month, @Param("year") Integer year);
3333

3434
@Query("SELECT COUNT(ar) FROM AuctionRegistration ar WHERE ar.auction.id = :auctionId AND ar.auctionRegistrationState = 'VALID'")
3535
Integer countValidParticipantsByAuctionId(@Param("auctionId") Integer auctionId);

src/main/java/vn/webapp/backend/auction/repository/AuctionRepository.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ Page<Auction> findByStateAndCategoryNotDeletedOrEmptyState(
5454
Integer countAllAuctionsFinished();
5555

5656
@Query("SELECT COUNT(a) FROM Auction a WHERE a.state = 'FINISHED' " +
57-
"AND a.id NOT IN (SELECT ah.auction.id FROM AuctionHistory ah)")
58-
Integer countAllAuctionsFailed();
57+
"AND a.id NOT IN (SELECT ah.auction.id FROM AuctionHistory ah) AND YEAR(a.endDate) = :year AND MONTH(a.endDate) = :month")
58+
Integer countAllAuctionsFailed(@Param("month") Integer month, @Param("year") Integer year);
5959

6060
@Query("SELECT COUNT(a) FROM Auction a WHERE a.state = 'FINISHED' " +
61-
"AND a.id IN (SELECT ah.auction.id FROM AuctionHistory ah)")
62-
Integer countAllAuctionsSuccessful();
61+
"AND a.id IN (SELECT ah.auction.id FROM AuctionHistory ah) AND YEAR(a.endDate) = :year AND MONTH(a.endDate) = :month")
62+
Integer countAllAuctionsSuccessful(@Param("month") Integer month, @Param("year") Integer year);
6363

6464
@Query("SELECT COUNT(a) FROM Auction a " +
6565
"WHERE MONTH(a.createDate) = :month AND YEAR(a.createDate) = :year")

src/main/java/vn/webapp/backend/auction/repository/TransactionRepository.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ Page<Transaction> findTransactionByTypeAndState(
3333
@Query("SELECT COUNT(t) FROM Transaction t WHERE t.type = 'REGISTRATION' AND t.user.username = :username")
3434
Integer getCountTransactionsRegistrationByUsername(@Param("username") String username);
3535

36-
@Query("SELECT SUM(t.totalPrice * 0.08) FROM Transaction t WHERE t.type = 'PAYMENT_TO_WINNER' AND t.state = 'SUCCEED'")
37-
Double getTotalCommissionRevenue();
36+
@Query("SELECT SUM(t.totalPrice * 0.08) FROM Transaction t WHERE t.type = 'PAYMENT_TO_WINNER' AND t.state = 'SUCCEED' AND YEAR(t.paymentTime) = :year")
37+
Double getTotalCommissionRevenueByYear(@Param("year") Integer year);
3838

39-
@Query("SELECT SUM(t.auction.participationFee) FROM Transaction t WHERE t.type = 'REGISTRATION' AND t.state = 'SUCCEED'")
40-
Double getTotalRegistrationFeeRevenue();
39+
@Query("SELECT SUM(t.auction.participationFee) FROM Transaction t WHERE t.type = 'REGISTRATION' AND t.state = 'SUCCEED' AND YEAR(t.paymentTime) = :year")
40+
Double getTotalRegistrationFeeRevenueByYear(@Param("year") Integer year);
4141

4242
@Query("SELECT COALESCE(SUM(t.totalPrice * 0.08), 0) " +
4343
"FROM Transaction t " +

src/main/java/vn/webapp/backend/auction/service/dashboard/DashBoardService.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import vn.webapp.backend.auction.enums.Role;
1010
import vn.webapp.backend.auction.repository.*;
1111

12+
import java.time.Year;
13+
1214
import java.time.LocalDateTime;
1315

1416
@Service
@@ -21,38 +23,35 @@ public class DashBoardService {
2123
private final AuctionRegistrationRepository auctionRegistrationRepository;
2224
private final TransactionRepository transactionRepository;
2325

24-
public DashBoardResponse getInformation(Integer yearGetRegisterAccount, Integer yearGetAuction, Integer yearGetRevenue) {
26+
public DashBoardResponse getInformation(Integer yearGetRegisterAccount, Integer yearGetAuction, Integer yearGetRevenue,
27+
Integer yearGetAuctionFailedAndSuccess, Integer monthGetAuctionFailedAndSuccess,
28+
Integer yearGetUserJoinAuction) {
2529
LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
2630
LocalDateTime startOfNextDay = startOfDay.plusDays(1);
2731

2832
// Total counts
2933
Integer totalUser = userRepository.getTotalUser();
30-
Integer totalAuction = auctionRepository.countAllAuctions();
3134
Integer totalActiveJewelry = jewelryRepository.countAllJewelriesByState(JewelryState.ACTIVE);
3235
Integer totalApprovingJewelry = jewelryRepository.countAllJewelriesByState(JewelryState.APPROVING);
3336
Integer totalAuctionJewelry = jewelryRepository.countAllJewelriesByState(JewelryState.AUCTION);
34-
Integer totalAuctionFailed = auctionRepository.countAllAuctionsFailed();
35-
Integer totalAuctionSuccess = auctionRepository.countAllAuctionsSuccessful();
36-
Integer totalAuctionsFinished = auctionRepository.countAllAuctionsFinished();
37+
Integer auctionFailed = auctionRepository.countAllAuctionsFailed(monthGetAuctionFailedAndSuccess, yearGetAuctionFailedAndSuccess);
38+
Integer auctionSuccess = auctionRepository.countAllAuctionsSuccessful(monthGetAuctionFailedAndSuccess, yearGetAuctionFailedAndSuccess);
3739
Integer totalUsersVerified = userRepository.getTotalUserByState(AccountState.VERIFIED);
3840
Integer totalUsersActive = userRepository.getTotalUserByState(AccountState.ACTIVE);
3941
Integer totalUsersInActive = userRepository.getTotalUserByState(AccountState.INACTIVE);
4042
Integer totalMembers = userRepository.getTotalUserByRole(Role.MEMBER);
4143
Integer totalStaffs = userRepository.getTotalUserByRole(Role.STAFF);
4244
Integer totalManagers = userRepository.getTotalUserByRole(Role.MANAGER);
4345
Integer totalAdmins = userRepository.getTotalUserByRole(Role.ADMIN);
44-
Long totalUsersRegister = auctionRegistrationRepository.countDistinctUsersRegistered();
45-
Long totalUsersNotRegistered = totalUser - totalUsersRegister;
4646

4747
// Total revenues
48-
Double totalCommissionRevenue = transactionRepository.getTotalCommissionRevenue();
4948
Double totalRevenueToday = transactionRepository.getTotalRevenueToday(startOfDay, startOfNextDay);
50-
Double totalRegistrationFeeRevenue = transactionRepository.getTotalRegistrationFeeRevenue();
5149

5250
// Arrays for monthly data
5351
Integer[] totalUsersRegisterByMonth = new Integer[12];
5452
Integer[] totalAuctionByMonth = new Integer[12];
5553
Double[] totalRevenueByMonth = new Double[12];
54+
Double[] totalParticipationByMonth = new Double[12];
5655

5756
// Calculate data for each month of the year
5857
for (int month = 1; month <= 12; month++) {
@@ -66,22 +65,31 @@ public DashBoardResponse getInformation(Integer yearGetRegisterAccount, Integer
6665
totalRegistrationFeeRevenueByMonthAndYear = (totalRegistrationFeeRevenueByMonthAndYear != null) ? totalRegistrationFeeRevenueByMonthAndYear : 0.0;
6766

6867
totalRevenueByMonth[month - 1] = totalRevenue + totalRegistrationFeeRevenueByMonthAndYear;
68+
69+
70+
Long participation = auctionRegistrationRepository.countDistinctUsersRegistered(month, yearGetUserJoinAuction);
71+
totalParticipationByMonth[month - 1] = participation.doubleValue();
6972
}
7073

71-
double percentAuctionFailed = (double) totalAuctionFailed / totalAuctionsFinished * 100;
72-
double percentAuctionSuccess = (double) totalAuctionSuccess / totalAuctionsFinished * 100;
73-
double participationRate = (double) totalUsersRegister / totalUser * 100;
74-
double notParticipationRate = (double) totalUsersNotRegistered / totalUser * 100;
74+
// Calculate revenue for the last 10 years
75+
Double[] totalRevenueNear10Year = new Double[10];
76+
for (int i = 0; i < 10; i++) {
77+
int year = Year.now().getValue() - 9 + i;
78+
Double totalRevenueForYear = transactionRepository.getTotalCommissionRevenueByYear(year);
79+
totalRevenueForYear = (totalRevenueForYear != null) ? totalRevenueForYear : 0.0;
7580

76-
Double totalRevenue = totalRegistrationFeeRevenue + totalCommissionRevenue;
81+
Double totalRegistrationFeeRevenueForYear = transactionRepository.getTotalRegistrationFeeRevenueByYear(year);
82+
totalRegistrationFeeRevenueForYear = (totalRegistrationFeeRevenueForYear != null) ? totalRegistrationFeeRevenueForYear : 0.0;
83+
84+
totalRevenueNear10Year[i] = totalRevenueForYear + totalRegistrationFeeRevenueForYear;
85+
}
7786

7887
return DashBoardResponse.builder()
7988
.totalUser(totalUser)
8089
.totalRevenueToday(totalRevenueToday)
8190
.totalJewelryActive(totalActiveJewelry)
8291
.totalJewelryWaitApproving(totalApprovingJewelry)
8392
.totalAuctionJewelry(totalAuctionJewelry)
84-
.totalAuctions(totalAuction)
8593
.totalUsersVerified(totalUsersVerified)
8694
.totalUsersActive(totalUsersActive)
8795
.totalUsersInActive(totalUsersInActive)
@@ -91,11 +99,10 @@ public DashBoardResponse getInformation(Integer yearGetRegisterAccount, Integer
9199
.totalAdmins(totalAdmins)
92100
.totalUsersByMonth(totalUsersRegisterByMonth)
93101
.totalAuctionByMonth(totalAuctionByMonth)
94-
.percentAuctionFailed(percentAuctionFailed)
95-
.percentAuctionSuccess(percentAuctionSuccess)
96-
.notParticipationRate(notParticipationRate)
97-
.participationRate(participationRate)
98-
.totalRevenue(totalRevenue)
102+
.auctionFailed(auctionFailed)
103+
.auctionSuccess(auctionSuccess)
104+
.totalParticipationByMonth(totalParticipationByMonth)
105+
.totalRevenueNear10Year(totalRevenueNear10Year)
99106
.totalRevenueByMonth(totalRevenueByMonth)
100107
.build();
101108
}

0 commit comments

Comments
 (0)