<?php

namespace App\Exports\Sheets;

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

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