package org.dromara.system.controller.recipe;

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.system.service.ISchoolRecipeService;
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.SchoolRecipeVo;
import org.dromara.system.domain.bo.SchoolRecipeBo;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 学校食谱
 *
 * @author boman
 * @date 2023-09-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/recipe")
public class SchoolRecipeController extends BaseController {

    private final ISchoolRecipeService schoolRecipeService;

    /**
     * 查询学校食谱列表
     */
    @SaCheckPermission("system:recipe:list")
    @GetMapping("/list")
    public TableDataInfo<SchoolRecipeVo> list(SchoolRecipeBo bo, PageQuery pageQuery) {
        return schoolRecipeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出学校食谱列表
     */
    @SaCheckPermission("system:recipe:export")
    @Log(title = "学校食谱", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SchoolRecipeBo bo, HttpServletResponse response) {
        List<SchoolRecipeVo> list = schoolRecipeService.queryList(bo);
        ExcelUtil.exportExcel(list, "学校食谱", SchoolRecipeVo.class, response);
    }

    /**
     * 获取学校食谱详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:recipe:query")
    @GetMapping("/{id}")
    public R<SchoolRecipeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(schoolRecipeService.queryById(id));
    }

    /**
     * 新增学校食谱
     */
    @SaCheckPermission("system:recipe:add")
    @Log(title = "学校食谱", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SchoolRecipeBo bo) {
        return toAjax(schoolRecipeService.insertByBo(bo));
    }

    /**
     * 修改学校食谱
     */
    @SaCheckPermission("system:recipe:edit")
    @Log(title = "学校食谱", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SchoolRecipeBo bo) {
        return toAjax(schoolRecipeService.updateByBo(bo));
    }

    /**
     * 删除学校食谱
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:recipe:remove")
    @Log(title = "学校食谱", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(schoolRecipeService.deleteWithValidByIds(List.of(ids), true));
    }
}
