wangqifan 2 лет назад
Родитель
Сommit
54b5d6a0f1

+ 2 - 0
src/main/java/com/izouma/nineth/repo/OrderRepo.java

@@ -42,6 +42,7 @@ public interface OrderRepo extends JpaRepository<Order, Long>, JpaSpecificationE
 
     /**
      * 更新实际支付的订单编号
+     *
      * @param orderId
      * @param orderNo
      */
@@ -49,4 +50,5 @@ public interface OrderRepo extends JpaRepository<Order, Long>, JpaSpecificationE
     @Query("update Order t set t.orderNo = ?2, t.transactionId = ?3 where t.id = ?1")
     void updateOrderNo(Long orderId, String orderNo, String transactionId);
 
+    List<Order> findAllByStatus(OrderStatus status);
 }

+ 2 - 0
src/main/java/com/izouma/nineth/repo/UserRepo.java

@@ -171,4 +171,6 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
 
     @Query("select u from User u where u.invitor = ?1 and u.createdAt > ?2")
     List<User> getUserInviteData(Long invitor, LocalDateTime time);
+
+    long countAllByDelFalse();
 }

+ 51 - 0
src/main/java/com/izouma/nineth/service/StatisticService.java

@@ -0,0 +1,51 @@
+package com.izouma.nineth.service;
+
+import com.izouma.nineth.domain.Order;
+import com.izouma.nineth.domain.User;
+import com.izouma.nineth.enums.CollectionSource;
+import com.izouma.nineth.enums.OrderStatus;
+import com.izouma.nineth.exception.BusinessException;
+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;
+    }
+}

+ 26 - 0
src/main/java/com/izouma/nineth/web/StatisticController.java

@@ -0,0 +1,26 @@
+package com.izouma.nineth.web;
+
+import com.izouma.nineth.service.CacheService;
+import com.izouma.nineth.service.StatisticService;
+import com.izouma.nineth.utils.SecurityUtils;
+import lombok.AllArgsConstructor;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Map;
+
+@RestController
+@RequestMapping("/statistic")
+@AllArgsConstructor
+public class StatisticController {
+    private StatisticService statisticService;
+    private CacheService     cacheService;
+
+    @PostMapping("/total")
+    @Cacheable("total")
+    @PreAuthorize("hasRole('ADMIN')")
+    public Map<String, Object> total() {
+        return statisticService.total();
+    }
+}