<?php


namespace App\Services;


use App\Http\logic\api\BaseApiLogic;

use Illuminate\Support\Facades\DB;

class MonthCard extends BaseApiLogic
{
    // 周卡验证
    public function verify($userId, $CardID)
    {
        // 验证周卡状态是否开启
        $CardState = DB::connection('write')->table('QPPlatformDB.dbo.MonthCard')
            ->where('CardID', $CardID)
            ->where('CardState', 1)
            ->first();

        if (!$CardState) {
            $this->error = 'There is no weekly card of this type in the current activity';
            return false;
        }

        // 验证最后一次领取时间  和 失效时间是不是今天
        $UserMonthCard = DB::connection('write')->table('QPPlatformDB.dbo.UserMonthCard')
            ->where('UserID', $userId)
            ->where('CardID', $CardID)
            ->first();

        if ($UserMonthCard) {

            $now = date('Ymd');
            $LastRewardDate = strtotime($UserMonthCard->LastRewardDate);  // 时间戳 -- 最后一次领取时间
            $LastDate = strtotime($UserMonthCard->LastDate);  // 时间戳 -- 失效时间

            if (date('Ymd', $LastDate) > $now) {        // 结束时间大于今天，不能购买

                $this->error = "The time is not over. You can't buy it again";
                return false;

            } elseif (date('Ymd', $LastDate) == $now) { // 结束时间今天，最后一次购买时间不是今天，不能购买

                if (date('Ymd', $LastRewardDate) != $now) {
                    $this->error = 'You need to receive today\'s reward before you can buy it';
                    return false;
                }

            }

        }

        return true;
    }
}
