<?php

namespace Modules\User\Models;

use App\Models\Model;
use Modules\User\Traits\BelongsToUser;

class Sign extends Model
{

    use BelongsToUser;

    protected $table = 'user_signs';

    protected $dates = [
        'last_sign_at',
        'reset_at',
    ];

    const FINISH_INIT = 0;
    const FINISH_SIGN = 1;
    const FINISH_LOG  = 2;
    const FINISHS     = [
        self::FINISH_INIT => '进行中',
        self::FINISH_SIGN => '打卡完成',
        self::FINISH_LOG  => '报告完成',
    ];

    protected static function boot()
    {
        parent::boot();

//        self::saved(function ($sign) {
//            $params = SignConfig::getParams();
//
//            if ($params['open'] == 1 && $sign->need_case == 0 && $sign->continue_days >= $params['cycle_day']) {
//                $sign->update(['need_case' => 1, 'is_finish' => 1]);
//            }
//
//        });
    }

    /**
     * Notes   : 获取最新连续签到数
     *
     * @Date   : 2021/5/28 12:00
     * @Author : Mr.wang
     * @return int|mixed
     */
    public function getContinueDays()
    {
        if ($this->last_sign_at->diffInDays() > 1) {
            $continue = 1;
        } else {
            $continue = $this->continue_days + 1;
        }

        return $continue;
    }

}
