优化复购功能:指定商品复购付款时直接立减(不返现),解耦分销买家佣金,纯后端实现
这个提交包含在:
@@ -128,11 +128,8 @@ class Commission
|
||||
|
||||
|
||||
|
||||
$hasRepurchaseSelf = $this->isRepurchase()
|
||||
&& $this->_config->isRepurchaseSelfEnable()
|
||||
&& $this->goods->isRepurchaseEnable();
|
||||
|
||||
|
||||
// 复购已在下单付款时直接减免,分销佣金奖池中不再发放买家本人复购返现
|
||||
$hasRepurchaseSelf = false;
|
||||
|
||||
if (!$hasChainCommission && !$hasTierCommission && !$hasRepurchaseSelf) {
|
||||
|
||||
@@ -677,135 +674,29 @@ class Commission
|
||||
|
||||
|
||||
protected function runRepurchaseRewards($commissionOrder)
|
||||
|
||||
{
|
||||
|
||||
if (!$this->isRepurchase() || !$this->_config->isRepurchaseSelfEnable() || !$this->goods->isRepurchaseEnable()) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$commission = 0;
|
||||
|
||||
$rule = ['award_type' => 'repurchase_self'];
|
||||
|
||||
if ($this->_config->getRepurchaseSelfType() === 'fixed') {
|
||||
|
||||
$commission = round($this->_config->getRepurchaseSelfAmount(), 2);
|
||||
|
||||
$rule['type'] = 'fixed';
|
||||
|
||||
$rule['amount'] = $commission;
|
||||
|
||||
} else {
|
||||
|
||||
$rate = $this->_config->getRepurchaseSelfRate();
|
||||
|
||||
if ($rate <= 0) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
$commission = round($this->amount * $rate * 0.01, 2);
|
||||
|
||||
$rule['type'] = 'rate';
|
||||
|
||||
$rule['rate'] = $rate;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ($commission <= 0) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$rewards = RewardModel::where('commission_order_id', $commissionOrder->id)
|
||||
|
||||
->where('agent_id', $this->buyer->user->id)
|
||||
|
||||
->where('commission_level', 0)
|
||||
|
||||
->select();
|
||||
|
||||
foreach ($rewards as $reward) {
|
||||
|
||||
$rewardRules = json_decode($reward->commission_rules, true);
|
||||
|
||||
if (($rewardRules['award_type'] ?? '') === 'repurchase_self') {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$reward = RewardModel::create([
|
||||
|
||||
'order_id' => $commissionOrder->order_id,
|
||||
|
||||
'order_item_id' => $commissionOrder->order_item_id,
|
||||
|
||||
'buyer_id' => $commissionOrder->buyer_id,
|
||||
|
||||
'commission_order_id' => $commissionOrder->id,
|
||||
|
||||
'agent_id' => $this->buyer->user->id,
|
||||
|
||||
'type' => 'money',
|
||||
|
||||
'commission' => $commission,
|
||||
|
||||
'status' => 0,
|
||||
|
||||
'original_commission' => $commission,
|
||||
|
||||
'commission_level' => 0,
|
||||
|
||||
'commission_rules' => json_encode($rule),
|
||||
|
||||
'agent_level' => 0,
|
||||
|
||||
]);
|
||||
|
||||
|
||||
|
||||
Log::write($this->buyer->user->id, 'reward', [
|
||||
|
||||
'action' => 'repurchase_self',
|
||||
|
||||
'reward' => $reward,
|
||||
|
||||
], ['oper_type' => 'user', 'oper_id' => $commissionOrder->buyer_id]);
|
||||
|
||||
// 复购优惠已在商品下单付款时直接立减,此处不再发放买家本人复购返现佣金
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected function isRepurchase()
|
||||
|
||||
{
|
||||
$goodsId = $this->item['goods_id'] ?? 0;
|
||||
$userId = $this->buyer->user->id ?? 0;
|
||||
if (!$goodsId || !$userId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$count = \addons\shopro\model\Order::where('user_id', $this->buyer->user->id)
|
||||
|
||||
->where('status', 'in', [1, 2])
|
||||
|
||||
->where('id', '<', $this->item['order_id'])
|
||||
|
||||
$count = \addons\shopro\model\OrderItem::alias('oi')
|
||||
->join('shopro_order o', 'o.id = oi.order_id')
|
||||
->where('o.user_id', $userId)
|
||||
->where('o.status', 'in', [1, 2])
|
||||
->where('oi.goods_id', $goodsId)
|
||||
->where('o.id', '<', $this->item['order_id'])
|
||||
->where('oi.refund_status', '<=', 0)
|
||||
->count();
|
||||
|
||||
return $count > 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\commission;
|
||||
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 复购立减服务类
|
||||
* 针对指定商品(开启复购规则)且买家历史买过的商品,在付款时直接减免
|
||||
*/
|
||||
class RepurchaseService
|
||||
{
|
||||
/**
|
||||
* 获取全局复购立减配置
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getConfig()
|
||||
{
|
||||
$config = [];
|
||||
try {
|
||||
$configModel = new Config();
|
||||
$config = [
|
||||
'enable' => $configModel->isRepurchaseEnable(),
|
||||
'self_enable' => $configModel->isRepurchaseSelfEnable(),
|
||||
'self_type' => $configModel->getRepurchaseSelfType(),
|
||||
'self_amount' => $configModel->getRepurchaseSelfAmount(),
|
||||
'self_rate' => $configModel->getRepurchaseSelfRate(),
|
||||
];
|
||||
} catch (\Throwable $e) {
|
||||
$config = [
|
||||
'enable' => false,
|
||||
'self_enable' => false,
|
||||
'self_type' => 'fixed',
|
||||
'self_amount' => 0.0,
|
||||
'self_rate' => 0.0,
|
||||
];
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查商品是否开启复购规则
|
||||
*
|
||||
* @param int $goodsId
|
||||
* @return bool
|
||||
*/
|
||||
public static function isGoodsRepurchaseEnable($goodsId)
|
||||
{
|
||||
if (!$goodsId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$row = Db::name('shopro_commission_goods')
|
||||
->where('goods_id', $goodsId)
|
||||
->where('status', 1)
|
||||
->field('repurchase_enable')
|
||||
->find();
|
||||
return !empty($row['repurchase_enable']);
|
||||
} catch (\Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否曾购买过该指定商品(有效付款订单)
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $goodsId
|
||||
* @return bool
|
||||
*/
|
||||
public static function isUserRepurchasedGoods($userId, $goodsId)
|
||||
{
|
||||
if (!$userId || !$goodsId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$count = Db::name('shopro_order_item')
|
||||
->alias('oi')
|
||||
->join('shopro_order o', 'o.id = oi.order_id')
|
||||
->where('o.user_id', $userId)
|
||||
->where('o.status', 'in', [1, 2])
|
||||
->where('oi.goods_id', $goodsId)
|
||||
->where('oi.refund_status', '<=', 0)
|
||||
->count();
|
||||
|
||||
return $count > 0;
|
||||
} catch (\Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算单项商品的复购立减金额
|
||||
*
|
||||
* @param array $item 商品信息 (goods_id, goods_num, goods_price, goods_amount)
|
||||
* @param array $config 复购配置
|
||||
* @param bool $isGoodsEnable 该商品是否开启复购
|
||||
* @param bool $isUserRepurchase 该用户是否复购该商品
|
||||
* @return float
|
||||
*/
|
||||
public static function calculateItemDiscount($item, $config, $isGoodsEnable, $isUserRepurchase)
|
||||
{
|
||||
if (empty($config['enable']) || empty($config['self_enable'])) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
if (!$isGoodsEnable || !$isUserRepurchase) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
$goodsNum = isset($item['goods_num']) ? intval($item['goods_num']) : 1;
|
||||
$goodsAmount = isset($item['goods_amount']) ? floatval($item['goods_amount']) : 0.0;
|
||||
$goodsPrice = isset($item['goods_price']) ? floatval($item['goods_price']) : ($goodsNum > 0 ? $goodsAmount / $goodsNum : 0.0);
|
||||
|
||||
if ($goodsAmount <= 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
$discount = 0.0;
|
||||
$selfType = $config['self_type'] ?? 'fixed';
|
||||
|
||||
if ($selfType === 'rate') {
|
||||
$rate = floatval($config['self_rate'] ?? 0);
|
||||
if ($rate > 0) {
|
||||
$discount = round($goodsAmount * $rate * 0.01, 2);
|
||||
}
|
||||
} else {
|
||||
// 固定金额模式:按件数累加立减
|
||||
$unitAmount = floatval($config['self_amount'] ?? 0);
|
||||
if ($unitAmount > 0) {
|
||||
// 单件立减不可超过单件售价
|
||||
$validUnitDiscount = min($unitAmount, $goodsPrice);
|
||||
$discount = round($validUnitDiscount * $goodsNum, 2);
|
||||
}
|
||||
}
|
||||
|
||||
// 兜底保护:总立减金额不可超过商品总额
|
||||
if ($discount > $goodsAmount) {
|
||||
$discount = $goodsAmount;
|
||||
}
|
||||
|
||||
return max(0.0, $discount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量计算订单中各商品的复购立减金额
|
||||
*
|
||||
* @param array $goodsList 商品列表
|
||||
* @param int $userId 用户ID
|
||||
* @param array|null $config 配置
|
||||
* @param callable|null $goodsRuleChecker 自定义商品规则检查器 (用于单测或依赖注入)
|
||||
* @param callable|null $historyChecker 自定义历史购买检查器 (用于单测或依赖注入)
|
||||
* @return array ['goods_list' => ..., 'total_repurchase_discount' => float]
|
||||
*/
|
||||
public static function calculateOrderRepurchaseDiscounts(
|
||||
$goodsList,
|
||||
$userId,
|
||||
$config = null,
|
||||
$goodsRuleChecker = null,
|
||||
$historyChecker = null
|
||||
) {
|
||||
if ($config === null) {
|
||||
$config = self::getConfig();
|
||||
}
|
||||
|
||||
$totalDiscount = 0.0;
|
||||
|
||||
foreach ($goodsList as $key => $item) {
|
||||
$goodsId = $item['goods_id'] ?? 0;
|
||||
|
||||
// 检查商品是否参与复购
|
||||
$isGoodsEnable = false;
|
||||
if ($goodsRuleChecker !== null) {
|
||||
$isGoodsEnable = (bool) call_user_func($goodsRuleChecker, $goodsId);
|
||||
} else {
|
||||
$isGoodsEnable = self::isGoodsRepurchaseEnable($goodsId);
|
||||
}
|
||||
|
||||
// 检查用户是否复购该商品
|
||||
$isUserRepurchase = false;
|
||||
if ($isGoodsEnable) {
|
||||
if ($historyChecker !== null) {
|
||||
$isUserRepurchase = (bool) call_user_func($historyChecker, $userId, $goodsId);
|
||||
} else {
|
||||
$isUserRepurchase = self::isUserRepurchasedGoods($userId, $goodsId);
|
||||
}
|
||||
}
|
||||
|
||||
$discount = self::calculateItemDiscount($item, $config, $isGoodsEnable, $isUserRepurchase);
|
||||
|
||||
$goodsList[$key]['repurchase_discount_money'] = $discount;
|
||||
$goodsList[$key]['is_repurchase'] = ($isGoodsEnable && $isUserRepurchase && $discount > 0) ? 1 : 0;
|
||||
|
||||
$totalDiscount = bcadd((string) $totalDiscount, (string) $discount, 2);
|
||||
}
|
||||
|
||||
return [
|
||||
'goods_list' => $goodsList,
|
||||
'total_repurchase_discount' => floatval($totalDiscount)
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -88,6 +88,41 @@ trait OrderOper
|
||||
$user_address
|
||||
);
|
||||
|
||||
// 计算满足复购规则的指定商品的复购立减
|
||||
$repurchase_discount_money = 0.0;
|
||||
try {
|
||||
$repurchaseResult = \addons\shopro\library\commission\RepurchaseService::calculateOrderRepurchaseDiscounts(
|
||||
$new_goods_list,
|
||||
$user ? $user->id : 0
|
||||
);
|
||||
$new_goods_list = $repurchaseResult['goods_list'];
|
||||
$repurchase_discount_money = $repurchaseResult['total_repurchase_discount'];
|
||||
|
||||
// 将复购立减注入活动优惠列表,前端零改动原生展示
|
||||
if ($repurchase_discount_money > 0) {
|
||||
$repurchaseGoodsIds = [];
|
||||
foreach ($new_goods_list as $g) {
|
||||
if (!empty($g['repurchase_discount_money']) && $g['repurchase_discount_money'] > 0) {
|
||||
$repurchaseGoodsIds[] = $g['goods_id'];
|
||||
}
|
||||
}
|
||||
$activity_discount_infos[] = [
|
||||
'activity_id' => 'repurchase',
|
||||
'activity_title' => '复购立减',
|
||||
'activity_type' => 'repurchase',
|
||||
'activity_discount_money' => $repurchase_discount_money,
|
||||
'rule_type' => 'money',
|
||||
'discount_rule' => [
|
||||
'discount' => $repurchase_discount_money,
|
||||
],
|
||||
'goods_ids' => implode(',', $repurchaseGoodsIds)
|
||||
];
|
||||
$activity_discount_money = bcadd((string)$activity_discount_money, (string)$repurchase_discount_money, 2);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$repurchase_discount_money = 0.0;
|
||||
}
|
||||
|
||||
// 计算订单总金额,需支付金额
|
||||
list(
|
||||
$new_goods_list,
|
||||
@@ -105,7 +140,8 @@ trait OrderOper
|
||||
$dispatch_discount_money,
|
||||
$free_shipping_goods_ids,
|
||||
$coupon_money,
|
||||
$deliveryFee
|
||||
$deliveryFee,
|
||||
$repurchase_discount_money
|
||||
);
|
||||
|
||||
// 获取发票金额
|
||||
@@ -150,7 +186,8 @@ trait OrderOper
|
||||
$invoice_amount,
|
||||
$deliveryFee,
|
||||
$expressList,
|
||||
$expressName
|
||||
$expressName,
|
||||
$repurchase_discount_money
|
||||
);
|
||||
}
|
||||
|
||||
@@ -242,7 +279,8 @@ trait OrderOper
|
||||
$invoice,
|
||||
$invoice_amount,
|
||||
$deliveryFee,
|
||||
$expressName
|
||||
$expressName,
|
||||
$repurchase_discount_money
|
||||
) {
|
||||
// 订单创建前
|
||||
$data = [
|
||||
@@ -260,6 +298,7 @@ trait OrderOper
|
||||
'coupon_fee' => $coupon_fee,
|
||||
'activity_discount_money' => $activity_discount_money,
|
||||
'dispatch_discount_money' => $dispatch_discount_money,
|
||||
'repurchase_discount_money' => $repurchase_discount_money,
|
||||
'activity_discount_infos' => $activity_discount_infos,
|
||||
'new_goods_list' => $new_goods_list,
|
||||
'activity_type' => $activity_type,
|
||||
@@ -348,6 +387,9 @@ trait OrderOper
|
||||
$orderData['platform'] = request()->header('platform');
|
||||
|
||||
$ext = $activity_discount_infos ? ['activity_discount_infos' => $activity_discount_infos] : []; // 促销活动信息
|
||||
if (!empty($repurchase_discount_money) && floatval($repurchase_discount_money) > 0) {
|
||||
$ext['repurchase_discount_money'] = $repurchase_discount_money;
|
||||
}
|
||||
$orderData['ext'] = json_encode($ext);
|
||||
$order = new Order();
|
||||
$order->allowField(true)->save($orderData);
|
||||
@@ -428,6 +470,9 @@ trait OrderOper
|
||||
if (isset($buyinfo['dispatch_phone'])) {
|
||||
$ext['dispatch_phone'] = $buyinfo['dispatch_phone'];
|
||||
}
|
||||
if (isset($buyinfo['repurchase_discount_money']) && floatval($buyinfo['repurchase_discount_money']) > 0) {
|
||||
$ext['repurchase_discount_money'] = $buyinfo['repurchase_discount_money'];
|
||||
}
|
||||
$orderItem->ext = json_encode($ext);
|
||||
$orderItem->save();
|
||||
}
|
||||
|
||||
@@ -717,7 +717,8 @@ trait OrderOperCreate
|
||||
$dispatch_discount_money,
|
||||
$free_shipping_goods_ids,
|
||||
$coupon_money,
|
||||
$deliveryFee
|
||||
$deliveryFee,
|
||||
$repurchase_discount_money = 0
|
||||
)
|
||||
{
|
||||
// (搜这个找注释)加上运费,dispatch_amount 没有重新计算,和 origin_dispatch_amount 相同
|
||||
@@ -737,7 +738,8 @@ trait OrderOperCreate
|
||||
$activity_discount_infos,
|
||||
$activity_discount_money,
|
||||
$dispatch_discount_money,
|
||||
$free_shipping_goods_ids
|
||||
$free_shipping_goods_ids,
|
||||
$repurchase_discount_money
|
||||
);
|
||||
|
||||
return [
|
||||
@@ -814,7 +816,8 @@ trait OrderOperCreate
|
||||
$invoice_amount,
|
||||
$deliveryFee,
|
||||
$expressList,
|
||||
$expressName
|
||||
$expressName,
|
||||
$repurchase_discount_money = 0
|
||||
)
|
||||
{
|
||||
// 需要处理小数点的数据
|
||||
@@ -829,6 +832,7 @@ trait OrderOperCreate
|
||||
"coupon_fee",
|
||||
"activity_discount_money",
|
||||
"dispatch_discount_money",
|
||||
"repurchase_discount_money",
|
||||
"invoice_amount",
|
||||
'deliveryFee'
|
||||
);
|
||||
@@ -882,7 +886,8 @@ trait OrderOperCreate
|
||||
$activity_discount_infos,
|
||||
$activity_discount_money,
|
||||
$dispatch_discount_money,
|
||||
$free_shipping_goods_ids = []
|
||||
$free_shipping_goods_ids = [],
|
||||
$repurchase_discount_money = 0
|
||||
)
|
||||
{
|
||||
// 如果邮费有优惠,分配邮费优惠的金额
|
||||
@@ -915,8 +920,8 @@ trait OrderOperCreate
|
||||
|
||||
if (floatval($activity_discount_money)) {
|
||||
foreach ($activity_discount_infos as $key => $info) {
|
||||
if ($info['activity_type'] == 'free_shipping') {
|
||||
// 上面已经单独处理过满包邮优惠
|
||||
if ($info['activity_type'] == 'free_shipping' || $info['activity_type'] == 'repurchase') {
|
||||
// 上面已经单独处理过满包邮优惠,复购立减优先按商品精准分配
|
||||
continue;
|
||||
}
|
||||
$current_discount_goods_ids = explode(',', $info['goods_ids']);
|
||||
@@ -946,8 +951,21 @@ trait OrderOperCreate
|
||||
}
|
||||
}
|
||||
|
||||
// 剩余要加权平均分配的优惠
|
||||
$last_discount_money = bcsub($discount_fee, bcadd($dispatch_discount_money, $activity_discount_money, 2), 2);
|
||||
// 如果存在复购立减,精准归属到满足复购的指定商品本身
|
||||
if (floatval($repurchase_discount_money)) {
|
||||
foreach ($new_goods_list as $key => $buyinfo) {
|
||||
$itemRepurchaseDiscount = $buyinfo['repurchase_discount_money'] ?? 0;
|
||||
if (floatval($itemRepurchaseDiscount) > 0) {
|
||||
$new_goods_list[$key]['discount_fee'] = bcadd($new_goods_list[$key]['discount_fee'], (string)$itemRepurchaseDiscount, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 剩余要加权平均分配的优惠(如优惠券),需剔除邮费优惠和全部活动优惠(含复购立减)
|
||||
$assigned_discount = bcadd($dispatch_discount_money, $activity_discount_money, 2);
|
||||
$last_discount_money = bcsub($discount_fee, $assigned_discount, 2);
|
||||
$last_discount_money = $last_discount_money > 0 ? $last_discount_money : 0;
|
||||
|
||||
// 计算每个商品分配到的优惠券的优惠金额,顺便计算出来 pay_price 的金额
|
||||
foreach ($new_goods_list as $key => $buyinfo) {
|
||||
$scale = 0; // 按照商品价格和总价计算每个 item 的比例
|
||||
@@ -959,7 +977,8 @@ trait OrderOperCreate
|
||||
$current_discount_fee = round(bcmul($last_discount_money, $scale, 3), 2);
|
||||
$new_goods_list[$key]['discount_fee'] = bcadd($new_goods_list[$key]['discount_fee'], $current_discount_fee, 2);
|
||||
// 每个商品除了运费之后分配的支付金额
|
||||
$new_goods_list[$key]['pay_price'] = bcsub($buyinfo['goods_amount'], $new_goods_list[$key]['discount_fee'], 2);
|
||||
$calc_pay_price = bcsub($buyinfo['goods_amount'], $new_goods_list[$key]['discount_fee'], 2);
|
||||
$new_goods_list[$key]['pay_price'] = $calc_pay_price < 0 ? '0.00' : $calc_pay_price;
|
||||
}
|
||||
|
||||
return $new_goods_list;
|
||||
|
||||
在新工单中引用
屏蔽一个用户