Relation.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace frontendApi\modules\v1\models;
  3. use common\helpers\Cache;
  4. use common\helpers\LoggerTool;
  5. class Relation extends \common\components\ActiveRecord
  6. {
  7. const LOOP_FINISH = 1;
  8. const LOOP_CONTINUE = 2;
  9. private $_limit = 5000;
  10. /**
  11. * 循环推荐网络的父级
  12. * @param $userId
  13. * @param callable $callbackFunc
  14. * @param int $offset
  15. * @return bool
  16. */
  17. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0): bool
  18. {
  19. $allParents = Cache::getAllRelationParents($userId);
  20. LoggerTool::debug(json_encode(['loopRelationParentDo', $userId ,$allParents]));
  21. $allData = array_slice($allParents, $offset, $this->_limit);
  22. unset($allParents);
  23. if ($allData) {
  24. foreach ($allData as $data) {
  25. $funcResult = $callbackFunc($data);
  26. if ($funcResult === self::LOOP_FINISH) {
  27. return true;
  28. } elseif ($funcResult === self::LOOP_CONTINUE) {
  29. continue;
  30. }
  31. unset($data, $funcResult);
  32. }
  33. unset($allData);
  34. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  35. }
  36. return true;
  37. }
  38. }