|
|
@@ -6,6 +6,7 @@ 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;
|
|
|
@@ -17,6 +18,7 @@ import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -30,16 +32,30 @@ public class StatisticService {
|
|
|
private OrderRepo orderRepo;
|
|
|
private TokenHistoryRepo tokenHistoryRepo;
|
|
|
|
|
|
- public Map<String, Object> total() {
|
|
|
+ public Map<String, Object> total(Long userId) {
|
|
|
+ User user1 = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("无用户"));
|
|
|
Map<String, Object> total = new HashMap<>();
|
|
|
+ List<Order> orders;
|
|
|
+
|
|
|
+ if (user1.isAdmin()) {
|
|
|
+ long user = userRepo.countAllByDelFalse();
|
|
|
+ total.put("userNum", user);
|
|
|
+ orders = orderRepo.findAllByStatus(OrderStatus.FINISH);
|
|
|
+ } else {
|
|
|
+ orders = orderRepo.findAllByStatusAndMinterId(OrderStatus.FINISH, userId);
|
|
|
+ }
|
|
|
|
|
|
- long user = userRepo.countAllByDelFalse();
|
|
|
- total.put("userNum", user);
|
|
|
-
|
|
|
- List<Order> orders = orderRepo.findAllByStatus(OrderStatus.FINISH);
|
|
|
Map<CollectionSource, List<Order>> orderMap = orders.stream().collect(Collectors.groupingBy(Order::getSource));
|
|
|
List<Order> officialOrders = orderMap.get(CollectionSource.OFFICIAL);
|
|
|
List<Order> transferOrders = orderMap.get(CollectionSource.TRANSFER);
|
|
|
+ if (officialOrders == null) {
|
|
|
+ total.put("officialNum", 0);
|
|
|
+ total.put("transferNum", 0);
|
|
|
+ total.put("officialPrice", 0);
|
|
|
+ total.put("transferPrice", 0);
|
|
|
+ return total;
|
|
|
+ }
|
|
|
+
|
|
|
total.put("officialNum", officialOrders.size());
|
|
|
total.put("transferNum", transferOrders.size());
|
|
|
|
|
|
@@ -66,13 +82,28 @@ public class StatisticService {
|
|
|
item -> DateTimeFormatter.ofPattern("yyyy-MM-dd").format(item.getCreatedAt()), Collectors.counting()));
|
|
|
}
|
|
|
|
|
|
- public Map<String, Map<String, Long>> orderNumTrend(int day) {
|
|
|
+ public Map<String, Map<String, Long>> orderNumTrend(int day, Long userId) {
|
|
|
LocalDateTime date = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
|
|
|
LocalDateTime start = date.minusDays(day);
|
|
|
- List<Order> orders = orderRepo.findAllByPayTimeIsAfter(start);
|
|
|
+
|
|
|
+ User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("无用户"));
|
|
|
+ List<Order> orders;
|
|
|
+ if (user.isAdmin()) {
|
|
|
+ orders = orderRepo.findAllByPayTimeIsAfter(start);
|
|
|
+ } else {
|
|
|
+ orders = orderRepo.findAllByPayTimeIsAfterAndMinterId(start, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
Map<CollectionSource, List<Order>> orderMap = orders.stream().collect(Collectors.groupingBy(Order::getSource));
|
|
|
- Map<String, Long> official = orderMap.get(CollectionSource.OFFICIAL).stream().collect(Collectors.groupingBy(
|
|
|
- item -> DateTimeFormatter.ofPattern("yyyy-MM-dd").format(item.getPayTime()), Collectors.counting()));
|
|
|
+
|
|
|
+ Map<String, Long> official = null;
|
|
|
+ if (CollUtil.isNotEmpty(orderMap.get(CollectionSource.OFFICIAL))) {
|
|
|
+ official = orderMap.get(CollectionSource.OFFICIAL).stream().collect(Collectors.groupingBy(
|
|
|
+ item -> DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
|
|
+ .format(item.getPayTime()), Collectors.counting()));
|
|
|
+ }
|
|
|
+
|
|
|
Map<String, Long> transfer = null;
|
|
|
if (CollUtil.isNotEmpty(orderMap.get(CollectionSource.TRANSFER))) {
|
|
|
transfer = orderMap.get(CollectionSource.TRANSFER).stream().collect(Collectors.groupingBy(
|
|
|
@@ -86,17 +117,28 @@ public class StatisticService {
|
|
|
return trend;
|
|
|
}
|
|
|
|
|
|
- public Map<String, Map<String, BigDecimal>> orderPriceTrend(int day) {
|
|
|
+ public Map<String, Map<String, BigDecimal>> orderPriceTrend(int day, Long userId) {
|
|
|
LocalDateTime date = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
|
|
|
LocalDateTime start = date.minusDays(day);
|
|
|
- List<Order> orders = orderRepo.findAllByPayTimeIsAfter(start);
|
|
|
+
|
|
|
+ User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("无用户"));
|
|
|
+ List<Order> orders;
|
|
|
+ if (user.isAdmin()) {
|
|
|
+ orders = orderRepo.findAllByPayTimeIsAfter(start);
|
|
|
+ } else {
|
|
|
+ orders = orderRepo.findAllByPayTimeIsAfterAndMinterId(start, userId);
|
|
|
+ }
|
|
|
+
|
|
|
Map<CollectionSource, List<Order>> orderMap = orders.stream().collect(Collectors.groupingBy(Order::getSource));
|
|
|
- Map<String, BigDecimal> official = orderMap.get(CollectionSource.OFFICIAL)
|
|
|
- .stream()
|
|
|
- .collect(Collectors.groupingBy(
|
|
|
- item -> DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
|
|
- .format(item.getPayTime()), Collectors.mapping(Order::getTotalPrice,
|
|
|
- Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
|
|
|
+ Map<String, BigDecimal> official = null;
|
|
|
+ if (CollUtil.isNotEmpty(orderMap.get(CollectionSource.OFFICIAL))) {
|
|
|
+ official = orderMap.get(CollectionSource.OFFICIAL)
|
|
|
+ .stream()
|
|
|
+ .collect(Collectors.groupingBy(
|
|
|
+ item -> DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
|
|
+ .format(item.getPayTime()), Collectors.mapping(Order::getTotalPrice,
|
|
|
+ Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
|
|
|
+ }
|
|
|
|
|
|
Map<String, BigDecimal> transfer = null;
|
|
|
if (CollUtil.isNotEmpty(orderMap.get(CollectionSource.TRANSFER))) {
|