yingzi 1 year ago
parent
commit
272032f53a
2 changed files with 49 additions and 10 deletions
  1. 3 1
      Dao/Shop/DShop.Class.php
  2. 46 9
      Util/Common/AgentTools.Class.php

+ 3 - 1
Dao/Shop/DShop.Class.php

@@ -51,7 +51,9 @@ class DShop extends BaseDao
             "agentCustomerId",
             "level",
             "pOneShopId",
-            "pTwoShopId"
+            "pTwoShopId",
+            "pZeroShopId",
+            "parentPath",
         ];
         $this->_readonly = ['id'];
         $this->_create_autofill = [

+ 46 - 9
Util/Common/AgentTools.Class.php

@@ -136,28 +136,65 @@ class AgentTools{
         if(!$parentShopData["pTwoShopId"]){
             $pOneShopId = $parentShopData["pTwoShopId"];
         }
-        
+        $parentShopData["parentPath"] = empty($parentShopData["parentPath"])?"": trim($parentShopData["parentPath"]);
+        $parentPath = empty($parentShopData["parentPath"]) ? $parentShopId : $parentShopData["parentPath"] . "," . $parentShopId;
         //更新子级门店数据
         $save=[
             "level"=>1,//门店级别,1为社区门店,2为代理门店,3为董事门店
             "pOneShopId"=>$pOneShopId,//爷爷级门店id
             "pTwoShopId"=>$pTwoShopId,//父级门店id
+            "parentPath"=>$parentPath,//推广路径
         ];
         $this->dbShop->update($save,["id"=>$childShopId]);
         //如果父级门店没绑定过其他子级则更新为1级社区门店
         if($parentShopData["level"]<=0){
             $this->dbShop->update(["level"=>1],["id"=>$parentShopId]);
         }
-        //更新父级等级
-        $this->updateShopLevel($parentShopId);
-        if($pOneShopId>0){
-            $this->updateShopLevel($pOneShopId);
-        }
-        
-        
+        //更新祖先等级
+        $this->updateParentShopLevel($childShopId);
         return ["code"=>1,"msg"=>"绑定成功","data"=>null];
     }
-    
+    /**
+     * 更新父级门店等级
+     * @param type $shopId
+     * @param type $type 0表示只算两级
+     */
+    public function updateParentShopLevel($shopId,$type=0){
+        //子级门店
+        $shopData = $this->dbShop->get($shopId);
+        if(empty($shopData) || empty($shopData["parentPath"])){
+            return false;
+        }
+        //从最底层开始更新等级
+        $pathData = array_reverse(explode(",", trim($shopData["parentPath"])));
+        for($i=0;$i<count($pathData);$i++){
+            if(empty($pathData[$i])){
+                break;
+            }
+            $pData = $this->dbShop->get((int)$pathData[$i]);
+            if(empty($pData)){
+                continue;
+            }
+            if($pData["level"]>=3){
+                break;
+            }
+            $level = 1;
+            $levelCount_1 = $this->dbShop->count([["level","=",1],["pOneShopId|pTwoShopId","=",$pData["id"]]]);
+            if($levelCount_1 && $levelCount_1>=$this->agentSettingData["oneToTwoNum"]){
+                $level = 2;//5个升级为代理
+            }
+            $levelCount_2 = $this->dbShop->count([["level","=",2],["pOneShopId|pTwoShopId","=",$pData["id"]]]);
+            if($levelCount_2 && $levelCount_2>=$this->agentSettingData["twoToThreeNum"]){
+                $level = 3;//5个代理升级为董事
+            }
+            if($level<=$pData["level"]){
+                continue;
+            }
+            $r = $this->dbShop->update(["level"=>$level],["id"=>$pData["id"]]);
+        }
+        return true;
+    }
+
     /**
      * 更新门店等级
      * @param type $shopId