分账的方法 fix bug
This commit is contained in:
parent
d4a31e1236
commit
cede2b4378
@ -9,6 +9,7 @@ import com.suisung.mall.common.utils.CommonUtil;
|
||||
import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
|
||||
import com.suisung.mall.shop.base.mapper.ShopBaseStoreCategoryMapper;
|
||||
import com.suisung.mall.shop.base.service.ShopBaseStoreCategoryService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@ -26,6 +27,7 @@ import java.util.Map;
|
||||
* @author Xinze
|
||||
* @since 2021-04-23
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ShopBaseStoreCategoryServiceImpl extends BaseServiceImpl<ShopBaseStoreCategoryMapper, ShopBaseStoreCategory> implements ShopBaseStoreCategoryService {
|
||||
|
||||
@ -111,27 +113,46 @@ public class ShopBaseStoreCategoryServiceImpl extends BaseServiceImpl<ShopBaseSt
|
||||
return tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺分类的分账比例数值
|
||||
*
|
||||
* @param storeCategoryId 店铺分类ID
|
||||
* @return 分账比例,返回值范围应在0-100之间,异常情况下返回默认值100
|
||||
*/
|
||||
@Override
|
||||
public BigDecimal getStoreCategoryRatio(Integer storeCategoryId) {
|
||||
BigDecimal splitRatio = new BigDecimal(100);
|
||||
if (ObjectUtil.isEmpty(storeCategoryId)) {
|
||||
// 默认分账比例为100%
|
||||
BigDecimal defaultRatio = new BigDecimal("100");
|
||||
|
||||
try {
|
||||
// 检查参数是否为空
|
||||
if (ObjectUtil.isEmpty(storeCategoryId)) {
|
||||
return defaultRatio;
|
||||
}
|
||||
|
||||
// 根据ID获取店铺分类信息
|
||||
ShopBaseStoreCategory shopBaseStoreCategory = get(storeCategoryId);
|
||||
if (ObjectUtil.isEmpty(shopBaseStoreCategory)) {
|
||||
return defaultRatio;
|
||||
}
|
||||
|
||||
// 获取分账比例,如果为空则使用默认值
|
||||
BigDecimal splitRatio = shopBaseStoreCategory.getSplit_ratio();
|
||||
if (splitRatio == null) {
|
||||
return defaultRatio;
|
||||
}
|
||||
|
||||
// 验证分账比例是否合法,不合法则使用默认值
|
||||
if (!CommonUtil.checkSplitRatio(splitRatio)) {
|
||||
return defaultRatio;
|
||||
}
|
||||
|
||||
return splitRatio;
|
||||
} catch (Exception e) {
|
||||
// 处理异常情况,确保方法有返回值
|
||||
log.error("获取店铺分类分账比例异常,storeCategoryId: {}", storeCategoryId, e);
|
||||
return defaultRatio;
|
||||
}
|
||||
|
||||
ShopBaseStoreCategory shopBaseStoreCategory = get(storeCategoryId);
|
||||
if (ObjectUtil.isEmpty(shopBaseStoreCategory)) {
|
||||
return splitRatio;
|
||||
}
|
||||
|
||||
splitRatio = shopBaseStoreCategory.getSplit_ratio();
|
||||
if (splitRatio == null) {
|
||||
splitRatio = new BigDecimal(100);
|
||||
}
|
||||
|
||||
if (!CommonUtil.checkSplitRatio(splitRatio)) {
|
||||
splitRatio = new BigDecimal(100);
|
||||
}
|
||||
|
||||
return splitRatio;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -78,6 +78,16 @@ public interface ShopOrderBaseMapper extends BaseMapper<ShopOrderBase> {
|
||||
*/
|
||||
IPage<MchOrderInfoDTO> selectMchOrderPageList(@Param("storeId") Integer storeId, @Param("keyword") String keyword, @Param("delivery") Integer delivery, @Param("status") Integer status, @Param("logisticsStatus") Integer logisticsStatus, @Param("expireSeconds") Long expireSeconds, @Param("beginTime") Long beginTime, @Param("endTime") Long endTime, IPage<MchOrderInfoDTO> page);
|
||||
|
||||
|
||||
/**
|
||||
* 获取商家版订单详情(新)
|
||||
*
|
||||
* @param orderId
|
||||
* @param expireSeconds 配送超时的秒数,单位秒
|
||||
* @return
|
||||
*/
|
||||
MchOrderInfoDTO getMchOrderDetail(@Param("orderId") String orderId, @Param("expireSeconds") Long expireSeconds);
|
||||
|
||||
/**
|
||||
* 商家版根据条件查询订单条数(新)
|
||||
*
|
||||
@ -94,15 +104,6 @@ public interface ShopOrderBaseMapper extends BaseMapper<ShopOrderBase> {
|
||||
Long countMchOrderByCondition(@Param("storeId") Integer storeId, @Param("keyword") String keyword, @Param("delivery") Integer delivery, @Param("status") Integer status, @Param("logisticsStatus") Integer logisticsStatus, @Param("expireSeconds") Long expireSeconds, @Param("beginTime") Long beginTime, @Param("endTime") Long endTime);
|
||||
|
||||
|
||||
/**
|
||||
* 获取商家版订单详情(新)
|
||||
*
|
||||
* @param orderId
|
||||
* @param expireSeconds 配送超时的秒数,单位秒
|
||||
* @return
|
||||
*/
|
||||
MchOrderInfoDTO getMchOrderDetail(@Param("orderId") String orderId, @Param("expireSeconds") Long expireSeconds);
|
||||
|
||||
/**
|
||||
* 获取微信发货订单基本信息
|
||||
*
|
||||
|
||||
@ -7432,6 +7432,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
|
||||
data_row.setDelivery_time_id(delivery_time_id); // 配送时间:要求,不限、周一~周五、周末等等
|
||||
data_row.setInvoice_type_id(invoice_type_id); // 发票类型(ENUM):0-none; 1-普通; 2-电子;3-增值
|
||||
data_row.setOrder_invoice_title(order_invoice_title); // 发票信息
|
||||
|
||||
// 配送时间新加
|
||||
data_row.setDelivery_time(delivery_time);
|
||||
data_row.setDelivery_time_rang(CheckUtil.isNotEmpty(delivery_istimer) ? delivery_time_rang : 0);
|
||||
@ -7451,7 +7452,10 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
|
||||
data_row.setOrder_shipping_fee(_freight); // 实际运费金额-卖家可修改
|
||||
|
||||
// 从 base_row 中获取应付款 order_payment_amount,计算平台和代理商的总计分账金额
|
||||
data_row.setTotal_separate_value(calculatePlatformAndAgentShareAmount(Convert.toInt(base_row.get("store_id")), order_payment_amount)); // 从拉卡拉分账,给平台和代理商的总计分账金额
|
||||
BigDecimal split_amount_from = order_payment_amount.subtract(_freight);
|
||||
BigDecimal platform_fee = calculatePlatformAndAgentShareAmount(Convert.toInt(base_row.get("store_id")), split_amount_from);
|
||||
data_row.setTotal_separate_value(platform_fee); // 从拉卡拉分账,给平台和代理商的总计分账金额
|
||||
data_row.setPlatform_fee(platform_fee);
|
||||
|
||||
Integer voucher_id = (Integer) order_voucher_row.get("voucher_id");
|
||||
String voucher_code = (String) order_voucher_row.get("voucher_code");
|
||||
@ -8903,31 +8907,42 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算平台与代理商的总分账金额:即支付金额 × (100% - 平台分账比例)
|
||||
* 计算平台与代理商的总分账金额
|
||||
*
|
||||
* @param storeId 包含订单基础信息的 Map,必须包含 store_id
|
||||
* @param orderPaymentAmount 订单实际支付金额(单位:元)
|
||||
* @param storeId 店铺ID
|
||||
* @param pendingAmount 待分账金额 =订单实际支付金额(单位:元)减去运费
|
||||
* @return 分账给平台和代理商的总金额(单位:元)
|
||||
*/
|
||||
public BigDecimal calculatePlatformAndAgentShareAmount(Integer storeId, BigDecimal orderPaymentAmount) {
|
||||
if (storeId == null || storeId <= 0 || orderPaymentAmount == null) {
|
||||
public BigDecimal calculatePlatformAndAgentShareAmount(Integer storeId, BigDecimal pendingAmount) {
|
||||
// 检查参数有效性
|
||||
if (storeId == null || storeId <= 0 || pendingAmount == null || pendingAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
||||
logger.warn("计算分账金额参数无效: storeId={}, pendingAmount={}", storeId, pendingAmount);
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
// 获取平台分账比例(例如 95 表示 95%)
|
||||
BigDecimal store_platform_ratio = shopStoreBaseService.getStorePlatformRatio(storeId, false);
|
||||
if (store_platform_ratio == null) {
|
||||
try {
|
||||
// 获取平台和代理商的分账比例(例如 5 表示 5%)
|
||||
BigDecimal platformAgentSplitRatio = shopStoreBaseService.getStoreSplitRatio(storeId, false);
|
||||
if (platformAgentSplitRatio == null) {
|
||||
logger.warn("获取店铺分账比例失败,storeId: {}", storeId);
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
// 确保比例在合理范围 [0, 100]
|
||||
platformAgentSplitRatio = platformAgentSplitRatio.max(BigDecimal.ZERO).min(new BigDecimal("100"));
|
||||
|
||||
// 分账金额 = 支付金额 × 平台和代理商分账比例 ÷ 100 (将百分比转换为小数)
|
||||
BigDecimal result = pendingAmount.multiply(platformAgentSplitRatio)
|
||||
.divide(new BigDecimal("100"), 2, RoundingMode.HALF_DOWN); // 保留两位小数
|
||||
|
||||
logger.debug("计算分账金额: storeId={}, pendingAmount={}, ratio={}, result={}",
|
||||
storeId, pendingAmount, platformAgentSplitRatio, result);
|
||||
|
||||
return result;
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("计算平台与代理商分账金额异常,storeId: {}, pendingAmount: {}", storeId, pendingAmount, e);
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
// 确保比例在合理范围 [0, 100]
|
||||
store_platform_ratio = store_platform_ratio.max(BigDecimal.ZERO).min(new BigDecimal(100));
|
||||
|
||||
// 分账比例 = 100% - 平台分账比例
|
||||
BigDecimal separateRatio = new BigDecimal(100).subtract(store_platform_ratio);
|
||||
|
||||
// 分账金额 = 支付金额 × 分账比例 ÷ 100 (将百分比转换为小数)
|
||||
return orderPaymentAmount.multiply(separateRatio)
|
||||
.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP); // 保留两位小数,四舍五入
|
||||
}
|
||||
}
|
||||
|
||||
@ -189,7 +189,7 @@ public class ShopOrderLklServiceImpl extends BaseServiceImpl<ShopOrderLklMapper,
|
||||
// 运费和商家分账比例
|
||||
BigDecimal shipperFee = shopOrderDataService.getOrderShippingFee(orderId);
|
||||
record.setShopping_fee(shipperFee.multiply(BigDecimal.valueOf(100)).intValue()); // 运费,单位:分
|
||||
record.setSplit_ratio(shopStoreBaseService.getStorePlatformRatio(storeId, false)); // 商家分账比例
|
||||
record.setSplit_ratio(shopStoreBaseService.getStoreSplitRatio(storeId, false)); // 商家分账比例
|
||||
|
||||
return addOrUpdateByStoreOrder(record);
|
||||
} catch (Exception e) {
|
||||
|
||||
@ -182,14 +182,14 @@ public interface ShopStoreBaseService extends IBaseService<ShopStoreBase> {
|
||||
Boolean updateStoreBaseQrCode(Integer storeId, String wxQrCode);
|
||||
|
||||
/**
|
||||
* 获取店铺的平台分账比例
|
||||
* 获取店铺的分账比例
|
||||
* 根据店铺的大分、小分类、来计算分账比例
|
||||
*
|
||||
* @param storeId
|
||||
* @param reCalculate 是否根据店铺的分类来重新计算
|
||||
* @return 平台分账比例的数值
|
||||
*/
|
||||
BigDecimal getStorePlatformRatio(Integer storeId, boolean reCalculate);
|
||||
BigDecimal getStoreSplitRatio(Integer storeId, boolean reCalculate);
|
||||
|
||||
/**
|
||||
* 修改店铺的营业状态
|
||||
|
||||
@ -3522,7 +3522,7 @@ public class ShopStoreBaseServiceImpl extends BaseServiceImpl<ShopStoreBaseMappe
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺的平台分账比例
|
||||
* 获取店铺的分账比例
|
||||
* 根据店铺的大分、小分类、来计算分账比例
|
||||
*
|
||||
* @param storeId
|
||||
@ -3530,15 +3530,16 @@ public class ShopStoreBaseServiceImpl extends BaseServiceImpl<ShopStoreBaseMappe
|
||||
* @return 平台分账比例的数值
|
||||
*/
|
||||
@Override
|
||||
public BigDecimal getStorePlatformRatio(Integer storeId, boolean reCalculate) {
|
||||
public BigDecimal getStoreSplitRatio(Integer storeId, boolean reCalculate) {
|
||||
BigDecimal defaultSplitRatio = new BigDecimal("100");
|
||||
if (storeId == null || storeId <= 0) {
|
||||
return null;
|
||||
return defaultSplitRatio;
|
||||
}
|
||||
|
||||
// 获取店铺基础信息
|
||||
ShopStoreBase shopStoreBase = get(storeId);
|
||||
if (shopStoreBase == null) {
|
||||
return null;
|
||||
return defaultSplitRatio;
|
||||
}
|
||||
|
||||
// 获取店铺已配置的分账比例
|
||||
@ -3548,7 +3549,7 @@ public class ShopStoreBaseServiceImpl extends BaseServiceImpl<ShopStoreBaseMappe
|
||||
if (reCalculate || splitRatio == null) {
|
||||
Integer storeCategoryId = shopStoreBase.getStore_category_id();
|
||||
if (storeCategoryId == null) {
|
||||
return new BigDecimal(100); // 默认 100%
|
||||
return defaultSplitRatio; // 默认 100%
|
||||
}
|
||||
|
||||
// 获取店铺分类信息
|
||||
@ -3556,12 +3557,12 @@ public class ShopStoreBaseServiceImpl extends BaseServiceImpl<ShopStoreBaseMappe
|
||||
if (category != null && category.getSplit_ratio() != null) {
|
||||
splitRatio = category.getSplit_ratio();
|
||||
} else {
|
||||
splitRatio = new BigDecimal(100); // 默认值
|
||||
splitRatio = defaultSplitRatio; // 默认值
|
||||
}
|
||||
|
||||
// 确保比例不超过 100%
|
||||
if (splitRatio.compareTo(new BigDecimal(100)) > 0) {
|
||||
splitRatio = new BigDecimal(100);
|
||||
if (splitRatio.compareTo(BigDecimal.ZERO) < 0 || splitRatio.compareTo(defaultSplitRatio) > 0) {
|
||||
splitRatio = defaultSplitRatio;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user