<?php


namespace App\Services;

use App\Models\Game;


class GameService
{
    /**
     * @description: 模型
     * @return {string}
     */
    public static function model(): string
    {
        return Game::class;
    }

    /**
     * @description: 枚举
     * @return {*}
     */
    public static function enum(): string
    {
        return '';
    }

    /**
     * @description: 获取查询条件
     * @param {array} $search 查询内容
     * @return {array}
     */
    public static function getWhere(array $search = []): array
    {
        $where = [];

        if (isset($search['id']) && !empty($search['id'])) {
            $where[] = ['id', '=', $search['id']];
        }
        if (isset($search['name']) && !empty($search['name'])) {
            $where[] = ['name', '=', $search['name']];
        }
        if (isset($search['display_name']) && !empty($search['display_name'])) {
            $where[] = ['display_name', 'like', '%' . $search['display_name'] . '%'];
        }

        return $where;
    }

    /**
     * @description: 查询单条数据
     * @param array $search
     * @return \App\Models\Coin|null
     */
    public static function findOne(array $search): ?Game
    {
        return self::model()::where(self::getWhere($search))->first();
    }

    /**
     * @description: 查询所有数据
     * @param array $search
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public static function findAll(array $search = [])
    {
        return self::model()::where(self::getWhere($search))->get();
    }

    /**
     * @description: 分页查询
     * @param array $search
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public static function paginate(array $search = [])
    {
        $limit = isset($search['limit']) ? $search['limit'] : 15;
        $paginator = self::model()::where(self::getWhere($search))
            ->paginate($limit);
        return ['total' => $paginator->total(), 'data' => $paginator->items()];
    }
}