|
|
@@ -1,11 +1,10 @@
|
|
|
package com.izouma.jiashanxia.service;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
-import com.izouma.jiashanxia.domain.CommissionRecord;
|
|
|
-import com.izouma.jiashanxia.domain.OrderInfo;
|
|
|
-import com.izouma.jiashanxia.domain.Withdraw;
|
|
|
-import com.izouma.jiashanxia.domain.WxFee;
|
|
|
+import com.izouma.jiashanxia.domain.*;
|
|
|
import com.izouma.jiashanxia.dto.StatisticDTO;
|
|
|
+import com.izouma.jiashanxia.dto.StatisticVO;
|
|
|
+import com.izouma.jiashanxia.enums.Member;
|
|
|
import com.izouma.jiashanxia.enums.OrderInfoStatus;
|
|
|
import com.izouma.jiashanxia.enums.WithdrawStatus;
|
|
|
import com.izouma.jiashanxia.repo.*;
|
|
|
@@ -17,10 +16,7 @@ import javax.persistence.criteria.Predicate;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@@ -41,7 +37,7 @@ public class StatisticService {
|
|
|
public Map<String, Object> dataOverview() {
|
|
|
long user = userRepo.count();
|
|
|
long company = attractionsRepo.count();
|
|
|
- long order = orderInfoRepo.countByStatus(OrderInfoStatus.PAID);
|
|
|
+ long order = orderInfoRepo.countByStatusIn(CollUtil.newArrayList(OrderInfoStatus.PAID, OrderInfoStatus.USED));
|
|
|
BigDecimal fee = wxFeeRepo.sumByAmount();
|
|
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
@@ -233,4 +229,90 @@ public class StatisticService {
|
|
|
}
|
|
|
return dtos;
|
|
|
}
|
|
|
+
|
|
|
+ public List<StatisticVO> getVO(String time, Long userId) {
|
|
|
+ // 时间段
|
|
|
+ LocalDateTime start = DateTimeUtils.toLocalDateTime(time, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ LocalDateTime end = start.plusMonths(1);
|
|
|
+ long day = end.toLocalDate().toEpochDay() - start.toLocalDate().toEpochDay();
|
|
|
+
|
|
|
+ // 用户
|
|
|
+ List<User> users = userRepo.findAllByParentAndDelFalse(userId);
|
|
|
+ List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());
|
|
|
+ List<Long> normal = users.stream()
|
|
|
+ .filter(user -> Member.NORMAL.equals(user.getMember()))
|
|
|
+ .map(User::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 下级的直推+间推
|
|
|
+ List<User> children = userRepo.findAllByParentInAndDelFalse(userIds);
|
|
|
+ List<Long> childrenIds = children.stream().map(User::getId).collect(Collectors.toList());
|
|
|
+ children.addAll(userRepo.findAllByParentInAndDelFalse(childrenIds));
|
|
|
+ childrenIds.addAll(userIds);
|
|
|
+ List<Long> childrenNormal = children.stream()
|
|
|
+ .filter(user -> Member.NORMAL.equals(user.getMember()))
|
|
|
+ .map(User::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 订单
|
|
|
+ List<OrderInfo> orderInfos = orderInfoRepo
|
|
|
+ .findAllByStatusInAndUserIdInAndPaidAtBetween(CollUtil.newArrayList(OrderInfoStatus.PAID, OrderInfoStatus.USED), childrenIds, start, end);
|
|
|
+
|
|
|
+ List<StatisticVO> vos = new ArrayList<>();
|
|
|
+ // 按日期查找
|
|
|
+ while (day > 0) {
|
|
|
+ end = start.plusDays(1);
|
|
|
+ Map<String, BigDecimal> map = this.getMap(users, normal, orderInfos, start, end);
|
|
|
+ Map<String, BigDecimal> childrenMap = this.getMap(children, childrenNormal, orderInfos, start, end);
|
|
|
+ vos.add(StatisticVO.builder()
|
|
|
+ .date(start.toLocalDate())
|
|
|
+ .userId(userId)
|
|
|
+ .expert(map.get("expert").intValue())
|
|
|
+ .expertAmount(map.get("reduce"))
|
|
|
+ .normalAmount(map.get("reduce2"))
|
|
|
+ .childrenExpert(childrenMap.get("expert").intValue())
|
|
|
+ .childrenExpertAmount(childrenMap.get("reduce"))
|
|
|
+ .childrenNormalAmount(childrenMap.get("reduce2"))
|
|
|
+ .build());
|
|
|
+ start = end;
|
|
|
+ day--;
|
|
|
+ }
|
|
|
+ return vos;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, BigDecimal> getMap(List<User> users, List<Long> normal, List<OrderInfo> orderInfos,
|
|
|
+ LocalDateTime start, LocalDateTime end) {
|
|
|
+ // 佳人数量
|
|
|
+ long expert = users.stream()
|
|
|
+ .filter(user -> (Member.EXPERT.equals(user.getMember()) || Member.BIG_EXPERT.equals(user.getMember())) && !start
|
|
|
+ .isAfter(user.getBecomeTime()) && end.isAfter(user.getBecomeTime()))
|
|
|
+ .count();
|
|
|
+
|
|
|
+ // 之前的佳人
|
|
|
+ List<Long> expertUser = users.stream()
|
|
|
+ .filter(user -> (Member.EXPERT.equals(user.getMember()) || Member.BIG_EXPERT.equals(user.getMember())) && start
|
|
|
+ .isAfter(user.getBecomeTime()))
|
|
|
+ .map(User::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 佳人销售额
|
|
|
+ BigDecimal reduce = orderInfos.stream()
|
|
|
+ .filter(order -> expertUser.contains(order.getUserId()) && !start.isAfter(order.getPaidAt()) && end
|
|
|
+ .isAfter(order.getPaidAt()))
|
|
|
+ .map(OrderInfo::getPrice)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+
|
|
|
+ // 普通用户销售额
|
|
|
+ BigDecimal reduce2 = orderInfos.stream()
|
|
|
+ .filter(order -> normal.contains(order.getUserId()) && !start.isAfter(order.getPaidAt()) && end
|
|
|
+ .isAfter(order.getPaidAt()))
|
|
|
+ .map(OrderInfo::getPrice)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+
|
|
|
+ Map<String, BigDecimal> map = new HashMap<>();
|
|
|
+ map.put("expert", new BigDecimal(expert));
|
|
|
+ map.put("reduce", reduce);
|
|
|
+ map.put("reduce2", reduce2);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
}
|