| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\order;
- use app\dao\order\StoreOrderDao;
- use app\services\activity\lottery\LuckLotteryServices;
- use app\services\activity\combination\StorePinkServices;
- use app\services\BaseServices;
- use app\services\pay\PayServices;
- use app\services\user\UserServices;
- use app\services\product\product\StoreProductGiftGroupService;
- use app\services\user\UserBrokerageServices;
- use crmeb\exceptions\ApiException;
- /**
- * Class StoreOrderSuccessServices
- * @package app\services\order
- * @method getOne(array $where, ?string $field = '*', array $with = []) 获取去一条数据
- */
- class StoreOrderSuccessServices extends BaseServices
- {
- /**
- *
- * StoreOrderSuccessServices constructor.
- * @param StoreOrderDao $dao
- */
- public function __construct(StoreOrderDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 0元支付
- * @param array $orderInfo
- * @param int $uid
- * @return bool
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function zeroYuanPayment(array $orderInfo, int $uid, string $payType = PayServices::YUE_PAY)
- {
- if ($orderInfo['paid']) {
- throw new ApiException(410265);
- }
- return $this->paySuccess($orderInfo, $payType);//余额支付成功
- }
- /**
- * 支付成功
- * @param array $orderInfo
- * @param string $paytype
- * @param array $other
- * @return bool
- * @throws \Psr\SimpleCache\InvalidArgumentException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function paySuccess(array $orderInfo, string $paytype = PayServices::WEIXIN_PAY, array $other = [])
- {
- $updata = ['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time()];
- $orderInfo['pay_time'] = $updata['pay_time'];
- $orderInfo['pay_type'] = $paytype;
- if ($other && isset($other['trade_no'])) {
- $updata['trade_no'] = $other['trade_no'];
- }
- /** @var StoreOrderCartInfoServices $orderInfoServices */
- $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
- $orderInfo['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
- $res1 = $this->dao->update($orderInfo['id'], $updata);
- $resPink = true;
- if ($orderInfo['combination_id'] && $res1 && !$orderInfo['refund_status']) {
- /** @var StorePinkServices $pinkServices */
- $pinkServices = app()->make(StorePinkServices::class);
- /** @var StoreOrderServices $orderServices */
- $orderServices = app()->make(StoreOrderServices::class);
- $resPink = $pinkServices->createPink($orderServices->tidyOrder($orderInfo, true));//创建拼团
- }
- //缓存抽奖次数 除过线下支付
- if (isset($orderInfo['pay_type']) && $orderInfo['pay_type'] != 'offline') {
- /** @var LuckLotteryServices $luckLotteryServices */
- $luckLotteryServices = app()->make(LuckLotteryServices::class);
- $luckLotteryServices->setCacheLotteryNum((int)$orderInfo['uid'], 'order');
- }
- $orderInfo['send_name'] = $orderInfo['real_name'];
- //订单支付成功后置事件
- event('OrderPaySuccessListener', [$orderInfo]);
- //用户推送消息事件
- event('NoticeListener', [$orderInfo, 'order_pay_success']);
- //支付成功给客服发送消息
- event('NoticeListener', [$orderInfo, 'admin_pay_success_code']);
- // 推送订单
- event('OutPushListener', ['order_pay_push', ['order_id' => (int)$orderInfo['id']]]);
- //自定义消息-订单支付成功
- $orderInfo['time'] = date('Y-m-d H:i:s');
- $orderInfo['phone'] = $orderInfo['user_phone'];
- event('CustomNoticeListener', [$orderInfo['uid'], $orderInfo, 'order_pay_success']);
- //自定义事件-订单支付
- event('CustomEventListener', ['order_pay', [
- 'uid' => $orderInfo['uid'],
- 'id' => (int)$orderInfo['id'],
- 'order_id' => $orderInfo['order_id'],
- 'real_name' => $orderInfo['real_name'],
- 'user_phone' => $orderInfo['user_phone'],
- 'user_address' => $orderInfo['user_address'],
- 'total_num' => $orderInfo['total_num'],
- 'pay_price' => $orderInfo['pay_price'],
- 'pay_postage' => $orderInfo['pay_postage'],
- 'deduction_price' => $orderInfo['deduction_price'],
- 'coupon_price' => $orderInfo['coupon_price'],
- 'store_name' => $orderInfo['storeName'],
- 'add_time' => date('Y-m-d H:i:s', $orderInfo['add_time']),
- ]]);
- // 获取购物车信息
- $orderCartInfo = $orderInfoServices->getCartColunm(
- ['oid' => $orderInfo['id']],
- 'cart_info',
- 'unique'
- );
- $orderInfo['cart_info'] = [];
- foreach ($orderCartInfo as $k => $v) {
- $cart_info = is_string($v) ? json_decode($v, true) : $v;
- $orderInfo['cart_info'][] = $cart_info;
- }
- if ($orderInfo['is_lb'] == 1) { //建立礼包推荐关系
- @file_put_contents('quanju.txt', $orderInfo['lb_spread_uid'] . "-礼包推荐人2\r\n", 8);
- self::createGiftRecommendationRelationship($orderInfo);
- self::giftRecommendationBonus($orderInfo);
- }
- // if ($orderInfo['is_lb'] == 1) { //赠送礼包推荐奖金
- // @file_put_contents('quanju.txt', $orderInfo['is_lb'] . "-礼包订单2\r\n", 8);
- //
- // }
- $res = $res1 && $resPink;
- return false !== $res;
- }
- // 创建礼包推荐关系
- public function createGiftRecommendationRelationship($order)
- {
- // @file_put_contents('quanju.txt', json_encode($order) . "-创建礼包推荐关系\r\n", 8);
- try {
- // 获取用户信息
- $userServices = app()->make(UserServices::class);
- $user = $userServices->getUserInfo($order['uid']);
- @file_put_contents('quanju.txt', $user['spread_uid'] . "-创建礼包推荐关系\r\n", 8);
- if ($user['spread_uid'] > 0) {
- // 如果用户有推广人,说明是通过推广链接支付的,需要创建礼包推荐关系,关系表是store_product_gift_group,需要保存product_id,uid,group_pid(小组上级),pid(真直推上级),fake_pid(假直推上级(直推上级的下级小组满人下放下来的人)),一个商品加一个pid推荐上级会形成一个小组,第一个加进小组的会是第二个加入的小组上级(group_pid),当一个小组满10人后,就会让第十人作为fake_pid直推上级建立新的10人小组,但pid还是真正的直推上级
- // 获取商品ID
- $product_id = $order['cart_info'][0]['product_id'] ?? 0;
- if (!$product_id) return false;
- // 获取推广人UID
- $spread_uid = $user['spread_uid'];
- // 获取礼包服务
- $giftGroupService = app()->make(StoreProductGiftGroupService::class);
- // 检查是否已有该商品的推荐关系
- $existRelation = $giftGroupService->getOne([
- 'product_id' => $product_id,
- 'uid' => $order['uid']
- ]);
- if ($existRelation) return true;
- // 检查推广人是否是自己的下级团队成员(防止循环推荐)
- if ($this->isSubordinate($order['uid'], $spread_uid, $product_id, $giftGroupService)) {
- return false;
- }
- // 获取直推上级的推荐关系
- $parentRelation = $giftGroupService->getOne([
- 'product_id' => $product_id,
- 'uid' => $spread_uid
- ]);
- // 设置关系数据
- $data = [
- 'product_id' => $product_id,
- 'uid' => $order['uid'],
- 'pid' => $spread_uid, // 真直推上级
- 'create_time' => time()
- ];
- // 处理小组关系
- // 获取小组人数上限配置
- $groupMaxNum = sys_config('gift_group_num', 10);
-
- // 判断直推上级在该商品下是否有下级
- $hasSubordinate = $giftGroupService->getOne([
- 'product_id' => $product_id,
- 'pid' => $spread_uid
- ]);
- if ($hasSubordinate) {
- // 直推上级已有下级,获取所有下级
- $allSubordinates = $giftGroupService->getList([
- 'product_id' => $product_id,
- 'pid' => $spread_uid
- ], '*', 0, 0, 'create_time ASC');
- // 获取最后一个下级的group_pid来判断小组是否满人
- $lastMember = $allSubordinates[count($allSubordinates) - 1];
- $lastGroupPid = $lastMember['group_pid'];
- // 统计小组人数:从最后一个成员开始,沿group_pid向上追溯到根节点
- // 然后统计所有pid=spread_uid且group_pid在链条上的成员
- $groupChain = [];
- $currentGroupPid = $lastGroupPid;
- // 追溯group_pid链条
- while ($currentGroupPid > 0) {
- $groupChain[] = $currentGroupPid;
- @file_put_contents('quanju4.txt', $currentGroupPid . "-group_pid链条节点\r\n", 8);
- // 查找这个group_pid对应的成员
- $chainMember = $giftGroupService->getOne([
- 'product_id' => $product_id,
- 'uid' => $currentGroupPid
- ]);
- if ($chainMember) {
- $currentGroupPid = $chainMember['group_pid'];
- @file_put_contents('quanju4.txt', $chainMember['group_pid'] . "-下一节点group_pid\r\n", 8);
- } else {
- break;
- }
- }
- // 统计小组人数:pid=spread_uid 且 group_pid 在链条中或为0
- $groupCount = 0;
- foreach ($allSubordinates as $sub) {
- if (in_array($sub['group_pid'], $groupChain) || $sub['group_pid'] == 0) {
- $groupCount++;
- }
- }
- $isGroupFull = $groupCount >= $groupMaxNum;
- @file_put_contents('quanju4.txt', $groupCount . "-小组人数\r\n", 8);
- @file_put_contents('quanju4.txt', $groupMaxNum . "-系统设置人数\r\n", 8);
- @file_put_contents('quanju4.txt', $lastGroupPid . "-最后一个人的group_pid\r\n", 8);
- @file_put_contents('quanju4.txt', json_encode($groupChain) . "-group_pid链条\r\n", 8);
- @file_put_contents('quanju4.txt', ($isGroupFull ? '小组已满' : '小组未满') . "-小组状态\r\n", 8);
- @file_put_contents('quanju4.txt', end($groupChain) . "-小组最后一人uid(end groupChain)\r\n", 8);
- // 判断小组是否满人
- if ($isGroupFull) {
- // 小组满人,需要滑落到小组最后一人的直推下级小组
- // 小组最后一人是group_pid链条中的最后一个uid
- $lastGroupMemberUid = end($groupChain);
- @file_put_contents('quanju4.txt', $lastGroupMemberUid . "-小组最后一人的uid\r\n", 8);
- // 查找小组最后一人的直推下级(pid等于最后一人的uid)
- $lastDirectSubordinates = $giftGroupService->getList([
- 'product_id' => $product_id,
- 'pid' => $lastGroupMemberUid
- ], '*', 0, 0, 'create_time ASC');
- @file_put_contents('quanju4.txt', count($lastDirectSubordinates) . "-最后一人的直推下级人数\r\n", 8);
- if (count($lastDirectSubordinates) < $groupMaxNum) {
- // 最后一人的直推下级未满,新成员应该加入这个小组
- // 查找这些直推下级的小组关系,判断是否有小组(group_pid指向的)
- if (count($lastDirectSubordinates) > 0) {
- // 最后一人的直推下级有成员,查找这些成员的group_pid
- $firstDirectSubordinate = $lastDirectSubordinates[0];
- // 如果第一个直推下级的group_pid不为0,说明有小组,查找该小组的最后一人
- if ($firstDirectSubordinate['group_pid'] > 0) {
- // 查找该小组的所有成员
- $subGroupMembers = $giftGroupService->getList([
- 'product_id' => $product_id,
- 'group_pid' => $firstDirectSubordinate['group_pid']
- ], '*', 0, 0, 'create_time ASC');
- // 新成员的group_pid是该小组的最后一人
- $lastSubGroupMember = end($subGroupMembers);
- $data['group_pid'] = $lastSubGroupMember['uid'];
- // 新成员的fake_pid是该小组的最后一人
- $data['fake_pid'] = $lastSubGroupMember['uid'];
- @file_put_contents('quanju4.txt', $lastSubGroupMember['uid'] . "-小组的最后一人(新成员fake_pid)\r\n", 8);
- } else {
- // 第一直推下级的group_pid为0,说明没有小组,新成员成为第一人
- $data['group_pid'] = 0;
- $data['fake_pid'] = $lastGroupMemberUid;
- @file_put_contents('quanju4.txt', '最后一人的直推下级没有小组,新成员成为第一人\r\n', 8);
- }
- } else {
- // 最后一人的直推下级没有成员,新成员成为第一人
- $data['group_pid'] = 0;
- $data['fake_pid'] = $lastGroupMemberUid;
- @file_put_contents('quanju4.txt', '最后一人的直推下级没有成员,新成员成为第一人\r\n', 8);
- }
- } else {
- // 最后一人的直推下级也满了,新成员应该滑落到这个小组的最后一人
- if (count($lastDirectSubordinates) > 0) {
- // 查找最后一个人的小组
- $lastDirectSubordinate = end($lastDirectSubordinates);
- if ($lastDirectSubordinate['group_pid'] > 0) {
- $subGroupMembers = $giftGroupService->getList([
- 'product_id' => $product_id,
- 'group_pid' => $lastDirectSubordinate['group_pid']
- ], '*', 0, 0, 'create_time ASC');
- $lastSubGroupMember = end($subGroupMembers);
- $data['group_pid'] = 0;
- $data['fake_pid'] = $lastSubGroupMember['uid'];
- @file_put_contents('quanju4.txt', $lastSubGroupMember['uid'] . "-直推下级已满,新成员的fake_pid\r\n", 8);
- } else {
- $data['group_pid'] = 0;
- $data['fake_pid'] = $lastDirectSubordinate['uid'];
- @file_put_contents('quanju4.txt', $lastDirectSubordinate['uid'] . "-直推下级已满,无小组,新成员的fake_pid\r\n", 8);
- }
- } else {
- // 理论上不会走到这里
- $data['group_pid'] = 0;
- $data['fake_pid'] = $lastGroupMemberUid;
- }
- }
- } else {
- // 小组未满,新成员应该加入当前小组
- // 新成员的group_pid是最后一个下级的uid(成为小组长)
- $data['group_pid'] = $lastMember['uid'];
- $data['fake_pid'] = $spread_uid;
- }
- } else {
- // 直推上级没有下级,这是该推广人的第一个下级,建立新小组
- $data['group_pid'] = 0;
- $data['fake_pid'] = $spread_uid;
- }
- // 保存推荐关系
- return $giftGroupService->save($data);
- }
- return false;
- } catch (\Exception $e) {
- @file_put_contents('quanju4.txt', $e->getMessage() . "-创建订单报错内容\r\n", 8);
- @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
- @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
- @file_put_contents('quanju4.txt', $e->getTraceAsString() . "-堆栈\r\n", 8);
- }
- }
- // 礼包上级分红
- public function giftRecommendationBonus($order)
- {
- try {
- @file_put_contents('quanju.txt', json_encode($order) . "-礼包上级分红\r\n", 8);
- // 获取礼包服务
- $giftGroupService = app()->make(StoreProductGiftGroupService::class);
- $userServices = app()->make(UserServices::class);
- // 检查 cart_info 是否存在且非空
- @file_put_contents('quanju4.txt', '开始检查cart_info' . "\r\n", 8);
- if (!isset($order['cart_info']) || empty($order['cart_info'])) {
- @file_put_contents('quanju4.txt', 'cart_info不存在或为空' . "\r\n", 8);
- return false;
- }
- // 获取商品ID和商品价格作为佣金计算基数
- $product_id = $order['cart_info'][0]['product_id'] ?? 0;
- $brokerage_price = $order['cart_info'][0]['truePrice'] ?? 0;
- @file_put_contents('quanju4.txt', 'product_id=' . $product_id . ', brokerage_price=' . $brokerage_price . "\r\n", 8);
- if (!$product_id || !$brokerage_price) {
- @file_put_contents('quanju4.txt', 'product_id或brokerage_price为0,返回false' . "\r\n", 8);
- return false;
- }
- @file_put_contents('quanju4.txt', $product_id . "-商品id\r\n", 8);
- // 获取用户的推荐关系
- $userRelation = $giftGroupService->getOne([
- 'product_id' => $product_id,
- 'uid' => $order['uid']
- ]);
- if (!$userRelation) return false;
- $userInfo = $userServices->getOne(['uid' => $order['uid']]);
- // 获取佣金服务(一次性创建,复用)
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- //冻结时间
- $frozenTime = time() + intval(sys_config('extract_time')) * 86400;
- // 直推上级分成
- $directBrokerage = $brokerage_price * (sys_config('gift_direct_referral', 10) / 100);
- @file_put_contents('quanju4.txt', json_encode($userRelation) . "-礼包推荐关系表\r\n", 8);
- if ($userRelation['pid'] == $userRelation['fake_pid']) {
- // 真直推上级和假直推上级相同,全部给直推上级
- $pid_brokerage = $userServices->getOne(['uid' => $userRelation['pid']]);
- $userServices->bcInc($userRelation['pid'], 'brokerage_price', $directBrokerage, 'uid');
- @file_put_contents('quanju4.txt', $directBrokerage . "-直推奖励\r\n", 8);
- $balance = bcadd($pid_brokerage['brokerage_price'], $directBrokerage, 2);
- $userBrokerageServices->income('get_direct_referral_brokerage', $userRelation['pid'], [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($order['pay_price']),
- 'number' => $directBrokerage,
- 'frozen_time' => $frozenTime
- ], $balance, $order['id']);
- } else {
- // 真直推上级和假直推上级不同,各分50%
- $halfBrokerage = $directBrokerage * (sys_config('gift_spread_spot_bonus', 50) / 100);
- // 真上级分得50%
- $pid_brokerage = $userServices->getOne(['uid' => $userRelation['pid']]);
- $userServices->bcInc($userRelation['pid'], 'brokerage_price', $halfBrokerage, 'uid');
- $balance = bcadd($pid_brokerage['brokerage_price'], $halfBrokerage, 2);
- $userBrokerageServices->income('get_direct_referral_brokerage', $userRelation['pid'], [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($order['pay_price']),
- 'number' => $halfBrokerage,
- 'frozen_time' => $frozenTime
- ], $balance, $order['id']);
- // 假上级分得50%
- $fake_pid_brokerage = $userServices->getOne(['uid' => $userRelation['fake_pid']]);
- $userServices->bcInc($userRelation['fake_pid'], 'brokerage_price', $halfBrokerage, 'uid');
- $balance = bcadd($fake_pid_brokerage['brokerage_price'], $halfBrokerage, 2);
- $userBrokerageServices->income('get_direct_referral_brokerage', $userRelation['fake_pid'], [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($order['pay_price']),
- 'number' => $halfBrokerage,
- 'frozen_time' => $frozenTime
- ], $balance, $order['id']);
- }
- // 见点奖处理
- $spotBrokerage = $brokerage_price * (sys_config('gift_spot_bonus', 2.5) / 100);
- // 1. 向上查找:先找小组上级,没有就找直推上级
- $spotUpLimit = sys_config('gift_spot_bonus_up', 15);
- $currentUid = $order['uid']; // 从当前用户开始
- $currentRelation = $userRelation; // 当前用户的推荐关系
- for ($i = 0; $i < $spotUpLimit; $i++) {
- $nextUid = null;
- // 优先查找小组上级
- if (!empty($currentRelation['group_pid'])) {
- $nextUid = $currentRelation['group_pid'];
- } else if (!empty($currentRelation['pid'])) {
- // 没有小组上级,查找直推上级
- $nextUid = $currentRelation['pid'];
- }
- // 找不到上级,退出循环
- if (!$nextUid) break;
- // 给上级发放见点奖
- $currentBrokerage = $userServices->getOne(['uid' => $nextUid]);
- $userServices->bcInc($nextUid, 'brokerage_price', $spotBrokerage, 'uid');
- $balance = bcadd($currentBrokerage['brokerage_price'], $spotBrokerage);
- $userBrokerageServices->income('get_spot_brokerage', $nextUid, [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($order['pay_price']),
- 'number' => $spotBrokerage,
- 'frozen_time' => $frozenTime
- ], $balance, $order['id']);
- @file_put_contents('quanju4.txt', '向上发放见点奖: uid=' . $nextUid . ', 金额=' . $spotBrokerage . "\r\n", 8);
- // 查找下一个上级的推荐关系
- $currentRelation = $giftGroupService->getOne([
- 'product_id' => $product_id,
- 'uid' => $nextUid
- ]);
- // 如果找不到推荐关系,退出循环
- if (!$currentRelation) break;
- }
- // 2. 向下查找:先找直推下级,不够再找同组下级
- $spotDownLimit = sys_config('gift_spot_bonus_down', 15);
- $totalDownSpots = 0;
-
- // 2.1 先找直推下级
- $directSubordinates = $giftGroupService->getList([
- 'product_id' => $product_id,
- 'pid' => $order['uid']
- ], '*', 0, $spotDownLimit, 'create_time asc');
- foreach ($directSubordinates as $subordinate) {
- $subBrokerage = $userServices->getOne(['uid' => $subordinate['uid']]);
- $userServices->bcInc($subordinate['uid'], 'brokerage_price', $spotBrokerage, 'uid');
- $balance = bcadd($subBrokerage['brokerage_price'], $spotBrokerage);
- $userBrokerageServices->income('get_spot_brokerage', $subordinate['uid'], [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($order['pay_price']),
- 'number' => $spotBrokerage,
- 'frozen_time' => $frozenTime
- ], $balance, $order['id']);
- @file_put_contents('quanju4.txt', '向下发放见点奖给直推下级: uid=' . $subordinate['uid'] . ', 金额=' . $spotBrokerage . "\r\n", 8);
- $totalDownSpots++;
- }
- // 2.2 如果直推下级不够,找同组下级
- if ($totalDownSpots < $spotDownLimit) {
- // 获取所有pid与当前用户pid相同的同级用户
- $samePidMembers = $giftGroupService->getList([
- 'product_id' => $product_id,
- 'pid' => $userRelation['pid']
- ], '*', 0, 0, 'create_time asc');
- // 找到当前用户在同级用户中的位置
- $currentIndex = null;
- foreach ($samePidMembers as $index => $member) {
- if ($member['uid'] == $order['uid']) {
- $currentIndex = $index;
- break;
- }
- }
- // 从当前用户之后的同级用户开始,查找同组下级
- if ($currentIndex !== null) {
- for ($i = $currentIndex + 1; $i < count($samePidMembers) && $totalDownSpots < $spotDownLimit; $i++) {
- $member = $samePidMembers[$i];
- // 检查这个同级用户的group_pid层级是否在当前用户或其下级中
- $isGroupSubordinate = $this->isGroupSubordinate($order['uid'], $member['group_pid'], $product_id, $giftGroupService);
- if ($isGroupSubordinate) {
- $memberBrokerage = $userServices->getOne(['uid' => $member['uid']]);
- $userServices->bcInc($member['uid'], 'brokerage_price', $spotBrokerage, 'uid');
- $balance = bcadd($memberBrokerage['brokerage_price'], $spotBrokerage);
- $userBrokerageServices->income('get_spot_brokerage', $member['uid'], [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($order['pay_price']),
- 'number' => $spotBrokerage,
- 'frozen_time' => $frozenTime
- ], $balance, $order['id']);
- @file_put_contents('quanju4.txt', '向下发放见点奖给同组下级: uid=' . $member['uid'] . ', 金额=' . $spotBrokerage . "\r\n", 8);
- $totalDownSpots++;
- }
- }
- }
- }
- return true;
- } catch (\Exception $e) {
- @file_put_contents('quanju4.txt', $e->getMessage() . "-上级分红报错内容\r\n", 8);
- @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
- @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
- @file_put_contents('quanju4.txt', $e->getTraceAsString() . "-堆栈\r\n", 8);
- }
- }
- /**
- * 检查某个用户是否是另一个用户的小组下级(通过group_pid层级关系)
- * @param int $baseUid 基准用户uid
- * @param int $groupPid 要检查的group_pid
- * @param int $productId 商品ID
- * @param StoreProductGiftGroupService $giftGroupService 礼包服务
- * @param array $checked 已检查的用户(防止循环)
- * @return bool 如果是小组下级返回true,否则返回false
- */
- protected function isGroupSubordinate($baseUid, $groupPid, $productId, $giftGroupService, $checked = [])
- {
- // 如果已经检查过这个groupPid,跳过(防止循环)
- if (isset($checked[$groupPid])) {
- return false;
- }
- $checked[$groupPid] = true;
- // 如果groupPid是0,说明没有小组关系
- if (empty($groupPid)) {
- return false;
- }
- // 如果groupPid等于基准用户,说明是直接下级
- if ($groupPid == $baseUid) {
- return true;
- }
- // 递归检查group_pid是否是基准用户的下级
- $member = $giftGroupService->getOne([
- 'product_id' => $productId,
- 'uid' => $groupPid
- ]);
- if ($member) {
- // 继续检查这个用户的group_pid
- return $this->isGroupSubordinate($baseUid, $member['group_pid'], $productId, $giftGroupService, $checked);
- }
- return false;
- }
- /**
- * 检查某个用户是否是另一个用户的下级团队成员
- * @param int $uid 当前用户
- * @param int $targetUid 要检查的目标用户
- * @param int $productId 商品ID
- * @param StoreProductGiftGroupService $giftGroupService 礼包服务
- * @param array $checked 已检查的用户(防止循环)
- * @return bool 如果是下级返回true,否则返回false
- */
- protected function isSubordinate($uid, $targetUid, $productId, $giftGroupService, $checked = [])
- {
- // 如果已经检查过这个用户,跳过(防止循环)
- if (isset($checked[$uid])) {
- return false;
- }
- $checked[$uid] = true;
- // 查找该用户的所有直推下级
- $subordinates = $giftGroupService->getList([
- 'product_id' => $productId,
- 'pid' => $uid
- ], 'uid', 0, 0, 'create_time asc');
- // 检查目标用户是否在直推下级中
- foreach ($subordinates as $sub) {
- if ($sub['uid'] == $targetUid) {
- return true; // 找到了,是下级
- }
- }
- // 递归检查每个下级的下级
- foreach ($subordinates as $sub) {
- if ($this->isSubordinate($sub['uid'], $targetUid, $productId, $giftGroupService, $checked)) {
- return true; // 在下级的下级中找到了
- }
- }
- return false; // 没找到,不是下级
- }
- }
|