|
@@ -1,7 +1,7 @@
|
|
|
package cn.iocoder.yudao.module.trade.service.brokerage;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
-import cn.hutool.core.math.Money;
|
|
|
+import cn.hutool.core.collection.ListUtil;
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
import cn.hutool.core.util.BooleanUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
@@ -10,7 +10,12 @@ import cn.hutool.extra.spring.SpringUtil;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
|
|
|
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
|
|
|
+import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
|
|
|
+import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
|
|
|
+import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
|
|
|
+import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
|
|
|
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.record.BrokerageRecordPageReqVO;
|
|
|
+import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.record.AppBrokerageProductPriceRespVO;
|
|
|
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokerageUserRankByPriceRespVO;
|
|
|
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokerageUserRankPageReqVO;
|
|
|
import cn.iocoder.yudao.module.trade.convert.brokerage.BrokerageRecordConvert;
|
|
@@ -37,6 +42,9 @@ import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getMaxValue;
|
|
|
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getMinValue;
|
|
|
+import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
|
|
|
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.BROKERAGE_WITHDRAW_USER_BALANCE_NOT_ENOUGH;
|
|
|
|
|
|
/**
|
|
@@ -56,6 +64,11 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
|
|
|
@Resource
|
|
|
private BrokerageUserService brokerageUserService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private ProductSpuApi productSpuApi;
|
|
|
+ @Resource
|
|
|
+ private ProductSkuApi productSkuApi;
|
|
|
+
|
|
|
@Override
|
|
|
public BrokerageRecordDO getBrokerageRecord(Integer id) {
|
|
|
return brokerageRecordMapper.selectById(id);
|
|
@@ -240,7 +253,6 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
|
|
|
return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
|
|
|
}
|
|
|
|
|
|
- // TODO @疯狂:这个求出来,应该是不准的?例如说超过 100+ 名后?
|
|
|
@Override
|
|
|
public Integer getUserRankByPrice(Long userId, LocalDateTime[] times) {
|
|
|
AppBrokerageUserRankPageReqVO pageParam = new AppBrokerageUserRankPageReqVO().setTimes(times);
|
|
@@ -261,15 +273,14 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
|
|
|
int balance = Optional.of(user)
|
|
|
.map(BrokerageUserDO::getBrokeragePrice).orElse(0);
|
|
|
if (balance + brokeragePrice < 0) {
|
|
|
- // TODO @疯狂:要不 MoneyUtils 那,统一搞个 format 金额的方法?然后把分到元的字符串,统一收口掉;
|
|
|
- throw exception(BROKERAGE_WITHDRAW_USER_BALANCE_NOT_ENOUGH, new Money(0, balance));
|
|
|
+ throw exception(BROKERAGE_WITHDRAW_USER_BALANCE_NOT_ENOUGH, MoneyUtils.fenToYuanStr(balance));
|
|
|
}
|
|
|
|
|
|
// 2. 更新佣金余额
|
|
|
boolean success = brokerageUserService.updateUserPrice(userId, brokeragePrice);
|
|
|
if (!success) {
|
|
|
// 失败时,则抛出异常。只会出现扣减佣金时,余额不足的情况
|
|
|
- throw exception(BROKERAGE_WITHDRAW_USER_BALANCE_NOT_ENOUGH, new Money(0, balance));
|
|
|
+ throw exception(BROKERAGE_WITHDRAW_USER_BALANCE_NOT_ENOUGH, MoneyUtils.fenToYuanStr(balance));
|
|
|
}
|
|
|
|
|
|
// 3. 新增记录
|
|
@@ -296,6 +307,50 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public AppBrokerageProductPriceRespVO calculateProductBrokeragePrice(Long spuId, Long userId) {
|
|
|
+ // 1. 构建默认的返回值
|
|
|
+ AppBrokerageProductPriceRespVO respVO = new AppBrokerageProductPriceRespVO().setEnabled(false)
|
|
|
+ .setBrokerageMinPrice(0).setBrokerageMaxPrice(0);
|
|
|
+
|
|
|
+ // 2.1 校验分销功能是否开启
|
|
|
+ TradeConfigDO tradeConfig = tradeConfigService.getTradeConfig();
|
|
|
+ if (tradeConfig == null || !BooleanUtil.isTrue(tradeConfig.getBrokerageEnabled())) {
|
|
|
+ return respVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2.2 校验用户是否有分销资格
|
|
|
+ respVO.setEnabled(brokerageUserService.getUserBrokerageEnabled(getLoginUserId()));
|
|
|
+ if (!BooleanUtil.isTrue(respVO.getEnabled())) {
|
|
|
+ return respVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer fixedMinPrice = 0;
|
|
|
+ Integer fixedMaxPrice = 0;
|
|
|
+ Integer spuMinPrice = 0;
|
|
|
+ Integer spuMaxPrice = 0;
|
|
|
+ // 2.3 校验商品是否存在
|
|
|
+ ProductSpuRespDTO spu = productSpuApi.getSpu(spuId);
|
|
|
+ if (spu == null) {
|
|
|
+ return respVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<ProductSkuRespDTO> skuList = productSkuApi.getSkuListBySpuId(ListUtil.of(spuId));
|
|
|
+ if (BooleanUtil.isTrue(spu.getSubCommissionType())) {
|
|
|
+ // 3.1 商品单独分佣模式
|
|
|
+ fixedMinPrice = getMinValue(skuList, ProductSkuRespDTO::getFirstBrokeragePrice);
|
|
|
+ fixedMaxPrice = getMaxValue(skuList, ProductSkuRespDTO::getFirstBrokeragePrice);
|
|
|
+ } else {
|
|
|
+ // 3.2 全局分佣模式(根据商品价格比例计算)
|
|
|
+ spuMinPrice = getMinValue(skuList, ProductSkuRespDTO::getPrice);
|
|
|
+ spuMaxPrice = getMaxValue(skuList, ProductSkuRespDTO::getPrice);
|
|
|
+ }
|
|
|
+
|
|
|
+ respVO.setBrokerageMinPrice(calculatePrice(spuMinPrice, tradeConfig.getBrokerageFirstPercent(), fixedMinPrice));
|
|
|
+ respVO.setBrokerageMaxPrice(calculatePrice(spuMaxPrice, tradeConfig.getBrokerageFirstPercent(), fixedMaxPrice));
|
|
|
+ return respVO;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获得自身的代理对象,解决 AOP 生效问题
|
|
|
*
|