diff --git a/app/Admin/Controllers/OrderProfitController.php b/app/Admin/Controllers/OrderProfitController.php index e7678877..003e6fa1 100644 --- a/app/Admin/Controllers/OrderProfitController.php +++ b/app/Admin/Controllers/OrderProfitController.php @@ -9,6 +9,7 @@ use Dcat\Admin\Show; use Dcat\Admin\Http\Controllers\AdminController; use App\Admin\Extensions\Grid\Tools\ProfitBatchSuccess; use App\Enums\PayWay; +use App\Models\Agent; use Dcat\Admin\Admin; class OrderProfitController extends AdminController @@ -44,6 +45,7 @@ class OrderProfitController extends AdminController $filter->like('fromUser.phone')->width(3); $filter->like('user.phone')->width(3); $filter->equal('status')->select(OrderProfit::$statusMap)->width(3); + $filter->where('role_name', fn($q) => $q->role($this->input))->select(Agent::$typeMap)->width(3); }); $grid->showRowSelector(); diff --git a/app/Models/OrderProfit.php b/app/Models/OrderProfit.php index c30ec87b..450a069f 100644 --- a/app/Models/OrderProfit.php +++ b/app/Models/OrderProfit.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Dcat\Admin\Traits\HasDateTimeFormatter; +use Illuminate\Support\Str; class OrderProfit extends Model { @@ -44,4 +45,14 @@ class OrderProfit extends Model { return $this->belongsTo(Order::class, 'order_id'); } + + public function scopeRole($q, $role) + { + return $q->where('role', 'like', $role.'-%'); + } + + public function isRole($role) + { + return Str::startsWith($this->role, $role); + } } diff --git a/app/Services/DistributeService.php b/app/Services/DistributeService.php index 66549aea..2ba7de54 100644 --- a/app/Services/DistributeService.php +++ b/app/Services/DistributeService.php @@ -77,40 +77,44 @@ class DistributeService { $sales_value = $order->sales_value; $user = $order->user; - $user_agent = $user->agent; // 上级返利 - $parent_ids = array_reverse($user->userInfo->parent_ids); - $parents = User::with(['userInfo', 'agent'])->whereIn('id', $parent_ids)->get(); + $parentIds = array_reverse($user->userInfo->parent_ids); + // 按照推荐的顺序,第一位是直属推荐人 + $parents = User::with(['userInfo', 'agent'])->whereIn('id', $parentIds)->get()->sortBy(fn($item) => array_search($item->id, $parentIds)); // 过滤掉 不是代理身份 的用户 // 过滤掉 相同等级 的 后者 用户 - // 过滤掉等级 低于 下单用户等级 的用户(sort = 1 为最高) - $exists_list = []; - $filtered = $parents->filter(function ($item) use ($user_agent, &$exists_list) { + // 过滤掉等级 低于 上一个分钱 的用户(sort = 1 为最高级, slug 要相同) + $existsList = []; + $lastAgent = null; + $filtered = collect(); + foreach($parents as $item) { if (!$item->agent) { - return false; + continue; } - if (in_array($item->agent->sort, $exists_list)) { - return false; + + if (in_array($item->agent->sort, $existsList)) { + continue; } - array_push($exists_list, $item->agent->sort); - if ($user_agent) { - return $user_agent->sort > $item->agent->sort; + + if ($lastAgent && $item->agent->sort > $lastAgent->sort && $item->agent->slug === $lastAgent->slug) { + continue; } - return true; - }); + $lastAgent = $item->agent; + + array_push($existsList, $item->agent->sort); + + $filtered->push($item); + } // 生成返利记录 $profit_list = []; $money_sum = 0; - foreach($filtered->reverse() as $item) { + foreach($filtered as $item) { $agent = $item->agent??''; if (!$agent) { continue; } $money = floor($sales_value * $agent->ratio) / 100 - $money_sum; - if ($money <= 0) { - continue; - } array_unshift($profit_list, [ 'from_user_id' => $user->id, 'user_id' => $item->id, @@ -176,8 +180,12 @@ class DistributeService // 商户订单号 $sn = serial_number(); foreach($list as $item) { - // 待付款, 同一收款用户, 金额 > 0 - if ($item->status === 4 && $item->user_id === $userId && $item->money > 0) { + // 待付款, 同一收款用户, 金额 > 0, 代理等级: Agent::TYPE_FAVOITE + if ($item->status === 4 && + $item->user_id === $userId && + $item->money > 0 && + $item->isRole(Agent::TYPE_FAVOITE) + ) { $item->update([ 'status' => 1, 'pay_no' => $sn diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php index 4ae02bc5..a2893b2a 100644 --- a/tests/Feature/ExampleTest.php +++ b/tests/Feature/ExampleTest.php @@ -2,8 +2,10 @@ namespace Tests\Feature; +use App\Models\Agent; use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; +use Illuminate\Support\Str; class ExampleTest extends TestCase { @@ -14,8 +16,26 @@ class ExampleTest extends TestCase */ public function test_example() { - $response = $this->get('/'); + // 过滤掉 不是代理身份 的用户 + // 过滤掉 相同等级 的 后者 用户 + // 过滤掉等级 低于 上一个分钱 的用户 + $parents = [2, 3, 1, 5, 6, null]; + $filtered = []; + $last = null; + foreach($parents as $item) { + if (!$item) { + continue; + } + if (in_array($item, $filtered)) { + continue; + } + if ($last && $item < $last) { + continue; + } + $last = $item; + array_push($filtered, $item); + } - $response->assertStatus(200); + dd($filtered); } }