<?php

namespace App\Modules\Admin\Services;

use App\Base\BaseService;
use App\Exceptions\ClientException;
use App\Models\Goods\Category;
use App\Models\Goods\GoodsCategoryMap;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;

class CategoryService extends BaseService
{
    public function paginate($params)
    {
        $p = Category::query()->with(["parent", "childS", "gcMap"])->when($params['level'], function (Builder $query) use ($params) {
            return $query->where("level", $params['level']);
        })->when($params['name'], function (Builder $query) use ($params) {
            return $query->where(function (Builder $query) use ($params) {
                return $query->where("name", "like", "%{$params['name']}%");
            });
        })->orderByDesc("id")->paginate($params['page_size']);

        return [
            "total" => $p->total(),
            "page_total" => $p->lastPage(),
            "list" => array_map(function (Category $model) {

                return [
                    "id" => $model->id,
                    "name" => $model->name,
                    "thumb" => $model->thumb,
                    "level" => $model->level,
                    "parent_id" => optional($model->parent)->id ?? 0,
                    "parent_name" => optional($model->parent)->name ?? "",
                    "child_category_count" => $model->childS->count(),
                    "child_goods_count" => $model->gcMap->count(),
                    "weight" => $model->weight,
                    "index_weight" => $model->index_weight,
                    "category_weight" => $model->category_weight,
                    "search_weight" => $model->search_weight,
                    "created_at" => $model->created_at->format("Y-m-d"),
                ];
            }, $p->items()),
        ];
    }

    public function store($params): Category
    {
        $id = $params['id'] ?? 0;
        if ($id) {
            $model = Category::find($id);
        } else {
            $model = new Category();
        }
        $model->name = Arr::get($params, 'name');
        $model->thumb = Arr::get($params, 'thumb');
        $model->index_weight = Arr::get($params, 'index_weight');
        $model->category_weight = Arr::get($params, 'category_weight');
        $model->search_weight = Arr::get($params, 'search_weight');
        $model->parent_id = Arr::get($params, 'parent_id', 0);
        $model->level = 1;
        if ($model->parent_id) {
            if ($model->parent_id == $model->id) {
                throw new ClientException("上级类目不能是当前类目");
            }
            $parent = Category::findOrFail($model->parent_id);
            $model->level = $parent->level + 1;
        }

        $model->save();

        return $model;
    }

    public function del($params)
    {
        $category = Category::findOrFail($params['id']);

        if ($category->level == 1) {
            throw new ClientException("一级类目不能删除");
        }

        if ($count = Category::where("parent_id", $category->id)->count() > 0) {
            throw new ClientException("该类目下还有{$count}个子分类,请先删除子类目");
        }

        if ($count = GoodsCategoryMap::where("category_id", $category->id)->count()) {
            throw new ClientException("该类目下有{$count}个商品,请先修改对应商品");
        }

        $category->delete();
    }
}