123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- namespace Util\Common;
- use JinDouYun\Dao\Customer\DCustomer;
- use JinDouYun\Cache\CustomerCache;
- use JinDouYun\Dao\Order\DOrderIndex;
- use JinDouYun\Dao\Order\DOrder;
- use JinDouYun\Dao\CommissionPartner\DNewCommissionBalanceDetail;
- use JinDouYun\Dao\CommissionPartner\DNewCommissionCash;
- use JinDouYun\Dao\CommissionPartner\DNewCommissionDetail;
- use JinDouYun\Dao\CommissionPartner\DNewCommissionPartner;
- class PartnerTools{
- private $dbCustomer;
-
- private $dbNewCommissionPartner;
- private $dbNewCommissionDetail;
- private $dbNewCommissionCash;
- private $dbNewCommissionBalanceDetail;
-
-
- private $enterpriseId;
- private $cutTable = 1;//客户按照企业id分表
- private $errorMsg = "";
- public function __construct($enterpriseId){
- $this->enterpriseId = $enterpriseId;
- //用户表
- $this->dbCustomer = new DCustomer('default');
- $customerTableName = $this->dbCustomer->getTableName($this->dbCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbCustomer->setTable($customerTableName);
-
- //新分销合伙人表
- $this->dbNewCommissionPartner = new DNewCommissionPartner('default');
- $newCommissionPartnerTableName = $this->dbNewCommissionPartner->getTableName($this->dbNewCommissionPartner->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbNewCommissionPartner->setTable($newCommissionPartnerTableName);
-
- //新分销佣金明细表
- $this->dbNewCommissionDetail = new DNewCommissionDetail('default');
- $newCommissionDetailTableName = $this->dbNewCommissionDetail->getTableName($this->dbNewCommissionDetail->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbNewCommissionDetail->setTable($newCommissionDetailTableName);
-
- //新分销合伙人提现表
- $this->dbNewCommissionCash = new DNewCommissionCash('default');
- $newCommissionCashTableName = $this->dbNewCommissionCash->getTableName($this->dbNewCommissionCash->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbNewCommissionCash->setTable($newCommissionCashTableName);
-
- //新分销合伙人余额明细表
- $this->dbNewCommissionBalanceDetail = new DNewCommissionBalanceDetail('default');
- $newCommissionBalanceDetailTableName = $this->dbNewCommissionBalanceDetail->getTableName($this->dbNewCommissionBalanceDetail->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbNewCommissionBalanceDetail->setTable($newCommissionBalanceDetailTableName);
-
-
- }
-
- /**
- * 设置推广用户
- * @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;//佣金比例
- $isUpgrade = 0;
- if($money>=20000 && $customerData["isPartner"]==0){
- $per = 0.05;
- $isUpgrade = 1;
- }
- $resData=[
- "childId"=>$customerData["id"],
- "childData"=>$customerData,
- "parentId"=>$parentData["id"],
- "parentData"=>$parentData,
- "per"=>$per,
- "money"=>$money,
- "commission"=>$money * $per,
- "isUpgrade"=>$isUpgrade
- ];
- return $resData;
- }
-
- /**
- * 订单消费计算佣金
- * @param type $orderCustomerId
- * @param type $orderMoney
- * @param type $isPart 订单用户如果是合伙人是否计算
- * @return bool
- */
- public function addCalcMoneyData($orderId){
- //获取订单信息
- if(empty($orderId)){
- return false;
- }
- $dbOrderIndex = new DOrderIndex();
- $dbOrderIndex->setTable('qianniao_order_index_' . $this->enterpriseId);
- $orderIndexData = $dbOrderIndex->get(['id'=>$orderId]);
- if(empty($orderIndexData)){
- return false;
- }
- // 切换订单分表,查询订单主单据数据
- $dbOrder = new DOrder('default');
- $fix = ceil($orderIndexData['userCenterId'] / 200000);
- $dbOrder->setTable('qianniao_order_' . $this->onlineEnterpriseId . '_' . $fix);
- $orderData = $dbOrder->get(['id' => $orderId]);
- if(empty($orderData)){
- return false;
- }
- //开始计算
- $isPart=false;//订单用户如果是合伙人是否计算
- $orderCustomerId = $orderData["customerId"];
- $orderMoney = $orderData["payAmount'"];
- //只计算小程序已完成订单
- if(empty($orderCustomerId) || empty($orderMoney) || $orderData["payType"]!=1 || $orderData["source"]!=3 || $orderData["orderStatus"]!=5){
- 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表示子级消费计算收益
- "orderId"=>$orderData["id"],
- "status"=>0,
- "title"=>"合伙人收益分佣",
- "isUpgrade"=>$topData["isUpgrade"],
- "mono"=>"",
- "time"=>$nowTime,
- ];
- }
- //记录底层收益
- $data[]=[
- "orderMoney"=>$orderMoney,//订单支付金额
- "calcMoney"=>$bottomData["money"],//佣金计算金额
- "sourceCustomerId"=>$bottomData["childId"],//来源客户id
- "partnerId"=>$bottomData["parentId"],//收钱合伙人id
- "commission"=>$bottomData["commission"],//佣金金额
- "per"=>$bottomData["per"],//佣金比例
- "type"=>0,
- "orderId"=>$orderData["id"],
- "status"=>0,
- "title"=>"子级消费分佣",
- "isUpgrade"=>$bottomData["isUpgrade"],
- "mono"=>"",
- "time"=>$nowTime,
- ];
- //添加佣金余额和明细
- //可以新建一个佣金余额表newCommissionPartner
- //newCommissionDetail9
-
-
-
-
- //佣金更新完后验证设置合伙人
- if($bottomData["isUpgrade"]==1){
- $this->setPartner($bottomData["childId"]);
- }
-
-
-
-
- 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);
- }
- }
|