StoreOrderSuccessServices.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\services\activity\lottery\LuckLotteryServices;
  14. use app\services\activity\combination\StorePinkServices;
  15. use app\services\BaseServices;
  16. use app\services\pay\PayServices;
  17. use app\services\user\UserServices;
  18. use app\services\product\product\StoreProductGiftGroupService;
  19. use app\services\user\UserBrokerageServices;
  20. use crmeb\exceptions\ApiException;
  21. /**
  22. * Class StoreOrderSuccessServices
  23. * @package app\services\order
  24. * @method getOne(array $where, ?string $field = '*', array $with = []) 获取去一条数据
  25. */
  26. class StoreOrderSuccessServices extends BaseServices
  27. {
  28. /**
  29. *
  30. * StoreOrderSuccessServices constructor.
  31. * @param StoreOrderDao $dao
  32. */
  33. public function __construct(StoreOrderDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 0元支付
  39. * @param array $orderInfo
  40. * @param int $uid
  41. * @return bool
  42. * @throws \think\Exception
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. */
  47. public function zeroYuanPayment(array $orderInfo, int $uid, string $payType = PayServices::YUE_PAY)
  48. {
  49. if ($orderInfo['paid']) {
  50. throw new ApiException(410265);
  51. }
  52. return $this->paySuccess($orderInfo, $payType);//余额支付成功
  53. }
  54. /**
  55. * 支付成功
  56. * @param array $orderInfo
  57. * @param string $paytype
  58. * @param array $other
  59. * @return bool
  60. * @throws \Psr\SimpleCache\InvalidArgumentException
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function paySuccess(array $orderInfo, string $paytype = PayServices::WEIXIN_PAY, array $other = [])
  66. {
  67. $updata = ['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time()];
  68. $orderInfo['pay_time'] = $updata['pay_time'];
  69. $orderInfo['pay_type'] = $paytype;
  70. if ($other && isset($other['trade_no'])) {
  71. $updata['trade_no'] = $other['trade_no'];
  72. }
  73. /** @var StoreOrderCartInfoServices $orderInfoServices */
  74. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  75. $orderInfo['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  76. $res1 = $this->dao->update($orderInfo['id'], $updata);
  77. $resPink = true;
  78. if ($orderInfo['combination_id'] && $res1 && !$orderInfo['refund_status']) {
  79. /** @var StorePinkServices $pinkServices */
  80. $pinkServices = app()->make(StorePinkServices::class);
  81. /** @var StoreOrderServices $orderServices */
  82. $orderServices = app()->make(StoreOrderServices::class);
  83. $resPink = $pinkServices->createPink($orderServices->tidyOrder($orderInfo, true));//创建拼团
  84. }
  85. //缓存抽奖次数 除过线下支付
  86. if (isset($orderInfo['pay_type']) && $orderInfo['pay_type'] != 'offline') {
  87. /** @var LuckLotteryServices $luckLotteryServices */
  88. $luckLotteryServices = app()->make(LuckLotteryServices::class);
  89. $luckLotteryServices->setCacheLotteryNum((int)$orderInfo['uid'], 'order');
  90. }
  91. $orderInfo['send_name'] = $orderInfo['real_name'];
  92. //订单支付成功后置事件
  93. event('OrderPaySuccessListener', [$orderInfo]);
  94. //用户推送消息事件
  95. event('NoticeListener', [$orderInfo, 'order_pay_success']);
  96. //支付成功给客服发送消息
  97. event('NoticeListener', [$orderInfo, 'admin_pay_success_code']);
  98. // 推送订单
  99. event('OutPushListener', ['order_pay_push', ['order_id' => (int)$orderInfo['id']]]);
  100. //自定义消息-订单支付成功
  101. $orderInfo['time'] = date('Y-m-d H:i:s');
  102. $orderInfo['phone'] = $orderInfo['user_phone'];
  103. event('CustomNoticeListener', [$orderInfo['uid'], $orderInfo, 'order_pay_success']);
  104. //自定义事件-订单支付
  105. event('CustomEventListener', ['order_pay', [
  106. 'uid' => $orderInfo['uid'],
  107. 'id' => (int)$orderInfo['id'],
  108. 'order_id' => $orderInfo['order_id'],
  109. 'real_name' => $orderInfo['real_name'],
  110. 'user_phone' => $orderInfo['user_phone'],
  111. 'user_address' => $orderInfo['user_address'],
  112. 'total_num' => $orderInfo['total_num'],
  113. 'pay_price' => $orderInfo['pay_price'],
  114. 'pay_postage' => $orderInfo['pay_postage'],
  115. 'deduction_price' => $orderInfo['deduction_price'],
  116. 'coupon_price' => $orderInfo['coupon_price'],
  117. 'store_name' => $orderInfo['storeName'],
  118. 'add_time' => date('Y-m-d H:i:s', $orderInfo['add_time']),
  119. ]]);
  120. // 获取购物车信息
  121. $orderCartInfo = $orderInfoServices->getCartColunm(
  122. ['oid' => $orderInfo['id']],
  123. 'cart_info',
  124. 'unique'
  125. );
  126. $orderInfo['cart_info'] = [];
  127. foreach ($orderCartInfo as $k => $v) {
  128. $cart_info = is_string($v) ? json_decode($v, true) : $v;
  129. $orderInfo['cart_info'][] = $cart_info;
  130. }
  131. if ($orderInfo['is_lb'] == 1) { //建立礼包推荐关系
  132. @file_put_contents('quanju.txt', $orderInfo['lb_spread_uid'] . "-礼包推荐人2\r\n", 8);
  133. self::createGiftRecommendationRelationship($orderInfo);
  134. self::giftRecommendationBonus($orderInfo);
  135. }
  136. // if ($orderInfo['is_lb'] == 1) { //赠送礼包推荐奖金
  137. // @file_put_contents('quanju.txt', $orderInfo['is_lb'] . "-礼包订单2\r\n", 8);
  138. //
  139. // }
  140. $res = $res1 && $resPink;
  141. return false !== $res;
  142. }
  143. // 创建礼包推荐关系
  144. public function createGiftRecommendationRelationship($order)
  145. {
  146. // @file_put_contents('quanju.txt', json_encode($order) . "-创建礼包推荐关系\r\n", 8);
  147. try {
  148. // 获取用户信息
  149. $userServices = app()->make(UserServices::class);
  150. $user = $userServices->getUserInfo($order['uid']);
  151. @file_put_contents('quanju.txt', $user['spread_uid'] . "-创建礼包推荐关系\r\n", 8);
  152. if ($user['spread_uid'] > 0) {
  153. // 如果用户有推广人,说明是通过推广链接支付的,需要创建礼包推荐关系,关系表是store_product_gift_group,需要保存product_id,uid,group_pid(小组上级),pid(真直推上级),fake_pid(假直推上级(直推上级的下级小组满人下放下来的人)),一个商品加一个pid推荐上级会形成一个小组,第一个加进小组的会是第二个加入的小组上级(group_pid),当一个小组满10人后,就会让第十人作为fake_pid直推上级建立新的10人小组,但pid还是真正的直推上级
  154. // 获取商品ID
  155. $product_id = $order['cart_info'][0]['product_id'] ?? 0;
  156. if (!$product_id) return false;
  157. // 获取推广人UID
  158. $spread_uid = $user['spread_uid'];
  159. // 获取礼包服务
  160. $giftGroupService = app()->make(StoreProductGiftGroupService::class);
  161. // 检查是否已有该商品的推荐关系
  162. $existRelation = $giftGroupService->getOne([
  163. 'product_id' => $product_id,
  164. 'uid' => $order['uid']
  165. ]);
  166. if ($existRelation) return true;
  167. // 检查推广人是否是自己的下级团队成员(防止循环推荐)
  168. if ($this->isSubordinate($order['uid'], $spread_uid, $product_id, $giftGroupService)) {
  169. return false;
  170. }
  171. // 获取直推上级的推荐关系
  172. $parentRelation = $giftGroupService->getOne([
  173. 'product_id' => $product_id,
  174. 'uid' => $spread_uid
  175. ]);
  176. // 设置关系数据
  177. $data = [
  178. 'product_id' => $product_id,
  179. 'uid' => $order['uid'],
  180. 'pid' => $spread_uid, // 真直推上级
  181. 'create_time' => time()
  182. ];
  183. // 处理小组关系
  184. // 获取小组人数上限配置
  185. $groupMaxNum = sys_config('gift_group_num', 10);
  186. // 判断直推上级在该商品下是否有下级
  187. $hasSubordinate = $giftGroupService->getOne([
  188. 'product_id' => $product_id,
  189. 'pid' => $spread_uid
  190. ]);
  191. if ($hasSubordinate) {
  192. // 直推上级已有下级,需要判断是否需要滑落
  193. // 小组定义:一个商品 + 一个 fake_id 形成一个小组
  194. // 37的直推下级首先加入 fake_id=37 的小组
  195. // 查找 fake_id=37 的小组(商品ID=6, fake_pid=spread_uid的所有人)
  196. $spreadFakeIdGroup = $giftGroupService->getList([
  197. 'product_id' => $product_id,
  198. 'fake_pid' => $spread_uid
  199. ], '*', 0, 0, 'create_time ASC');
  200. @file_put_contents('quanju4.txt', count($spreadFakeIdGroup) . "-fake_id=" . $spread_uid . "的小组人数\r\n", 8);
  201. // 判断该小组是否满人
  202. if (count($spreadFakeIdGroup) < $groupMaxNum) {
  203. // 小组未满,新成员加入该小组
  204. if (count($spreadFakeIdGroup) > 0) {
  205. // 该小组已有成员,新成员的group_pid是该小组的最后一人
  206. $lastMember = end($spreadFakeIdGroup);
  207. $data['group_pid'] = $lastMember['uid'];
  208. $data['fake_pid'] = $spread_uid;
  209. @file_put_contents('quanju4.txt', $lastMember['uid'] . "-小组最后一人,新成员group_pid\r\n", 8);
  210. } else {
  211. // 该小组没有成员,新成员成为第一人
  212. $data['group_pid'] = 0;
  213. $data['fake_pid'] = $spread_uid;
  214. @file_put_contents('quanju4.txt', 'fake_id小组没有成员,新成员成为第一人\r\n', 8);
  215. }
  216. } else {
  217. // 小组已满,需要滑落到该小组最后一人的 fake_id 下
  218. $lastMember = end($spreadFakeIdGroup);
  219. @file_put_contents('quanju4.txt', $lastMember['uid'] . "-小组最后一人uid\r\n", 8);
  220. @file_put_contents('quanju4.txt', $lastMember['fake_pid'] . "-最后一人的fake_id\r\n", 8);
  221. // 查找最后一人的 fake_id 对应的小组
  222. $lastMemberFakeIdGroup = $giftGroupService->getList([
  223. 'product_id' => $product_id,
  224. 'fake_pid' => $lastMember['fake_pid']
  225. ], '*', 0, 0, 'create_time ASC');
  226. @file_put_contents('quanju4.txt', count($lastMemberFakeIdGroup) . "-最后一人的fake_id小组人数\r\n", 8);
  227. if (count($lastMemberFakeIdGroup) < $groupMaxNum) {
  228. // 该小组未满,新成员加入该小组
  229. if (count($lastMemberFakeIdGroup) > 0) {
  230. // 该小组已有成员,新成员的group_pid是该小组的最后一人
  231. $lastSubMember = end($lastMemberFakeIdGroup);
  232. $data['group_pid'] = $lastSubMember['uid'];
  233. $data['fake_pid'] = $lastMember['fake_pid'];
  234. @file_put_contents('quanju4.txt', $lastSubMember['uid'] . "-fake_id小组最后一人,新成员group_pid\r\n", 8);
  235. } else {
  236. // 该小组没有成员,新成员成为第一人
  237. $data['group_pid'] = 0;
  238. $data['fake_pid'] = $lastMember['fake_pid'];
  239. @file_put_contents('quanju4.txt', '最后一人的fake_id小组没有成员,新成员成为第一人\r\n', 8);
  240. }
  241. } else {
  242. // 该小组也满了,继续滑落(最后一人的 fake_id 可能不等于自己的 fake_pid)
  243. // 这种情况下,最后一人的 fake_pid 就是滑落后的上级
  244. $data['group_pid'] = 0;
  245. $data['fake_pid'] = $lastMember['fake_pid'];
  246. @file_put_contents('quanju4.txt', '最后一人的fake_id小组已满,新成员fake_pid=' . $lastMember['fake_pid'] . "\r\n", 8);
  247. }
  248. }
  249. } else {
  250. // 直推上级没有下级,这是该推广人的第一个下级,建立新小组
  251. $data['group_pid'] = 0;
  252. $data['fake_pid'] = $spread_uid;
  253. }
  254. // 保存推荐关系
  255. return $giftGroupService->save($data);
  256. }
  257. return false;
  258. } catch (\Exception $e) {
  259. @file_put_contents('quanju4.txt', $e->getMessage() . "-创建订单报错内容\r\n", 8);
  260. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  261. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  262. @file_put_contents('quanju4.txt', $e->getTraceAsString() . "-堆栈\r\n", 8);
  263. }
  264. }
  265. // 礼包上级分红
  266. public function giftRecommendationBonus($order)
  267. {
  268. try {
  269. @file_put_contents('quanju.txt', json_encode($order) . "-礼包上级分红\r\n", 8);
  270. // 获取礼包服务
  271. $giftGroupService = app()->make(StoreProductGiftGroupService::class);
  272. $userServices = app()->make(UserServices::class);
  273. // 检查 cart_info 是否存在且非空
  274. @file_put_contents('quanju4.txt', '开始检查cart_info' . "\r\n", 8);
  275. if (!isset($order['cart_info']) || empty($order['cart_info'])) {
  276. @file_put_contents('quanju4.txt', 'cart_info不存在或为空' . "\r\n", 8);
  277. return false;
  278. }
  279. // 获取商品ID和商品价格作为佣金计算基数
  280. $product_id = $order['cart_info'][0]['product_id'] ?? 0;
  281. $brokerage_price = $order['cart_info'][0]['truePrice'] ?? 0;
  282. @file_put_contents('quanju4.txt', 'product_id=' . $product_id . ', brokerage_price=' . $brokerage_price . "\r\n", 8);
  283. if (!$product_id || !$brokerage_price) {
  284. @file_put_contents('quanju4.txt', 'product_id或brokerage_price为0,返回false' . "\r\n", 8);
  285. return false;
  286. }
  287. @file_put_contents('quanju4.txt', $product_id . "-商品id\r\n", 8);
  288. // 获取用户的推荐关系
  289. $userRelation = $giftGroupService->getOne([
  290. 'product_id' => $product_id,
  291. 'uid' => $order['uid']
  292. ]);
  293. if (!$userRelation) return false;
  294. $userInfo = $userServices->getOne(['uid' => $order['uid']]);
  295. // 获取佣金服务(一次性创建,复用)
  296. /** @var UserBrokerageServices $userBrokerageServices */
  297. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  298. //冻结时间
  299. $frozenTime = time() + intval(sys_config('extract_time')) * 86400;
  300. // 直推上级分成(全部给真直推上级)
  301. $directBrokerage = $brokerage_price * (sys_config('gift_direct_referral', 10) / 100);
  302. @file_put_contents('quanju4.txt', json_encode($userRelation) . "-礼包推荐关系表\r\n", 8);
  303. // 直推奖全部给真直推上级
  304. $pid_brokerage = $userServices->getOne(['uid' => $userRelation['pid']]);
  305. $userServices->bcInc($userRelation['pid'], 'brokerage_price', $directBrokerage, 'uid');
  306. @file_put_contents('quanju4.txt', $directBrokerage . "-直推奖励\r\n", 8);
  307. $balance = bcadd($pid_brokerage['brokerage_price'], $directBrokerage, 2);
  308. $userBrokerageServices->income('get_direct_referral_brokerage', $userRelation['pid'], [
  309. 'nickname' => $userInfo['nickname'],
  310. 'pay_price' => floatval($order['pay_price']),
  311. 'number' => $directBrokerage,
  312. 'frozen_time' => $frozenTime
  313. ], $balance, $order['id']);
  314. // 见点奖处理
  315. $spotBrokerage = $brokerage_price * (sys_config('gift_spot_bonus', 2.5) / 100);
  316. // 1. 向上查找:先找小组上级,没有就找假直推上级
  317. $spotUpLimit = sys_config('gift_spot_bonus_up', 15);
  318. $currentUid = $order['uid']; // 从当前用户开始
  319. $currentRelation = $userRelation; // 当前用户的推荐关系
  320. $originalPid = $userRelation['pid']; // 记录原始用户的真直推上级
  321. $splitRatio = sys_config('gift_fake_pid_split_ratio', 50); // 假直推上级分成比例(给真直推上级的比例)
  322. for ($i = 0; $i < $spotUpLimit; $i++) {
  323. $nextUid = null;
  324. // 优先查找小组上级
  325. if (!empty($currentRelation['group_pid'])) {
  326. $nextUid = $currentRelation['group_pid'];
  327. } else if (!empty($currentRelation['fake_pid'])) {
  328. // 没有小组上级,查找假直推上级
  329. $nextUid = $currentRelation['fake_pid'];
  330. }
  331. // 找不到上级,退出循环
  332. if (!$nextUid) break;
  333. // 判断是否需要分成(如果上级是fake_pid且不等于原始用户的pid)
  334. $needSplit = false;
  335. @file_put_contents('quanju4.txt', '分成判断检查: currentUid=' . $currentUid . ', nextUid=' . $nextUid . ', currentRelation[fake_pid]=' . ($currentRelation['fake_pid'] ?? 'null') . ', originalPid=' . $originalPid . "\r\n", 8);
  336. if (!empty($currentRelation['fake_pid']) && $nextUid == $currentRelation['fake_pid'] && $currentRelation['fake_pid'] != $originalPid) {
  337. $needSplit = true;
  338. }
  339. @file_put_contents('quanju4.txt', '分成判断结果: needSplit=' . ($needSplit ? 'true' : 'false') . "\r\n", 8);
  340. if ($needSplit) {
  341. // 需要分成:给真直推上级和假直推上级分别发放
  342. $splitAmount = $spotBrokerage * ($splitRatio / 100); // 给真直推上级的份额
  343. $remainderAmount = $spotBrokerage - $splitAmount; // 给假直推上级的份额
  344. // 给真直推上级发放
  345. $pidBrokerage = $userServices->getOne(['uid' => $originalPid]);
  346. $userServices->bcInc($originalPid, 'brokerage_price', $splitAmount, 'uid');
  347. $balance = bcadd($pidBrokerage['brokerage_price'], $splitAmount, 2);
  348. $userBrokerageServices->income('get_spot_brokerage', $originalPid, [
  349. 'nickname' => $userInfo['nickname'],
  350. 'pay_price' => floatval($order['pay_price']),
  351. 'number' => $splitAmount,
  352. 'frozen_time' => $frozenTime
  353. ], $balance, $order['id']);
  354. @file_put_contents('quanju4.txt', '向上发放见点奖给真直推上级: uid=' . $originalPid . ', 金额=' . $splitAmount . "\r\n", 8);
  355. // 给假直推上级发放
  356. $currentBrokerage = $userServices->getOne(['uid' => $nextUid]);
  357. $userServices->bcInc($nextUid, 'brokerage_price', $remainderAmount, 'uid');
  358. $balance = bcadd($currentBrokerage['brokerage_price'], $remainderAmount, 2);
  359. $userBrokerageServices->income('get_spot_brokerage', $nextUid, [
  360. 'nickname' => $userInfo['nickname'],
  361. 'pay_price' => floatval($order['pay_price']),
  362. 'number' => $remainderAmount,
  363. 'frozen_time' => $frozenTime
  364. ], $balance, $order['id']);
  365. @file_put_contents('quanju4.txt', '向上发放见点奖给假直推上级: uid=' . $nextUid . ', 金额=' . $remainderAmount . "\r\n", 8);
  366. } else {
  367. // 不需要分成:全部给上级
  368. $currentBrokerage = $userServices->getOne(['uid' => $nextUid]);
  369. $userServices->bcInc($nextUid, 'brokerage_price', $spotBrokerage, 'uid');
  370. $balance = bcadd($currentBrokerage['brokerage_price'], $spotBrokerage);
  371. $userBrokerageServices->income('get_spot_brokerage', $nextUid, [
  372. 'nickname' => $userInfo['nickname'],
  373. 'pay_price' => floatval($order['pay_price']),
  374. 'number' => $spotBrokerage,
  375. 'frozen_time' => $frozenTime
  376. ], $balance, $order['id']);
  377. @file_put_contents('quanju4.txt', '向上发放见点奖: uid=' . $nextUid . ', 金额=' . $spotBrokerage . "\r\n", 8);
  378. }
  379. // 查找下一个上级的推荐关系
  380. $currentRelation = $giftGroupService->getOne([
  381. 'product_id' => $product_id,
  382. 'uid' => $nextUid
  383. ]);
  384. // 如果找不到推荐关系,退出循环
  385. if (!$currentRelation) break;
  386. }
  387. // 2. 向下查找:先找直推下级,不够再找同组下级
  388. $spotDownLimit = sys_config('gift_spot_bonus_down', 15);
  389. $totalDownSpots = 0;
  390. // 2.1 先找直推下级
  391. $directSubordinates = $giftGroupService->getList([
  392. 'product_id' => $product_id,
  393. 'pid' => $order['uid']
  394. ], '*', 0, $spotDownLimit, 'create_time asc');
  395. foreach ($directSubordinates as $subordinate) {
  396. $subBrokerage = $userServices->getOne(['uid' => $subordinate['uid']]);
  397. $userServices->bcInc($subordinate['uid'], 'brokerage_price', $spotBrokerage, 'uid');
  398. $balance = bcadd($subBrokerage['brokerage_price'], $spotBrokerage);
  399. $userBrokerageServices->income('get_spot_brokerage', $subordinate['uid'], [
  400. 'nickname' => $userInfo['nickname'],
  401. 'pay_price' => floatval($order['pay_price']),
  402. 'number' => $spotBrokerage,
  403. 'frozen_time' => $frozenTime
  404. ], $balance, $order['id']);
  405. @file_put_contents('quanju4.txt', '向下发放见点奖给直推下级: uid=' . $subordinate['uid'] . ', 金额=' . $spotBrokerage . "\r\n", 8);
  406. $totalDownSpots++;
  407. }
  408. // 2.2 如果直推下级不够,找同组下级
  409. if ($totalDownSpots < $spotDownLimit) {
  410. // 获取所有pid与当前用户pid相同的同级用户
  411. $samePidMembers = $giftGroupService->getList([
  412. 'product_id' => $product_id,
  413. 'pid' => $userRelation['pid']
  414. ], '*', 0, 0, 'create_time asc');
  415. // 找到当前用户在同级用户中的位置
  416. $currentIndex = null;
  417. foreach ($samePidMembers as $index => $member) {
  418. if ($member['uid'] == $order['uid']) {
  419. $currentIndex = $index;
  420. break;
  421. }
  422. }
  423. // 从当前用户之后的同级用户开始,查找同组下级
  424. if ($currentIndex !== null) {
  425. for ($i = $currentIndex + 1; $i < count($samePidMembers) && $totalDownSpots < $spotDownLimit; $i++) {
  426. $member = $samePidMembers[$i];
  427. // 检查这个同级用户的group_pid层级是否在当前用户或其下级中
  428. $isGroupSubordinate = $this->isGroupSubordinate($order['uid'], $member['group_pid'], $product_id, $giftGroupService);
  429. if ($isGroupSubordinate) {
  430. $memberBrokerage = $userServices->getOne(['uid' => $member['uid']]);
  431. $userServices->bcInc($member['uid'], 'brokerage_price', $spotBrokerage, 'uid');
  432. $balance = bcadd($memberBrokerage['brokerage_price'], $spotBrokerage);
  433. $userBrokerageServices->income('get_spot_brokerage', $member['uid'], [
  434. 'nickname' => $userInfo['nickname'],
  435. 'pay_price' => floatval($order['pay_price']),
  436. 'number' => $spotBrokerage,
  437. 'frozen_time' => $frozenTime
  438. ], $balance, $order['id']);
  439. @file_put_contents('quanju4.txt', '向下发放见点奖给同组下级: uid=' . $member['uid'] . ', 金额=' . $spotBrokerage . "\r\n", 8);
  440. $totalDownSpots++;
  441. }
  442. }
  443. }
  444. }
  445. return true;
  446. } catch (\Exception $e) {
  447. @file_put_contents('quanju4.txt', $e->getMessage() . "-上级分红报错内容\r\n", 8);
  448. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  449. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  450. @file_put_contents('quanju4.txt', $e->getTraceAsString() . "-堆栈\r\n", 8);
  451. }
  452. }
  453. /**
  454. * 检查某个用户是否是另一个用户的小组下级(通过group_pid层级关系)
  455. * @param int $baseUid 基准用户uid
  456. * @param int $groupPid 要检查的group_pid
  457. * @param int $productId 商品ID
  458. * @param StoreProductGiftGroupService $giftGroupService 礼包服务
  459. * @param array $checked 已检查的用户(防止循环)
  460. * @return bool 如果是小组下级返回true,否则返回false
  461. */
  462. protected function isGroupSubordinate($baseUid, $groupPid, $productId, $giftGroupService, $checked = [])
  463. {
  464. // 如果已经检查过这个groupPid,跳过(防止循环)
  465. if (isset($checked[$groupPid])) {
  466. return false;
  467. }
  468. $checked[$groupPid] = true;
  469. // 如果groupPid是0,说明没有小组关系
  470. if (empty($groupPid)) {
  471. return false;
  472. }
  473. // 如果groupPid等于基准用户,说明是直接下级
  474. if ($groupPid == $baseUid) {
  475. return true;
  476. }
  477. // 递归检查group_pid是否是基准用户的下级
  478. $member = $giftGroupService->getOne([
  479. 'product_id' => $productId,
  480. 'uid' => $groupPid
  481. ]);
  482. if ($member) {
  483. // 继续检查这个用户的group_pid
  484. return $this->isGroupSubordinate($baseUid, $member['group_pid'], $productId, $giftGroupService, $checked);
  485. }
  486. return false;
  487. }
  488. /**
  489. * 检查某个用户是否是另一个用户的下级团队成员
  490. * @param int $uid 当前用户
  491. * @param int $targetUid 要检查的目标用户
  492. * @param int $productId 商品ID
  493. * @param StoreProductGiftGroupService $giftGroupService 礼包服务
  494. * @param array $checked 已检查的用户(防止循环)
  495. * @return bool 如果是下级返回true,否则返回false
  496. */
  497. protected function isSubordinate($uid, $targetUid, $productId, $giftGroupService, $checked = [])
  498. {
  499. // 如果已经检查过这个用户,跳过(防止循环)
  500. if (isset($checked[$uid])) {
  501. return false;
  502. }
  503. $checked[$uid] = true;
  504. // 查找该用户的所有直推下级
  505. $subordinates = $giftGroupService->getList([
  506. 'product_id' => $productId,
  507. 'pid' => $uid
  508. ], 'uid', 0, 0, 'create_time asc');
  509. // 检查目标用户是否在直推下级中
  510. foreach ($subordinates as $sub) {
  511. if ($sub['uid'] == $targetUid) {
  512. return true; // 找到了,是下级
  513. }
  514. }
  515. // 递归检查每个下级的下级
  516. foreach ($subordinates as $sub) {
  517. if ($this->isSubordinate($sub['uid'], $targetUid, $productId, $giftGroupService, $checked)) {
  518. return true; // 在下级的下级中找到了
  519. }
  520. }
  521. return false; // 没找到,不是下级
  522. }
  523. }