<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;

trait HasStatus
{
    /**
     * Notes   : 获取状态字段，主模型可配置 $status_field
     *
     * @Date   : 2021/3/16 4:34 下午
     * @Author : <Jason.C>
     * @return string
     */
    protected function getStatusField(): string
    {
        return $this->status_field ?? 'status';
    }

    /**
     * Notes   : 获取各状态的名称
     *
     * @Date   : 2021/5/27 11:50 上午
     * @Author : <Jason.C>
     * @return string[]
     */
    protected function getStatusMap(): array
    {
        return isset($this->status_map) && ! empty($this->status_map) ? $this->status_map : [
            0 => '待审核',
            1 => '正常',
            2 => '驳回',
            3 => '关闭',
        ];
    }

    /**
     * 正常显示的数据
     *
     * @Author:<Mr.Wang>
     * @Date  :2021-04-09
     * @param  Builder  $query
     * @return Builder
     */
    public function scopeShown(Builder $query): Builder
    {
        return $query->where($this->getStatusField(), 1);
    }

    /**
     * 不显示的数据
     *
     * @Author :<Mr.Wang>
     * @Date   :2021-04-09
     * @param  Builder  $query
     * @return Builder
     */
    public function scopeHidden(Builder $query): Builder
    {
        return $query->where($this->getStatusField(), 0);
    }

    /**
     * Notes   : 状态查询
     *
     * @Date   : 2021/6/28 10:25 上午
     * @Author : <Jason.C>
     * @param  Builder  $query
     * @param  int  $status
     * @return Builder
     */
    public function scopeOfStatus(Builder $query, int $status): Builder
    {
        return $query->where($this->getStatusField(), $status);
    }

    /**
     * Notes   : 获取状态的文本信息
     *
     * @Date   : 2021/4/25 2:10 下午
     * @Author : <Jason.C>
     * @return string
     */
    public function getStatusTextAttribute(): string
    {
        $map = $this->getStatusMap();

        return $map[$this->{$this->getStatusField()}] ?? '未知';
    }
}
