package org.dromara.system.controller.system;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
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;
import org.dromara.system.domain.vo.SysFoodCategoryVo;
import org.dromara.system.domain.bo.SysFoodCategoryBo;
import org.dromara.system.service.ISysFoodCategoryService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 食材分类
 *
 * @author Lion Li
 * @date 2025-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/foodCategory")
public class SysFoodCategoryController extends BaseController {

    private final ISysFoodCategoryService sysFoodCategoryService;

    /**
     * 查询食材分类列表
     */
    @SaCheckPermission("system:foodCategory:list")
    @GetMapping("/list")
    public TableDataInfo<SysFoodCategoryVo> list(SysFoodCategoryBo bo, PageQuery pageQuery) {
        return sysFoodCategoryService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("system:foodCategory:list")
    @GetMapping("/listTree")
    public TableDataInfo<SysFoodCategoryVo> listTree(SysFoodCategoryBo bo) {
        return sysFoodCategoryService.listTree(bo);
    }

    /**
     * 导出食材分类列表
     */
    @SaCheckPermission("system:foodCategory:export")
    @Log(title = "食材分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysFoodCategoryBo bo, HttpServletResponse response) {
        List<SysFoodCategoryVo> list = sysFoodCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "食材分类", SysFoodCategoryVo.class, response);
    }

    /**
     * 获取食材分类详细信息
     *
     * @param foodCategoryId 主键
     */
    @SaCheckPermission("system:foodCategory:query")
    @GetMapping("/{foodCategoryId}")
    public R<SysFoodCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long foodCategoryId) {
        return R.ok(sysFoodCategoryService.queryById(foodCategoryId));
    }

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

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

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