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.SuppliesCategoryBo;
import org.dromara.web.domain.vo.SuppliesCategoryVo;
import org.dromara.web.service.ISuppliesCategoryService;
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-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/warehouse/supplies")
public class SuppliesCategoryController extends BaseController {

    private final ISuppliesCategoryService suppliesCategoryService;

    /**
     * 查询耗材分类列表
     */
    @SaCheckPermission("warehouse:supplies:list")
    @GetMapping("/list")
    public TableDataInfo<SuppliesCategoryVo> list(SuppliesCategoryBo bo, PageQuery pageQuery) {
        return suppliesCategoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出耗材分类列表
     */
    @SaCheckPermission("warehouse:supplies:export")
    @Log(title = "耗材分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SuppliesCategoryBo bo, HttpServletResponse response) {
        List<SuppliesCategoryVo> list = suppliesCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "耗材分类", SuppliesCategoryVo.class, response);
    }

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

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

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

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