ActivityFilter.php 3.7 KB
<?php

namespace App\ModelFilters;

use App\Support\ModelFilter;
use Auth;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Arr;

class ActivityFilter extends ModelFilter
{
    /**
     * @param string $songName
     * @return void
     */
    public function songName(string $songName): void
    {
        $this->whereLike('song_name', $songName);
    }

    /**
     * @param string $projectName
     * @return void
     */
    public function projectName(string $projectName): void
    {
        $this->whereRelation('project', 'projects.name', 'like', '%' . $projectName . '%');
    }

    /**
     * @param string $tagName
     * @return void
     */
    public function tagName(string $tagName): void
    {
        $this->whereRelation('tags', 'system_tags.name', 'like', '%' . $tagName . '%');
    }

    /**
     * @param string $userName
     * @return void
     */
    public function userName(string $userName): void
    {
        $this->whereRelation('user', 'users.nick_name', 'like', '%' . $userName . '%');
    }

    /**
     * @param $tag
     * @return void
     */
    public function tag($tag): void
    {
        $this->whereRelation('tags', fn(Builder $builder) => $builder->whereIn('system_tags.id', Arr::wrap($tag)));
    }

    /**
     * @param string|array $sex
     * @return void
     */
    public function sex(array|string $sex): void
    {
        $this->whereIn('sex', Arr::wrap($sex));
    }

    /**
     * @param array|string $lang
     * @return void
     */
    public function lang(array|string $lang): void
    {
        $this->where(function ($query) use ($lang) {
            foreach (Arr::wrap($lang) as $value) {
                $query->orWhereJsonContains('lang', $value);
            }
        });
    }

    /**
     * @param $value
     * @return void
     */
    public function unListen($value): void
    {
        if ($value) {
            $this->whereNotExists(function (QueryBuilder $builder) {
                $builder->selectRaw(1)->from('user_view_activitys')
                    ->whereColumn('user_view_activitys.activity_id', 'activitys.id')
                    ->where('user_view_activitys.user_id', Auth::id());
            });
        }
    }

    /**
     * @param int|array $project
     * @return void
     */
    public function project(int|array $project): void
    {
        $this->where('project_id', Arr::wrap($project));
    }

    /**
     * @param int $user
     * @return void
     */
    public function user(int $user): void
    {
        $this->where('user_id', $user);
    }

    /**
     * @param int|array $status
     * @return void
     */
    public function auditStatus(int|array $status): void
    {
        $this->whereIn('audit_status', Arr::wrap($status));
    }

    /**
     * @param int|array $status
     * @return void
     */
    public function status(int|array $status): void
    {
        $this->whereIn('status', Arr::wrap($status));
    }

    /**
     * @param int|array $weight
     * @return void
     */
    public function weight(int|array $weight): void
    {
        $this->whereIn('weight', Arr::wrap($weight));
    }

    /**
     * @param int|array $form
     * @return void
     */
    public function createdForm(int|array $form): void
    {
        $this->whereIn('created_form', Arr::wrap($form));
    }

    /**
     * @param int|array $type
     * @return void
     */
    public function songType(int|array $type): void
    {
        $this->where('song_type', Arr::wrap($type));
    }

    /**
     * @param array<int,string> $createBetween
     * @return void
     */
    public function auditBetween(array $createBetween): void
    {
        $this->whereBetween('audit_at', $createBetween);
    }
}