重构: 积分抵扣重构 - 订单支付后减少积分相关操作重构

优化:积分变更日志记录变更积分类型
添加:积分变更日志 - 商户端变更日志添加购买商品赠送积分类型
This commit is contained in:
wuhui_zzw 2023-11-08 18:19:41 +08:00
parent 03fe59ae83
commit 90468dfe66
4 changed files with 133 additions and 71 deletions

View File

@ -980,9 +980,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository
{
$uid = $user->uid;
$orderInfo = Cache::get('order_create_cache' . $uid . '_' . $key);
if(!$orderInfo){
throw new ValidateException('订单操作超时,请刷新页面');
}
if(!$orderInfo) throw new ValidateException('订单操作超时,请刷新页面');
$order_model = $orderInfo['order_model'];
$order_extend = $orderInfo['order_extend'];
if (!$orderInfo['order_delivery_status']) {
@ -1140,40 +1139,44 @@ class StoreOrderCreateRepository extends StoreOrderRepository
$user_address = isset($address) ? ($address['province'] . $address['city'] . $address['district'] . $address['street'] . $address['detail']) : '';
//整理订单数据
$_order = [
'cartInfo' => $merchantCart,
'activity_type' => $orderInfo['order_type'],
'commission_rate' => (float)$rate,
'order_type' => $merchantCart['order']['isTake'] ? 1 : 0,
'is_virtual' => $order_model,
'extension_one' => $total_extension_one,
'extension_two' => $total_extension_two,
'order_sn' => $this->getNewOrderId(StoreOrderRepository::TYPE_SN_ORDER) . ($k + 1),
'uid' => $uid,
'spread_uid' => $spreadUid,
'top_uid' => $topUid,
'is_selfbuy' => $isSelfBuy,
'real_name' => $merchantCart['order']['isTake'] ? $post['real_name'] : ($address['real_name'] ?? ''),
'user_phone' => $merchantCart['order']['isTake'] ? $post['phone'] : ($address['phone'] ?? ''),
'user_address' => $user_address,
'cart_id' => implode(',', array_column($merchantCart['list'], 'cart_id')),
'total_num' => $merchantCart['order']['total_num'],
'total_price' => $merchantCart['order']['total_price'],
'total_postage' => $merchantCart['order']['postage_price'],
'pay_postage' => $merchantCart['order']['postage_price'],
'svip_discount' => $merchantCart['order']['svip_discount'],
'pay_price' => $merchantCart['order']['pay_price'],
'integral' => $merchantCart['order']['total_integral'],
'integral_price' => $merchantCart['order']['total_integral_price'],
'give_integral' => $merchantCart['order']['total_give_integral'],
'mer_id' => $merchantCart['mer_id'],
'cost' => $cost,
'order_extend' => count($extend) ? json_encode($extend, JSON_UNESCAPED_UNICODE) : '',
'coupon_id' => implode(',', $merchantCart['order']['useCouponIds']),
'mark' => $mark[$merchantCart['mer_id']] ?? '',
'coupon_price' => bcadd($merchantCart['order']['coupon_price'], $merchantCart['order']['platform_coupon_price'], 2),
'cartInfo' => $merchantCart,
'activity_type' => $orderInfo['order_type'],
'commission_rate' => (float)$rate,
'order_type' => $merchantCart['order']['isTake'] ? 1 : 0,
'is_virtual' => $order_model,
'extension_one' => $total_extension_one,
'extension_two' => $total_extension_two,
'order_sn' => $this->getNewOrderId(StoreOrderRepository::TYPE_SN_ORDER).($k + 1),
'uid' => $uid,
'spread_uid' => $spreadUid,
'top_uid' => $topUid,
'is_selfbuy' => $isSelfBuy,
'real_name' => $merchantCart['order']['isTake'] ? $post['real_name'] : ($address['real_name'] ?? ''),
'user_phone' => $merchantCart['order']['isTake'] ? $post['phone'] : ($address['phone'] ?? ''),
'user_address' => $user_address,
'cart_id' => implode(',',array_column($merchantCart['list'],'cart_id')),
'total_num' => $merchantCart['order']['total_num'],
'total_price' => $merchantCart['order']['total_price'],
'total_postage' => $merchantCart['order']['postage_price'],
'pay_postage' => $merchantCart['order']['postage_price'],
'svip_discount' => $merchantCart['order']['svip_discount'],
'pay_price' => $merchantCart['order']['pay_price'],
'integral' => $merchantCart['order']['total_integral'],
'integral_price' => $merchantCart['order']['total_integral_price'],
'give_integral' => $merchantCart['order']['total_give_integral'],
'use_mer_integral' => (float)$merchantCart['order']['use_mer_integral'] ?? 0,
'use_mer_integral_price' => (float)$merchantCart['order']['use_mer_integral_price'] ?? 0,
'use_platform_integral' => (float)$merchantCart['order']['use_platform_integral'] ?? 0,
'use_platform_integral_price' => (float)$merchantCart['order']['use_platform_integral_price'] ?? 0,
'mer_id' => $merchantCart['mer_id'],
'cost' => $cost,
'order_extend' => count($extend) ? json_encode($extend,JSON_UNESCAPED_UNICODE) : '',
'coupon_id' => implode(',',$merchantCart['order']['useCouponIds']),
'mark' => $mark[$merchantCart['mer_id']] ?? '',
'coupon_price' => bcadd($merchantCart['order']['coupon_price'],$merchantCart['order']['platform_coupon_price'],2),
'platform_coupon_price' => $merchantCart['order']['platform_coupon_price'],
'pay_type' => $pay_type,
'refund_switch' => $merchantCart['order']['order_refund_switch'],
'pay_type' => $pay_type,
'refund_switch' => $merchantCart['order']['order_refund_switch'],
];
$allUseCoupon = array_merge($allUseCoupon, $merchantCart['order']['useCouponIds']);
$orderList[] = $_order;
@ -1273,47 +1276,87 @@ class StoreOrderCreateRepository extends StoreOrderRepository
//创建订单
$groupOrder = $storeGroupOrderRepository->create($groupOrder);
$bills = [];
if ($groupOrder['integral'] > 0) {
$user->integral = bcsub($user->integral, $groupOrder['integral'], 0);
app()->make(UserBillRepository::class)->decBill($user['uid'], 'integral', 'deduction', [
'link_id' => $groupOrder['group_order_id'],
'status' => 1,
'title' => '购买商品',
'number' => $groupOrder['integral'],
'mark' => '购买商品使用积分抵扣' . floatval($groupOrder['integral_price']) . '元',
'balance' => $user->integral
]);
$user->save();
}
foreach ($orderList as $k => $order) {
$orderList[$k]['group_order_id'] = $groupOrder->group_order_id;
}
$orderProduct = [];
$orderStatus = [];
$bills = [];
$integralRepositoryMake = app()->make(IntegralRepository::class);
foreach ($orderList as $order) {
$cartInfo = $order['cartInfo'];
unset($order['cartInfo']);
//创建子订单
$_order = $this->dao->create($order);
// 积分抵扣处理 由于区分平台积分和商户积分 这里只能每个订单单独处理
if ($order['integral'] > 0) {
$bills[] = [
'uid' => $uid,
'link_id' => $_order->order_id,
'pm' => 0,
'title' => '积分抵扣',
'category' => 'mer_integral',
'type' => 'deduction',
'number' => $order['integral'],
'balance' => $user->integral,
'mark' => '购买商品使用' . $order['integral'] . '积分抵扣' . floatval($order['integral_price']) . '元',
'mer_id' => $order['mer_id'],
'status' => 1
];
// 修改用户持有的平台积分
if((float)$order['use_platform_integral'] > 0) {
$user->integral = bcsub($user->integral, $order['use_platform_integral'], 2);
$integralRepositoryMake->changeIntegral((int)$uid,(int)0,(float)sprintf("%.2f",0 - $order['use_platform_integral']));
// 记录总平台端积分变更日志
$bills[] = [
'uid' => $uid,
'link_id' => $_order->order_id,
'pm' => 0,
'title' => '购买商品',
'category' => 'integral',
'type' => 'deduction',
'number' => $order['use_platform_integral'],
'balance' => $user->integral,
'mark' => '购买商品使用'.$order['use_platform_integral'].'平台积分抵扣'.floatval($order['use_platform_integral_price']).'元',
'mer_id' => 0,
'status' => 1
];
// 记录商户端总积分变更日志
$bills[] = [
'uid' => $uid,
'link_id' => $_order->order_id,
'pm' => 0,
'title' => '积分抵扣',
'category' => 'mer_integral',
'type' => 'deduction',
'number' => $order['use_platform_integral'],
'balance' => $user->integral,
'mark' => '购买商品使用'.$order['use_platform_integral'].'平台积分抵扣'.floatval($order['use_platform_integral_price']).'元',
'mer_id' => $order['mer_id'],
'status' => 1
];
}
// 修改用户持有的本商户积分
if((float)$order['use_mer_integral'] > 0) {
$user->integral = bcsub($user->integral, $order['use_mer_integral'], 2);
$integralRepositoryMake->changeIntegral((int)$uid,(int)$order['mer_id'],(float)sprintf("%.2f",0 - $order['use_mer_integral']));
// 记录总平台端积分变更日志
$bills[] = [
'uid' => $uid,
'link_id' => $_order->order_id,
'pm' => 0,
'title' => '购买商品',
'category' => 'integral',
'type' => 'deduction',
'number' => $order['use_mer_integral'],
'balance' => $user->integral,
'mark' => '购买商品使用'.$order['use_mer_integral'].'平台积分抵扣'.floatval($order['use_mer_integral_price']).'元',
'mer_id' => 0,
'status' => 1
];
// 记录商户端总积分变更日志
$bills[] = [
'uid' => $uid,
'link_id' => $_order->order_id,
'pm' => 0,
'title' => '积分抵扣',
'category' => 'mer_integral',
'type' => 'deduction',
'number' => $order['use_mer_integral'],
'balance' => $user->integral,
'mark' => '购买商品使用'.$order['use_mer_integral'].'平台积分抵扣'.floatval($order['use_mer_integral_price']).'元',
'mer_id' => $order['mer_id'],
'status' => 1
];
}
}
//创建发票信息
@ -1416,10 +1459,12 @@ class StoreOrderCreateRepository extends StoreOrderRepository
$userMerchantRepository->getInfo($uid, $order['mer_id']);
app()->make(MerchantRepository::class)->incSales($order['mer_id'], $order['total_num']);
}
// 保存用户积分变更
$user->save();
// 添加记录
if (count($bills) > 0) app()->make(UserBillRepository::class)->insertAll($bills);
if (count($bills) > 0) {
app()->make(UserBillRepository::class)->insertAll($bills);
}
$storeOrderStatusRepository->batchCreateLog($orderStatus);
$storeOrderProductRepository->insertAll($orderProduct);
event('order.create', compact('groupOrder'));

View File

@ -651,13 +651,29 @@ class StoreOrderRepository extends BaseRepository
foreach($groupOrder->orderList as $orderInfo){
$giveIntegral = (float)sprintf("%.2f",$orderInfo->give_integral);
if ($giveIntegral > 0) {
// 总积分到账
// 总积分到账 & 记录总平台积分变更日志
app()->make(UserRepository::class)->incIntegral((int)$orderInfo->uid,$giveIntegral,'订单赠送积分','lock',[
'link_id' => $orderInfo->order_id,
'number' => $giveIntegral,
'mark' => '成功消费' . (float)sprintf("%.2f",$orderInfo->pay_price) . '元,赠送积分' . $giveIntegral
'mark' => '成功消费' . (float)sprintf("%.2f",$orderInfo->pay_price) . '元,赠送商户积分' . $giveIntegral
]);
// 商户积分变更
app()->make(IntegralRepository::class)->changeIntegral((int)$orderInfo->uid,(int)$orderInfo->mer_id,$giveIntegral);
// 记录商户端积分变更日志
$user = app()->make(UserRepository::class)->get($orderInfo->uid);
app()->make(UserBillRepository::class)->insertAll([[
'uid' => $orderInfo->uid,
'link_id' => $orderInfo->order_id,
'pm' => 0,
'title' => '订单赠送积分',
'category' => 'mer_integral',
'type' => 'lock',
'number' => $giveIntegral,
'balance' => $user->integral,
'mark' => '成功消费' . (float)sprintf("%.2f",$orderInfo->pay_price) . '元,赠送商户积分' . $giveIntegral,
'mer_id' => $orderInfo->mer_id,
'status' => 1
]]);
}
}

View File

@ -55,6 +55,7 @@ class UserBillRepository extends BaseRepository
'integral/timeout' => '积分过期',
'mer_integral/deduction' => '积分抵扣',
'mer_integral/refund' => '订单退款',
'mer_integral/lock' => '下单赠送积分',
'mer_lock_money/order' => '商户佣金冻结',
'now_money/brokerage' => '佣金转入余额',
'now_money/pay_product' => '购买商品',

View File

@ -1288,7 +1288,7 @@ class UserRepository extends BaseRepository
app()->make(UserBillRepository::class)
->incBill($uid, 'integral', $type,
[
'link_id' => 0,
'link_id' => $data['link_id'] ?? 0,
'status' => 1,
'title' => $title,
'number' => $data['number'] ?? $number,