<?php

namespace App\Exports\Sheets;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;
use App\Models\S2ShopOrder;
use App\Models\AgentUser;
use App\Models\ActivityOrder;
use App\Models\User;

class BandMember implements FromCollection,WithTitle
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        //手环抽奖名单  有手环订单的VIP + 有余额的批发商
        $user_ids = S2ShopOrder::where("sku_id", 86)->whereIn("status", [2,6])->with("user")->whereHas("agentUser", function($sql) {
            $sql->where("level", AgentUser::AgentVIP);
        })->whereHas("lottoCode")->pluck("user_id")->toArray();
        $rows = [
            ["用户ID", "姓名", "手机号", "类型"]
        ];
        $users = User::whereIn("id", $user_ids)->get();
        foreach($users as $user) {
            $rows[] = [
                $user->id,
                $user->name,
                $user->mobile,
                "有手环订单的VIP"
            ];
        }

        $user_ids = ActivityOrder::whereHas("agentUser",function ($sql) {
            $sql->where("level", AgentUser::AgentLevelBeing);
        })->with("user")->where('is_pay', 1)->where('residue_amount', ">", 0)->whereIn('activity_id', [17,18])->pluck("user_id")->toArray();
        $users = User::whereIn("id", $user_ids)->get();

        foreach($users as $user) {
            $rows[] = [
                $user->id,
                $user->name,
                $user->mobile,
                "有余额的批发商"
            ];
        }
        return collect($rows);
    }

    /**
     * @return string
     */
    public function title(): string
    {
        return '手环抽奖名单';
    }
}
