PartnerTools.Class.php 8.7 KB

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