|
|
@@ -1,16 +1,20 @@
|
|
|
package com.izouma.jiashanxia.service;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.izouma.jiashanxia.domain.OrderInfo;
|
|
|
import com.izouma.jiashanxia.domain.Withdraw;
|
|
|
import com.izouma.jiashanxia.domain.WxFee;
|
|
|
+import com.izouma.jiashanxia.dto.PageQuery;
|
|
|
import com.izouma.jiashanxia.dto.StatisticDTO;
|
|
|
import com.izouma.jiashanxia.enums.OrderInfoStatus;
|
|
|
import com.izouma.jiashanxia.enums.WithdrawStatus;
|
|
|
import com.izouma.jiashanxia.repo.*;
|
|
|
+import com.izouma.jiashanxia.utils.JpaUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
@@ -23,11 +27,13 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
|
public class StatisticService {
|
|
|
- private final UserRepo userRepo;
|
|
|
- private final CompanyRepo companyRepo;
|
|
|
- private final OrderInfoRepo orderInfoRepo;
|
|
|
- private final WxFeeRepo wxFeeRepo;
|
|
|
- private final WithdrawRepo withdrawRepo;
|
|
|
+ private final UserRepo userRepo;
|
|
|
+ private final CompanyRepo companyRepo;
|
|
|
+ private final OrderInfoRepo orderInfoRepo;
|
|
|
+ private final WxFeeRepo wxFeeRepo;
|
|
|
+ private final WithdrawRepo withdrawRepo;
|
|
|
+ private final UserService userService;
|
|
|
+ private final OrderInfoService orderInfoService;
|
|
|
|
|
|
/*
|
|
|
数据总揽
|
|
|
@@ -97,11 +103,64 @@ public class StatisticService {
|
|
|
return dtos;
|
|
|
}
|
|
|
|
|
|
- public List<StatisticDTO> dayData2(Integer day) {
|
|
|
- List<StatisticDTO> dtos = new ArrayList<>();
|
|
|
+ public List<StatisticDTO> dayData2(int day) {
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
LocalDateTime start = LocalDateTime.of(now.toLocalDate().minusDays(day - 1), LocalTime.MIN);
|
|
|
List<OrderInfo> orderInfos = orderInfoRepo.findAllByPaidAtBetweenAndStatus(start, now, OrderInfoStatus.PAID);
|
|
|
+
|
|
|
+// List<StatisticDTO> dtos = new ArrayList<>();
|
|
|
+// while (day > 0) {
|
|
|
+// LocalDateTime end = start.plusDays(1);
|
|
|
+// LocalDateTime finalStart = start;
|
|
|
+// long order = orderInfos.stream()
|
|
|
+// .filter(orderInfo -> !finalStart.isAfter(orderInfo.getPaidAt()) && end.isAfter(orderInfo.getPaidAt()))
|
|
|
+// .count();
|
|
|
+// dtos.add(StatisticDTO.builder()
|
|
|
+// .date(finalStart.toLocalDate())
|
|
|
+// .dayOrder(order)
|
|
|
+// .build());
|
|
|
+// day--;
|
|
|
+// start = end;
|
|
|
+// }
|
|
|
+// return dtos;
|
|
|
+ return this.getDTOS(orderInfos, start, day);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<StatisticDTO> employee(int year, int month, Long userId) {
|
|
|
+ LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0);
|
|
|
+ LocalDateTime end = start.plusMonths(1);
|
|
|
+ List<Long> childrenId = userService.childrenId(userId);
|
|
|
+ List<OrderInfo> orderInfos = orderInfoRepo.findAll(((root, criteriaQuery, criteriaBuilder) -> {
|
|
|
+ List<Predicate> and = new ArrayList<>();
|
|
|
+ and.add(criteriaBuilder.equal(root.get("status"), OrderInfoStatus.PAID));
|
|
|
+ and.add(root.get("userId").in(childrenId));
|
|
|
+ and.add(criteriaBuilder.greaterThanOrEqualTo(root.get("paidAt"), start));
|
|
|
+ and.add(criteriaBuilder.lessThan(root.get("paidAt"), end));
|
|
|
+ return criteriaBuilder.and(and.toArray(new Predicate[0]));
|
|
|
+ }));
|
|
|
+ long day = end.toLocalDate().toEpochDay() - start.toLocalDate().toEpochDay() - 1;
|
|
|
+ return this.getDTOS(orderInfos, start, (int) day);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<StatisticDTO> employee2(int year, int month, Long userId) {
|
|
|
+ LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0);
|
|
|
+ LocalDateTime end = start.plusMonths(1);
|
|
|
+ long day = end.toLocalDate().toEpochDay() - start.toLocalDate().toEpochDay() - 1;
|
|
|
+ Map<Integer, List<WxFee>> collect = wxFeeRepo.findAllByCreatedAtBetweenAndUserId(start, end, userId)
|
|
|
+ .stream()
|
|
|
+ .collect(Collectors.groupingBy(WxFee::getAction));
|
|
|
+ List<WxFee> wxFees = collect.get(0);
|
|
|
+ List<WxFee> refunds = new ArrayList<>();
|
|
|
+ List<WxFee> wxFees1 = collect.get(1);
|
|
|
+ if (CollUtil.isNotEmpty(wxFees1)) {
|
|
|
+ refunds = wxFees1;
|
|
|
+ }
|
|
|
+ List<Withdraw> withdraws = withdrawRepo.findAllByStatusAndUserId(WithdrawStatus.SUCCESS, userId);
|
|
|
+ return getDTOS2(wxFees, refunds, withdraws, start, (int) day);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<StatisticDTO> getDTOS(List<OrderInfo> orderInfos, LocalDateTime start, int day) {
|
|
|
+ List<StatisticDTO> dtos = new ArrayList<>();
|
|
|
while (day > 0) {
|
|
|
LocalDateTime end = start.plusDays(1);
|
|
|
LocalDateTime finalStart = start;
|
|
|
@@ -117,4 +176,39 @@ public class StatisticService {
|
|
|
}
|
|
|
return dtos;
|
|
|
}
|
|
|
+
|
|
|
+ public List<StatisticDTO> getDTOS2(List<WxFee> wxFees, List<WxFee> refunds, List<Withdraw> withdraws,
|
|
|
+ LocalDateTime start, int day) {
|
|
|
+ List<StatisticDTO> dtos = new ArrayList<>();
|
|
|
+ while (day > 0) {
|
|
|
+ LocalDateTime end = start.plusDays(1);
|
|
|
+ LocalDateTime finalStart = start;
|
|
|
+// long order = orderInfos.stream()
|
|
|
+// .filter(orderInfo -> !finalStart.isAfter(orderInfo.getPaidAt()) && end.isAfter(orderInfo.getPaidAt()))
|
|
|
+// .count();
|
|
|
+// BigDecimal fee = wxFees.stream()
|
|
|
+// .filter(wxFee -> !finalStart.isAfter(wxFee.getCreatedAt()) && end.isAfter(wxFee.getCreatedAt()))
|
|
|
+// .map(WxFee::getAmount)
|
|
|
+// .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+// BigDecimal refund = refunds.stream()
|
|
|
+// .filter(wxFee -> !finalStart.isAfter(wxFee.getCreatedAt()) && end.isAfter(wxFee.getCreatedAt()))
|
|
|
+// .map(WxFee::getAmount)
|
|
|
+// .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ BigDecimal withdraw = withdraws.stream()
|
|
|
+ .filter(wxFee -> !finalStart.isAfter(wxFee.getAuditTime()) && end.isAfter(wxFee.getAuditTime()))
|
|
|
+ .map(Withdraw::getAmount)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+
|
|
|
+ dtos.add(StatisticDTO.builder()
|
|
|
+ .date(finalStart.toLocalDate())
|
|
|
+// .dayOrder(order)
|
|
|
+// .dayFee(fee)
|
|
|
+ .dayWithdraw(withdraw)
|
|
|
+// .dayRefund(refund)
|
|
|
+ .build());
|
|
|
+ day--;
|
|
|
+ start = end;
|
|
|
+ }
|
|
|
+ return dtos;
|
|
|
+ }
|
|
|
}
|