<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2019/7/2
 * Time: 14:47
 */

namespace App\Modules\User\Services;

use App\Modules\User\Models\ForceGuidePersonAccount;
use Carbon\Carbon;

class ForceGuidePersonAccountService
{
    public static function isShow($uid,$bid,$cid)
    {
        $model = new ForceGuidePersonAccount();
        $result = $model->where('uid', $uid)
            ->where('day', date('Y-m-d'))
            ->where('bid',$bid)
            ->where('cid',$cid)
            ->select('bid', 'cid', 'created_at')
            ->first();
        if($result){
            return false;
        }
        $result = $model->where('uid', $uid)->where('day', date('Y-m-d'))->select('bid', 'cid', 'created_at')->get();
        if ($result->isEmpty()) {
            $yesterday = $model->where('uid', $uid)
                ->where('day', date('Y-m-d', time() - 86400))
                ->select('bid', 'cid', 'created_at')
                ->orderBy('id', 'desc')
                ->first();
            if (!$yesterday) {
                return true;
            }
            $now = Carbon::now()->timestamp;
            if ($now - $yesterday->created_at->timestamp <= 6 * 3600) {
                return false;
            }
            return true;
        }
        if ($result->count() >= 2) {
            return false;
        }
        return true;
    }

    public static function create($uid,$bid,$cid){
        $model = new ForceGuidePersonAccount();
        $day = date('Y-m-d');
        $result = $model->where('uid', $uid)
            ->where('day', date('Y-m-d'))
            ->where('bid',$bid)
            ->where('cid',$cid)
            ->select('bid', 'cid', 'created_at')
            ->first();
        if($result) return false;
        $model->create(compact('uid','day','bid','cid'));
        return true;
    }
}