<?php


namespace App\Modules\Report\Services;


use Psr\Http\Message\ResponseInterface;
use App\Cache\User\UserCache;
use App\Modules\User\Services\UserService;
use GuzzleHttp\Client;

class ReportService
{
    /**
     * 次留回传
     * @param $uid
     * @return false|mixed
     */
    public static function reportActive($uid)
    {
        if (empty($uid)) {
            return false;
        }

        // 获取用户缓存
        $now       = date('Y-m-d H:i:s');
        $userCache = UserCache::getUserInfo($uid);
        if (empty($userCache)) {
            $user = UserService::getById($uid);
            if ($user === null) {
                return false;
            }

            // 初始化缓存
            $userCache = [
                'uid'           => $uid,
                'channel_id'    => getProp($user, 'distribution_channel_id', 0),
                'send_order_id' => getProp($user, 'send_order_id', 0),
                'register_time' => $user->created_at->format('Y-m-d H:i:s'),
                'active_time'   => $now
            ];
            UserCache::setUserInfo($uid, $userCache);
        }

        // 获取注册时间、活跃时间
        $registerAt      = getProp($userCache, 'register_time');
        $activeAt        = getProp($userCache, 'active_time');
        $registerYmd     = date('Y-m-d', strtotime($registerAt));
        $registerNextYmd = date('Y-m-d', strtotime($registerYmd . ' +1 day'));
        $activeYmd       = date('Y-m-d', strtotime($activeAt));
        $todayYmd        = date('Y-m-d');

        // 判断是否是次留回传
        if ($registerYmd === $activeYmd && $todayYmd === $registerNextYmd) {
            myLog('reportSActive')->info('', compact('uid', 'registerYmd'));
            self::report('/api/reportSActive', [
                'uid'           => getProp($userCache, 'uid'),
                'channel_id'    => getProp($userCache, 'channel_id'),
                'register_time' => getProp($userCache, 'register_time'),
                'active_time'   => getProp($userCache, 'active_time'),
                'platform'      => 'zhuishuyun',
            ]);
        }

        // 更新用户活跃时间
        return UserCache::setUserInfo($uid, ['active_time' => $now]);
    }

    /**
     * @param $user
     * @return ResponseInterface
     */
    public static function reportRegister($user)
    {
        return self::report('/api/reportRegister', [
            'platform' => 'zhuishuyun',
            'register' => [
                'uid'           => $user->id,
                'channel_id'    => $user->channel_id,
                'register_time' => $user->created_at->format('Y-m-d H:i:s'),
                'register_ip'   => $user->register_ip,
            ]
        ]);
    }

    /**
     * @param $order
     * @return false|ResponseInterface
     */
    public static function reportRecharge($order)
    {
        $channelId = getProp($order, 'distribution_channel_id');
        if (empty($channelId)) {
            return false;
        }

        return self::report('/api/reportOrder', [
            'platform' => 'zhuishuyun',
            'order'    => [
                'uid'              => getProp($order, 'uid'),
                'order_no'         => getProp($order, 'trade_no'),
                'price'            => getProp($order, 'price'),
                'channel_id'       => $channelId,
                'pay_time'         => getProp($order, 'pay_end_at'),
                'order_created_at' => $order->created_at->format('Y-m-d H:i:s')
            ]
        ]);
    }

    /**
     * 上报
     * @param $uri
     * @param $data
     * @return ResponseInterface
     */
    private static function report($uri, $data)
    {
        $client = new Client(['timeout' => 3, 'verify' => false]);

        // 执行上报
        $url         = env('REPORT_URI') . $uri;
        $result      = $client->post($url, [
            'headers' => [
                'x-code' => 'Mvnx1Yr3O8i!TS5u'
            ],
            'json'    => $data
        ]);
        $statusCode  = $result->getStatusCode();
        $contentJson = $result->getBody()->getContents();
        $contentArr  = json_decode($contentJson, true);
        myLog('report')->info($uri, compact('data', 'statusCode', 'contentArr'));

        return $contentArr;
    }
}