package org.dromara.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.web.domain.bo.ProductCategoryBo;
import org.dromara.web.domain.vo.ProductCategoryVo;
import org.dromara.web.service.IProductCategoryService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;


/**
 * 产品分类
 *
 * @author Lion Li
 * @date 2025-06-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/warehouse/productCategory")
public class ProductCategoryController extends BaseController {

    private final IProductCategoryService productCategoryService;

    /**
     * 查询产品分类列表
     */
    @SaCheckPermission("warehouse:productCategory:list")
    @GetMapping("/list")
    public TableDataInfo<ProductCategoryVo> list(ProductCategoryBo bo, PageQuery pageQuery) {
        return productCategoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品分类列表
     */
    @SaCheckPermission("warehouse:productCategory:export")
    @Log(title = "产品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductCategoryBo bo, HttpServletResponse response) {
        List<ProductCategoryVo> list = productCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品分类", ProductCategoryVo.class, response);
    }

    /**
     * 获取产品分类详细信息
     *
     * @param categoryId 主键
     */
    @SaCheckPermission("warehouse:productCategory:query")
    @GetMapping("/{categoryId}")
    public R<ProductCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long categoryId) {
        return R.ok(productCategoryService.queryById(categoryId));
    }

    /**
     * 新增产品分类
     */
    @SaCheckPermission("warehouse:productCategory:add")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductCategoryBo bo) {
        return toAjax(productCategoryService.insertByBo(bo));
    }

    /**
     * 修改产品分类
     */
    @SaCheckPermission("warehouse:productCategory:edit")
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductCategoryBo bo) {
        return toAjax(productCategoryService.updateByBo(bo));
    }

    /**
     * 删除产品分类
     *
     * @param categoryIds 主键串
     */
    @SaCheckPermission("warehouse:productCategory:remove")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] categoryIds) {
        return toAjax(productCategoryService.deleteWithValidByIds(List.of(categoryIds), true));
    }
}
