分账的方法 fix bug

This commit is contained in:
Jack 2025-08-03 09:59:47 +08:00
parent d4a31e1236
commit cede2b4378
6 changed files with 95 additions and 57 deletions

View File

@ -9,6 +9,7 @@ import com.suisung.mall.common.utils.CommonUtil;
import com.suisung.mall.core.web.service.impl.BaseServiceImpl; import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
import com.suisung.mall.shop.base.mapper.ShopBaseStoreCategoryMapper; import com.suisung.mall.shop.base.mapper.ShopBaseStoreCategoryMapper;
import com.suisung.mall.shop.base.service.ShopBaseStoreCategoryService; import com.suisung.mall.shop.base.service.ShopBaseStoreCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -26,6 +27,7 @@ import java.util.Map;
* @author Xinze * @author Xinze
* @since 2021-04-23 * @since 2021-04-23
*/ */
@Slf4j
@Service @Service
public class ShopBaseStoreCategoryServiceImpl extends BaseServiceImpl<ShopBaseStoreCategoryMapper, ShopBaseStoreCategory> implements ShopBaseStoreCategoryService { public class ShopBaseStoreCategoryServiceImpl extends BaseServiceImpl<ShopBaseStoreCategoryMapper, ShopBaseStoreCategory> implements ShopBaseStoreCategoryService {
@ -111,27 +113,46 @@ public class ShopBaseStoreCategoryServiceImpl extends BaseServiceImpl<ShopBaseSt
return tree; return tree;
} }
/**
* 获取店铺分类的分账比例数值
*
* @param storeCategoryId 店铺分类ID
* @return 分账比例返回值范围应在0-100之间异常情况下返回默认值100
*/
@Override @Override
public BigDecimal getStoreCategoryRatio(Integer storeCategoryId) { public BigDecimal getStoreCategoryRatio(Integer storeCategoryId) {
BigDecimal splitRatio = new BigDecimal(100); // 默认分账比例为100%
if (ObjectUtil.isEmpty(storeCategoryId)) { 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; 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;
} }
} }

View File

@ -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); 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); 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);
/** /**
* 获取微信发货订单基本信息 * 获取微信发货订单基本信息
* *

View File

@ -7432,6 +7432,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
data_row.setDelivery_time_id(delivery_time_id); // 配送时间:要求不限周一~周五周末等等 data_row.setDelivery_time_id(delivery_time_id); // 配送时间:要求不限周一~周五周末等等
data_row.setInvoice_type_id(invoice_type_id); // 发票类型(ENUM):0-none; 1-普通; 2-电子;3-增值 data_row.setInvoice_type_id(invoice_type_id); // 发票类型(ENUM):0-none; 1-普通; 2-电子;3-增值
data_row.setOrder_invoice_title(order_invoice_title); // 发票信息 data_row.setOrder_invoice_title(order_invoice_title); // 发票信息
// 配送时间新加 // 配送时间新加
data_row.setDelivery_time(delivery_time); data_row.setDelivery_time(delivery_time);
data_row.setDelivery_time_rang(CheckUtil.isNotEmpty(delivery_istimer) ? delivery_time_rang : 0); 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); // 实际运费金额-卖家可修改 data_row.setOrder_shipping_fee(_freight); // 实际运费金额-卖家可修改
// base_row 中获取应付款 order_payment_amount计算平台和代理商的总计分账金额 // 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"); Integer voucher_id = (Integer) order_voucher_row.get("voucher_id");
String voucher_code = (String) order_voucher_row.get("voucher_code"); 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 storeId 店铺ID
* @param orderPaymentAmount 订单实际支付金额单位 * @param pendingAmount 待分账金额 =订单实际支付金额单位减去运费
* @return 分账给平台和代理商的总金额单位 * @return 分账给平台和代理商的总金额单位
*/ */
public BigDecimal calculatePlatformAndAgentShareAmount(Integer storeId, BigDecimal orderPaymentAmount) { public BigDecimal calculatePlatformAndAgentShareAmount(Integer storeId, BigDecimal pendingAmount) {
if (storeId == null || storeId <= 0 || orderPaymentAmount == null) { // 检查参数有效性
if (storeId == null || storeId <= 0 || pendingAmount == null || pendingAmount.compareTo(BigDecimal.ZERO) <= 0) {
logger.warn("计算分账金额参数无效: storeId={}, pendingAmount={}", storeId, pendingAmount);
return BigDecimal.ZERO; return BigDecimal.ZERO;
} }
// 获取平台分账比例例如 95 表示 95% try {
BigDecimal store_platform_ratio = shopStoreBaseService.getStorePlatformRatio(storeId, false); // 获取平台和代理商的分账比例例如 5 表示 5%
if (store_platform_ratio == null) { 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; 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); // 保留两位小数四舍五入
} }
} }

View File

@ -189,7 +189,7 @@ public class ShopOrderLklServiceImpl extends BaseServiceImpl<ShopOrderLklMapper,
// 运费和商家分账比例 // 运费和商家分账比例
BigDecimal shipperFee = shopOrderDataService.getOrderShippingFee(orderId); BigDecimal shipperFee = shopOrderDataService.getOrderShippingFee(orderId);
record.setShopping_fee(shipperFee.multiply(BigDecimal.valueOf(100)).intValue()); // 运费单位 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); return addOrUpdateByStoreOrder(record);
} catch (Exception e) { } catch (Exception e) {

View File

@ -182,14 +182,14 @@ public interface ShopStoreBaseService extends IBaseService<ShopStoreBase> {
Boolean updateStoreBaseQrCode(Integer storeId, String wxQrCode); Boolean updateStoreBaseQrCode(Integer storeId, String wxQrCode);
/** /**
* 获取店铺的平台分账比例 * 获取店铺的分账比例
* 根据店铺的大分小分类来计算分账比例 * 根据店铺的大分小分类来计算分账比例
* *
* @param storeId * @param storeId
* @param reCalculate 是否根据店铺的分类来重新计算 * @param reCalculate 是否根据店铺的分类来重新计算
* @return 平台分账比例的数值 * @return 平台分账比例的数值
*/ */
BigDecimal getStorePlatformRatio(Integer storeId, boolean reCalculate); BigDecimal getStoreSplitRatio(Integer storeId, boolean reCalculate);
/** /**
* 修改店铺的营业状态 * 修改店铺的营业状态

View File

@ -3522,7 +3522,7 @@ public class ShopStoreBaseServiceImpl extends BaseServiceImpl<ShopStoreBaseMappe
} }
/** /**
* 获取店铺的平台分账比例 * 获取店铺的分账比例
* 根据店铺的大分小分类来计算分账比例 * 根据店铺的大分小分类来计算分账比例
* *
* @param storeId * @param storeId
@ -3530,15 +3530,16 @@ public class ShopStoreBaseServiceImpl extends BaseServiceImpl<ShopStoreBaseMappe
* @return 平台分账比例的数值 * @return 平台分账比例的数值
*/ */
@Override @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) { if (storeId == null || storeId <= 0) {
return null; return defaultSplitRatio;
} }
// 获取店铺基础信息 // 获取店铺基础信息
ShopStoreBase shopStoreBase = get(storeId); ShopStoreBase shopStoreBase = get(storeId);
if (shopStoreBase == null) { if (shopStoreBase == null) {
return null; return defaultSplitRatio;
} }
// 获取店铺已配置的分账比例 // 获取店铺已配置的分账比例
@ -3548,7 +3549,7 @@ public class ShopStoreBaseServiceImpl extends BaseServiceImpl<ShopStoreBaseMappe
if (reCalculate || splitRatio == null) { if (reCalculate || splitRatio == null) {
Integer storeCategoryId = shopStoreBase.getStore_category_id(); Integer storeCategoryId = shopStoreBase.getStore_category_id();
if (storeCategoryId == null) { 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) { if (category != null && category.getSplit_ratio() != null) {
splitRatio = category.getSplit_ratio(); splitRatio = category.getSplit_ratio();
} else { } else {
splitRatio = new BigDecimal(100); // 默认值 splitRatio = defaultSplitRatio; // 默认值
} }
// 确保比例不超过 100% // 确保比例不超过 100%
if (splitRatio.compareTo(new BigDecimal(100)) > 0) { if (splitRatio.compareTo(BigDecimal.ZERO) < 0 || splitRatio.compareTo(defaultSplitRatio) > 0) {
splitRatio = new BigDecimal(100); splitRatio = defaultSplitRatio;
} }
} }