<?php

namespace App\Console\Commands\User;

use App\Managers\CouponManager;
use App\Models\NoticeModel;
use App\Models\User\UserModel;
use App\Models\User\UserSysTagModel;
use App\Services\Pay\CouponService;
use Illuminate\Console\Command;

class SupervipCommand extends Command
{
    protected $signature = "supervip:store";

    protected $description = "";

    public function handle()
    {
        $tags = UserSysTagModel::where([
            ['supvip_endat', '>', time()],
            ['last_be_supvip_at', '<', 1559116560]
        ])->get();
        foreach ($tags as $tag) {
            $user = UserModel::findOrFail($tag->uid);
            // 1. 判断要给用户增加超级会员天数
            $xuvip = false;
            $user->supvip_endat > time() && $xuvip = true;
            $addday = 1;
            $xuvip ? $user->supvip_endat += $addday * 86400 : $user->supvip_endat = mktime(
                0,
                0,
                0
            ) + (1 + $addday) * 86400;

            // 2. 记录kdgx_fpdx_user_record增加天数、修改到期时间
            if (UserSysTagModel::where('uid', $user->uid)->exists()) {
                $update = array('supvip_endat' => $user->supvip_endat, 'last_supvip_expire_notice_at' => 0);
                !$xuvip && $update['last_be_supvip_at'] = mktime(0, 0, 0);
                UserSysTagModel::where('uid', $user->uid)->increment('supvip_days', $addday, $update);
            } else {
                UserSysTagModel::create(array(
                    'uid' => $user->uid,
                    'supvip_days' => $addday,
                    'supvip_endat' => $user->supvip_endat,
                    'last_be_supvip_at' => mktime(0, 0, 0)
                ));
            }
            $user->save();
            // 3. 发放72h入场券
            $couponManager = new CouponManager();
            $couponManager->generateSuperVip72FreeCoupons($user->uid);
            NoticeModel::create([
                'uid' => $user->uid,
                'title' => '收到一张系统赠发的"72小时恋爱"活动入场券',
                'content' => "亲爱的超级会员，近日小象陆续收到反馈，因时间原因部分月份存在5期活动开启报名的情况，为保证超级会员权益，系统特此赠发一张“入场券”，感谢你对小象的支持。",
                'type' => 5,
                'type_id' => 0,
                'create_at' => time(),
                'update_at' => time(),
                'tab_content' => '去查看',
                'tab_url' => '/pages/tickets/tickets',
            ]);
        }
    }
}
