<?php

namespace Modules\User\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Modules\Mall\Models\Order;
use Modules\User\Models\User;

trait RankDataTrait
{
    /**
     * 根据时间范围获取推荐VIP排行
     *
     * @param  array  $timeBetween
     * @return mixed
     */
    public function getRankDataTrait(array $timeBetween)
    {
        $vipJion = DB::table('user_identity')
            ->whereBetween('started_at', $timeBetween)
            ->where('ended_at', '>=', now())
            ->select('user_id as middle_user_Id',
                DB::raw('any_value(started_at) as started_at'),
                DB::raw('any_value(identity_id) as identity_id'))
            ->groupBy('middle_user_Id');

        $vipRelationJion = DB::table('user_relations')
            ->leftJoinSub($vipJion, 'vipJion', function ($join) {
                $join->on('user_relations.user_id', '=', 'vipJion.middle_user_Id');
            })
            ->where('identity_id', '>', 1)
            ->select('parent_id',
                DB::raw('ifnull(count(user_id),0) as countChild'),
                DB::raw('max(started_at) as lastOpenTime'))
            ->groupBy('parent_id');

        return User::query()
            ->leftJoinSub($vipRelationJion, 'vipRelationJion', function ($join) {
                $join->on('users.id', '=', 'vipRelationJion.parent_id');
            })
            ->where('countChild', '>', 0)
            ->orderBy('countChild', 'desc')
            ->orderBy('lastOpenTime', 'asc')
            ->limit(10)
            ->get();
    }

    public function getTotalUser(array $timeBetween)
    {
        $relationJion = DB::table('user_relations')
            ->whereBetween('created_at', $timeBetween)
            ->select('parent_id',
                DB::raw('ifnull(count(user_id),0) as countChild'),
                DB::raw('max(created_at) as lastRegTime')
            )
            ->groupBy('parent_id');

        return User::query()
            ->leftJoinSub($relationJion, 'relationJion', function ($join) {
                $join->on('users.id', '=', 'relationJion.parent_id');
            })
            ->where('countChild', '>', 0)
            ->orderBy('countChild', 'desc')
            ->orderBy('lastRegTime', 'asc')
            ->limit(10)
            ->get();
    }


    /**
     * Notes: 功德榜
     *
     * @Author: 玄尘
     * @Date: 2022/12/31 18:25
     */
    public function getAllUserJz()
    {
        return User::query()
            ->withSum([
                'orderItems' => function (Builder $query) {
                    $query->whereHas('order', function ($q) {
                        $q->whereIn('state', [
                            Order::STATUS_PAID,
                            Order::STATUS_DELIVERED,
                            Order::STATUS_SIGNED,
                            Order::STATUS_COMPLETED,
                        ]);
                    });
                }
            ], 'qty')
            ->latest('order_items_sum_qty')
            ->oldest('id')
            ->take(10)
            ->get()
            ->where('order_items_sum_qty', '>', 0)
            ->map(function ($info) {

                return [
                    'user'  => [
                        'user_id'  => $info->id,
                        'username' => substr_replace($info->username, '****', -4, 4),
                        'nickname' => $info->info->nickname_text ?? '',
                        'avatar'   => $info->info->avatar ?? '',
                    ],
                    'total' => $info->order_items_sum_qty > 0 ? $info->order_items_sum_qty : '-',
                ];
            })
            ->pad(10, [
                'user'  => [
                    'user_id'  => 0,
                    'username' => '-',
                    'nickname' => '-',
                    'avatar'   => '-',
                ],
                'total' => '-',
            ]);

    }
}
