Multiple if judges how to simplify code and reduce code redundancy

such as the following code. Filled with all kinds of if statements. I have no idea where to start the optimization.. I don"t know if there is any way to solve this kind of multiple if judgment. Reduce code redundancy and improve efficiency. The function of the following code is to find an online agent at level 7 [query whether there is an online agent for this user-you need to query level 7 up, and if so, give a certain amount of cash back. The first three levels need to be stored in a field through serialization, and the last five levels only need to be returned. ] .

public function bonusUser($user, $orderinfo, $order_goods) {
        $first_price = $this->getPrice($user["level"]);
        if ($user["agentid"] != 0) {
            //
            //
            $user_one = \think\Db::table("ims_ewei_shop_member")->where("id", $user["agentid"])->find();
            $money_one = $orderinfo["price"] / $order_goods["total"];
            //
            $new_price = $this->getPrice($user_one["level"]);
            $return_price = 0;
            //
            $all_moeny = unserialize($order_goods["commissions"]);
            if ($user_one["level"] == 11) {
                $result_num = $money_one - 168;
                if ($result_num > 0) {
                    $return_price = $result_num * $order_goods["total"];
                }
            } else if ($user_one["level"] == 12) {
                $result_num = $money_one - 138;
                if ($result_num > 0) {
                    $return_price = $result_num * $order_goods["total"];
                }
            } else if ($user_one["level"] == 13) {
                $result_num = $money_one - 118;
                if ($result_num > 0) {
                    $return_price = $result_num * $order_goods["total"];
                }
            } else {
                $result_num = $money_one - 268;
                if ($result_num > 0) {
                    $return_price = $result_num * $order_goods["total"];
                }
            }
            if ($return_price > 0) {
                $send_one = $this->send_Mess($user_one["openid"], $user["nickname"], 1);
                $all_moeny["level1"] = $return_price;
            } else {
                $all_moeny["level1"] = 0;
            }
            if ($user_one["agentid"] != 0) {
                //
                $user_two = \think\Db::table("ims_ewei_shop_member")->where("id", $user_one["agentid"])->find();
                $new_price_two = $this->getPrice($user_two["level"]);
                //
                if ($user_two["level"] > $user_one["level"] && $user_one["level"] >= $user["level"]) {
                    $result_two = $new_price - $new_price_two;
                    \think\Log::write($new_price, "1j");
                    if ($result_two > 0) {
                        $all_moeny["level2"] = $result_two * $order_goods["total"];
                        $send_two = $this->send_Mess($user_two["openid"], $user["nickname"], 2);
                    } else {
                        $all_moeny["level2"] = 0;
                    }
                } elseif ($user_one["level"] <= $user["level"] && $user_two["level"] > $user["level"]) {
                    //
                    $result_two = $first_price - $new_price_two;
                    $all_moeny["level2"] = $result_two * $order_goods["total"];
                    if ($result_two > 0) {
                        $send_three = $this->send_Mess($user_two["openid"], $user["nickname"], 2);
                    }
                } else {
                    $all_moeny["level2"] = 0;
                }
                if ($user_two["agentid"] != 0) {
                    //
                    $user_three = \think\Db::table("ims_ewei_shop_member")->where("id", $user_two["agentid"])->find();
                    $new_price_three = $this->getPrice($user_three["level"]);
                    //
                    if ($user_three["level"] > $user_two["level"] && $user_two["level"] >= $user_one["level"] && $user_one["level"] >= $user["level"]) {
                        $result_three = $new_price_two - $new_price_three;
                        if ($result_three > 0) {
                            $all_moeny["level3"] = $result_three * $order_goods["total"];
                            $send_three = $this->send_Mess($user_three["openid"], $user["nickname"], 3);
                        } else {
                            $all_moeny["level3"] = 0;
                        }
                    } elseif ($user_two["level"] <= $user["level"] && $user_one["level"] <= $user["level"] && $user_three["level"] > $user["level"]) {
                        //
                        $result_three = $first_price - $new_price_three;
                        if ($result_three > 0) {
                            $all_moeny["level3"] = $result_three * $order_goods["total"];
                            $send_three = $this->send_Mess($user_three["openid"], $user["nickname"], 3);
                        } else {
                            $all_moeny["level3"] = 0;
                        }
                    } elseif ($user_one["level"] >= $user["level"] && $user_two["level"] <= $user_one["level"] && $user_three["level"] > $user_two["level"]) {
                        $result_three = $new_price - $new_price_three;
                        if ($result_three > 0) {
                            $all_moeny["level3"] = $result_three * $order_goods["total"];
                            $send_three = $this->send_Mess($user_three["openid"], $user["nickname"], 3);
                        } else {
                            $all_moeny["level3"] = 0;
                        }
                    } else {
                        $all_moeny["level3"] = 0;
                    }
                    if ($user_three["agentid"] != 0) {
                        //
                        $user_four = \think\Db::table("ims_ewei_shop_member")->where("id", $user_three["agentid"])->find();
                        $new_price_four = $this->getPrice($user_four["level"]);
                        //;
                        if ($user_four["level"] > $user_three["level"] && $user_three["level"] >= $user_two["level"] && $user_two["level"] >= $user_one["level"] && $user_one["level"] >= $user["level"]) {
                            $result_four = $new_price_three - $new_price_four;
                            if ($result_four > 0) {
                                $four_money = $result_four * $order_goods["total"];
                                $result = $this->shop_money_log($four_money, $user_four, 4);
                                $send_four = $this->send_Mess($user_four["openid"], $user["nickname"], 4);
                            }
                        } else if ($user_three["level"] <= $user["level"] && $user_two["level"] <= $user["level"] && $user_one["level"] <= $user["level"] && $user_four["level"] > $user["level"]) {
                            //
                            $result_four = $first_price - $new_price_four;
                            $four_money = $result_four * $order_goods["total"];
                            if ($result_four > 0) {
                                $result = $this->shop_money_log($four_money, $user_four, 4);
                                $send_four = $this->send_Mess($user_four["openid"], $user["nickname"], 4);
                            }
                        } elseif ($user_two["level"] >= $user["level"] && $user_two["level"] >= $user_one["level"] && $user_three["level"] <= $user_two["level"] && $user_four["level"] > $user_two["level"]) {
                            $result_four = $new_price_two - $new_price_four;
                            $four_money = $result_four * $order_goods["total"];
                            if ($result_four > 0) {
                                $result = $this->shop_money_log($four_money, $user_four, 4);
                                $send_four = $this->send_Mess($user_four["openid"], $user["nickname"], 4);
                            }
                        } elseif ($user_one["level"] >= $user["level"] && $user_three["level"] <= $user_one["level"] && $user_one["level"] >= $user_two["level"] && $user_four["level"] > $user_one["level"]) {
                            $result_four = $new_price - $new_price_four;
                            $four_money = $result_four * $order_goods["total"];
                            if ($result_four > 0) {
                                $result = $this->shop_money_log($four_money, $user_four, 4);
                                $send_four = $this->send_Mess($user_four["openid"], $user["nickname"], 4);
                            }
                        }
                        if ($user_four["agentid"] != 0) {
                            //
                            $user_five = \think\Db::table("ims_ewei_shop_member")->where("id", $user_four["agentid"])->find();
                            $new_price_five = $this->getPrice($user_five["level"]);
                            if ($user_five["level"] > $user_four["level"] && $user_four["level"] >= $user_three["level"] && $user_three["level"] >= $user_two["level"] && $user_two["level"] >= $user_one["level"] && $user_one["level"] >= $user["level"]) {
                                $result_five = $new_price_four - $new_price_five;
                                $five_money = $result_five * $order_goods["total"];
                                if ($result_five > 0) {
                                    $result = $this->shop_money_log($five_money, $user_five, 5);
                                    $send_five = $this->send_Mess($user_five["openid"], $user["nickname"], 5);
                                }
                            } elseif ($user_four["level"] <= $user["level"] && $user_three["level"] <= $user["level"] && $user_two["level"] <= $user["level"] && $user_one["level"] <= $user["level"] && $user_five["level"] >= $user["level"]) {
                                //
                                $result_five = $first_price - $new_price_five;
                                $five_money = $result_five * $order_goods["total"];
                                if ($result_five > 0) {
                                    $result = $this->shop_money_log($five_money, $user_five, 5);
                                    $send_five = $this->send_Mess($user_five["openid"], $user["nickname"], 5);
                                }
                            } elseif ($user_two["level"] >= $user["level"] && $user_two["level"] >= $user_one["level"] && $user_three["level"] <= $user_two["level"] && $user_four["level"] <= $user_two["level"] && $user_five["level"] > $user_two["level"]) {
                                //
                                $result_five = $new_price_two - $new_price_five;
                                $five_money = $result_five * $order_goods["total"];
                                if ($result_five > 0) {
                                    $result = $this->shop_money_log($five_money, $user_five, 5);
                                    $send_five = $this->send_Mess($user_five["openid"], $user["nickname"], 5);
                                }
                            } elseif ($user_one["level"] >= $user["level"] && $user_three["level"] <= $user_one["level"] && $user_one["level"] >= $user_two["level"] && $user_four["level"] <= $user_one["level"] && $user_five["level"] > $user_two["level"]) {
                                //
                                $result_five = $new_price - $new_price_five;
                                $five_money = $result_five * $order_goods["total"];
                                if ($result_five > 0) {
                                    $result = $this->shop_money_log($five_money, $user_five, 5);
                                    $send_five = $this->send_Mess($user_five["openid"], $user["nickname"], 5);
                                }
                            } elseif ($user_three["level"] >= $user["level"] && $user_three["level"] >= $user_one["level"] && $user_three["level"] >= $user_two["level"] && $user_four["level"] <= $user_three["level"] && $user_five["level"] > $user_three["level"]) {
                                //
                                $result_five = $new_price_three - $new_price_five;
                                $five_money = $result_five * $order_goods["total"];
                                if ($result_five > 0) {
                                    $result = $this->shop_money_log($five_money, $user_five, 5);
                                    $send_five = $this->send_Mess($user_five["openid"], $user["nickname"], 5);
                                }
                            }
                            //6-7
                            if ($user_five["agentid"] != 0) {
                                //
                                $user_six = \think\Db::table("ims_ewei_shop_member")->where("id", $user_five["agentid"])->find();
                                $new_price_six = $this->getPrice($user_six["level"]);
                                if ($user_six["level"] > $user_five["level"] && $user_five["level"] >= $user_four["level"] && $user_four["level"] >= $user_three["level"] && $user_three["level"] >= $user_two["level"] && $user_two["level"] >= $user_one["level"] && $user_one["level"] >= $user["level"]) {
                                    $result_six = $new_price_five - $new_price_six;
                                    $six_money = $result_six * $order_goods["total"];
                                    if ($result_six > 0) {
                                        $result = $this->shop_money_log($six_money, $user_six, 6);
                                        $send_five = $this->send_Mess($user_six["openid"], $user["nickname"], 6);
                                    }
                                } elseif ($user_five["level"] <= $user["level"] && $user_four["level"] <= $user["level"] && $user_three["level"] <= $user["level"] && $user_two["level"] <= $user["level"] && $user_one["level"] <= $user["level"] && $user_six["level"] >= $user["level"]) {
                                    //
                                    $result_six = $first_price - $new_price_six;
                                    $six_money = $result_six * $order_goods["total"];
                                    if ($result_six > 0) {
                                        $result = $this->shop_money_log($six_money, $user_six, 6);
                                        $send_five = $this->send_Mess($user_six["openid"], $user["nickname"], 6);
                                    }
                                } elseif ($user_two["level"] >= $user["level"] && $user_two["level"] >= $user_one["level"] && $user_three["level"] <= $user_two["level"] && $user_four["level"] <= $user_two["level"] && $user_five["level"] <= $user_two["level"] && $user_six["level"] > $user_two["level"]) {
                                    //
                                    $result_six = $new_price_two - $new_price_six;
                                    $six_money = $result_six * $order_goods["total"];
                                    if ($result_six > 0) {
                                        $result = $this->shop_money_log($six_money, $user_six, 6);
                                        $send_five = $this->send_Mess($user_six["openid"], $user["nickname"], 6);
                                    }
                                } elseif ($user_one["level"] >= $user["level"] && $user_three["level"] <= $user_one["level"] && $user_one["level"] >= $user_two["level"] && $user_four["level"] <= $user_one["level"] && $user_five["level"] <= $user_one["level"] && $user_six["level"] > $user_two["level"]) {
                                    //
                                    $result_six = $new_price - $new_price_six;
                                    $six_money = $result_six * $order_goods["total"];
                                    if ($result_six > 0) {
                                        $result = $this->shop_money_log($six_money, $user_six, 6);
                                        $send_five = $this->send_Mess($user_six["openid"], $user["nickname"], 6);
                                    }
                                } elseif ($user_three["level"] >= $user["level"] && $user_three["level"] >= $user_one["level"] && $user_three["level"] >= $user_two["level"] && $user_four["level"] <= $user_three["level"] && $user_five["level"] <= $user_three["level"] && $user_six["level"] > $user_three["level"]) {
                                    //
                                    $result_six = $new_price_three - $new_price_six;
                                    $six_money = $result_six * $order_goods["total"];
                                    if ($result_six > 0) {
                                        $result = $this->shop_money_log($six_money, $user_six, 6);
                                        $send_five = $this->send_Mess($user_six["openid"], $user["nickname"], 6);
                                    }
                                } elseif ($user_four["level"] >= $user["level"] && $user_four["level"] >= $user_one["level"] && $user_four["level"] >= $user_two["level"] && $user_four["level"] >= $user_three["level"] && $user_five["level"] <= $user_four["level"] && $user_six["level"] > $user_four["level"]) {
                                    //
                                    $result_six = $new_price_four - $new_price_six;
                                    $six_money = $result_six * $order_goods["total"];
                                    if ($result_six > 0) {
                                        $result = $this->shop_money_log($six_money, $user_six, 6);
                                        $send_five = $this->send_Mess($user_six["openid"], $user["nickname"], 6);
                                    }
                                }
                                if ($user_six["agentid"] != 0) {
                                    //
                                    $user_seven = \think\Db::table("ims_ewei_shop_member")->where("id", $user_six["agentid"])->find();
                                    $new_price_seven = $this->getPrice($user_seven["level"]);
                                    if ($user_seven["level"] >= $user_six["level"] && $user_six["level"] >= $user_five["level"] && $user_five["level"] >= $user_four["level"] && $user_four["level"] >= $user_three["level"] && $user_three["level"] >= $user_two["level"] && $user_two["level"] >= $user_one["level"] && $user_one["level"] >= $user["level"]) {
                                        $result_seven = $new_price_six - $new_price_seven;
                                        $seven_money = $result_seven * $order_goods["total"];
                                        if ($result_seven > 0) {
                                            $result = $this->shop_money_log($seven_money, $user_seven, 7);
                                            $send_seven = $this->send_Mess($user_seven["openid"], $user["nickname"], 7);
                                        }
                                    } elseif ($user_six["level"] <= $user["level"] && $user_five["level"] <= $user["level"] && $user_four["level"] <= $user["level"] && $user_three["level"] <= $user["level"] && $user_two["level"] <= $user["level"] && $user_one["level"] <= $user["level"] && $user_seven["level"] >= $user["level"]) {
                                        //
                                        $result_seven = $first_price - $new_price_seven;
                                        $seven_money = $result_seven * $order_goods["total"];
                                        if ($result_seven > 0) {
                                            $result = $this->shop_money_log($seven_money, $user_seven, 7);
                                            $send_seven = $this->send_Mess($user_seven["openid"], $user["nickname"], 7);
                                        }
                                    } elseif ($user_two["level"] >= $user["level"] && $user_two["level"] >= $user_one["level"] && $user_three["level"] <= $user_two["level"] && $user_four["level"] <= $user_two["level"] && $user_five["level"] <= $user_two["level"] && $user_six["level"] <= $user_two["level"] && $user_seven["level"] > $user_two["level"]) {
                                        //
                                        $result_seven = $new_price_two - $new_price_seven;
                                        $seven_money = $result_seven * $order_goods["total"];
                                        if ($result_seven > 0) {
                                            $result = $this->shop_money_log($seven_money, $user_seven, 7);
                                            $send_seven = $this->send_Mess($user_seven["openid"], $user["nickname"], 7);
                                        }
                                    } elseif ($user_one["level"] >= $user["level"] && $user_three["level"] <= $user_one["level"] && $user_one["level"] >= $user_two["level"] && $user_four["level"] <= $user_one["level"] && $user_five["level"] <= $user_one["level"] && $user_six["level"] <= $user_one["level"] && $user_seven["level"] > $user_two["level"]) {
                                        //
                                        $result_seven = $new_price - $new_price_seven;
                                        $seven_money = $result_seven * $order_goods["total"];
                                        if ($result_seven > 0) {
                                            $result = $this->shop_money_log($seven_money, $user_seven, 7);
                                            $send_seven = $this->send_Mess($user_seven["openid"], $user["nickname"], 7);
                                        }
                                    } elseif ($user_three["level"] >= $user["level"] && $user_three["level"] >= $user_one["level"] && $user_three["level"] >= $user_two["level"] && $user_four["level"] <= $user_three["level"] && $user_five["level"] <= $user_three["level"] && $user_six["level"] <= $user_three["level"] && $user_seven["level"] > $user_three["level"]) {
                                        //
                                        $result_seven = $new_price_three - $new_price_seven;
                                        $seven_money = $result_seven * $order_goods["total"];
                                        if ($result_seven > 0) {
                                            $result = $this->shop_money_log($seven_money, $user_seven, 7);
                                            $send_seven = $this->send_Mess($user_seven["openid"], $user["nickname"], 7);
                                        }
                                    } elseif ($user_four["level"] >= $user["level"] && $user_four["level"] >= $user_one["level"] && $user_four["level"] >= $user_two["level"] && $user_four["level"] >= $user_three["level"] && $user_five["level"] <= $user_four["level"] && $user_six["level"] <= $user_four["level"] && $user_seven["level"] > $user_four["level"]) {
                                        //
                                        $result_seven = $new_price_four - $new_price_seven;
                                        $seven_money = $result_seven * $order_goods["total"];
                                        if ($result_seven > 0) {
                                            $result = $this->shop_money_log($seven_money, $user_seven, 7);
                                            $send_seven = $this->send_Mess($user_seven["openid"], $user["nickname"], 7);
                                        }
                                    } elseif ($user_five["level"] >= $user["level"] && $user_five["level"] >= $user_one["level"] && $user_five["level"] >= $user_two["level"] && $user_five["level"] >= $user_three["level"] && $user_five["level"] >= $user_four["level"] && $user_six["level"] <= $user_five["level"] && $user_seven["level"] > $user_five["level"]) {
                                        //
                                        $result_seven = $new_price_five - $new_price_seven;
                                        $seven_money = $result_seven * $order_goods["total"];
                                        if ($result_seven > 0) {
                                            $result = $this->shop_money_log($seven_money, $user_seven, 7);
                                            $send_seven = $this->send_Mess($user_seven["openid"], $user["nickname"], 7);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $order_goods_data["commissions"] = serialize($all_moeny);
            \think\Db::table("ims_ewei_shop_order_goods")->where("id", $order_goods["id"])->update($order_goods_data);
            return true;
        }
        return false;
    }
Mar.22,2021

one of the most direct and effective methods, take your code as an example:

if($user['agentid']!=0)
{//
    ... //
}
return false;

you can convert it to

if($user['agentid']==0)
{
    return false;
}
... //

so the logic of first-level online is missing a layer of nesting? In the same way, the nesting inside can be optimized

< hr >

finally, what I want to tell you is not to indiscriminately tag . Is this a javascript problem?


Agent relationship is related to business. I don't understand your code

$level = $user_one['level'];
$levelArray = [11=>168, 12=>138];

if(isset($levelArray[$level])){
    $subNum = $levelArray[$level];
} else {
    $subNum = 268;
}

$result_num=$money_one-$subNum;
if($result_num>0){
    $return_price=$result_num*$order_goods['total'];
}

rational use of switch and ternary operators and short-circuit operation

it is easier to use the ternary operator PHP7 when using switch to judge multiple if else.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3d9a7-413ea.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3d9a7-413ea.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?