<?php

namespace App\Console\Commands;

use App\Facades\HttpService;
use App\Models\LotteryMember;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info("同步经销商抽奖名单");
        $url = config('app.shop_url') . "admin/agent/lotto/codes?export=1";
        $res = HttpService::getData($url);
        $members = $res['data'];
        foreach ($members as $member) {
            $user_id = 0;
            if ($member['user']['accid']) {
                $user_id = explode('_', $member['user']['accid'])[1];
            }
            $lottery_member = LotteryMember::where('lottery_id', 1)->where('code', $member['code'])->first();
            if (empty($lottery_member)) {
                LotteryMember::create(['user_id'=>$user_id, 'lottery_id'=>1, 'name'=>$member['user']['nickname'], 'mobile'=>$member['user']['mobile'],
                    'code'=>$member['code']
                ]);
            }
        }
        return Command::SUCCESS;
    }
}
