PartnerTools.Class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace Util\Common;
  3. use JinDouYun\Dao\Customer\DCustomer;
  4. use JinDouYun\Cache\CustomerCache;
  5. use JinDouYun\Dao\Order\DOrderIndex;
  6. use JinDouYun\Dao\Order\DOrder;
  7. use JinDouYun\Dao\CommissionPartner\DNewCommissionBalanceDetail;
  8. use JinDouYun\Dao\CommissionPartner\DNewCommissionCash;
  9. use JinDouYun\Dao\CommissionPartner\DNewCommissionDetail;
  10. use JinDouYun\Dao\CommissionPartner\DNewCommissionPartner;
  11. class PartnerTools{
  12. private $dbCustomer;
  13. private $dbNewCommissionPartner;
  14. private $dbNewCommissionDetail;
  15. private $dbNewCommissionCash;
  16. private $dbNewCommissionBalanceDetail;
  17. private $enterpriseId;
  18. private $cutTable = 1;//客户按照企业id分表
  19. private $errorMsg = "";
  20. public function __construct($enterpriseId){
  21. $this->enterpriseId = $enterpriseId;
  22. //用户表
  23. $this->dbCustomer = new DCustomer('default');
  24. $customerTableName = $this->dbCustomer->getTableName($this->dbCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
  25. $this->dbCustomer->setTable($customerTableName);
  26. //新分销合伙人表
  27. $this->dbNewCommissionPartner = new DNewCommissionPartner('default');
  28. $newCommissionPartnerTableName = $this->dbNewCommissionPartner->getTableName($this->dbNewCommissionPartner->get_Table(), $this->enterpriseId, $this->cutTable);
  29. $this->dbNewCommissionPartner->setTable($newCommissionPartnerTableName);
  30. //新分销佣金明细表
  31. $this->dbNewCommissionDetail = new DNewCommissionDetail('default');
  32. $newCommissionDetailTableName = $this->dbNewCommissionDetail->getTableName($this->dbNewCommissionDetail->get_Table(), $this->enterpriseId, $this->cutTable);
  33. $this->dbNewCommissionDetail->setTable($newCommissionDetailTableName);
  34. //新分销合伙人提现表
  35. $this->dbNewCommissionCash = new DNewCommissionCash('default');
  36. $newCommissionCashTableName = $this->dbNewCommissionCash->getTableName($this->dbNewCommissionCash->get_Table(), $this->enterpriseId, $this->cutTable);
  37. $this->dbNewCommissionCash->setTable($newCommissionCashTableName);
  38. //新分销合伙人余额明细表
  39. $this->dbNewCommissionBalanceDetail = new DNewCommissionBalanceDetail('default');
  40. $newCommissionBalanceDetailTableName = $this->dbNewCommissionBalanceDetail->getTableName($this->dbNewCommissionBalanceDetail->get_Table(), $this->enterpriseId, $this->cutTable);
  41. $this->dbNewCommissionBalanceDetail->setTable($newCommissionBalanceDetailTableName);
  42. }
  43. /**
  44. * 设置推广用户
  45. * @param type $parentId
  46. * @param type $childId
  47. * @return bool
  48. */
  49. public function setPushCustomer($parentId=0,$childId=0){
  50. if($parentId == 0 || $childId == 0 || $parentId == $childId){
  51. return false;
  52. }
  53. //子级用户信息
  54. $childData = $this->dbCustomer->get($childId);
  55. if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
  56. return false;
  57. }
  58. //父级用户信息
  59. $parentData = $this->dbCustomer->get($parentId);
  60. if(empty($parentData)){
  61. return false;
  62. }
  63. $beginStatus = $this->dbCustomer->beginTransaction();
  64. //保存父级数据
  65. if($parentData["isParentHead"] == 0){
  66. $parentSave=["isParentHead"=>1];
  67. $pres = $this->dbCustomer->update($parentSave, ["id"=>$parentId]);
  68. if(empty($pres)){
  69. $this->dbCustomer->rollBack();
  70. return false;
  71. }
  72. }
  73. //保存子级数据
  74. $parentPath = empty($parentData["parentPath"]) ? $parentId : $parentData["parentPath"] . "," . $parentId;
  75. $childSave=["parentId"=>$parentId,"parentPath"=>$parentPath];
  76. $res = $this->dbCustomer->update($childSave, ["id"=>$childId]);
  77. if(empty($res)){
  78. $this->dbCustomer->rollBack();
  79. return false;
  80. }else{
  81. $beginStatus && $this->dbCustomer->commit();
  82. // $this->delCustomerCache();
  83. return true;
  84. }
  85. }
  86. /**
  87. * 设置用户为合伙人
  88. * @param type $customerId
  89. */
  90. public function setPartner($customerId){
  91. $userData = $this->dbCustomer->get($customerId);
  92. if (empty($userData)) {
  93. return false;
  94. }
  95. if($userData["isPartner"]==1){
  96. return true;
  97. }
  98. $save=["isPartner"=>1];
  99. $res = $this->dbCustomer->update($save,["id"=>$customerId]);
  100. if(empty($res)){
  101. return false;
  102. }else{
  103. return true;
  104. }
  105. }
  106. /**
  107. * 获取用户父级合伙人
  108. * @param type $customerId
  109. * @return bool
  110. */
  111. public function getParentPartner($userData){
  112. if (empty($userData) || empty($userData["parentPath"])) {
  113. return false;
  114. }
  115. $where["isPartner"]=1;
  116. $where["id"]=["in",trim($userData["parentPath"])];
  117. $parentData = $this->dbCustomer->select($where);
  118. if(empty($parentData)){
  119. return false;
  120. }
  121. $parentPartner = null;
  122. //逆序祖先,从最近的祖先先查
  123. $pathData = array_reverse(explode(",", trim($userData["parentPath"])));
  124. for($i=0;$i<count($pathData);$i++){
  125. $itemData = [];
  126. foreach($parentData as $k=>$v){
  127. if($pathData[$i]==$v["id"]){
  128. $itemData = $v;
  129. break;
  130. }
  131. }
  132. if(!empty($itemData) && $itemData["isPartner"]==1){
  133. $parentPartner = $itemData;
  134. break;
  135. }
  136. }
  137. if(empty($parentPartner)){
  138. return false;
  139. }
  140. return $parentPartner;
  141. }
  142. /**
  143. * 佣金计算
  144. * @param type $customerId
  145. * @param type $money
  146. * @param type $type 1表示子级消费,2表示合伙人收益
  147. * @return bool
  148. */
  149. public function calcMoney($customerData,$money){
  150. $parentData = $this->getParentPartner($customerData);
  151. if(empty($parentData) || $parentData["isPartner"]==0){
  152. return false;
  153. }
  154. $per = 0.1;//佣金比例
  155. $isUpgrade = 0;
  156. if($money>=20000 && $customerData["isPartner"]==0){
  157. $per = 0.05;
  158. $isUpgrade = 1;
  159. }
  160. $resData=[
  161. "childId"=>$customerData["id"],
  162. "childData"=>$customerData,
  163. "parentId"=>$parentData["id"],
  164. "parentData"=>$parentData,
  165. "per"=>$per,
  166. "money"=>$money,
  167. "commission"=>$money * $per,
  168. "isUpgrade"=>$isUpgrade
  169. ];
  170. return $resData;
  171. }
  172. /**
  173. * 订单消费计算佣金
  174. * @param type $orderCustomerId
  175. * @param type $orderMoney
  176. * @param type $isPart 订单用户如果是合伙人是否计算
  177. * @return bool
  178. */
  179. public function addCalcMoneyData($orderId){
  180. //获取订单信息
  181. if(empty($orderId)){
  182. return false;
  183. }
  184. $dbOrderIndex = new DOrderIndex();
  185. $dbOrderIndex->setTable('qianniao_order_index_' . $this->enterpriseId);
  186. $orderIndexData = $dbOrderIndex->get(['id'=>$orderId]);
  187. if(empty($orderIndexData)){
  188. return false;
  189. }
  190. // 切换订单分表,查询订单主单据数据
  191. $dbOrder = new DOrder('default');
  192. $fix = ceil($orderIndexData['userCenterId'] / 200000);
  193. $dbOrder->setTable('qianniao_order_' . $this->onlineEnterpriseId . '_' . $fix);
  194. $orderData = $dbOrder->get(['id' => $orderId]);
  195. if(empty($orderData)){
  196. return false;
  197. }
  198. //开始计算
  199. $isPart=false;//订单用户如果是合伙人是否计算
  200. $orderCustomerId = $orderData["customerId"];
  201. $orderMoney = $orderData["payAmount'"];
  202. //只计算小程序已完成订单
  203. if(empty($orderCustomerId) || empty($orderMoney) || $orderData["payType"]!=1 || $orderData["source"]!=3 || $orderData["orderStatus"]!=5){
  204. return false;
  205. }
  206. $customerData = $this->dbCustomer->get($orderCustomerId);
  207. if (empty($customerData) || empty($customerData["parentPath"])) {
  208. return false;
  209. }
  210. //订单用户如果是合伙人消费则不计算收益
  211. if(!$isPart && $customerData["isPartner"]==1){
  212. return false;
  213. }
  214. $data=[];
  215. $nowTime = time();
  216. //计算低层收益
  217. $bottomData = $this->calcMoney($customerData,$orderMoney);
  218. if(empty($bottomData)){
  219. return false;
  220. }
  221. //计算顶层收益
  222. $topData = $this->calcMoney($bottomData["parentData"],$bottomData["commission"]);
  223. if(!empty($topData)){
  224. //记录顶层收益
  225. $data[]=[
  226. "orderMoney"=>$orderMoney,//订单支付金额
  227. "calcMoney"=>$topData["money"],//佣金计算金额
  228. "sourceCustomerId"=>$topData["childId"],//来源客户id
  229. "partnerId"=>$topData["parentId"],//收钱合伙人id
  230. "commission"=>$topData["commission"],//佣金金额
  231. "per"=>$topData["per"],//佣金比例
  232. "type"=>1,//1表示子级合伙人收益计算收益,0表示子级消费计算收益
  233. "orderId"=>$orderData["id"],
  234. "status"=>0,
  235. "title"=>"合伙人收益分佣",
  236. "isUpgrade"=>$topData["isUpgrade"],
  237. "mono"=>"",
  238. "time"=>$nowTime,
  239. ];
  240. }
  241. //记录底层收益
  242. $data[]=[
  243. "orderMoney"=>$orderMoney,//订单支付金额
  244. "calcMoney"=>$bottomData["money"],//佣金计算金额
  245. "sourceCustomerId"=>$bottomData["childId"],//来源客户id
  246. "partnerId"=>$bottomData["parentId"],//收钱合伙人id
  247. "commission"=>$bottomData["commission"],//佣金金额
  248. "per"=>$bottomData["per"],//佣金比例
  249. "type"=>0,
  250. "orderId"=>$orderData["id"],
  251. "status"=>0,
  252. "title"=>"子级消费分佣",
  253. "isUpgrade"=>$bottomData["isUpgrade"],
  254. "mono"=>"",
  255. "time"=>$nowTime,
  256. ];
  257. //添加佣金余额和明细
  258. //可以新建一个佣金余额表newCommissionPartner
  259. //newCommissionDetail9
  260. //佣金更新完后验证设置合伙人
  261. if($bottomData["isUpgrade"]==1){
  262. $this->setPartner($bottomData["childId"]);
  263. }
  264. return $data;
  265. }
  266. /**
  267. * 删除用户缓存
  268. * @param type $customerId
  269. * @param type $userCenterId
  270. */
  271. public function delCustomerCache($customerId,$userCenterId){
  272. $objCustomerCache = new CustomerCache();
  273. $objCustomerCache->delCustomerData($this->enterpriseId, $customerId);
  274. $objCustomerCache->delCustomerUserData($this->enterpriseId, $userCenterId);
  275. }
  276. }