Browse Source

新分销代码

yingzi 2 years ago
parent
commit
a9551a6e79
1 changed files with 188 additions and 19 deletions
  1. 188 19
      Util/Common/PartnerTools.Class.php

+ 188 - 19
Util/Common/PartnerTools.Class.php

@@ -9,6 +9,7 @@ class PartnerTools{
     private $dbCustomer;
     private $enterpriseId;
     private $cutTable = 1;//客户按照企业id分表
+    private $errorMsg = "";
     public function __construct($enterpriseId){
         $this->enterpriseId = $enterpriseId;
         $this->dbCustomer = new DCustomer('default');
@@ -16,34 +17,202 @@ class PartnerTools{
         $this->dbCustomer->setTable($tableName);
     }
     
-    
+    /**
+     * 设置推广用户
+     * @param type $parentId
+     * @param type $childId
+     * @return bool
+     */
     public function setPushCustomer($parentId=0,$childId=0){
         if($parentId == 0 || $childId == 0 || $parentId == $childId){
             return false;
         }
+        //子级用户信息
         $childData = $this->dbCustomer->get($childId);
-        var_dump($childData);
-        if (empty($childData)) {
+        if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
             return false;
         }
-
-        
-        return true;
-        
-//        $beginStatus = $this->dbCustomer->beginTransaction();
-//        $this->dbCustomer->rollBack();
-//        $beginStatus && $this->dbCustomer->commit();
-        
-        
-        
+        //父级用户信息
+        $parentData = $this->dbCustomer->get($parentId);
+        if(empty($parentData)){
+            return false;
+        }
+        $beginStatus = $this->dbCustomer->beginTransaction();
+        //保存父级数据
+        if($parentData["isParentHead"] == 0){
+            $parentSave=["isParentHead"=>1];
+            $pres = $this->dbCustomer->update($parentSave, ["id"=>$parentId]);
+            if(empty($pres)){
+                $this->dbCustomer->rollBack();
+                return false;
+            } 
+        }
+        //保存子级数据
+        $parentPath = empty($parentData["parentPath"]) ? $parentId : $parentData["parentPath"] . "," . $parentId;
+        $childSave=["parentId"=>$parentId,"parentPath"=>$parentPath];
+        $res = $this->dbCustomer->update($childSave, ["id"=>$childId]);
+        if(empty($res)){
+            $this->dbCustomer->rollBack();
+            return false;
+        }else{
+            $beginStatus && $this->dbCustomer->commit();
+//            $this->delCustomerCache();
+            return true;
+        }
+    }
+    /**
+     * 设置用户为合伙人
+     * @param type $customerId
+     */
+    public function setPartner($customerId){
+        $userData = $this->dbCustomer->get($customerId);
+        if (empty($userData)) {
+            return false;
+        }
+        if($userData["isPartner"]==1){
+            return true;
+        }
+        $save=["isPartner"=>1];
+        $res = $this->dbCustomer->update($save,["id"=>$customerId]);
+        if(empty($res)){
+            return false;
+        }else{
+            return true;
+        }
     }
-    public function delCustomerCache(){
-        $enterpriseId = 0;
-        $customerId = 0;
-        $userCenterId = 0;
+    
+    
+    
+    
+    /**
+     * 获取用户父级合伙人
+     * @param type $customerId
+     * @return bool
+     */
+    public function getParentPartner($userData){
+        if (empty($userData) || empty($userData["parentPath"])) {
+            return false;
+        }
+        $where["isPartner"]=1;
+        $where["id"]=["in",trim($userData["parentPath"])];
+        $parentData = $this->dbCustomer->select($where);
+        if(empty($parentData)){
+            return false;
+        }
+        $parentPartner = null;
+        //逆序祖先,从最近的祖先先查
+        $pathData = array_reverse(explode(",", trim($userData["parentPath"])));
+        for($i=0;$i<count($pathData);$i++){
+            $itemData = [];
+            foreach($parentData as $k=>$v){
+                if($pathData[$i]==$v["id"]){
+                    $itemData = $v;
+                    break;
+                }
+            }
+            if(!empty($itemData) && $itemData["isPartner"]==1){
+                $parentPartner = $itemData;
+                break;
+            }
+        }
+        if(empty($parentPartner)){
+            return false;
+        }
+        return $parentPartner;
+    }
+    
+    /**
+     * 佣金计算
+     * @param type $customerId
+     * @param type $money
+     * @param type $type 1表示子级消费,2表示合伙人收益
+     * @return bool
+     */
+    public function calcMoney($customerData,$money){
+        $parentData = $this->getParentPartner($customerData);
+        if(empty($parentData) || $parentData["isPartner"]==0){
+            return false;
+        }
+        $per = 0.1;//佣金比例
+        if($money>=20000 && $customerData["isPartner"]==0){
+            $per = 0.05;
+        }
+        $resData=[
+            "childId"=>$customerData["id"],
+            "childData"=>$customerData,
+            "parentId"=>$parentData["id"],
+            "parentData"=>$parentData,
+            "per"=>$per,
+            "money"=>$money,
+            "commission"=>$money * $per
+        ];
+        return $resData;
+    }
+    
+    /**
+     * 订单消费计算佣金
+     * @param type $orderCustomerId
+     * @param type $orderMoney
+     * @param type $isPart 订单用户如果是合伙人是否计算
+     * @return bool
+     */
+    public function getCalcMoneyData($orderCustomerId,$orderMoney,$isPart=false){
+        if(empty($orderCustomerId) || empty($orderMoney)){
+            return false;
+        }
+        $customerData = $this->dbCustomer->get($orderCustomerId);
+        if (empty($customerData) || empty($customerData["parentPath"])) {
+            return false;
+        }
+        //订单用户如果是合伙人消费则不计算收益
+        if(!$isPart && $customerData["isPartner"]==1){
+            return false;
+        }
+        $data=[];
+        $nowTime = time();
+        //计算低层收益
+        $bottomData = $this->calcMoney($customerData,$orderMoney);
+        if(empty($bottomData)){
+            return false;
+        }
+        //计算顶层收益
+        $topData = $this->calcMoney($bottomData["parentData"],$bottomData["commission"]);
+        if(!empty($topData)){
+            //记录顶层收益
+            $data[]=[
+                "orderMoney"=>$orderMoney,//订单支付金额
+                "calcMoney"=>$topData["money"],//佣金计算金额
+                "sourceCustomerId"=>$topData["childId"],//来源客户id
+                "partnerId"=>$topData["parentId"],//收钱合伙人id
+                "commission"=>$topData["commission"],//佣金金额
+                "per"=>$topData["per"],//佣金比例
+                "type"=>1,//1表示子级合伙人收益计算收益,0表示子级消费计算收益
+                "time"=>$nowTime,
+            ];
+        }
+        //记录底层收益
+        $data[]=[
+            "orderMoney"=>$orderMoney,//订单支付金额
+            "calcMoney"=>$topData["money"],//佣金计算金额
+            "sourceCustomerId"=>$topData["childId"],//来源客户id
+            "partnerId"=>$topData["parentId"],//收钱合伙人id
+            "commission"=>$topData["commission"],//佣金金额
+            "time"=>$nowTime,
+            "type"=>0
+        ];
+        return $data;
+    }
+    
+    
+    /**
+     * 删除用户缓存
+     * @param type $customerId
+     * @param type $userCenterId
+     */
+    public function delCustomerCache($customerId,$userCenterId){
         $objCustomerCache = new CustomerCache();
-        $objCustomerCache->delCustomerData($enterpriseId, $customerId);
-        $objCustomerCache->delCustomerUserData($enterpriseId, $userCenterId);
+        $objCustomerCache->delCustomerData($this->enterpriseId, $customerId);
+        $objCustomerCache->delCustomerUserData($this->enterpriseId, $userCenterId);
     }
 }