package com.gxwebsoft.cms.controller;

import com.gxwebsoft.cms.entity.CmsProductCategory;
import com.gxwebsoft.cms.param.CmsProductCategoryParam;
import com.gxwebsoft.cms.service.CmsProductCategoryService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

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

/**
 * 产品分类控制器
 *
 * @author WorkBuddy
 * @since 2026-07-17
 */
@Tag(name = "产品分类管理")
@RestController
@RequestMapping("/api/cms/cms-product-category")
public class CmsProductCategoryController extends BaseController {
    @Resource
    private CmsProductCategoryService cmsProductCategoryService;

    @Operation(summary = "分页查询产品分类")
    @GetMapping("/page")
    public ApiResult<PageResult<CmsProductCategory>> page(CmsProductCategoryParam param) {
        return success(cmsProductCategoryService.pageRel(param));
    }

    @Operation(summary = "查询全部产品分类")
    @GetMapping()
    public ApiResult<List<CmsProductCategory>> list(CmsProductCategoryParam param) {
        return success(cmsProductCategoryService.listRel(param));
    }

    @Operation(summary = "根据id查询产品分类")
    @GetMapping("/{id}")
    public ApiResult<CmsProductCategory> get(@PathVariable("id") Integer id) {
        return success(cmsProductCategoryService.getByIdRel(id));
    }

    @Operation(summary = "添加产品分类")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CmsProductCategory category) {
        if (cmsProductCategoryService.save(category)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改产品分类")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CmsProductCategory category) {
        if (cmsProductCategoryService.updateById(category)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除产品分类")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cmsProductCategoryService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "修改分类状态(启用/禁用)")
    @PutMapping("/status")
    public ApiResult<?> updateStatus(@RequestBody CmsProductCategory category) {
        CmsProductCategory entity = new CmsProductCategory();
        entity.setCategoryId(category.getCategoryId());
        entity.setStatus(category.getStatus());
        if (cmsProductCategoryService.updateById(entity)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }
}
