<?php

namespace frontendApi\modules\v1\models;

use common\helpers\Cache;
use common\helpers\LoggerTool;

class Relation extends \common\components\ActiveRecord
{
    const LOOP_FINISH = 1;
    const LOOP_CONTINUE = 2;
    private $_limit = 5000;

    /**
     * 循环推荐网络的父级
     * @param $userId
     * @param callable $callbackFunc
     * @param int $offset
     * @return bool
     */
    public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0): bool
    {
        $allParents = Cache::getAllRelationParents($userId);
        $allData = array_slice($allParents, $offset, $this->_limit);
        unset($allParents);
        if ($allData) {
            foreach ($allData as $data) {
                $funcResult = $callbackFunc($data);
                if ($funcResult === self::LOOP_FINISH) {
                    return true;
                } elseif ($funcResult === self::LOOP_CONTINUE) {
                    continue;
                }
                unset($data, $funcResult);
            }

            unset($allData);
            return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
        }
        return true;
    }
}