package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.product.service.CategoryService;
import com.atguigu.ssyx.vo.product.CategoryQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-09-15
 */
@Api(value = "Category管理", tags = "商品分类管理")
@RestController

@RequestMapping(value = "/admin/product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "获取商品分类分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(name = "categoryQueryVo", value = "查询对象", required = false) CategoryQueryVo categoryQueryVo) {
        try {
            Page<Category> pageParam = new Page<>(page, limit);
            IPage<Category> pageModel = categoryService.selectPage(pageParam, categoryQueryVo);
            return Result.ok(pageModel);
        } catch (Exception e) {
            throw new RuntimeException("获取商品分类分页列表异常", e);
        }
    }

    @ApiOperation(value = "获取商品分类信息")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        try {
            Category category = categoryService.getById(id);
            return StringUtils.isNotEmpty(category.toString()) ? Result.ok(category) : Result.fail(category);
        } catch (Exception e) {
            throw new RuntimeException("获取商品分类信息异常", e);
        }
    }

    @ApiOperation(value = "新增商品分类")
    @PostMapping("save")
    public Result save(@RequestBody Category category) {
        try {
            categoryService.save(category);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("新增商品分类异常", e);
        }
    }

    @ApiOperation(value = "修改商品分类")
    @PutMapping("update")
    public Result updateById(@RequestBody Category category) {
        try {
            categoryService.updateById(category);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("修改商品分类异常", e);
        }
    }

    @ApiOperation(value = "删除商品分类")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        try {
            categoryService.removeById(id);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("删除商品分类异常", e);
        }
    }

    @ApiOperation(value = "根据id列表删除商品分类")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        try {
            categoryService.removeByIds(idList);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("根据id列表删除商品分类异常", e);
        }
    }

    @ApiOperation(value = "获取全部商品分类")
    @GetMapping("findAllList")
    public Result findAllList() {
        try {
            return Result.ok(categoryService.findAllList());
        } catch (Exception e) {
            throw new RuntimeException("获取全部商品分类异常", e);
        }
    }
}

