| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace Util\Common;
- use JinDouYun\Dao\Customer\DCustomer;
- use JinDouYun\Cache\CustomerCache;
- class PartnerTools{
- private $dbCustomer;
- private $enterpriseId;
- private $cutTable = 1;//客户按照企业id分表
- private $errorMsg = "";
- public function __construct($enterpriseId){
- $this->enterpriseId = $enterpriseId;
- $this->dbCustomer = new DCustomer('default');
- $tableName = $this->dbCustomer->getTableName($this->dbCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
- $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);
- if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
- return false;
- }
- //父级用户信息
- $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;
- }
- }
-
-
-
-
- /**
- * 获取用户父级合伙人
- * @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($this->enterpriseId, $customerId);
- $objCustomerCache->delCustomerUserData($this->enterpriseId, $userCenterId);
- }
- }
|