<?php

namespace App\Models\Fpdx;

use App\Models\Model;
use Illuminate\Database\Eloquent\Builder;

/**
 * App\Models\Experience\ActivityModel
 *
 * @property int $stage_id
 * @property string $name
 * @property int $enroll_begin_time
 * @property int $enroll_end_time
 * @property int $activity_begin_time
 * @property int $activity_end_time
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @property string $activity_name 活动名称
 * @property int $activity_time 活动开始时间
 * @property int $signbegin_time 报名开始时间
 * @property int $signend_time 报名结束时间
 * @property int $open_time 开启聊天时间
 * @property int $close_time 活动结束时间
 * @property int $rematch_begin_at 重匹开始时间
 * @property int $rematched_at 重匹公布时间
 * @property int $create_time 创建时间
 * @property int $update_time 修改时间
 * @property int $qbj_stage_id 情报局活动id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel currently()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel enrolling()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereActivityName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereActivityTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereCloseTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereCreateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereOpenTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereQbjStageId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereRematchBeginAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereRematchedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereSignbeginTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereSignendTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereStageId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereUpdateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class ActivityModel extends Model
{
    protected $table = 'kdgx_partner_charge_activity';

    protected $dateFormat = 'U';

    protected $fillable = [
        'activity_name',
        'activity_time',
        'signbegin_time',
        'signend_time',
        'open_time',
        'close_time',
        'rematch_begin_at',
        'rematched_at',
        'qbj_stage_id'
    ];

    protected $primaryKey = "stage_id";

    protected $appends = ['id'];


    public const ENROLLING = 1;    //报名中 周四12:00-下周四12:00

    public const MATCHING = 2;     //匹配中 周四12:00-周四18:00

    public const CONFIRMING = 3;   //确认中 周四18:00-周五10:00

    public const REMATCHING = 4;   //重配中 周五10:00-周五12:00

    public const CHATTING = 5;     //活动中 周四18:00-周日24:00

    public const END = 6;          //活动已结束


    /**
     * 获取最近活动
     * @return array
     * [
     *      'last' => 上一期活动,
     *      'ing' => 正在进行中的活动(匹配结束到分销结束期间-1未不存在),
     *      'next' => 下一期活动(正在报名中的活动)
     * ]
     */
    public static function getActivitys()
    {
        /** @var ActivityModel $ing */
        $ing = self::where([['signend_time', '<', time()], ['close_time', '>', time()]])->first();
        if (collect($ing)->isEmpty()) {
            /** @var ActivityModel $next */
            $next = self::orderBy('stage_id', 'desc')->first();
            if (collect($next)->isEmpty()) {
                return array(
                    'last' => 0,
                    'ing' => 0,
                    'next' => 0
                );
            }
            return [
                'last' => $next->stage_id - 1,
                'ing' => -1,
                'next' => $next->stage_id
            ];
        }
        return [
            'last' => $ing->stage_id - 1,
            'ing' => $ing->stage_id,
            'next' => $ing->stage_id + 1
        ];
    }


    /**
     * 当前活动中的一期
     * @param $query
     * @return mixed
     */
    public function scopeCurrently($query)
    {
        /** @var Builder $query */
        return $query->where('close_time', '>', time())->orderBy($this->primaryKey, 'asc');
    }


    /**
     * 匹配中的一期
     * @param $query
     * @return mixed
     */
    public function scopeMatching($query)
    {
        $time = time();
        return $query->where('signend_time', '<=', $time)
            ->where('open_time', '>', $time);
    }

    /**
     * 当前报名中的一期
     */
    public function scopeEnrolling($query)
    {
        $time = time();
        return $query->where('signbegin_time', '<=', $time)
            ->where('signend_time', '>', $time);
    }

    /**
     * 活动ID
     * @return int
     */
    public function getIdAttribute()
    {
        return $this->stage_id;
    }



    /**
     * 获取当前活动的进度
     * @return int
     */
    public function getProgress()
    {
        if (time() >= $this->signbegin_time && time() < $this->signend_time) {
            return self::ENROLLING;
        } elseif (time() >= $this->signend_time && time() < $this->open_time) {
            return self::MATCHING;
        } elseif (time() >= $this->open_time && time() < $this->rematch_begin_at) {
            return self::CONFIRMING;
        } elseif (time() >= $this->rematch_begin_at && time() < $this->rematched_at) {
            return self::REMATCHING;
        } elseif (time() >= $this->rematched_at && time() < $this->close_time) {
            return self::CHATTING;
        } else {
            return self::END;
        }
    }
}
