<?php

namespace App\Services\goods;

use App\Models\goods\mysql\Category;

class CategoryService
{
    /**
     * 商品分类列表数据
     * @return array
     */
    public static function getCategoryList()
    {
        //todo::加上redis
        $allCategoryList = Category::getRecordList([], ['category_id', 'parent_category_id', 'name', 'level']);
        $data = [];
        if (!empty($allCategoryList)) {
            foreach ($allCategoryList as $caregory) {
                if ($caregory["level"] == Category::LEVEL_1) {
                    $data[] = [
                        "category_id" => $caregory["category_id"],
                        "name" => $caregory["name"],
                    ];
                }
            }
            foreach ($data as $key=>$item) {
                foreach ($allCategoryList as $caregory) {
                    if ($caregory["level"] == Category::LEVEL_2 && $caregory["parent_category_id"] == $item["category_id"]) {
                        $data[$key]["sub_list"][] = [
                            "category_id" => $caregory["category_id"],
                            "name" => $caregory["name"],
                        ];
                    }
                }
            }
        }
        return $data;
    }
}