|
@@ -27,8 +27,13 @@ class PartnerTools{
|
|
|
private $enterpriseId;
|
|
|
private $cache;
|
|
|
private $cutTable = 1;//客户按照企业id分表
|
|
|
- private $errorMsg = "";
|
|
|
|
|
|
+ private $levMoney = 20000;//升级条件
|
|
|
+ private $levPer = 0.05;//升级佣金比例
|
|
|
+ private $incomePer = 0.1;//佣金比例
|
|
|
+ private $errorMsg = "";
|
|
|
+
|
|
|
+
|
|
|
private $codeAr = [
|
|
|
"order_calc"=>"子级用户消费分佣",
|
|
|
"partner_calc"=>"子级合伙人收益分佣",
|
|
@@ -74,6 +79,15 @@ class PartnerTools{
|
|
|
$newCommissionTaskTableName = $this->dbNewCommissionTask->getTableName($this->dbNewCommissionTask->get_Table(), $this->enterpriseId, $this->cutTable);
|
|
|
$this->dbNewCommissionTask->setTable($newCommissionTaskTableName);
|
|
|
}
|
|
|
+ public function cacheTest(){
|
|
|
+ $key = "partnertaskcache".'::'.$this->enterpriseId;
|
|
|
+ $result = $this->cache->get($key);
|
|
|
+ if(!empty($result)){
|
|
|
+ return "重复操作";
|
|
|
+ }
|
|
|
+ $this->cache->set($key,1,10);
|
|
|
+ return "开始执行操作";
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 添加任务
|
|
@@ -83,16 +97,17 @@ class PartnerTools{
|
|
|
*/
|
|
|
public static function addTask($orderId,$enterpriseId){
|
|
|
if(empty($orderId) || empty($enterpriseId)){
|
|
|
- return false;
|
|
|
+
|
|
|
+ return ["code"=>-1,"msg"=>"参数为空"];
|
|
|
}
|
|
|
$dbTask = new DNewCommissionTask('default');
|
|
|
$taskTableName = $dbTask->getTableName($dbTask->get_Table(), $enterpriseId, 1);
|
|
|
$dbTask->setTable($taskTableName);
|
|
|
- $count = $taskTableName->count(["orderId"=>$orderId]);
|
|
|
+ $count = $dbTask->count(["orderId"=>$orderId]);
|
|
|
if($count>0){
|
|
|
- return false;
|
|
|
+ return ["code"=>-1,"msg"=>"任务订单已存在"];
|
|
|
}
|
|
|
- $dbTask->insert([
|
|
|
+ $r = $dbTask->insert([
|
|
|
"orderId"=>$orderId,
|
|
|
"enterpriseId"=>$enterpriseId,
|
|
|
"title"=>"订单完成计算分佣",
|
|
@@ -101,6 +116,7 @@ class PartnerTools{
|
|
|
"run_time"=>0,
|
|
|
"time"=>time(),
|
|
|
]);
|
|
|
+ return ["code"=>1,"msg"=>"任务已插入[{$r}]"];
|
|
|
}
|
|
|
|
|
|
public function runTask(){
|
|
@@ -108,15 +124,15 @@ class PartnerTools{
|
|
|
$key = "partnertaskcache".'::'.$this->enterpriseId;
|
|
|
$result = $this->cache->get($key);
|
|
|
if(!empty($result)){
|
|
|
- return false;
|
|
|
+ return ["code"=>-1,"msg"=>"操作频繁"];
|
|
|
}
|
|
|
$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]);
|
|
|
+ $data = $dbTask->select(["status"=>0],"*","id asc");
|
|
|
if(empty($data)){
|
|
|
- return false;
|
|
|
+ return ["code"=>-1,"msg"=>"没有可执行任务"];
|
|
|
}
|
|
|
$nowTime=time();
|
|
|
foreach($data as $k=>$v){
|
|
@@ -129,6 +145,8 @@ class PartnerTools{
|
|
|
}
|
|
|
$dbTask->update($save, ["id"=>$v["id"]]);
|
|
|
}
|
|
|
+ $this->cache->set($key,null);
|
|
|
+ return ["code"=>1,"msg"=>"执行结束"];
|
|
|
|
|
|
}
|
|
|
|
|
@@ -142,17 +160,17 @@ class PartnerTools{
|
|
|
*/
|
|
|
public function setPushCustomer($parentId=0,$childId=0){
|
|
|
if($parentId == 0 || $childId == 0 || $parentId == $childId){
|
|
|
- return false;
|
|
|
+ return ["code"=>-1,"msg"=>"参数错误","data"=>null];
|
|
|
}
|
|
|
//子级用户信息
|
|
|
$childData = $this->dbCustomer->get($childId);
|
|
|
if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
|
|
|
- return false;
|
|
|
+ return ["code"=>-1,"msg"=>"用户不能被绑定","data"=>$childData];
|
|
|
}
|
|
|
//父级用户信息
|
|
|
$parentData = $this->dbCustomer->get($parentId);
|
|
|
if(empty($parentData)){
|
|
|
- return false;
|
|
|
+ return ["code"=>-1,"msg"=>"父级用户不存在","data"=>null];
|
|
|
}
|
|
|
$beginStatus = $this->dbCustomer->beginTransaction();
|
|
|
//保存父级数据
|
|
@@ -161,7 +179,7 @@ class PartnerTools{
|
|
|
$pres = $this->dbCustomer->update($parentSave, ["id"=>$parentId]);
|
|
|
if(empty($pres)){
|
|
|
$this->dbCustomer->rollBack();
|
|
|
- return false;
|
|
|
+ return ["code"=>-1,"msg"=>"父级数据保存失败01","data"=>$parentSave];
|
|
|
}
|
|
|
}
|
|
|
//保存子级数据
|
|
@@ -170,11 +188,11 @@ class PartnerTools{
|
|
|
$res = $this->dbCustomer->update($childSave, ["id"=>$childId]);
|
|
|
if(empty($res)){
|
|
|
$this->dbCustomer->rollBack();
|
|
|
- return false;
|
|
|
+ return ["code"=>-1,"msg"=>"绑定失败","data"=>$childSave];
|
|
|
}else{
|
|
|
$beginStatus && $this->dbCustomer->commit();
|
|
|
// $this->delCustomerCache();
|
|
|
- return true;
|
|
|
+ return ["code"=>1,"msg"=>"绑定成功","data"=>null];
|
|
|
}
|
|
|
}
|
|
|
/**
|
|
@@ -211,15 +229,16 @@ class PartnerTools{
|
|
|
if (empty($userData) || empty($userData["parentPath"])) {
|
|
|
return false;
|
|
|
}
|
|
|
- $where["isPartner"]=1;
|
|
|
- $where["id"]=["in",trim($userData["parentPath"])];
|
|
|
+ //逆序祖先,从最近的祖先先查
|
|
|
+ $pathData = array_reverse(explode(",", trim($userData["parentPath"])));
|
|
|
+ $where=[];
|
|
|
+ $where[]=["isPartner","=",1];
|
|
|
+ $where[]=["id","in",$pathData];
|
|
|
$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){
|
|
@@ -249,12 +268,16 @@ class PartnerTools{
|
|
|
public function calcMoney($customerData,$money){
|
|
|
$parentData = $this->getParentPartner($customerData);
|
|
|
if(empty($parentData) || $parentData["isPartner"]==0){
|
|
|
+ //上级合伙人不存在
|
|
|
+ if($money>=$this->levMoney && $customerData["isPartner"]==0){
|
|
|
+ $this->setPartner($customerData["id"]);
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
- $per = 0.1;//佣金比例
|
|
|
+ $per = $this->incomePer;
|
|
|
$isUpgrade = 0;
|
|
|
- if($money>=20000 && $customerData["isPartner"]==0){
|
|
|
- $per = 0.05;
|
|
|
+ if($money>=$this->levMoney && $customerData["isPartner"]==0){
|
|
|
+ $per = $this->levPer;
|
|
|
$isUpgrade = 1;
|
|
|
}
|
|
|
$resData=[
|
|
@@ -289,7 +312,7 @@ class PartnerTools{
|
|
|
// 切换订单分表,查询订单主单据数据
|
|
|
$dbOrder = new DOrder('default');
|
|
|
$fix = ceil($orderIndexData['userCenterId'] / 200000);
|
|
|
- $dbOrder->setTable('qianniao_order_' . $this->onlineEnterpriseId . '_' . $fix);
|
|
|
+ $dbOrder->setTable('qianniao_order_' . $this->enterpriseId . '_' . $fix);
|
|
|
$orderData = $dbOrder->get(['id' => $orderId]);
|
|
|
if(empty($orderData)){
|
|
|
return ["code"=>-1,"msg"=>"订单数据不存在"];
|
|
@@ -297,14 +320,20 @@ class PartnerTools{
|
|
|
//开始计算
|
|
|
$isPart=false;//订单用户如果是合伙人是否计算
|
|
|
$orderCustomerId = $orderData["customerId"];
|
|
|
- $orderMoney = $orderData["payAmount'"];
|
|
|
+ $orderMoney = $orderData["payAmount"];
|
|
|
//只计算小程序已完成订单
|
|
|
if(empty($orderCustomerId) || empty($orderMoney) || $orderData["payType"]!=1 || $orderData["source"]!=3 || $orderData["orderStatus"]!=5){
|
|
|
return ["code"=>-1,"msg"=>"只计算小程序微信支付已完成订单"];
|
|
|
}
|
|
|
$customerData = $this->dbCustomer->get($orderCustomerId);
|
|
|
- if (empty($customerData) || empty($customerData["parentPath"])) {
|
|
|
- return ["code"=>-1,"msg"=>"用户不存在或不存在父级"];
|
|
|
+ if (empty($customerData)) {
|
|
|
+ return ["code"=>-1,"msg"=>"用户不存在"];
|
|
|
+ }
|
|
|
+ if(empty($customerData["parentPath"])){
|
|
|
+ if($orderMoney>=$this->levMoney && $customerData["isPartner"]==0){
|
|
|
+ $this->setPartner($orderCustomerId);
|
|
|
+ }
|
|
|
+ return ["code"=>-1,"msg"=>"用户不存在父级"];
|
|
|
}
|
|
|
//订单用户如果是合伙人消费则不计算收益
|
|
|
if(!$isPart && $customerData["isPartner"]==1){
|
|
@@ -322,6 +351,27 @@ class PartnerTools{
|
|
|
if(empty($bottomPartnerData)){
|
|
|
return ["code"=>-1,"msg"=>"上级合伙人账户错误"];
|
|
|
}
|
|
|
+
|
|
|
+ //记录底层收益
|
|
|
+ $data[]=[
|
|
|
+ "orderMoney"=>$orderMoney,//订单支付金额
|
|
|
+ "calcMoney"=>$bottomData["money"],//佣金计算金额
|
|
|
+ "sourceCustomerId"=>$bottomData["childId"],//来源客户id
|
|
|
+ "partnerId"=>$bottomPartnerData["id"],//收钱合伙人账号id
|
|
|
+ "customerId"=>$bottomPartnerData["customerId"],//收钱合伙人
|
|
|
+ "userCenterId"=>$bottomPartnerData["userCenterId"],//收钱合伙人
|
|
|
+ "commission"=>$bottomData["commission"],//佣金金额
|
|
|
+ "per"=>$bottomData["per"],//佣金比例
|
|
|
+ "type"=>0,
|
|
|
+ "orderId"=>$orderData["id"],
|
|
|
+ "status"=>0,//暂时不需要
|
|
|
+ "title"=>"子级用户消费分佣",
|
|
|
+ "isUpgrade"=>$bottomData["isUpgrade"],
|
|
|
+ "mono"=>"",
|
|
|
+ "time"=>$nowTime,
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
//计算顶层收益
|
|
|
$topData = $this->calcMoney($bottomData["parentData"],$bottomData["commission"]);
|
|
|
if(!empty($topData)){
|
|
@@ -348,24 +398,7 @@ class PartnerTools{
|
|
|
"time"=>$nowTime,
|
|
|
];
|
|
|
}
|
|
|
- //记录底层收益
|
|
|
- $data[]=[
|
|
|
- "orderMoney"=>$orderMoney,//订单支付金额
|
|
|
- "calcMoney"=>$bottomData["money"],//佣金计算金额
|
|
|
- "sourceCustomerId"=>$bottomData["childId"],//来源客户id
|
|
|
- "partnerId"=>$bottomPartnerData["id"],//收钱合伙人账号id
|
|
|
- "customerId"=>$bottomPartnerData["customerId"],//收钱合伙人
|
|
|
- "userCenterId"=>$bottomPartnerData["userCenterId"],//收钱合伙人
|
|
|
- "commission"=>$bottomData["commission"],//佣金金额
|
|
|
- "per"=>$bottomData["per"],//佣金比例
|
|
|
- "type"=>0,
|
|
|
- "orderId"=>$orderData["id"],
|
|
|
- "status"=>0,//暂时不需要
|
|
|
- "title"=>"子级用户消费分佣",
|
|
|
- "isUpgrade"=>$bottomData["isUpgrade"],
|
|
|
- "mono"=>"",
|
|
|
- "time"=>$nowTime,
|
|
|
- ];
|
|
|
+
|
|
|
$num=0;
|
|
|
//开启事务
|
|
|
$this->dbNewCommissionDetail->beginTransaction();
|
|
@@ -397,7 +430,7 @@ class PartnerTools{
|
|
|
$dres = $this->dbNewCommissionBalanceDetail->insert([
|
|
|
"type"=>1,
|
|
|
"title"=>$v["title"],
|
|
|
- "code"=>$v["type"]==1 ? "order_calc" : "partner_calc",
|
|
|
+ "code"=>$v["type"]==1 ? "partner_calc" : "order_calc",
|
|
|
"money"=>$v["commission"],
|
|
|
"content"=>$v["title"],
|
|
|
"admin_id"=>0,
|