package com.ylx.web.controller.massage;

import com.ylx.common.annotation.Log;
import com.ylx.common.core.controller.BaseController;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.massage.domain.ProductCategory;
import com.ylx.massage.domain.vo.CategoryTreeVO;
import com.ylx.massage.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = {"商品分类管理"})
@RestController
@RequestMapping("product")
public class ProductCategoryController extends BaseController {

    @Resource
    private ProductCategoryService productCategoryService;

    /**
     * 新增商品分类
     *
     * @param category 分类实体
     * @return R 新增结果
     */
    @PostMapping("category/create")
    @ApiOperation("新增商品分类")
    public R createCategory(@RequestBody ProductCategory category) {
        return R.ok(productCategoryService.save(category));
    }

    /**
     * 修改商品分类
     *
     * @param category 分类实体
     * @return 修改结果
     */
    @PostMapping("category/update")
    @Log(title = "商品分类修改", businessType = BusinessType.UPDATE)
    @ApiOperation("修改商品分类")
    public R updateCategory(@RequestBody ProductCategory category) {
        return R.ok(productCategoryService.updateById(category));
    }

    /**
     * 删除商品分类
     *
     * @param id 分类ID
     * @return 删除结果
     */
    @PostMapping("category/delete")
    @ApiOperation("删除商品分类")
    public R deleteCategory(@RequestParam("id") Long id) {
        return R.ok(productCategoryService.removeById(id));
    }

    /**
     * 查询商品分类树形结构
     *
     * @return R<List<CategoryTreeVO>> 分类树形列表
     */
    @GetMapping("category/list")
    @ApiOperation("查询商品分类树形结构")
    public R<List<CategoryTreeVO>> categoryList() {
        return R.ok(productCategoryService.getCategoryTree());
    }
}
