看到某程序的一个函数逻辑,这些的代码读起来,怕不怕?

乱弹 · Fecmall · 于 5年前 发布 · 1789 次阅读
public function caculatecard($card_id, $dispatch_price, $lotterydiscountprice, $taskcut, $goodsarr, $totalprice, $discountprice, $isdiscountprice, $liveid, $isSubmit = 0) 
	{
		global $_W;
		$openid = $_W["openid"];
		$uniacid = $_W["uniacid"];
		if( empty($goodsarr) ) 
		{
			return false;
		}
		if( p("membercard") ) 
		{
			$data = p("membercard")->getMemberCard($card_id);
		}
		if( empty($data) ) 
		{
			return NULL;
		}
		if( is_array($goodsarr) ) 
		{
			$goods = array( );
			$member_discount = intval($data["member_discount"]);
			$discount_rate = floatval($data["discount_rate"]);
			if( isset($data["shipping"]) && $data["shipping"] == 1 ) 
			{
				$dispatch_price = 0;
			}
			$gprice = 0;
			$cardgoodprice = 0;
			$newtotalprice = 0;
			$deductprice = 0;
			$discountprice = 0;
			$isdiscountprice = 0;
			foreach( $goodsarr as $k => &$g ) 
			{
				if( $g["status"] != 2 ) 
				{
					if( 0 < $liveid ) 
					{
						$live_product = pdo_fetch("SELECT *  FROM " . tablename("ewei_shop_goods") . " WHERE id = '" . $g["goodsid"] . "'");
						if( $live_product ) 
						{
							$g["marketprice"] = $live_product["marketprice"];
						}
					}
					$gprice = (double) $g["marketprice"] * (double) $g["total"];
					$newtotalprice = 0 + $gprice;
					if( $g["discounttype"] == 2 ) 
					{
						if( 0 < $data["discount"] ) 
						{
							$cardgoodprice += $gprice - (double) $g["discountunitprice"] * (double) $g["total"];
							$goodprice = $gprice - (double) $g["discountunitprice"] * (double) $g["total"];
							if( 0 < $discount_rate ) 
							{
								$deductprice += $goodprice * (1 - $discount_rate / 10);
								$discountprice += (double) $g["discountunitprice"] * (double) $g["total"];
							}
							else 
							{
								$discountprice -= (double) $g["discountunitprice"] * (double) $g["total"];
							}
						}
						else 
						{
							$discountprice = 0;
							if( 0 < $discount_rate ) 
							{
								$deductprice += $gprice * (1 - $discount_rate / 10);
								$cardgoodprice += $gprice * (1 - $discount_rate / 10);
							}
						}
					}
					else 
					{
						if( $g["discounttype"] == 1 ) 
						{
							$cardgoodprice += $gprice - (double) $g["isdiscountunitprice"] * (double) $g["total"];
							$goodprice = $gprice - (double) $g["isdiscountunitprice"] * (double) $g["total"];
							if( 0 < $data["discount"] ) 
							{
								$deductprice += $goodprice * (1 - $discount_rate / 10);
								$isdiscountprice += $g["isdiscountunitprice"] * (double) $g["total"];
							}
							else 
							{
								$isdiscountprice -= (double) $g["isdiscountunitprice"] * (double) $g["total"];
								if( 0 < $discount_rate ) 
								{
									$deductprice += $gprice * (1 - $discount_rate / 10);
									$cardgoodprice += $gprice * (1 - $discount_rate / 10);
								}
							}
						}
						else 
						{
							if( $g["discounttype"] == 0 ) 
							{
								if( $g["isnodiscount"] == 1 ) 
								{
									$cardgoodprice += $gprice;
									if( $data["discount"] ) 
									{
										if( 0 < $discount_rate ) 
										{
											$deductprice += $cardgoodprice * (1 - $discount_rate / 10);
										}
									}
									else 
									{
										if( 0 < $discount_rate ) 
										{
											$deductprice += $gprice * (1 - $discount_rate / 10);
											$cardgoodprice += $gprice * (1 - $discount_rate / 10);
										}
									}
								}
								else 
								{
									$cardgoodprice += $gprice;
									if( $data["discount"] ) 
									{
										if( 0 < $discount_rate ) 
										{
											$deductprice += $cardgoodprice * (1 - $discount_rate / 10);
										}
									}
									else 
									{
										if( 0 < $discount_rate ) 
										{
											$deductprice += $gprice * (1 - $discount_rate / 10);
											$cardgoodprice += $gprice * (1 - $discount_rate / 10);
										}
									}
								}
							}
						}
					}
				}
			}
			$carddeduct_text = "";
			if( $data["discount_rate"] ) 
			{
				$carddeduct_text = "会员卡优惠";
			}
		}
		$sale_plugin = com("sale");
		$saleset = false;
		if( $sale_plugin ) 
		{
			$saleset = $_W["shopset"]["sale"];
			$saleset["enoughs"] = $sale_plugin->getEnoughs();
		}
		if( $saleset ) 
		{
			foreach( $saleset["enoughs"] as $e ) 
			{
				if( floatval($e["enough"]) <= $totalprice - $discountprice - $isdiscountprice - $deductprice && 0 < floatval($e["money"]) ) 
				{
					$deductenough_money = floatval($e["money"]);
					$deductenough_enough = floatval($e["enough"]);
					break;
				}
			}
		}
		$taskreward = $_SESSION["taskcut"];
		if( $taskreward && p("task") ) 
		{
			$taskcut = 0;
		}
		$totalprice = $totalprice - $deductprice;
		$return_array = array( );
		$return_array["carddeductprice"] = (double) $deductprice;
		$return_array["cardgoodprice"] = (double) $cardgoodprice;
		$return_array["carddeduct_text"] = $carddeduct_text;
		$return_array["totalprice"] = (double) $totalprice;
		$return_array["dispatch_price"] = (double) $dispatch_price;
		$return_array["cardname"] = $data["name"];
		$return_array["taskcut"] = (double) $taskcut;
		$return_array["lotterydiscountprice"] = 0;
		$return_array["discountprice"] = (double) $discountprice;
		$return_array["isdiscountprice"] = (double) $isdiscountprice;
		$return_array["live_id"] = $liveid;
		$return_array["shipping"] = $data["shipping"];
		$return_array["cardgoodprice"] = (double) $cardgoodprice;
		$return_array["goodsprice"] = (double) $newtotalprice;
		$return_array["deductenough_money"] = (double) $deductenough_money;
		$return_array["deductenough_enough"] = (double) $deductenough_enough;
		$return_array["\$goodsarr"] = $goodsarr;
		return $return_array;
	}
共收到 5 条回复
Kittyfamous#15年前 0 个赞

我的天啦,谁写的?

吓死人了。

Kittyfamous#25年前 0 个赞

感觉foreach里面,要封闭几个语义化的函数,否则可读性太特妈的吓人。

Fecmall#35年前 0 个赞

脑子内存得大,把这些逻辑一次性放到脑子里面,代码维护很麻烦,估计写这个逻辑的人一个月后读起来也费劲。

这种逻辑,最好是拆分成多个函数依次处理,由大到小拆分。

Kittyfamous#45年前 0 个赞

@fecshop 参数如何减少了?

感觉还是参数挺多的啊。

successgo#55年前 0 个赞

可怕可怕

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics