<?php

namespace App\Console\Commands\Exports;

use App\Models\User\UserModel;
use Illuminate\Console\Command;
use Illuminate\Http\File;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class NoticeUserCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'export:notice_user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '导出通知用户';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $uuid = uuid();
        $path = sprintf("media/%s", date('Y-m-d'));
        $local_file = sprintf("storage/app/public/%s.csv", $uuid);
        $fp = fopen($local_file, 'w');
        fputcsv($fp, [
            'to_user',
        ]);
//
//        $users = DB::connection("mysql_datalog")
//            ->table("datalog.app_user_visit_list")
//            ->where("version","1.1.1")
//            ->where("appid","wx3e6bb0f55902391b")
//            ->distinct()
//            ->pluck('user_id');
//        $users = DB::connection("mysql_datalog")
//            ->table('datalog.app_visit_user')
//            ->whereIn('user_id',$users)
//            ->pluck('uid');
        $users = DB::table('ta_users')->pluck('uid');

        foreach ($users as $uid) {
            $user = UserModel::find($uid);
            fputcsv($fp, [
                json_encode($user->getAuth()),
            ]);
        }
        fclose($fp);

        $file = new File($local_file);
        $file = Storage::disk('oss')->putFileAs($path, $file, $uuid . '.' . $file->getExtension());
        $fileUrl = Storage::disk('oss')->url($file);
        dd($fileUrl);
        return $fileUrl;
    }
}
