多重if判断如何简化代码,减少代码冗余

发布于 2022-09-07 15:44:45 字数 26935 浏览 15 评论 0

如下列代码。充斥着各种if语句。完全不知道从哪里开始优化..不知道有什么办法可以解决这种多重if判断..减少代码冗余提升效率。下列代码的功能就是一个查找7级上线代理的功能【查询该用户的上线是否存在上线代理-需要往上查询7级,如果存在,给予一定的返现,前三级需要通过序列化保存在一个字段,后五级只需要返现。】。

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

時窥 2022-09-14 15:44:45

一个最直接有效的方法,以你的代码为例:

if($user['agentid']!=0)
{//一级上线
    ... //一级上线逻辑
}
return false;

你可以转换成

if($user['agentid']==0)
{
    return false;
}
... //一级上线逻辑

这样 一级上线 的逻辑不就少了一层嵌套了?里面的嵌套同理可以优化


最后,要告诉你的是不要乱打标签,你这是 javascript 问题吗?

魂牵梦绕锁你心扉 2022-09-14 15:44:45

代理关系和业务相关,没看明白你代码

$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'];
}
神仙妹妹 2022-09-14 15:44:45

合理使用switch和三元运算符 还有短路运算

多个if else 的时候使用switch 判断的时候使用三元运算符 PHP7就更简单了!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文