人人商城分销插件commission分析之checkOrderFinish()订单完成后执行操作

今天来分享一下人人商城分销插件commission中订单完成后执行操作的函数 checkOrderFinish(),本函数主要执行升级为分销商的判断,分销商等级升级判断以及其他插件相关的升级判断。

函数位置:

ewei_shopv2/plugin/commission/core/model.php

函数代码:

//YangJunwei: 订单完成后执行操作, 如分销等级升级判断
public function checkOrderFinish($orderid = "") 
{
	global $_W;
	global $_GPC;
	if( empty($orderid) ) 
	{
		return NULL;
	}
	$order = pdo_fetch("select id,openid, ordersn,price,goodsprice,agentid,finishtime from " . tablename("yjw_shopws_order") . " where id=:id and status>=3 and uniacid=:uniacid limit 1", array( ":id" => $orderid, ":uniacid" => $_W["uniacid"] ));
	if( empty($order) ) 
	{
		return NULL;
	}
	$set = $this->getSet();
	if( empty($set["level"]) ) 
	{
		return NULL;
	}
	$openid = $order["openid"];
	$member = m("member")->getMember($openid); //YangJunwei:读取订单所有者的信息
	if( empty($member) ) 
	{
		return NULL;
	}
	$this->orderFinishTask($order, ($set["selfbuy"] ? true : false), $member); //执行相关任务
	$time = time();
	$become_check = intval($set["become_check"]); //是否需要审核
	$isagent = $member["isagent"] == 1 && $member["status"] == 1; //YangJunwei:判断订单所有者是否为已审核的分销商
	$parentisagent = true;
	if( !empty($member["agentid"]) ) //Y:判断上级是否为分销商
	{
		$parent = m("member")->getMember($member["agentid"]);
		if( empty($parent) || $parent["isagent"] != 1 || $parent["status"] != 1 ) 
		{
			$parentisagent = false;
		}
	}
	
	/*
	 * 成为分销商的条件become: 0无条件  1申请  2消费次数  3消费金额  4购买商品
	 * 前两个条件跟本函数无关(订单完成后执行的函数),故不作判断
	 */
	if( !$isagent && $set["become_order"] == "1" ) //当前为非分销商且判断条件设置为“订单完成后”
	{
		//YangJunwei:成为分销商条件 购买商品
		if( $set["become"] == "4" && !empty($set["become_goodsid"]) ) 
		{
			$order_goods = pdo_fetchall("select goodsid from " . tablename("yjw_shopws_order_goods") . " where orderid=:orderid and uniacid=:uniacid  ", array( ":uniacid" => $_W["uniacid"], ":orderid" => $order["id"] ), "goodsid"); //返回的数组的键值为goodsid
			
			//完成的订单商品ID包含指定购买的商品ID 且 用户未在黑名单
			if( in_array($set["become_goodsid"], array_keys($order_goods)) && empty($member["agentblack"]) ) 
			{
				pdo_update("yjw_shopws_member", array( "status" => $become_check, "isagent" => 1, "agenttime" => ($become_check == 1 ? $time : 0) ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
				
				if( $become_check == 1 ) //不需要审核 则执行推送消息和相关分销商的升级判断操作、其他插件的操作
				{
					$this->sendMessage($openid, array( "nickname" => $member["nickname"], "agenttime" => $time ), TM_COMMISSION_BECOME);
					if( $parentisagent ) 
					{
						$this->upgradeLevelByAgent($parent["id"]);
						if( p("globonus") ) 
						{
							p("globonus")->upgradeLevelByAgent($parent["id"]);//全民股东升级(根据下级数)
						}
						if( p("abonus") ) 
						{
							p("abonus")->upgradeLevelByAgent($parent["id"]);
						}
						if( p("author") ) 
						{
							p("author")->upgradeLevelByAgent($parent["id"]);
						}
					}
				}
			}
		}
		else 
		{
			if( $set["become"] == 2 || $set["become"] == 3 ) 
			{
				$can = false;
				//YangJunwei:成为分销商条件 消费次数
				if( $set["become"] == "2" )
				{
					$ordercount = pdo_fetchcolumn("select count(*) from " . tablename("yjw_shopws_order") . " where openid=:openid and status>=3 and uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":openid" => $openid ));
					$can = intval($set["become_ordercount"]) <= $ordercount;
				}
				else 
				{
					//YangJunwei:成为分销商条件 消费金额
					if( $set["become"] == "3" )
					{
						//【老杨自定义的条件】一次性消费的金额,直接读取订单金额即可。[参考:人人商城分销资格中消费金额新增按一次性消费计算 http://yangjunwei.com/a/3193.html]
						if ($set['moneycount_type'] == '1'){
							$moneycount = $order['price'];
						}else{//按总计消费金额
							$moneycount = pdo_fetchcolumn("select sum(goodsprice) from " . tablename("yjw_shopws_order") . " where openid=:openid and status>=3 and uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":openid" => $openid ));
						}
						$can = floatval($set["become_moneycount"]) <= $moneycount;
					}
				}
				//分销商黑名单
				if( $can && empty($member["agentblack"]) ) 
				{
					pdo_update("yjw_shopws_member", array( "status" => $become_check, "isagent" => 1, "agenttime" => $time ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
					if( $become_check == 1 ) 
					{
						$this->sendMessage($member["openid"], array( "nickname" => $member["nickname"], "agenttime" => $time ), TM_COMMISSION_BECOME);
					}
				}
			}
		}
	}
	
	//推送消息
	if( !empty($member["agentid"]) ) 
	{
		$parent = m("member")->getMember($member["agentid"]);
		if( !empty($parent) && $parent["isagent"] == 1 && $parent["status"] == 1 ) 
		{
			$order_goods = pdo_fetchall("select g.id,g.title,og.total,og.realprice,og.price,og.optionname as optiontitle,g.noticeopenid,g.noticetype,og.commission1,og.commissions from " . tablename("yjw_shopws_order_goods") . " og " . " left join " . tablename("yjw_shopws_goods") . " g on g.id=og.goodsid " . " where og.uniacid=:uniacid and og.orderid=:orderid ", array( ":uniacid" => $_W["uniacid"], ":orderid" => $order["id"] ));
			$goods = "";
			$commission_total1 = 0;
			$commission_total2 = 0;
			$commission_total3 = 0;
			$pricetotal = 0;
			foreach( $order_goods as $og ) 
			{
				$goods .= "" . $og["title"] . "( ";
				if( !empty($og["optiontitle"]) ) 
				{
					$goods .= " 规格: " . $og["optiontitle"];
				}
				$goods .= " 单价: " . $og["realprice"] / $og["total"] . " 数量: " . $og["total"] . " 总价: " . $og["realprice"] . "); ";
				$commissions = iunserializer($og["commissions"]);
				$commission_total1 += (isset($commissions["level1"]) ? floatval($commissions["level1"]) : 0);
				$commission_total2 += (isset($commissions["level2"]) ? floatval($commissions["level2"]) : 0);
				$commission_total3 += (isset($commissions["level3"]) ? floatval($commissions["level3"]) : 0);
				$pricetotal += $og["realprice"];
			}
			if( $order["agentid"] == $member["id"] ) 
			{
				$this->sendMessage($member["openid"], array( "nickname" => $member["nickname"], "ordersn" => $order["ordersn"], "orderopenid" => $order["openid"], "price" => $pricetotal, "goods" => $goods, "commission1" => $commission_total1, "commission2" => $commission_total2, "commission3" => $commission_total3, "finishtime" => $order["finishtime"] ), TM_COMMISSION_ORDER_FINISH);
			}
			else 
			{
				if( $order["agentid"] == $parent["id"] ) 
				{
					$this->sendMessage($parent["openid"], array( "nickname" => $member["nickname"], "ordersn" => $order["ordersn"], "orderopenid" => $order["openid"], "price" => $pricetotal, "goods" => $goods, "commission1" => $commission_total1, "commission2" => $commission_total2, "commission3" => $commission_total3, "finishtime" => $order["finishtime"] ), TM_COMMISSION_ORDER_FINISH);
				}
			}
		}
	}
	
	/*
	 * 分销商等级升级
	 */
	$this->upgradeLevelByOrder($openid);
	
	/*
	 * 其他插件等级升级
	 */
	if( $isagent ) {
		//plugin author
		$plugin_author = p("author");
		if( $plugin_author ) 
		{
			$set = $plugin_author->getSet();
			if( !empty($set["open"]) ) 
			{
				$isauthor = $member["isauthor"] && $member["authorstatus"];
				if( $isauthor ) 
				{
					$plugin_author->upgradeLevelByOrder($openid);
				}
				else 
				{
					$become_check = intval($set["become_check"]);
					if( $set["become_order"] == "1" ) 
					{
						$info = $this->getInfo($member["id"], array( "ordercount3", "ordermoney3", "order13money", "order13" ));
						$can = false;
						if( $set["become"] == "3" ) 
						{
							$can = floatval($set["become_moneycount"]) <= floatval($info["ordermoney3"]);
						}
						else 
						{
							if( $set["become"] == "4" ) 
							{
								$moneycount = pdo_fetchcolumn("select sum(goodsprice) from " . tablename("yjw_shopws_order") . " where openid=:openid and status>=3 and uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":openid" => $openid ));
								$can = floatval($set["become_selfmoneycount"]) <= floatval($moneycount);
							}
						}
						if( $can && empty($member["authorblack"]) ) 
						{
							pdo_update("yjw_shopws_member", array( "authorstatus" => $become_check, "isauthor" => 1, "authortime" => $time ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
							if( $become_check == 1 ) 
							{
								$plugin_author->sendMessage($member["openid"], array( "nickname" => $member["nickname"], "authortime" => $time ), TM_AUTHOR_BECOME);
							}
						}
					}
				}
			}
		}
		
		//plugin globonus 全民股东 全球分红
		$plugin_globonus = p("globonus");
		if( !$plugin_globonus ) 
		{
			return NULL;
		}
		$set = $plugin_globonus->getSet();
		if( empty($set["open"]) ) 
		{
			return NULL;
		}
		$ispartner = $member["ispartner"] && $member["partnerstatus"]; //状态正常的股东
		if( $ispartner ) 
		{
			$plugin_globonus->upgradeLevelByOrder($openid); //股东升级
			return NULL;
		}
		$become_check = intval($set["become_check"]);
		if( $set["become_order"] == "1" ) //订单完成后 执行
		{
			if( $set["become"] == "4" && !empty($set["become_goodsid"]) ) 
			{
				$order_goods = pdo_fetchall("select goodsid from " . tablename("yjw_shopws_order_goods") . " where orderid=:orderid and uniacid=:uniacid  ", array( ":uniacid" => $_W["uniacid"], ":orderid" => $order["id"] ), "goodsid");
				if( in_array($set["become_goodsid"], array_keys($order_goods)) && empty($member["partnerblack"]) ) 
				{
					pdo_update("yjw_shopws_member", array( "partnerstatus" => $become_check, "ispartner" => 1, "partnertime" => ($become_check == 1 ? $time : 0) ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
					if( $become_check == 1 ) 
					{
						$plugin_globonus->sendMessage($openid, array( "nickname" => $member["nickname"], "partnertime" => $time ), TM_GLOBONUS_BECOME);
					}
				}
			}
			else 
			{
				if( $set["become"] == 2 || $set["become"] == 3 ) 
				{
					$can = false;
					if( $set["become"] == "2" ) 
					{
						$ordercount = pdo_fetchcolumn("select count(*) from " . tablename("yjw_shopws_order") . " where openid=:openid and status>=3 and uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":openid" => $openid ));
						$can = intval($set["become_ordercount"]) <= $ordercount;
					}
					else 
					{
						if( $set["become"] == "3" ) 
						{
							$moneycount = pdo_fetchcolumn("select sum(goodsprice) from " . tablename("yjw_shopws_order") . " where openid=:openid and status>=3 and uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":openid" => $openid ));
							$can = floatval($set["become_moneycount"]) <= $moneycount;
						}
					}
					if( $can && empty($member["partnerblack"]) ) 
					{
						pdo_update("yjw_shopws_member", array( "partnerstatus" => $become_check, "ispartner" => 1, "partnertime" => $time ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
						if( $become_check == 1 ) 
						{
							$plugin_globonus->sendMessage($member["openid"], array( "nickname" => $member["nickname"], "partnertime" => $time ), TM_GLOBONUS_BECOME);
						}
					}
				}
			}
		}
	}
}

还有个“付款后”执行的函数 checkOrderPay(),老杨有空再注释吧!

参与评论

游客评论不支持回复他人评论内容,如需回复他人评论内容请