<?php

/**
 * Description of JwUser.php
 *
 * @author haiming
 * Date: 2021/5/21
 * Time: 10:32 PM
 */

namespace  Api\PhpServices\JwUser;

use Api\PhpUtils\Common\TimeOut;
use Api\PhpUtils\Http\Request;
use App\Exception\custom\CodeSpecialException;
use Api\PhpUtils\Log\FileLog;
use Api\PhpUtils\Mon\MonUtil;

class JwUser
{
    /**
     * 获取简网用户信息
     *
     *@param $params['mobile'] 手机号
     *
     */
    public function getUserInfo($params)
    {

        $url = config('interface', 'service.jw_user.get_user_info');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['mobile'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["mobilePhone" => $params['mobile']];
        $begin =  microtime(true);
        $user_info  = (new Request())->get($url, $params);
        $end = microtime(true);
        $total_time = round(($end - $begin), 4) * 1000;
        if (!empty($user_info['response'])) {
            $code = $user_info['response']['code'];
        } else {
            $code = -1;
        }
        $env = \Yaf\Application::app()->environ() ?? "test";
        MonUtil::proxyMon($url, $code, 'jw_user_'.$env, $total_time);
        FileLog::info('jw_user_getUserInfo', json_encode($user_info));
        if (!$user_info) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($user_info['response']) ? $user_info['response'] : [];
    }
    /**
     * 获取单条信息
     *
     */
    public function getUserByUserId($params)
    {
        $url = config('interface', 'service.jw_user.get_user_info_by_user_id');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['user_id'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["userId" => $params['user_id']];
        $begin =  microtime(true);
        $user_info  = (new Request())->post($url, $params);
        $end = microtime(true);
        $total_time = round(($end - $begin), 4) * 1000;
        if (!empty($user_info['response'])) {
            $code = $user_info['response']['code'];
        } else {
            $code = -1;
        }

        $env = \Yaf\Application::app()->environ() ?? "test";
        MonUtil::proxyMon($url, $code, 'jw_user_'.$env, $total_time);
        FileLog::info('jw_user_getUserByUserId', json_encode($user_info));
        if (!$user_info) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($user_info['response']) ? $user_info['response'] : [];
    }

    public function getUserList($params)
    {
        $url = config('interface', 'service.jw_user.get_user_list');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['user_id'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["userIds" => $params['user_id']];
        $begin =  microtime(true);
        $user_info  = (new Request())->post($url, $params);
        $end = microtime(true);
        $total_time = round(($end - $begin), 4) * 1000;
        if (!empty($user_info['response'])) {
            $code = $user_info['response']['code'];
        } else {
            $code = -1;
        }
        $env = \Yaf\Application::app()->environ() ?? "test";
        MonUtil::proxyMon($url, $code, 'jw_user_'.$env, $total_time);
        FileLog::info('jw_user_getUserList', json_encode($user_info));
        if (!$user_info) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($user_info['response']) ? $user_info['response'] : [];
    }


    /**
     * 获取城市树接口
     *
     *
     */
    public function getAllCityTree()
    {
        $url = config('interface', 'service.jw_user.get_all_city_tree');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }
        $params = [];
        //$city_list  = (new TimeOut())->runGet($url, $params);
        $city_list  = (new Request())->get($url, $params);
        FileLog::info('jw_user_getAllCityTree', json_encode($city_list));
        if (!$city_list) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($city_list['response']) ? $city_list['response'] : [];
    }


    /**
     * 保存用户与token的对应关系接口
     * 支持的token prefix：MMPP 小米推送，UMPP 友盟推送，GTPP 个推
     *
     */
    public function savePushToken($params)
    {
        $url = config('interface', 'service.jw_user.save_push_token');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params)) {
            throw new CodeSpecialException("failed");
        }
        //$pushToken  = (new TimeOut())->runPost($url, $params);
        $begin =  microtime(true);
        $pushToken  = (new Request())->post($url, $params);
        $end = microtime(true);
        $total_time = round(($end - $begin), 4) * 1000;
        if (!empty($pushToken['response'])) {
            $code = $pushToken['response']['code'];
        } else {
            $code = -1;
        }
        $env = \Yaf\Application::app()->environ() ?? "test";
        MonUtil::proxyMon($url, $code, 'jw_user_'.$env, $total_time);
        FileLog::info('jw_user_savePushToken', json_encode($pushToken));
        if (!$pushToken) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($pushToken['response']) ? $pushToken['response'] : [];
    }

    /**
     * 获取简网用户动态
     */

    public static function getUserListWithDynamic($params)
    {
        $url = config('interface', 'service.jw_user.get_user_dynamic');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['user_id'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["userIds" => $params['user_id']];
        $begin =  microtime(true);
        $list  = (new Request())->post($url, $params);
        $end = microtime(true);
        $total_time = round(($end - $begin), 4) * 1000;
        if (!empty($list['response'])) {
            $code = $list['response']['code'];
        } else {
            $code = -1;
        }
        $env = \Yaf\Application::app()->environ() ?? "test";
        MonUtil::proxyMon($url, $code, 'jw_user_'.$env, $total_time);
        FileLog::info('jw_user_getUserListWithDynamic', json_encode($list));
        if (!$list) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($list['response']) ? $list['response'] : [];
    }

    /**
     * 获取简网用户历史赞
     */

    public static function getUserDigg($params)
    {
        $url = config('interface', 'service.jw_user.get_user_digg');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['user_id'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["userId" => $params['user_id']];
        $begin =  microtime(true);
        $list  = (new Request())->post($url, $params);
        $end = microtime(true);
        $total_time = round(($end - $begin), 4) * 1000;
        if (!empty($list['response'])) {
            $code = $list['response']['code'];
        } else {
            $code = -1;
        }
        $env = \Yaf\Application::app()->environ() ?? "test";
        MonUtil::proxyMon($url, $code, 'jw_user_'.$env, $total_time);
        FileLog::info('jw_user_getUserDigg', json_encode($list));
        if (!$list) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($list['response']) ? $list['response'] : [];
    }

    /**
     * 给简网同步生活号
     */
    public static function saveUpdateLife($params)
    {
        $url = config('interface', 'service.jw_user.save_update_life');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }
        $begin =  microtime(true);
        $ret  = (new Request())->post($url, $params);
        $end = microtime(true);
        $total_time = round(($end - $begin), 4) * 1000;
        if (!empty($ret['response'])) {
            $code = $ret['response']['code'];
        } else {
            $code = -1;
        }
        $env = \Yaf\Application::app()->environ() ?? "test";
        MonUtil::proxyMon($url, $code, 'jw_user_'.$env, $total_time);
        FileLog::info('jw_user_saveUpdateLife', json_encode($ret));
        if (!$ret) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($ret['response']) ? $ret['response'] : [];
    }

    /**
     * 获取简网微信openid
     */
    public static function getWechatOpenid($params)
    {
        $url = config('interface', 'service.jw_user.get_wechat_openid');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }
        $begin =  microtime(true);
        $ret  = (new Request())->post($url, $params);
        $end = microtime(true);
        $total_time = round(($end - $begin), 4) * 1000;
        if (!empty($ret['response'])) {
            $code = $ret['response']['code'];
        } else {
            $code = -1;
        }
        $env = \Yaf\Application::app()->environ() ?? "test";
        MonUtil::proxyMon($url, $code, 'jw_user_'.$env, $total_time);
        FileLog::info('getWechatOpenid', json_encode($ret));
        if (!$ret) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($ret['response']) ? $ret['response'] : [];
    }

    /**
     * 是否关注公众号
     */

    public static function isSubscribe($params)
    {
        $url = config('interface', 'service.jw_user.is_subscribe');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }
        $begin =  microtime(true);
        $ret  = (new Request())->post($url, $params);
        $end = microtime(true);
        $total_time = round(($end - $begin), 4) * 1000;
        if (!empty($ret['response'])) {
            $code = $ret['response']['code'];
        } else {
            $code = -1;
        }
        $env = \Yaf\Application::app()->environ() ?? "test";
        MonUtil::proxyMon($url, $code, 'jw_user_'.$env, $total_time);
        FileLog::info('getWechatOpenid', json_encode($ret));
        if (!$ret) {
            throw new CodeSpecialException("timeout");
        }
        return !empty($ret['response']) ? $ret['response'] : [];
    }
}
