package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Category;
import com.example.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    // 列出所有分类
    @RequestMapping("/list")
    public String list(){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        QueryWrapper<Category> childrenWrapper = new QueryWrapper<>();
        Map<String,Object> result=new HashMap<>();
        wrapper.eq("parent_id", 0);
        List<Category> categoryParentList=categoryService.list(wrapper);
        childrenWrapper.ne("parent_id", 0);
        List<Category> categoryChildrenList=categoryService.list(childrenWrapper);
        result.put("categoryParentList",categoryParentList);
        result.put("categoryChildrenList",categoryChildrenList);
        return JSON.toJSONString(result);
    }

    // 列出所有子分类
    @RequestMapping("/listChildrenCategory")
    public String listChildrenCategory(){
        QueryWrapper<Category> childrenWrapper = new QueryWrapper<>();
        childrenWrapper.eq("category_level", 2);
        List<Category> categoryChildrenList=categoryService.list(childrenWrapper);
        return JSON.toJSONString(categoryChildrenList);
    }
}
