CategoryService.php 3.4 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 逻辑层:Category 服务类,处理业务逻辑
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Services
 * @package   App\Services
 * @author    Richer <yangzi1028@163.com>
 * @date      2023年4月20日16:28:41
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Services;

use App\Models\Category\Category;

/**
 * Class CategoryService
 * @package App\Services
 */
class CategoryService extends BaseService
{
    /**
     * CategoryService constructor.
     *
     * @param Category $model
     */
    public function __construct(Category $model)
    {
         // 执行父类构造方法
        parent::__construct($model);
    }

    /**
     * Display a listing of the resource.
     *
     * @return array|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection
     */
    public function list()
    {
        return $this->model->with(['children'])
            ->where(function ($query) {
                $query->whereNull('pid')->orWhere('pid', 0)->orWhere('pid', '');
            })
            ->when($q = request('name'), function ($query) use ($q) {
                $query->where("name", 'like', "%$q%");
            })
            ->where('status', 1)
            ->orderByDesc('sort')
            ->groupBy('id')
            ->latest()
            ->get();
    }

    /**
     * @param $id
     */
    public function examples($id)
    {
        $model = $this->model->findOrFail($id);
//        $list = $model->examples()->with(['answer:id,pid,body,created_at'])
//            ->where('pid', 0)
//            ->latest()
//            ->paginate(request('per_page', config('constants.PER_PAGE')), ['id','category_id','body','created_at']);

        $model = $this->model->findOrFail($id);
        $list = $model->examples()
            ->latest()
            ->paginate(request('per_page', config('constants.PER_PAGE')), ['id','category_id','question','answer','created_at']);

        return $list;
    }

    /**
     * @param $id
     */
    public function labels($id)
    {
        $model = $this->model->findOrFail($id);
        $list = $model->labels()->with(['items'])
            ->latest()
            ->get()->map(function ($label) {
                return [
                    'id' => (int) $label->id,
                    'category_id' => (int) $label->category_id,
                    'name' => (string) $label->name,
                    'description' => (string) $label->description,
                    'can_multiple' => (int) $label->can_multiple,
                    'can_multiple_show' => (string) $label->can_multiple_show,
                    'created_at' => format_date($label->created_at),
                    'items' => $label->items->map(function ($item) {
                        return [
                            'id' => (int) $item->id,
                            'name' => (string) $item->name,
                            'description' => (string) $item->description,
                            'created_at' => format_date($item->created_at),
                        ];
                    })->toArray()
                ];
            });

        return $list;
    }
}