StoreOrderSuccessServices.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. $allSubordinates = $giftGroupService->getList([
  194. 'product_id' => $product_id,
  195. 'pid' => $spread_uid
  196. ], '*', 0, 0, 'create_time ASC');
  197. // 获取最后一个下级的group_pid来判断小组是否满人
  198. $lastMember = $allSubordinates[count($allSubordinates) - 1];
  199. $lastGroupPid = $lastMember['group_pid'];
  200. // 统计小组人数:从最后一个成员开始,沿group_pid向上追溯到根节点
  201. // 然后统计所有pid=spread_uid且group_pid在链条上的成员
  202. $groupChain = [];
  203. $currentGroupPid = $lastGroupPid;
  204. // 追溯group_pid链条
  205. while ($currentGroupPid > 0) {
  206. $groupChain[] = $currentGroupPid;
  207. @file_put_contents('quanju4.txt', $currentGroupPid . "-group_pid链条节点\r\n", 8);
  208. // 查找这个group_pid对应的成员
  209. $chainMember = $giftGroupService->getOne([
  210. 'product_id' => $product_id,
  211. 'uid' => $currentGroupPid
  212. ]);
  213. if ($chainMember) {
  214. $currentGroupPid = $chainMember['group_pid'];
  215. @file_put_contents('quanju4.txt', $chainMember['group_pid'] . "-下一节点group_pid\r\n", 8);
  216. } else {
  217. break;
  218. }
  219. }
  220. // 统计小组人数:pid=spread_uid 且 group_pid 在链条中或为0
  221. $groupCount = 0;
  222. foreach ($allSubordinates as $sub) {
  223. if (in_array($sub['group_pid'], $groupChain) || $sub['group_pid'] == 0) {
  224. $groupCount++;
  225. }
  226. }
  227. $isGroupFull = $groupCount >= $groupMaxNum;
  228. @file_put_contents('quanju4.txt', $groupCount . "-小组人数\r\n", 8);
  229. @file_put_contents('quanju4.txt', $groupMaxNum . "-系统设置人数\r\n", 8);
  230. @file_put_contents('quanju4.txt', $lastGroupPid . "-最后一个人的group_pid\r\n", 8);
  231. @file_put_contents('quanju4.txt', json_encode($groupChain) . "-group_pid链条\r\n", 8);
  232. @file_put_contents('quanju4.txt', ($isGroupFull ? '小组已满' : '小组未满') . "-小组状态\r\n", 8);
  233. @file_put_contents('quanju4.txt', end($groupChain) . "-小组最后一人uid(end groupChain)\r\n", 8);
  234. // 判断小组是否满人
  235. if ($isGroupFull) {
  236. // 小组满人,需要滑落到小组最后一人的直推下级小组
  237. // 小组最后一人是group_pid链条中的最后一个uid
  238. $lastGroupMemberUid = end($groupChain);
  239. @file_put_contents('quanju4.txt', $lastGroupMemberUid . "-小组最后一人的uid\r\n", 8);
  240. // 查找小组最后一人的直推下级(pid等于最后一人的uid)
  241. $lastDirectSubordinates = $giftGroupService->getList([
  242. 'product_id' => $product_id,
  243. 'pid' => $lastGroupMemberUid
  244. ], '*', 0, 0, 'create_time ASC');
  245. @file_put_contents('quanju4.txt', count($lastDirectSubordinates) . "-最后一人的直推下级人数\r\n", 8);
  246. if (count($lastDirectSubordinates) < $groupMaxNum) {
  247. // 最后一人的直推下级未满,新成员应该加入这个小组
  248. // 查找这些直推下级的小组关系,判断是否有小组(group_pid指向的)
  249. if (count($lastDirectSubordinates) > 0) {
  250. // 最后一人的直推下级有成员,查找这些成员的group_pid
  251. $firstDirectSubordinate = $lastDirectSubordinates[0];
  252. // 如果第一个直推下级的group_pid不为0,说明有小组,查找该小组的最后一人
  253. if ($firstDirectSubordinate['group_pid'] > 0) {
  254. // 查找该小组的所有成员
  255. $subGroupMembers = $giftGroupService->getList([
  256. 'product_id' => $product_id,
  257. 'group_pid' => $firstDirectSubordinate['group_pid']
  258. ], '*', 0, 0, 'create_time ASC');
  259. // 新成员的group_pid是该小组的最后一人
  260. $lastSubGroupMember = end($subGroupMembers);
  261. $data['group_pid'] = $lastSubGroupMember['uid'];
  262. // 新成员的fake_pid是该小组的最后一人
  263. $data['fake_pid'] = $lastSubGroupMember['uid'];
  264. @file_put_contents('quanju4.txt', $lastSubGroupMember['uid'] . "-小组的最后一人(新成员fake_pid)\r\n", 8);
  265. } else {
  266. // 第一直推下级的group_pid为0,说明没有小组,新成员成为第一人
  267. $data['group_pid'] = 0;
  268. $data['fake_pid'] = $lastGroupMemberUid;
  269. @file_put_contents('quanju4.txt', '最后一人的直推下级没有小组,新成员成为第一人\r\n', 8);
  270. }
  271. } else {
  272. // 最后一人的直推下级没有成员,新成员成为第一人
  273. $data['group_pid'] = 0;
  274. $data['fake_pid'] = $lastGroupMemberUid;
  275. @file_put_contents('quanju4.txt', '最后一人的直推下级没有成员,新成员成为第一人\r\n', 8);
  276. }
  277. } else {
  278. // 最后一人的直推下级也满了,新成员应该滑落到这个小组的最后一人
  279. if (count($lastDirectSubordinates) > 0) {
  280. // 查找最后一个人的小组
  281. $lastDirectSubordinate = end($lastDirectSubordinates);
  282. if ($lastDirectSubordinate['group_pid'] > 0) {
  283. $subGroupMembers = $giftGroupService->getList([
  284. 'product_id' => $product_id,
  285. 'group_pid' => $lastDirectSubordinate['group_pid']
  286. ], '*', 0, 0, 'create_time ASC');
  287. $lastSubGroupMember = end($subGroupMembers);
  288. $data['group_pid'] = 0;
  289. $data['fake_pid'] = $lastSubGroupMember['uid'];
  290. @file_put_contents('quanju4.txt', $lastSubGroupMember['uid'] . "-直推下级已满,新成员的fake_pid\r\n", 8);
  291. } else {
  292. $data['group_pid'] = 0;
  293. $data['fake_pid'] = $lastDirectSubordinate['uid'];
  294. @file_put_contents('quanju4.txt', $lastDirectSubordinate['uid'] . "-直推下级已满,无小组,新成员的fake_pid\r\n", 8);
  295. }
  296. } else {
  297. // 理论上不会走到这里
  298. $data['group_pid'] = 0;
  299. $data['fake_pid'] = $lastGroupMemberUid;
  300. }
  301. }
  302. } else {
  303. // 小组未满,新成员应该加入当前小组
  304. // 新成员的group_pid是最后一个下级的uid(成为小组长)
  305. $data['group_pid'] = $lastMember['uid'];
  306. $data['fake_pid'] = $spread_uid;
  307. }
  308. } else {
  309. // 直推上级没有下级,这是该推广人的第一个下级,建立新小组
  310. $data['group_pid'] = 0;
  311. $data['fake_pid'] = $spread_uid;
  312. }
  313. // 保存推荐关系
  314. return $giftGroupService->save($data);
  315. }
  316. return false;
  317. } catch (\Exception $e) {
  318. @file_put_contents('quanju4.txt', $e->getMessage() . "-创建订单报错内容\r\n", 8);
  319. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  320. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  321. @file_put_contents('quanju4.txt', $e->getTraceAsString() . "-堆栈\r\n", 8);
  322. }
  323. }
  324. // 礼包上级分红
  325. public function giftRecommendationBonus($order)
  326. {
  327. try {
  328. @file_put_contents('quanju.txt', json_encode($order) . "-礼包上级分红\r\n", 8);
  329. // 获取礼包服务
  330. $giftGroupService = app()->make(StoreProductGiftGroupService::class);
  331. $userServices = app()->make(UserServices::class);
  332. // 检查 cart_info 是否存在且非空
  333. @file_put_contents('quanju4.txt', '开始检查cart_info' . "\r\n", 8);
  334. if (!isset($order['cart_info']) || empty($order['cart_info'])) {
  335. @file_put_contents('quanju4.txt', 'cart_info不存在或为空' . "\r\n", 8);
  336. return false;
  337. }
  338. // 获取商品ID和商品价格作为佣金计算基数
  339. $product_id = $order['cart_info'][0]['product_id'] ?? 0;
  340. $brokerage_price = $order['cart_info'][0]['truePrice'] ?? 0;
  341. @file_put_contents('quanju4.txt', 'product_id=' . $product_id . ', brokerage_price=' . $brokerage_price . "\r\n", 8);
  342. if (!$product_id || !$brokerage_price) {
  343. @file_put_contents('quanju4.txt', 'product_id或brokerage_price为0,返回false' . "\r\n", 8);
  344. return false;
  345. }
  346. @file_put_contents('quanju4.txt', $product_id . "-商品id\r\n", 8);
  347. // 获取用户的推荐关系
  348. $userRelation = $giftGroupService->getOne([
  349. 'product_id' => $product_id,
  350. 'uid' => $order['uid']
  351. ]);
  352. if (!$userRelation) return false;
  353. $userInfo = $userServices->getOne(['uid' => $order['uid']]);
  354. // 获取佣金服务(一次性创建,复用)
  355. /** @var UserBrokerageServices $userBrokerageServices */
  356. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  357. //冻结时间
  358. $frozenTime = time() + intval(sys_config('extract_time')) * 86400;
  359. // 直推上级分成
  360. $directBrokerage = $brokerage_price * (sys_config('gift_direct_referral', 10) / 100);
  361. @file_put_contents('quanju4.txt', json_encode($userRelation) . "-礼包推荐关系表\r\n", 8);
  362. if ($userRelation['pid'] == $userRelation['fake_pid']) {
  363. // 真直推上级和假直推上级相同,全部给直推上级
  364. $pid_brokerage = $userServices->getOne(['uid' => $userRelation['pid']]);
  365. $userServices->bcInc($userRelation['pid'], 'brokerage_price', $directBrokerage, 'uid');
  366. @file_put_contents('quanju4.txt', $directBrokerage . "-直推奖励\r\n", 8);
  367. $balance = bcadd($pid_brokerage['brokerage_price'], $directBrokerage, 2);
  368. $userBrokerageServices->income('get_direct_referral_brokerage', $userRelation['pid'], [
  369. 'nickname' => $userInfo['nickname'],
  370. 'pay_price' => floatval($order['pay_price']),
  371. 'number' => $directBrokerage,
  372. 'frozen_time' => $frozenTime
  373. ], $balance, $order['id']);
  374. } else {
  375. // 真直推上级和假直推上级不同,各分50%
  376. $halfBrokerage = $directBrokerage * (sys_config('gift_spread_spot_bonus', 50) / 100);
  377. // 真上级分得50%
  378. $pid_brokerage = $userServices->getOne(['uid' => $userRelation['pid']]);
  379. $userServices->bcInc($userRelation['pid'], 'brokerage_price', $halfBrokerage, 'uid');
  380. $balance = bcadd($pid_brokerage['brokerage_price'], $halfBrokerage, 2);
  381. $userBrokerageServices->income('get_direct_referral_brokerage', $userRelation['pid'], [
  382. 'nickname' => $userInfo['nickname'],
  383. 'pay_price' => floatval($order['pay_price']),
  384. 'number' => $halfBrokerage,
  385. 'frozen_time' => $frozenTime
  386. ], $balance, $order['id']);
  387. // 假上级分得50%
  388. $fake_pid_brokerage = $userServices->getOne(['uid' => $userRelation['fake_pid']]);
  389. $userServices->bcInc($userRelation['fake_pid'], 'brokerage_price', $halfBrokerage, 'uid');
  390. $balance = bcadd($fake_pid_brokerage['brokerage_price'], $halfBrokerage, 2);
  391. $userBrokerageServices->income('get_direct_referral_brokerage', $userRelation['fake_pid'], [
  392. 'nickname' => $userInfo['nickname'],
  393. 'pay_price' => floatval($order['pay_price']),
  394. 'number' => $halfBrokerage,
  395. 'frozen_time' => $frozenTime
  396. ], $balance, $order['id']);
  397. }
  398. // 见点奖处理
  399. $spotBrokerage = $brokerage_price * (sys_config('gift_spot_bonus', 2.5) / 100);
  400. // 1. 向上查找:先找小组上级,没有就找直推上级
  401. $spotUpLimit = sys_config('gift_spot_bonus_up', 15);
  402. $currentUid = $order['uid']; // 从当前用户开始
  403. $currentRelation = $userRelation; // 当前用户的推荐关系
  404. for ($i = 0; $i < $spotUpLimit; $i++) {
  405. $nextUid = null;
  406. // 优先查找小组上级
  407. if (!empty($currentRelation['group_pid'])) {
  408. $nextUid = $currentRelation['group_pid'];
  409. } else if (!empty($currentRelation['pid'])) {
  410. // 没有小组上级,查找直推上级
  411. $nextUid = $currentRelation['pid'];
  412. }
  413. // 找不到上级,退出循环
  414. if (!$nextUid) break;
  415. // 给上级发放见点奖
  416. $currentBrokerage = $userServices->getOne(['uid' => $nextUid]);
  417. $userServices->bcInc($nextUid, 'brokerage_price', $spotBrokerage, 'uid');
  418. $balance = bcadd($currentBrokerage['brokerage_price'], $spotBrokerage);
  419. $userBrokerageServices->income('get_spot_brokerage', $nextUid, [
  420. 'nickname' => $userInfo['nickname'],
  421. 'pay_price' => floatval($order['pay_price']),
  422. 'number' => $spotBrokerage,
  423. 'frozen_time' => $frozenTime
  424. ], $balance, $order['id']);
  425. @file_put_contents('quanju4.txt', '向上发放见点奖: uid=' . $nextUid . ', 金额=' . $spotBrokerage . "\r\n", 8);
  426. // 查找下一个上级的推荐关系
  427. $currentRelation = $giftGroupService->getOne([
  428. 'product_id' => $product_id,
  429. 'uid' => $nextUid
  430. ]);
  431. // 如果找不到推荐关系,退出循环
  432. if (!$currentRelation) break;
  433. }
  434. // 2. 向下查找:先找直推下级,不够再找同组下级
  435. $spotDownLimit = sys_config('gift_spot_bonus_down', 15);
  436. $totalDownSpots = 0;
  437. // 2.1 先找直推下级
  438. $directSubordinates = $giftGroupService->getList([
  439. 'product_id' => $product_id,
  440. 'pid' => $order['uid']
  441. ], '*', 0, $spotDownLimit, 'create_time asc');
  442. foreach ($directSubordinates as $subordinate) {
  443. $subBrokerage = $userServices->getOne(['uid' => $subordinate['uid']]);
  444. $userServices->bcInc($subordinate['uid'], 'brokerage_price', $spotBrokerage, 'uid');
  445. $balance = bcadd($subBrokerage['brokerage_price'], $spotBrokerage);
  446. $userBrokerageServices->income('get_spot_brokerage', $subordinate['uid'], [
  447. 'nickname' => $userInfo['nickname'],
  448. 'pay_price' => floatval($order['pay_price']),
  449. 'number' => $spotBrokerage,
  450. 'frozen_time' => $frozenTime
  451. ], $balance, $order['id']);
  452. @file_put_contents('quanju4.txt', '向下发放见点奖给直推下级: uid=' . $subordinate['uid'] . ', 金额=' . $spotBrokerage . "\r\n", 8);
  453. $totalDownSpots++;
  454. }
  455. // 2.2 如果直推下级不够,找同组下级
  456. if ($totalDownSpots < $spotDownLimit) {
  457. // 获取所有pid与当前用户pid相同的同级用户
  458. $samePidMembers = $giftGroupService->getList([
  459. 'product_id' => $product_id,
  460. 'pid' => $userRelation['pid']
  461. ], '*', 0, 0, 'create_time asc');
  462. // 找到当前用户在同级用户中的位置
  463. $currentIndex = null;
  464. foreach ($samePidMembers as $index => $member) {
  465. if ($member['uid'] == $order['uid']) {
  466. $currentIndex = $index;
  467. break;
  468. }
  469. }
  470. // 从当前用户之后的同级用户开始,查找同组下级
  471. if ($currentIndex !== null) {
  472. for ($i = $currentIndex + 1; $i < count($samePidMembers) && $totalDownSpots < $spotDownLimit; $i++) {
  473. $member = $samePidMembers[$i];
  474. // 检查这个同级用户的group_pid层级是否在当前用户或其下级中
  475. $isGroupSubordinate = $this->isGroupSubordinate($order['uid'], $member['group_pid'], $product_id, $giftGroupService);
  476. if ($isGroupSubordinate) {
  477. $memberBrokerage = $userServices->getOne(['uid' => $member['uid']]);
  478. $userServices->bcInc($member['uid'], 'brokerage_price', $spotBrokerage, 'uid');
  479. $balance = bcadd($memberBrokerage['brokerage_price'], $spotBrokerage);
  480. $userBrokerageServices->income('get_spot_brokerage', $member['uid'], [
  481. 'nickname' => $userInfo['nickname'],
  482. 'pay_price' => floatval($order['pay_price']),
  483. 'number' => $spotBrokerage,
  484. 'frozen_time' => $frozenTime
  485. ], $balance, $order['id']);
  486. @file_put_contents('quanju4.txt', '向下发放见点奖给同组下级: uid=' . $member['uid'] . ', 金额=' . $spotBrokerage . "\r\n", 8);
  487. $totalDownSpots++;
  488. }
  489. }
  490. }
  491. }
  492. return true;
  493. } catch (\Exception $e) {
  494. @file_put_contents('quanju4.txt', $e->getMessage() . "-上级分红报错内容\r\n", 8);
  495. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  496. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  497. @file_put_contents('quanju4.txt', $e->getTraceAsString() . "-堆栈\r\n", 8);
  498. }
  499. }
  500. /**
  501. * 检查某个用户是否是另一个用户的小组下级(通过group_pid层级关系)
  502. * @param int $baseUid 基准用户uid
  503. * @param int $groupPid 要检查的group_pid
  504. * @param int $productId 商品ID
  505. * @param StoreProductGiftGroupService $giftGroupService 礼包服务
  506. * @param array $checked 已检查的用户(防止循环)
  507. * @return bool 如果是小组下级返回true,否则返回false
  508. */
  509. protected function isGroupSubordinate($baseUid, $groupPid, $productId, $giftGroupService, $checked = [])
  510. {
  511. // 如果已经检查过这个groupPid,跳过(防止循环)
  512. if (isset($checked[$groupPid])) {
  513. return false;
  514. }
  515. $checked[$groupPid] = true;
  516. // 如果groupPid是0,说明没有小组关系
  517. if (empty($groupPid)) {
  518. return false;
  519. }
  520. // 如果groupPid等于基准用户,说明是直接下级
  521. if ($groupPid == $baseUid) {
  522. return true;
  523. }
  524. // 递归检查group_pid是否是基准用户的下级
  525. $member = $giftGroupService->getOne([
  526. 'product_id' => $productId,
  527. 'uid' => $groupPid
  528. ]);
  529. if ($member) {
  530. // 继续检查这个用户的group_pid
  531. return $this->isGroupSubordinate($baseUid, $member['group_pid'], $productId, $giftGroupService, $checked);
  532. }
  533. return false;
  534. }
  535. /**
  536. * 检查某个用户是否是另一个用户的下级团队成员
  537. * @param int $uid 当前用户
  538. * @param int $targetUid 要检查的目标用户
  539. * @param int $productId 商品ID
  540. * @param StoreProductGiftGroupService $giftGroupService 礼包服务
  541. * @param array $checked 已检查的用户(防止循环)
  542. * @return bool 如果是下级返回true,否则返回false
  543. */
  544. protected function isSubordinate($uid, $targetUid, $productId, $giftGroupService, $checked = [])
  545. {
  546. // 如果已经检查过这个用户,跳过(防止循环)
  547. if (isset($checked[$uid])) {
  548. return false;
  549. }
  550. $checked[$uid] = true;
  551. // 查找该用户的所有直推下级
  552. $subordinates = $giftGroupService->getList([
  553. 'product_id' => $productId,
  554. 'pid' => $uid
  555. ], 'uid', 0, 0, 'create_time asc');
  556. // 检查目标用户是否在直推下级中
  557. foreach ($subordinates as $sub) {
  558. if ($sub['uid'] == $targetUid) {
  559. return true; // 找到了,是下级
  560. }
  561. }
  562. // 递归检查每个下级的下级
  563. foreach ($subordinates as $sub) {
  564. if ($this->isSubordinate($sub['uid'], $targetUid, $productId, $giftGroupService, $checked)) {
  565. return true; // 在下级的下级中找到了
  566. }
  567. }
  568. return false; // 没找到,不是下级
  569. }
  570. }