lip hace 1 semana
padre
commit
1f4642c324

+ 5 - 0
app/Http/Controllers/admin/ActivityReward.php

@@ -29,6 +29,11 @@ class ActivityReward extends Controller
             $count = $query->count();
             $list = $query->orderByDesc('id')
                 ->forPage($page, $limit)->get()->toArray();
+            foreach ($list as &$item) {
+                if (!empty($item['params'])) {
+                    $item['params'] = json_decode($item['params'], true);
+                }
+            }
             $result = ['total' => $count, 'data' => $list];
         } catch (ValidationException $e) {
             return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());

+ 23 - 0
app/Models/WalletBonus.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Models;
+class WalletBonus extends BaseModel
+{
+    protected $table = 'wallet_bonus';
+    protected $fillable = ['member_id','activity_id','related_id','amount','need_flow','remaind_flow','status'];
+
+    public static function addData($memberId, $type, $bonusAmount, $activityId, $related_id, $need_flow)
+    {
+        $data = [
+            'member_id' => $memberId,
+            'type' => $type,    //类型1充值活动返彩;2余额宝收益;3老用户回归日充值返彩 
+            'activity_id' => $activityId,
+            'related_id' => $related_id,
+            'amount' => $bonusAmount,
+            'need_flow' => $need_flow,
+            'remaind_flow' => $need_flow,
+            'status' => 1,
+        ];
+        self::create($data);
+    }
+}

+ 68 - 0
app/Services/BalanceLogService.php

@@ -2,7 +2,11 @@
 
 namespace App\Services;
 
+use App\Models\ActivityReward;
 use App\Models\BalanceLog;
+use App\Models\User;
+use App\Models\Config;
+use App\Models\WalletBonus;
 
 // 余额额变动记录
 class BalanceLogService extends BaseService
@@ -135,6 +139,24 @@ class BalanceLogService extends BaseService
             BackflowService::updateOrCreate((string)$memberId, floatval($amount));
         }
 
+        //充值返现活动
+        if ($change_type == '充值' || $change_type == '三方充值') {
+            //返现比例(给邀请人返现)
+            $rate = Config::where('field', 'recharge_rate')->first()->val ?? 0;
+            $add_amount = bcmul($amount, $rate, 10);
+            $add_amount = bcmul($add_amount, 100, 2);
+            if ($add_amount > 0) {
+                //被邀请人每次充值,都给邀请人返现,直接可用余额
+                $agent_user_code = User::where('member_id', $memberId)->first()->agent_user_code;
+                if (!empty($agent_user_code)) {
+                    $agent_member_id = User::where('agent_user_code', $agent_user_code)->first()->member_id;
+                    if (!empty($agent_member_id)) {
+                        $balanceData = WalletService::updateBalance($agent_member_id, $add_amount);
+                        BalanceLogService::addLog($agent_member_id, $add_amount, $balanceData['before_balance'], $balanceData['after_balance'], '充值返现', $related_id, '被邀请人:'.$memberId."充值金额为:{$amount}");
+                    }
+                }
+            }
+        }
 
         $data = [];
         $data['member_id'] = $memberId;
@@ -253,4 +275,50 @@ class BalanceLogService extends BaseService
         ];
     }
 
+    /**
+     * 计算充值的返现金额
+     * @param float $amount 充值金额(人民币)
+     */
+    public static function calculateRechargeBonus($amount,$memberId, $related_id = null)
+    {
+        $activity = ActivityReward::where('lang','zh')->where('type',3)->where('status',1)->where('start_time', '<', time())->where('end_time', '>', time())->first();
+        if (!$activity) {
+            return false;
+        }
+        $params = $activity->params ? json_decode($activity->params, true) : [];
+        if (!$params || !empty($params['rate'])) {
+            return false;
+        }
+
+        // 规则1:单笔金额必须≥3000元
+        if (isset($params['min_amount']) && $amount < $params['min_amount']) {
+            return false;
+        }
+
+        // 规则2:必须是每日前5笔充值
+        if (!empty($params['limit'])) {
+            //今日已充值数
+            $dailyOrder = static::model()::where('member_id', $memberId)
+                ->whereBetween('created_at', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')])
+                ->whereIn('change_type', ['充值','三方充值'])
+                ->count();
+            if ($dailyOrder > $params['limit']) {
+                return false;
+            }
+        }
+
+        // 规则3:按0.5%计算返现,不超过单笔最高10000元
+        $rate = bcdiv($params['rate'], 100, 6); // 0.5%
+        $maxBonus = $params['max_bonus'] ?? 10000;
+        $calculatedBonus = bcmul($amount, $rate, 2); // 保留两位小数
+        $bonusAmount = min($calculatedBonus, $maxBonus);
+
+        $need_flow = !empty($params['need_flow']) ? bcmul($bonusAmount, $params['need_flow'], 2) : $bonusAmount;
+        
+        //返彩流水
+        WalletBonus::addData($memberId, 1, $bonusAmount, $activity->id, $related_id, $need_flow);
+
+        return $bonusAmount;
+    }
+
 }