|
@@ -1,7 +1,7 @@
|
|
<?php
|
|
<?php
|
|
|
|
|
|
namespace Util\Common;
|
|
namespace Util\Common;
|
|
-
|
|
|
|
|
|
+use Mall\Framework\Factory;
|
|
use JinDouYun\Dao\Customer\DCustomer;
|
|
use JinDouYun\Dao\Customer\DCustomer;
|
|
use JinDouYun\Cache\CustomerCache;
|
|
use JinDouYun\Cache\CustomerCache;
|
|
use JinDouYun\Dao\Order\DOrderIndex;
|
|
use JinDouYun\Dao\Order\DOrderIndex;
|
|
@@ -11,26 +11,43 @@ use JinDouYun\Dao\CommissionPartner\DNewCommissionBalanceDetail;
|
|
use JinDouYun\Dao\CommissionPartner\DNewCommissionCash;
|
|
use JinDouYun\Dao\CommissionPartner\DNewCommissionCash;
|
|
use JinDouYun\Dao\CommissionPartner\DNewCommissionDetail;
|
|
use JinDouYun\Dao\CommissionPartner\DNewCommissionDetail;
|
|
use JinDouYun\Dao\CommissionPartner\DNewCommissionPartner;
|
|
use JinDouYun\Dao\CommissionPartner\DNewCommissionPartner;
|
|
|
|
+use JinDouYun\Dao\CommissionPartner\DNewCommissionTask;
|
|
|
|
+
|
|
|
|
|
|
class PartnerTools{
|
|
class PartnerTools{
|
|
private $dbCustomer;
|
|
private $dbCustomer;
|
|
-
|
|
|
|
|
|
+ private $dbOrderIndex;
|
|
|
|
+
|
|
private $dbNewCommissionPartner;
|
|
private $dbNewCommissionPartner;
|
|
private $dbNewCommissionDetail;
|
|
private $dbNewCommissionDetail;
|
|
private $dbNewCommissionCash;
|
|
private $dbNewCommissionCash;
|
|
private $dbNewCommissionBalanceDetail;
|
|
private $dbNewCommissionBalanceDetail;
|
|
-
|
|
|
|
|
|
+ private $dbNewCommissionTask;
|
|
|
|
|
|
private $enterpriseId;
|
|
private $enterpriseId;
|
|
|
|
+ private $cache;
|
|
private $cutTable = 1;//客户按照企业id分表
|
|
private $cutTable = 1;//客户按照企业id分表
|
|
private $errorMsg = "";
|
|
private $errorMsg = "";
|
|
|
|
+
|
|
|
|
+ private $codeAr = [
|
|
|
|
+ "order_calc"=>"子级用户消费分佣",
|
|
|
|
+ "partner_calc"=>"子级合伙人收益分佣",
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
public function __construct($enterpriseId){
|
|
public function __construct($enterpriseId){
|
|
$this->enterpriseId = $enterpriseId;
|
|
$this->enterpriseId = $enterpriseId;
|
|
|
|
+ $this->cache = Factory::cache('systask');
|
|
//用户表
|
|
//用户表
|
|
$this->dbCustomer = new DCustomer('default');
|
|
$this->dbCustomer = new DCustomer('default');
|
|
$customerTableName = $this->dbCustomer->getTableName($this->dbCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
|
|
$customerTableName = $this->dbCustomer->getTableName($this->dbCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
|
|
$this->dbCustomer->setTable($customerTableName);
|
|
$this->dbCustomer->setTable($customerTableName);
|
|
|
|
|
|
|
|
+ //订单索引表
|
|
|
|
+ $this->dbOrderIndex = new DOrderIndex();
|
|
|
|
+ $this->dbOrderIndex->setTable('qianniao_order_index_' . $this->enterpriseId);
|
|
|
|
+
|
|
//新分销合伙人表
|
|
//新分销合伙人表
|
|
$this->dbNewCommissionPartner = new DNewCommissionPartner('default');
|
|
$this->dbNewCommissionPartner = new DNewCommissionPartner('default');
|
|
$newCommissionPartnerTableName = $this->dbNewCommissionPartner->getTableName($this->dbNewCommissionPartner->get_Table(), $this->enterpriseId, $this->cutTable);
|
|
$newCommissionPartnerTableName = $this->dbNewCommissionPartner->getTableName($this->dbNewCommissionPartner->get_Table(), $this->enterpriseId, $this->cutTable);
|
|
@@ -51,10 +68,72 @@ class PartnerTools{
|
|
$newCommissionBalanceDetailTableName = $this->dbNewCommissionBalanceDetail->getTableName($this->dbNewCommissionBalanceDetail->get_Table(), $this->enterpriseId, $this->cutTable);
|
|
$newCommissionBalanceDetailTableName = $this->dbNewCommissionBalanceDetail->getTableName($this->dbNewCommissionBalanceDetail->get_Table(), $this->enterpriseId, $this->cutTable);
|
|
$this->dbNewCommissionBalanceDetail->setTable($newCommissionBalanceDetailTableName);
|
|
$this->dbNewCommissionBalanceDetail->setTable($newCommissionBalanceDetailTableName);
|
|
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ //新分销合伙人分佣任务表
|
|
|
|
+ $this->dbNewCommissionTask = new DNewCommissionTask('default');
|
|
|
|
+ $newCommissionTaskTableName = $this->dbNewCommissionTask->getTableName($this->dbNewCommissionTask->get_Table(), $this->enterpriseId, $this->cutTable);
|
|
|
|
+ $this->dbNewCommissionTask->setTable($newCommissionTaskTableName);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 添加任务
|
|
|
|
+ * @param type $orderId
|
|
|
|
+ * @param type $enterpriseId
|
|
|
|
+ * @return bool
|
|
|
|
+ */
|
|
|
|
+ public static function addTask($orderId,$enterpriseId){
|
|
|
|
+ if(empty($orderId) || empty($enterpriseId)){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ $dbTask = new DNewCommissionTask('default');
|
|
|
|
+ $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $enterpriseId, 1);
|
|
|
|
+ $dbTask->setTable($taskTableName);
|
|
|
|
+ $count = $taskTableName->count(["orderId"=>$orderId]);
|
|
|
|
+ if($count>0){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ $dbTask->insert([
|
|
|
|
+ "orderId"=>$orderId,
|
|
|
|
+ "enterpriseId"=>$enterpriseId,
|
|
|
|
+ "title"=>"订单完成计算分佣",
|
|
|
|
+ "mono"=>"",
|
|
|
|
+ "status"=>0,
|
|
|
|
+ "run_time"=>0,
|
|
|
|
+ "time"=>time(),
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function runTask(){
|
|
|
|
+
|
|
|
|
+ $key = "partnertaskcache".'::'.$this->enterpriseId;
|
|
|
|
+ $result = $this->cache->get($key);
|
|
|
|
+ if(!empty($result)){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ $this->cache->set($key,1,10);
|
|
|
|
+ $dbTask = new DNewCommissionTask('default');
|
|
|
|
+ $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $this->enterpriseId, 1);
|
|
|
|
+ $dbTask->setTable($taskTableName);
|
|
|
|
+ $data = $dbTask->select(["status"=>0]);
|
|
|
|
+ if(empty($data)){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ $nowTime=time();
|
|
|
|
+ foreach($data as $k=>$v){
|
|
|
|
+ $res = $this->runCalcMoneyData($v["orderId"]);
|
|
|
|
+ $save=[];
|
|
|
|
+ $save=["status"=>1,"mono"=>"执行成功","run_time"=>$nowTime];
|
|
|
|
+ if(empty($res) || $res["code"]==-1){
|
|
|
|
+ $save["mono"] = empty($res["msg"])?"系统错误执行失败":$res["msg"];
|
|
|
|
+ $save["status"] = -1;
|
|
|
|
+ }
|
|
|
|
+ $dbTask->update($save, ["id"=>$v["id"]]);
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 设置推广用户
|
|
* 设置推广用户
|
|
* @param type $parentId
|
|
* @param type $parentId
|
|
@@ -185,7 +264,7 @@ class PartnerTools{
|
|
"parentData"=>$parentData,
|
|
"parentData"=>$parentData,
|
|
"per"=>$per,
|
|
"per"=>$per,
|
|
"money"=>$money,
|
|
"money"=>$money,
|
|
- "commission"=>$money * $per,
|
|
|
|
|
|
+ "commission"=>bcmul($money,$per,2),
|
|
"isUpgrade"=>$isUpgrade
|
|
"isUpgrade"=>$isUpgrade
|
|
];
|
|
];
|
|
return $resData;
|
|
return $resData;
|
|
@@ -198,16 +277,14 @@ class PartnerTools{
|
|
* @param type $isPart 订单用户如果是合伙人是否计算
|
|
* @param type $isPart 订单用户如果是合伙人是否计算
|
|
* @return bool
|
|
* @return bool
|
|
*/
|
|
*/
|
|
- public function addCalcMoneyData($orderId){
|
|
|
|
|
|
+ public function runCalcMoneyData($orderId){
|
|
//获取订单信息
|
|
//获取订单信息
|
|
if(empty($orderId)){
|
|
if(empty($orderId)){
|
|
- return false;
|
|
|
|
|
|
+ return ["code"=>-1,"msg"=>"订单id错误"];
|
|
}
|
|
}
|
|
- $dbOrderIndex = new DOrderIndex();
|
|
|
|
- $dbOrderIndex->setTable('qianniao_order_index_' . $this->enterpriseId);
|
|
|
|
- $orderIndexData = $dbOrderIndex->get(['id'=>$orderId]);
|
|
|
|
|
|
+ $orderIndexData = $this->dbOrderIndex->get(['id'=>$orderId]);
|
|
if(empty($orderIndexData)){
|
|
if(empty($orderIndexData)){
|
|
- return false;
|
|
|
|
|
|
+ return ["code"=>-1,"msg"=>"订单索引数据不存在"];
|
|
}
|
|
}
|
|
// 切换订单分表,查询订单主单据数据
|
|
// 切换订单分表,查询订单主单据数据
|
|
$dbOrder = new DOrder('default');
|
|
$dbOrder = new DOrder('default');
|
|
@@ -215,7 +292,7 @@ class PartnerTools{
|
|
$dbOrder->setTable('qianniao_order_' . $this->onlineEnterpriseId . '_' . $fix);
|
|
$dbOrder->setTable('qianniao_order_' . $this->onlineEnterpriseId . '_' . $fix);
|
|
$orderData = $dbOrder->get(['id' => $orderId]);
|
|
$orderData = $dbOrder->get(['id' => $orderId]);
|
|
if(empty($orderData)){
|
|
if(empty($orderData)){
|
|
- return false;
|
|
|
|
|
|
+ return ["code"=>-1,"msg"=>"订单数据不存在"];
|
|
}
|
|
}
|
|
//开始计算
|
|
//开始计算
|
|
$isPart=false;//订单用户如果是合伙人是否计算
|
|
$isPart=false;//订单用户如果是合伙人是否计算
|
|
@@ -223,36 +300,35 @@ class PartnerTools{
|
|
$orderMoney = $orderData["payAmount'"];
|
|
$orderMoney = $orderData["payAmount'"];
|
|
//只计算小程序已完成订单
|
|
//只计算小程序已完成订单
|
|
if(empty($orderCustomerId) || empty($orderMoney) || $orderData["payType"]!=1 || $orderData["source"]!=3 || $orderData["orderStatus"]!=5){
|
|
if(empty($orderCustomerId) || empty($orderMoney) || $orderData["payType"]!=1 || $orderData["source"]!=3 || $orderData["orderStatus"]!=5){
|
|
- return false;
|
|
|
|
|
|
+ return ["code"=>-1,"msg"=>"只计算小程序微信支付已完成订单"];
|
|
}
|
|
}
|
|
$customerData = $this->dbCustomer->get($orderCustomerId);
|
|
$customerData = $this->dbCustomer->get($orderCustomerId);
|
|
if (empty($customerData) || empty($customerData["parentPath"])) {
|
|
if (empty($customerData) || empty($customerData["parentPath"])) {
|
|
- return false;
|
|
|
|
|
|
+ return ["code"=>-1,"msg"=>"用户不存在或不存在父级"];
|
|
}
|
|
}
|
|
//订单用户如果是合伙人消费则不计算收益
|
|
//订单用户如果是合伙人消费则不计算收益
|
|
if(!$isPart && $customerData["isPartner"]==1){
|
|
if(!$isPart && $customerData["isPartner"]==1){
|
|
- return false;
|
|
|
|
|
|
+ return ["code"=>-1,"msg"=>"合伙人消费不计算上级收益"];
|
|
}
|
|
}
|
|
$data=[];
|
|
$data=[];
|
|
$nowTime = time();
|
|
$nowTime = time();
|
|
//计算低层收益
|
|
//计算低层收益
|
|
$bottomData = $this->calcMoney($customerData,$orderMoney);
|
|
$bottomData = $this->calcMoney($customerData,$orderMoney);
|
|
if(empty($bottomData)){
|
|
if(empty($bottomData)){
|
|
- return false;
|
|
|
|
|
|
+ return ["code"=>-1,"msg"=>"上级合伙人不存在"];
|
|
}
|
|
}
|
|
//合伙人账户
|
|
//合伙人账户
|
|
$bottomPartnerData = $this->getPartnerData($bottomData["parentData"]["id"], $bottomData["parentData"]["userCenterId"]);
|
|
$bottomPartnerData = $this->getPartnerData($bottomData["parentData"]["id"], $bottomData["parentData"]["userCenterId"]);
|
|
if(empty($bottomPartnerData)){
|
|
if(empty($bottomPartnerData)){
|
|
- return false;
|
|
|
|
|
|
+ return ["code"=>-1,"msg"=>"上级合伙人账户错误"];
|
|
}
|
|
}
|
|
//计算顶层收益
|
|
//计算顶层收益
|
|
$topData = $this->calcMoney($bottomData["parentData"],$bottomData["commission"]);
|
|
$topData = $this->calcMoney($bottomData["parentData"],$bottomData["commission"]);
|
|
if(!empty($topData)){
|
|
if(!empty($topData)){
|
|
$topPartnerData = $this->getPartnerData($topData["parentData"]["id"], $topData["parentData"]["userCenterId"]);
|
|
$topPartnerData = $this->getPartnerData($topData["parentData"]["id"], $topData["parentData"]["userCenterId"]);
|
|
if(empty($topPartnerData)){
|
|
if(empty($topPartnerData)){
|
|
- return false;
|
|
|
|
|
|
+ return ["code"=>-1,"msg"=>"顶级合伙人账户错误"];
|
|
}
|
|
}
|
|
-
|
|
|
|
//记录顶层收益
|
|
//记录顶层收益
|
|
$data[]=[
|
|
$data[]=[
|
|
"orderMoney"=>$orderMoney,//订单支付金额
|
|
"orderMoney"=>$orderMoney,//订单支付金额
|
|
@@ -266,7 +342,7 @@ class PartnerTools{
|
|
"type"=>1,//1表示子级合伙人收益计算收益,0表示子级消费计算收益
|
|
"type"=>1,//1表示子级合伙人收益计算收益,0表示子级消费计算收益
|
|
"orderId"=>$orderData["id"],
|
|
"orderId"=>$orderData["id"],
|
|
"status"=>0,
|
|
"status"=>0,
|
|
- "title"=>"合伙人收益分佣",
|
|
|
|
|
|
+ "title"=>"子级合伙人收益分佣",
|
|
"isUpgrade"=>$topData["isUpgrade"],
|
|
"isUpgrade"=>$topData["isUpgrade"],
|
|
"mono"=>"",
|
|
"mono"=>"",
|
|
"time"=>$nowTime,
|
|
"time"=>$nowTime,
|
|
@@ -284,32 +360,81 @@ class PartnerTools{
|
|
"per"=>$bottomData["per"],//佣金比例
|
|
"per"=>$bottomData["per"],//佣金比例
|
|
"type"=>0,
|
|
"type"=>0,
|
|
"orderId"=>$orderData["id"],
|
|
"orderId"=>$orderData["id"],
|
|
- "status"=>0,
|
|
|
|
- "title"=>"子级消费分佣",
|
|
|
|
|
|
+ "status"=>0,//暂时不需要
|
|
|
|
+ "title"=>"子级用户消费分佣",
|
|
"isUpgrade"=>$bottomData["isUpgrade"],
|
|
"isUpgrade"=>$bottomData["isUpgrade"],
|
|
"mono"=>"",
|
|
"mono"=>"",
|
|
"time"=>$nowTime,
|
|
"time"=>$nowTime,
|
|
- "partnerId"=>$bottomPartnerData["id"],
|
|
|
|
];
|
|
];
|
|
$num=0;
|
|
$num=0;
|
|
|
|
+ //开启事务
|
|
|
|
+ $this->dbNewCommissionDetail->beginTransaction();
|
|
|
|
+ $lms = "";
|
|
foreach($data as $k=>$v){
|
|
foreach($data as $k=>$v){
|
|
$count = $this->dbNewCommissionDetail->count(["orderId"=>$v["orderId"],"partnerId"=>$v["partnerId"]]);
|
|
$count = $this->dbNewCommissionDetail->count(["orderId"=>$v["orderId"],"partnerId"=>$v["partnerId"]]);
|
|
if($count>0){
|
|
if($count>0){
|
|
|
|
+ $lms.="当前订单已计算佣金[{$v['orderId']}];";
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- $id = $this->dbNewCommissionDetail->insert($v);
|
|
|
|
- if(!empty($id)){
|
|
|
|
- $num++;
|
|
|
|
|
|
+ $nid = $this->dbNewCommissionDetail->insert($v);
|
|
|
|
+ if(empty($nid)){
|
|
|
|
+ $lms.="佣金明细插入失败[{$v['orderId']}];";
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ //查询合伙人账户
|
|
|
|
+ $partnerItem = $this->dbNewCommissionPartner->get(["id"=>$v["partnerId"]]);
|
|
|
|
+ if(empty($partnerItem)){
|
|
|
|
+ $num = 0;
|
|
|
|
+ $lms.="合伙人账户不存在[{$v['orderId']}];";
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ $update = [
|
|
|
|
+ 'balance' => bcadd($partnerItem['balance'], $v["commission"], 2),
|
|
|
|
+ 'totalMoney' => bcadd($partnerItem['totalMoney'], $v["commission"], 2),
|
|
|
|
+ 'updateTime' => $nowTime,
|
|
|
|
+ ];
|
|
|
|
+ //添加账户余额明细记录
|
|
|
|
+ $dres = $this->dbNewCommissionBalanceDetail->insert([
|
|
|
|
+ "type"=>1,
|
|
|
|
+ "title"=>$v["title"],
|
|
|
|
+ "code"=>$v["type"]==1 ? "order_calc" : "partner_calc",
|
|
|
|
+ "money"=>$v["commission"],
|
|
|
|
+ "content"=>$v["title"],
|
|
|
|
+ "admin_id"=>0,
|
|
|
|
+ "customer_id"=>$v["customerId"],
|
|
|
|
+ "partner_id"=>$v["partnerId"],
|
|
|
|
+ "user_center_id"=>$v["userCenterId"],
|
|
|
|
+ "cash_id"=>0,
|
|
|
|
+ "old_balance"=>$partnerItem["balance"],
|
|
|
|
+ "now_balance"=>$update["balance"],
|
|
|
|
+ "time"=>$nowTime,
|
|
|
|
+ "expand"=>"",
|
|
|
|
+ "detail_id"=>$nid,
|
|
|
|
+ ]);
|
|
|
|
+ if(empty($dres)){
|
|
|
|
+ $num = 0;
|
|
|
|
+ $lms.="账户余额明细插入失败[{$v['orderId']}];";
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
|
|
+ //更新账户余额
|
|
|
|
+ $upRes = $this->dbNewCommissionPartner->update($update, ['id' => $v["partnerId"]]);
|
|
|
|
+ if(empty($upRes)){
|
|
|
|
+ $num = 0;
|
|
|
|
+ $lms.="更新账户余额失败[{$v['orderId']}];";
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ $num++;
|
|
}
|
|
}
|
|
if($num<=0){
|
|
if($num<=0){
|
|
- return false;
|
|
|
|
|
|
+ $this->dbNewCommissionDetail->rollBack();
|
|
|
|
+ return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
|
|
}
|
|
}
|
|
|
|
+ $this->dbNewCommissionDetail->commit();
|
|
//佣金更新完后验证设置合伙人
|
|
//佣金更新完后验证设置合伙人
|
|
if($bottomData["isUpgrade"]==1){
|
|
if($bottomData["isUpgrade"]==1){
|
|
$this->setPartner($bottomData["childId"]);
|
|
$this->setPartner($bottomData["childId"]);
|
|
}
|
|
}
|
|
- return true;
|
|
|
|
|
|
+ return ["code"=>1,"msg"=>"执行成功".$lms];
|
|
}
|
|
}
|
|
|
|
|
|
public function getPartnerData($customerId,$userCenterId){
|
|
public function getPartnerData($customerId,$userCenterId){
|