PartnerTools.Class.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Util\Common;
  3. use JinDouYun\Dao\Customer\DCustomer;
  4. use JinDouYun\Cache\CustomerCache;
  5. class PartnerTools{
  6. private $dbCustomer;
  7. private $enterpriseId;
  8. private $cutTable = 1;//客户按照企业id分表
  9. private $errorMsg = "";
  10. public function __construct($enterpriseId){
  11. $this->enterpriseId = $enterpriseId;
  12. $this->dbCustomer = new DCustomer('default');
  13. $tableName = $this->dbCustomer->getTableName($this->dbCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
  14. $this->dbCustomer->setTable($tableName);
  15. }
  16. /**
  17. * 设置推广用户
  18. * @param type $parentId
  19. * @param type $childId
  20. * @return bool
  21. */
  22. public function setPushCustomer($parentId=0,$childId=0){
  23. if($parentId == 0 || $childId == 0 || $parentId == $childId){
  24. return false;
  25. }
  26. //子级用户信息
  27. $childData = $this->dbCustomer->get($childId);
  28. if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
  29. return false;
  30. }
  31. //父级用户信息
  32. $parentData = $this->dbCustomer->get($parentId);
  33. if(empty($parentData)){
  34. return false;
  35. }
  36. $beginStatus = $this->dbCustomer->beginTransaction();
  37. //保存父级数据
  38. if($parentData["isParentHead"] == 0){
  39. $parentSave=["isParentHead"=>1];
  40. $pres = $this->dbCustomer->update($parentSave, ["id"=>$parentId]);
  41. if(empty($pres)){
  42. $this->dbCustomer->rollBack();
  43. return false;
  44. }
  45. }
  46. //保存子级数据
  47. $parentPath = empty($parentData["parentPath"]) ? $parentId : $parentData["parentPath"] . "," . $parentId;
  48. $childSave=["parentId"=>$parentId,"parentPath"=>$parentPath];
  49. $res = $this->dbCustomer->update($childSave, ["id"=>$childId]);
  50. if(empty($res)){
  51. $this->dbCustomer->rollBack();
  52. return false;
  53. }else{
  54. $beginStatus && $this->dbCustomer->commit();
  55. // $this->delCustomerCache();
  56. return true;
  57. }
  58. }
  59. /**
  60. * 设置用户为合伙人
  61. * @param type $customerId
  62. */
  63. public function setPartner($customerId){
  64. $userData = $this->dbCustomer->get($customerId);
  65. if (empty($userData)) {
  66. return false;
  67. }
  68. if($userData["isPartner"]==1){
  69. return true;
  70. }
  71. $save=["isPartner"=>1];
  72. $res = $this->dbCustomer->update($save,["id"=>$customerId]);
  73. if(empty($res)){
  74. return false;
  75. }else{
  76. return true;
  77. }
  78. }
  79. /**
  80. * 获取用户父级合伙人
  81. * @param type $customerId
  82. * @return bool
  83. */
  84. public function getParentPartner($userData){
  85. if (empty($userData) || empty($userData["parentPath"])) {
  86. return false;
  87. }
  88. $where["isPartner"]=1;
  89. $where["id"]=["in",trim($userData["parentPath"])];
  90. $parentData = $this->dbCustomer->select($where);
  91. if(empty($parentData)){
  92. return false;
  93. }
  94. $parentPartner = null;
  95. //逆序祖先,从最近的祖先先查
  96. $pathData = array_reverse(explode(",", trim($userData["parentPath"])));
  97. for($i=0;$i<count($pathData);$i++){
  98. $itemData = [];
  99. foreach($parentData as $k=>$v){
  100. if($pathData[$i]==$v["id"]){
  101. $itemData = $v;
  102. break;
  103. }
  104. }
  105. if(!empty($itemData) && $itemData["isPartner"]==1){
  106. $parentPartner = $itemData;
  107. break;
  108. }
  109. }
  110. if(empty($parentPartner)){
  111. return false;
  112. }
  113. return $parentPartner;
  114. }
  115. /**
  116. * 佣金计算
  117. * @param type $customerId
  118. * @param type $money
  119. * @param type $type 1表示子级消费,2表示合伙人收益
  120. * @return bool
  121. */
  122. public function calcMoney($customerData,$money){
  123. $parentData = $this->getParentPartner($customerData);
  124. if(empty($parentData) || $parentData["isPartner"]==0){
  125. return false;
  126. }
  127. $per = 0.1;//佣金比例
  128. if($money>=20000 && $customerData["isPartner"]==0){
  129. $per = 0.05;
  130. }
  131. $resData=[
  132. "childId"=>$customerData["id"],
  133. "childData"=>$customerData,
  134. "parentId"=>$parentData["id"],
  135. "parentData"=>$parentData,
  136. "per"=>$per,
  137. "money"=>$money,
  138. "commission"=>$money * $per
  139. ];
  140. return $resData;
  141. }
  142. /**
  143. * 订单消费计算佣金
  144. * @param type $orderCustomerId
  145. * @param type $orderMoney
  146. * @param type $isPart 订单用户如果是合伙人是否计算
  147. * @return bool
  148. */
  149. public function getCalcMoneyData($orderCustomerId,$orderMoney,$isPart=false){
  150. if(empty($orderCustomerId) || empty($orderMoney)){
  151. return false;
  152. }
  153. $customerData = $this->dbCustomer->get($orderCustomerId);
  154. if (empty($customerData) || empty($customerData["parentPath"])) {
  155. return false;
  156. }
  157. //订单用户如果是合伙人消费则不计算收益
  158. if(!$isPart && $customerData["isPartner"]==1){
  159. return false;
  160. }
  161. $data=[];
  162. $nowTime = time();
  163. //计算低层收益
  164. $bottomData = $this->calcMoney($customerData,$orderMoney);
  165. if(empty($bottomData)){
  166. return false;
  167. }
  168. //计算顶层收益
  169. $topData = $this->calcMoney($bottomData["parentData"],$bottomData["commission"]);
  170. if(!empty($topData)){
  171. //记录顶层收益
  172. $data[]=[
  173. "orderMoney"=>$orderMoney,//订单支付金额
  174. "calcMoney"=>$topData["money"],//佣金计算金额
  175. "sourceCustomerId"=>$topData["childId"],//来源客户id
  176. "partnerId"=>$topData["parentId"],//收钱合伙人id
  177. "commission"=>$topData["commission"],//佣金金额
  178. "per"=>$topData["per"],//佣金比例
  179. "type"=>1,//1表示子级合伙人收益计算收益,0表示子级消费计算收益
  180. "time"=>$nowTime,
  181. ];
  182. }
  183. //记录底层收益
  184. $data[]=[
  185. "orderMoney"=>$orderMoney,//订单支付金额
  186. "calcMoney"=>$topData["money"],//佣金计算金额
  187. "sourceCustomerId"=>$topData["childId"],//来源客户id
  188. "partnerId"=>$topData["parentId"],//收钱合伙人id
  189. "commission"=>$topData["commission"],//佣金金额
  190. "time"=>$nowTime,
  191. "type"=>0
  192. ];
  193. return $data;
  194. }
  195. /**
  196. * 删除用户缓存
  197. * @param type $customerId
  198. * @param type $userCenterId
  199. */
  200. public function delCustomerCache($customerId,$userCenterId){
  201. $objCustomerCache = new CustomerCache();
  202. $objCustomerCache->delCustomerData($this->enterpriseId, $customerId);
  203. $objCustomerCache->delCustomerUserData($this->enterpriseId, $userCenterId);
  204. }
  205. }