package org.dromara.system.controller.system;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.system.domain.bo.SysRoleBo;
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.SysRecipeVo;
import org.dromara.system.domain.bo.SysRecipeBo;
import org.dromara.system.service.ISysRecipeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 食谱管理
 *
 * @author Lion Li
 * @date 2025-07-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/recipe")
public class SysRecipeController extends BaseController {

    private final ISysRecipeService sysRecipeService;

    /**
     * 查询食谱管理列表
     */
    @SaCheckPermission("system:recipe:list")
    @GetMapping("/list")
    public TableDataInfo<SysRecipeVo> list(SysRecipeBo bo, PageQuery pageQuery) {
        return sysRecipeService.queryPageList(bo, pageQuery);
    }

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

    /**
     * 状态修改
     */
    @SaCheckPermission("system:recipe:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SysRecipeBo recipeBo) {
        if (StrUtil.isBlank(recipeBo.getRecipeIds())) {
            throw new ServiceException("id不能为空！");
        }
        return toAjax(sysRecipeService.updateRecipeStatus(
            Arrays.stream(recipeBo.getRecipeIds().split(","))
                .map(Long::valueOf).collect(Collectors.toList()), recipeBo.getStatus()));
    }

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

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

    /**
     * 修改食谱管理
     */
    @SaCheckPermission("system:recipe:edit")
    @Log(title = "食谱管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysRecipeBo bo) {
        return toAjax(sysRecipeService.updateByBo(bo));
    }

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