gpt4 book ai didi

php - 如果用户排名不存在,laravel 迭代会为下一个用户增加值(value)

转载 作者:行者123 更新时间:2023-12-05 08:30:23 25 4
gpt4 key购买 nike

我有一个功能,可以根据 parent_id 向用户插入一个数量,直到最后一个等级。 .我可以得到 parent_id直到最后一级,都很好,我正在使用 iteration或类似 recursion 的东西.但如果用户等级不存在,我无法添加金额。

该函数的预期结果是为每个 User Rank 插入一个金额, 但是 如果迭代/递归没有找到具有 rank 的用户要插入他的金额将在下一次添加user rank .

一旦用户购买 product 就会触发该函数,所以无论谁购买产品,都会得到他的 parent_id并插入 amount分配给该级别。

我有一个等级列表 in order谁将收到这笔款项。

Basic - 50
Junior - 100
Premium - 150
Advanced - 200
Senior - 250

数据库:

+------+------------+-------------+------------+
| id | username | parent_id | rank |
+------+------------+-------------+------------+
| 1 | john | NULL | Senior |
| 2 | jane | 1 | Advanced |
| 3 | josh | 2 | Premium |
| 4 | joey | 3 | Junior |
| 5 | jade | 4 | Basic |
------------------------------------------------

例如:

Jade买了一件商品,一旦订单成功,我将调用函数插入 earnings .

在这种情况下 Joey, Josh, Jane and John将根据他们的相应金额获得他们的 yield rank .

这在这种情况下工作正常,因为所有等级都存在,但我的问题是添加 amount设置为 rank如果用户不在 iteration 中,

例如,Joeyrank - Junior不在迭代中,指定给他的数量是100下期补上rank这是 Premium , 并在下一个级别继续收入插入,但防止添加 amount对他们来说,因为金额should only be added如果迭代中不存在具有该等级的用户,则进入下一个等级。

// successfully bought a product and saved to database

$user_id = 5; // jade
$parent_id = 4;

// call the function to insert the earnings

self::insertEarnings($user_id,$parent_id);

这是将插入 yield 的函数:

private function insertEarnings($user_id,$parent_id) {

if ($parent_id > 0) {

$user_parent = $parent_id;

$has_parent = true;

// start iteration
while($has_parent == true){

$account = User::where('id',$user_parent)->first();

$amount = 0;

if ($account->rank == "Junior" ) {

$amount = 100;

} elseif ($account->rank == "Premium") {

$amount = 150;

} elseif ($account->rank == "Advanced") {

$amount = 200;

} elseif ($account->rank == "Senior") {

$amount = 250;

// set to false to stop the iteration
$has_parent = false;
}

$earnings = new Earnings;
$earnings->user_id = $account->id;
$earnings->amount = $amount;
$earnings->save();

$next_parent = User::where('id',$user_parent)->first();
$user_parent = $next_parent->parent_id;

if($user_parent == 0){
$has_parent = false;
}
}
}
}

收入被插入到 Joey、Josh、Jane 和 John,因为他们都出现在迭代中,真正的问题是,如果他不出现在迭代中,如何插入分配给排名的金额。

编辑:

有人帮帮我吗?我真的无法为这段代码制定解决方案。如果在迭代/循环中找不到排名,则将金额添加到下一个用户排名。

Jade 订购一个产品,假设它是成功的。现在我将触发 insertEarnings功能。我会得到他的 parent_id这是 Joey ,

Joey将获得数量 100 , 还有 Josh, Jane and John他们将根据他们的等级获得一定的金额。

主要问题是如果有人是NOT怎么办?在迭代/循环中,rank 的金额无论该等级是什么,都将被添加到下一个等级。

最佳答案

看了评论,我想你现在的问题是,如果某个用户等级不存在,如何计算金额?

我有一个逻辑来使用这段代码迭代排名。

public function insertEarnings()
{
// Rank Map
$ranks = [[
'name' => 'Basic',
'point' => 50,
], [
'name' => 'Junior',
'point' => 100,
], [
'name' => 'Premium',
'point' => 150,
], [
'name' => 'Advanced',
'point' => 200,
], [
'name' => 'Senior',
'point' => 250,
]];

// Find Current Rank & Init Others
$current = array_search($this->rank, array_column($ranks, 'name'));
$rank = null;
$user = null;
$amount = 0;

// Loop while next rank still exist
while (array_key_exists($current + 1, $ranks)) {
// Find next rank, user within that rank, and sum up the amount.
$rank = $ranks[$current + 1];
$user = User::where('rank', $rank['name'])->first();
$amount += $rank['point'];

// Add current to iterate trough ranks
$current++;

// If user is found save the earning and restart the amount to 0
if(!is_null($user)){
$earnings = new Earning;
$earnings->user_id = $user->id;
$earnings->amount = $amount;
$earnings->save();

$amount = 0;
}
}
}

使用该代码,我可以通过调用获得如下表所示的收入

$user->insertEarnings() // jane

+------+--------+----------+
| id | name | amount |
+------+--------+----------+
| 1 | josh | 250 |
| 2 | jane | 200 |
| 3 | john | 250 |
----------------------------

这就是你想要的吗?

关于php - 如果用户排名不存在,laravel 迭代会为下一个用户增加值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64672928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com