licailing 5 年 前
コミット
b8dd5c50d0

+ 45 - 26
src/main/java/com/izouma/jiashanxia/service/OrderInfoService.java

@@ -33,15 +33,15 @@ import java.util.stream.Collectors;
 @AllArgsConstructor
 public class OrderInfoService {
 
-    private OrderInfoRepo        orderInfoRepo;
-    private PackageRepo          packageRepo;
-    private PackageGoodsRepo     packageGoodsRepo;
-    private UserRepo             userRepo;
-    private SysConfigService     sysConfigService;
-    private CommissionRecordRepo commissionRecordRepo;
-    private UserPackageService   userPackageService;
-    private CompanyRepo          companyRepo;
-    private WithdrawService      withdrawService;
+    private final OrderInfoRepo        orderInfoRepo;
+    private final PackageRepo          packageRepo;
+    private final PackageGoodsRepo     packageGoodsRepo;
+    private final UserRepo             userRepo;
+    private final SysConfigService     sysConfigService;
+    private final CommissionRecordRepo commissionRecordRepo;
+    private final UserPackageService   userPackageService;
+    private final CompanyRepo          companyRepo;
+    private final WithdrawService      withdrawService;
 
     public Page<OrderInfo> all(PageQuery pageQuery) {
         pageQuery.setSort("createdAt,desc");
@@ -111,7 +111,7 @@ public class OrderInfoService {
         // 用户id和上级id相同
         if (user.getParent() != null && !userId.equals(user.getParent())) {
             // 上级分销
-            this.distribution(userId, user.getParent(), transactionId);
+            this.distribution(userId, user.getParent(), transactionId, order.getPrice());
         }
     }
 
@@ -135,7 +135,7 @@ public class OrderInfoService {
     企业拉员工,员工的上级是否改为企业领导人?
     如果上级和企业不是一个人,收益如何算?
      */
-    public void distribution(Long userId, Long parent, String transactionId) {
+    public void distribution(Long userId, Long parent, String transactionId, BigDecimal amount) {
         User parentUser = userRepo.findById(parent).orElseThrow(new BusinessException("无用户"));
         // 判断是否可分享
         boolean flag = false;
@@ -143,7 +143,7 @@ public class OrderInfoService {
         if (ObjectUtil.isNotEmpty(parentUser.getCompanyId())) {
             flag = true;
             // 公司可得分销
-            this.companyDis(parentUser.getCompanyId(), transactionId, userId);
+            this.companyDis(parentUser.getCompanyId(), transactionId, userId, amount);
         }
         if (!flag) {
             // 是否购买了套餐
@@ -157,29 +157,48 @@ public class OrderInfoService {
             return;
         }
 
-        BigDecimal personalAmount = sysConfigService.getBigDecimal("PERSONAL_AMOUNT");
-        parentUser.setAmount(parentUser.getAmount().add(personalAmount));
-        userRepo.save(parentUser);
+        //分销级别
+        int distributor = sysConfigService.getInt("DISTRIBUTOR");
+        for (int i = 0; i < distributor; i++) {
+            String name = "PERSONAL_AMOUNT_" + i;
+            // 百分比
+            BigDecimal percentage = sysConfigService.getBigDecimal(name);
+            BigDecimal personalAmount = amount.multiply(percentage);
+            parentUser.setAmount(parentUser.getAmount().add(personalAmount));
+            userRepo.save(parentUser);
 
-        // 个人佣金流水
-        commissionRecordRepo.save(CommissionRecord.builder()
-                .userId(parent)
-                .amount(personalAmount)
-                .payMethod(PayMethod.YUE)
-                .fromUserId(userId)
-                .transactionType(TransactionType.PROMOTE)
-                .transactionId(transactionId)
-                .build());
+            // 个人佣金流水
+            commissionRecordRepo.save(CommissionRecord.builder()
+                    .userId(parentUser.getId())
+                    .amount(personalAmount)
+                    .payMethod(PayMethod.YUE)
+                    .fromUserId(userId)
+                    .transactionType(TransactionType.PROMOTE)
+                    .transactionId(transactionId)
+                    .build());
+            if (ObjectUtil.isEmpty(parentUser.getParent())) {
+                return;
+            }
+            parentUser = userRepo.findById(parentUser.getParent()).orElse(null);
+            if (parentUser == null) {
+                return;
+            }
+            if (!parentUser.isVip() || ObjectUtil.isEmpty(parentUser.getCompanyId())) {
+                return;
+            }
+        }
     }
 
     /*
     公司分销
      */
-    public void companyDis(Long companyId, String transactionId, Long userId) {
+    public void companyDis(Long companyId, String transactionId, Long userId, BigDecimal amount) {
         Company company = companyRepo.findById(companyId).orElseThrow(new BusinessException("无企业"));
         // 用户余额
         User user = userRepo.findById(company.getUserId()).orElseThrow(new BusinessException("无用户"));
-        BigDecimal companyAmount = sysConfigService.getBigDecimal("COMPANY_AMOUNT");
+        // 百分比
+        BigDecimal percentage = sysConfigService.getBigDecimal("COMPANY_AMOUNT");
+        BigDecimal companyAmount = amount.multiply(percentage);
         user.setAmount(user.getAmount().add(companyAmount));
         userRepo.save(user);
         // 公司余额

+ 6 - 0
src/test/java/com/izouma/jiashanxia/service/OrderInfoServiceTest.java

@@ -11,6 +11,7 @@ import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.data.domain.Pageable;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 @SpringBootTest
@@ -40,4 +41,9 @@ public class OrderInfoServiceTest {
         List<OrderInfoVO> contents = orderInfoService.my(Pageable.unpaged(), 68L, null).getContent();
         contents.forEach(content -> System.out.println(content.getCreatedAt()));
     }
+
+    @Test
+    public void test1() {
+        orderInfoService.distribution(636L,255L,"xxx", BigDecimal.valueOf(600));
+    }
 }