|
|
@@ -0,0 +1,49 @@
|
|
|
+package com.izouma.nineth.service;
|
|
|
+
|
|
|
+import com.izouma.nineth.domain.Order;
|
|
|
+import com.izouma.nineth.enums.CollectionSource;
|
|
|
+import com.izouma.nineth.enums.OrderStatus;
|
|
|
+import com.izouma.nineth.repo.OrderRepo;
|
|
|
+import com.izouma.nineth.repo.TokenHistoryRepo;
|
|
|
+import com.izouma.nineth.repo.UserRepo;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class StatisticService {
|
|
|
+ private UserRepo userRepo;
|
|
|
+ private OrderRepo orderRepo;
|
|
|
+ private TokenHistoryRepo tokenHistoryRepo;
|
|
|
+
|
|
|
+ public Map<String, Object> total() {
|
|
|
+ Map<String, Object> total = new HashMap<>();
|
|
|
+ List<Order> orders;
|
|
|
+ long user = userRepo.countAllByDelFalse();
|
|
|
+ total.put("userNum", user);
|
|
|
+ orders = orderRepo.findAllByStatus(OrderStatus.FINISH);
|
|
|
+
|
|
|
+ Map<CollectionSource, List<Order>> orderMap = orders.stream().collect(Collectors.groupingBy(Order::getSource));
|
|
|
+ List<Order> officialOrders = Optional.ofNullable(orderMap.get(CollectionSource.OFFICIAL))
|
|
|
+ .orElse(Collections.emptyList());
|
|
|
+ List<Order> transferOrders = Optional.ofNullable(orderMap.get(CollectionSource.TRANSFER))
|
|
|
+ .orElse(Collections.emptyList());
|
|
|
+
|
|
|
+ total.put("officialNum", officialOrders.size());
|
|
|
+ total.put("transferNum", transferOrders.size());
|
|
|
+
|
|
|
+ BigDecimal officialPrice = officialOrders.stream()
|
|
|
+ .map(Order::getTotalPrice)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ BigDecimal transferPrice = transferOrders.stream()
|
|
|
+ .map(Order::getTotalPrice)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ total.put("officialPrice", officialPrice);
|
|
|
+ total.put("transferPrice", transferPrice);
|
|
|
+ return total;
|
|
|
+ }
|
|
|
+}
|