|
|
@@ -4,9 +4,12 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import com.izouma.jiashanxia.domain.*;
|
|
|
import com.izouma.jiashanxia.domain.Package;
|
|
|
-import com.izouma.jiashanxia.dto.*;
|
|
|
+import com.izouma.jiashanxia.domain.*;
|
|
|
+import com.izouma.jiashanxia.dto.CreateOrder;
|
|
|
+import com.izouma.jiashanxia.dto.OrderInfoDTO;
|
|
|
+import com.izouma.jiashanxia.dto.OrderInfoVO;
|
|
|
+import com.izouma.jiashanxia.dto.PageQuery;
|
|
|
import com.izouma.jiashanxia.enums.*;
|
|
|
import com.izouma.jiashanxia.exception.BusinessException;
|
|
|
import com.izouma.jiashanxia.repo.*;
|
|
|
@@ -21,7 +24,10 @@ import javax.persistence.criteria.Predicate;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
@@ -214,12 +220,12 @@ public class OrderInfoService {
|
|
|
// .build());
|
|
|
|
|
|
// 上级分销
|
|
|
- this.distribution2(order);
|
|
|
+ this.distribution3(order);
|
|
|
// 短信通知
|
|
|
-// User user = order.getUser();
|
|
|
-// if (ObjectUtil.isNotNull(user.getPhone())) {
|
|
|
-// smsService.sendNotification(user.getPhone(), order.getName(), order.getOrderNumber());
|
|
|
-// }
|
|
|
+ User user = order.getUser();
|
|
|
+ if (ObjectUtil.isNotNull(user.getPhone())) {
|
|
|
+ smsService.sendNotification(user.getPhone(), order.getName(), order.getOrderNumber());
|
|
|
+ }
|
|
|
|
|
|
// orderDelayService.remove(order.getId(), order.getCreatedAt());
|
|
|
}
|
|
|
@@ -242,11 +248,6 @@ public class OrderInfoService {
|
|
|
case CANCELLED:
|
|
|
return orderInfo;
|
|
|
}
|
|
|
-// if (OrderInfoStatus.PAID.equals(status)) {
|
|
|
-// throw new BusinessException("订单已支付,无法取消");
|
|
|
-// } else if (OrderInfoStatus.CANCELLED.equals(status)) {
|
|
|
-// return orderInfo;
|
|
|
-// }
|
|
|
orderInfo.setStatus(OrderInfoStatus.CANCELLED);
|
|
|
return orderInfoRepo.save(orderInfo);
|
|
|
}
|
|
|
@@ -309,6 +310,9 @@ public class OrderInfoService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ 分销
|
|
|
+ */
|
|
|
public void distribution1(Long userId, Long parent, OrderInfo orderInfo) {
|
|
|
Package aPackage = packageRepo.findById(orderInfo.getPackageId()).orElseThrow(new BusinessException("无套餐"));
|
|
|
|
|
|
@@ -411,21 +415,141 @@ public class OrderInfoService {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public void distribution3(OrderInfo orderInfo) {
|
|
|
+ // 分销
|
|
|
+ Package aPackage = packageRepo.findById(orderInfo.getPackageId()).orElseThrow(new BusinessException("无记录"));
|
|
|
+ // 售价-结算价
|
|
|
+ int balance;
|
|
|
+ if (ObjectUtil.isNotNull(orderInfo.getStockId())) {
|
|
|
+ Stock stock = stockRepo.findById(orderInfo.getStockId()).orElseThrow(new BusinessException("无规格"));
|
|
|
+ balance = stock.getPrice().compareTo(stock.getSettlementPrice());
|
|
|
+ } else {
|
|
|
+ balance = aPackage.getAmount().compareTo(aPackage.getSettlementPrice());
|
|
|
+ }
|
|
|
+ if (balance <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ BigDecimal balanceAmount = BigDecimal.valueOf(balance);
|
|
|
+
|
|
|
Long userId = orderInfo.getUserId();
|
|
|
User user = userRepo.findById(userId).orElseThrow(new BusinessException("无用户"));
|
|
|
- // 不是普通用户
|
|
|
- if (!Member.NORMAL.equals(user.getMember())) {
|
|
|
- // 拿一级
|
|
|
+ User one = user;
|
|
|
+ User two;
|
|
|
+ // 普通用户
|
|
|
+ if (Member.NORMAL.equals(user.getMember())) {
|
|
|
+ one = this.getParent(user);
|
|
|
+ if (ObjectUtil.isNull(one)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.getDistribution(orderInfo, one, 1, balanceAmount, aPackage);
|
|
|
+ two = this.getParent(one);
|
|
|
+ if (ObjectUtil.isNotNull(two)) {
|
|
|
+ this.getDistribution(orderInfo, two, 2, balanceAmount, aPackage);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
+ // 获得上级
|
|
|
+ private User getParent(User user) {
|
|
|
+ // 查找上级
|
|
|
+ if (ObjectUtil.isNotNull(user.getParent())) {
|
|
|
+ User parent = userRepo.findById(user.getParent()).orElseThrow(new BusinessException("无用户"));
|
|
|
+ if (!Member.NORMAL.equals(parent.getMember())) {
|
|
|
+ return parent;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户得到的分销
|
|
|
+ private void getDistribution(OrderInfo orderInfo, User user, int level, BigDecimal balance, Package aPackage) {
|
|
|
+ BigDecimal amount;
|
|
|
+
|
|
|
+ boolean separateDis = aPackage.isSeparateDistribution();
|
|
|
+ // 个人佣金流水
|
|
|
+ CommissionRecord commission = CommissionRecord.builder()
|
|
|
+ .userId(user.getId())
|
|
|
+ .payMethod(PayMethod.YUE)
|
|
|
+ .fromUserId(orderInfo.getUserId())
|
|
|
+ .transactionType(TransactionType.PROMOTE)
|
|
|
+ .transactionId(orderInfo.getTransactionId())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ switch (user.getMember()) {
|
|
|
+ case EXPERT:
|
|
|
+ if (level == 1) {
|
|
|
+ if (separateDis && ObjectUtil.isNotEmpty(aPackage.getPersonalRatio0())) {
|
|
|
+ amount = balance.multiply(aPackage.getPersonalRatio0());
|
|
|
+ } else {
|
|
|
+ BigDecimal personalRatio0 = sysConfigService.getBigDecimal("PERSONAL_RATIO_0");
|
|
|
+ amount = balance.multiply(personalRatio0);
|
|
|
+ }
|
|
|
+ if (BigDecimal.ZERO.compareTo(amount) <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 加用户余额
|
|
|
+ user.setAmount(user.getAmount().add(amount));
|
|
|
+ // 直推类型
|
|
|
+ commission.setTransactionType(TransactionType.PROMOTE);
|
|
|
+ } else {
|
|
|
+ if (separateDis && ObjectUtil.isNotEmpty(aPackage.getPersonalRatio1())) {
|
|
|
+ amount = balance.multiply(aPackage.getPersonalRatio1());
|
|
|
+ } else {
|
|
|
+ BigDecimal personalRatio1 = sysConfigService.getBigDecimal("PERSONAL_RATIO_1");
|
|
|
+ amount = balance.multiply(personalRatio1);
|
|
|
+ }
|
|
|
+ if (BigDecimal.ZERO.compareTo(amount) <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 二级收益暂存
|
|
|
+ user.setCacheAmount(user.getCacheAmount().add(amount));
|
|
|
+ // 间推类型
|
|
|
+ commission.setTransactionType(TransactionType.CHILD_PROMOTE);
|
|
|
+ }
|
|
|
+ userRepo.save(user);
|
|
|
+ commission.setAmount(amount);
|
|
|
+ commissionRecordRepo.save(commission);
|
|
|
+ break;
|
|
|
+ case BIG_EXPERT:
|
|
|
+ if (level == 1) {
|
|
|
+ if (separateDis && ObjectUtil.isNotEmpty(aPackage.getBigExpertRatio0())) {
|
|
|
+ amount = balance.multiply(aPackage.getBigExpertRatio0());
|
|
|
+ } else {
|
|
|
+ BigDecimal personalRatio0 = sysConfigService.getBigDecimal("BIG_EXPERT_RATIO_0");
|
|
|
+ amount = balance.multiply(personalRatio0);
|
|
|
+ }
|
|
|
+ if (BigDecimal.ZERO.compareTo(amount) <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 直推类型
|
|
|
+ commission.setTransactionType(TransactionType.PROMOTE);
|
|
|
+ } else {
|
|
|
+ if (separateDis && ObjectUtil.isNotEmpty(aPackage.getBigExpertRatio1())) {
|
|
|
+ amount = balance.multiply(aPackage.getBigExpertRatio1());
|
|
|
+ } else {
|
|
|
+ BigDecimal personalRatio1 = sysConfigService.getBigDecimal("BIG_EXPERT_RATIO_1");
|
|
|
+ amount = balance.multiply(personalRatio1);
|
|
|
+ }
|
|
|
+ if (BigDecimal.ZERO.compareTo(amount) <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 间推类型
|
|
|
+ commission.setTransactionType(TransactionType.CHILD_PROMOTE);
|
|
|
+ }
|
|
|
+ user.setAmount(user.getAmount().add(amount));
|
|
|
+ userRepo.save(user);
|
|
|
+ commission.setAmount(amount);
|
|
|
+ commissionRecordRepo.save(commission);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/*
|
|
|
间推
|
|
|
*/
|