<?php
/**
 * 接入API_GATEWAY实现平台，wiki地址：http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=34241933
 * Description of TuffyService.php
 *
 * @author caoyunmeng
 * Date: 2021/2/18
 * Time: 2:20 PM
 */

namespace Api\PhpServices\Tuffy;

use Api\PhpUtils\Http\Request;

class TuffyService{

    private $request = false;

    public function __construct() {
        $this->request = new Request();
    }

    /**
     * 计算前端全量Bucket（基于画像数据，用于APP等有画像场景）
     *
     * @param $userid
     * @param $appid
     * @return bool|mixed
     * @throws
     */
    function getFullTuffyBucket($userid, $appid)
    {
        if (empty($userid)) {
            throw new \Exception('userid params is empty');
        }

        if (empty($appid)) {
            throw new \Exception('appid params is empty');
        }

        $options = array(
            'userId' => $userid,
            'appId' => $appid
        );

        return $this->request->post(\Yaconf::get('interface.common.service.tuffy.full_buckets'),
            $options,
            1,
            'json',
            array('Content-type' => 'application/json')
        );
    }

    /**
     * 计算前端基础Bucket（不取画像，用于Web页面等无画像场景）
     * 
     * @param $userid
     * @param $appid
     * @param $extraInfo array 补充信息：举例：net、extraInfo、brand、androidId、net、type 等，所有客户端请求的信息统一传递到extraInfo里
     * $extraInfo = array(
            'deviceid'      => $_GET['deviceid'],
            'type'          => $_GET['type'],
            'cv'            => $cv,
            'ip'            => ip(),
            'version'       => '999999',
            'userid'        => $wuid,
            'net'           => isset($_GET['net'])? $_GET['net']: '',
            'createTime'    => $_GET['createTime']??'',
            'appid'         => $_GET['appid'],
            'env'           => $_GET['env']??'',
            'distribution'  => isset($_GET['distribution'])? $_GET['distribution']:''
            'net'           => isset($_GET['net]) ? $_GET['net]:''
        );
     * @return bool|mixed
     * @throws
     */
    function getBasicTuffyBucket($userid, $appid, $extraInfo = array())
    {
        if (empty($userid)) {
            throw new \Exception('userid params is empty');
        }

        if (empty($appid)) {
            throw new \Exception('appid params is empty');
        }

        $options = array(
            'userId' => $userid,
            'appId' => $appid
        );

        if(!empty($extraInfo)){
            $options['extraInfo'] = $extraInfo;
        }

        return $this->request->post(
            \Yaconf::get('interface.common.service.tuffy.basic_buckets'),
            $options,
            0.1,
            'json',
            array('Content-type' => 'application/json')
        );
    }

    /**
     * 获取用户bucket
     * 
     * @param $userid
     * @param $appid
     * @return bool|mixed
     * @throws
     */
    function getUserBucket($userid, $appid){
        if (empty($userid)) {
            throw new \Exception('userid params is empty');
        }

        if (empty($appid)) {
            throw new \Exception('appid params is empty');
        }

        $options = array(
            'service_name' => 'app_api',
            'userid' => $userid,
            'appId' => $appid,
        );

        return $this->request->get(
            \Yaconf::get('interface.common.service.tuffy.user_buckets'),
            $options,
            0.1,
            array('Content-type' => 'application/json')
        );
    }
}