<?php

namespace App\Modules\User\Models;

use Illuminate\Database\Eloquent\Model;

class TempsUserSign extends Model
{
    protected $table = 'temps_user_sign';
    protected $fillable = ['uid','price','day','sign_time'];

    /**
     * 用户是否已签到
     * @param $uid
     * @param $day
     * @return mixed
     */
    public static function isSign($uid,$day){
        return self::where('uid',$uid)->where('day',$day)->first();
    }
    
    /**
     * 删除N天前的数据，保留热数据
     */
    static function deleteTempsUserSignByTime($end_time)
    {
        while (true)
        {
            $affect_rows = self::where('day','<=',$end_time)->limit(50000)->delete();
            if(!$affect_rows) break;
            sleep(1);
        }
        return true;
    	//return self::where('day','<=',$end_time)->delete();
    }

    /**
     * 签到
     * @param $uid
     * @param $day
     * @return mixed
     */
    public static function sign($uid,$day,$fee){
        $data = ['uid'=>$uid,'price'=>$fee,'day'=>$day,'sign_time'=>time()];
        return self::create($data);
    }

    /**
     * 用户签到记录
     */
    public static function getUserSignRecord($uid){
        return self::where('uid',$uid)->select('price','sign_time')->orderBy('sign_time','desc')->paginate();
    }
}
