package com.fuyuanshen.app.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.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.validate.AddGroup;
import com.fuyuanshen.common.core.validate.EditGroup;
import com.fuyuanshen.common.log.enums.BusinessType;
import com.fuyuanshen.common.excel.utils.ExcelUtil;
import com.fuyuanshen.app.domain.vo.AppRoleMenuVo;
import com.fuyuanshen.app.domain.bo.AppRoleMenuBo;
import com.fuyuanshen.app.service.IAppRoleMenuService;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;

/**
 * 角色和菜单关联
 *
 * @author Lion Li
 * @date 2025-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/roleMenu")
public class AppRoleMenuController extends BaseController {

    private final IAppRoleMenuService appRoleMenuService;

    /**
     * 查询角色和菜单关联列表
     */
    @SaCheckPermission("app:roleMenu:list")
    @GetMapping("/list")
    public TableDataInfo<AppRoleMenuVo> list(AppRoleMenuBo bo, PageQuery pageQuery) {
        return appRoleMenuService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出角色和菜单关联列表
     */
    @SaCheckPermission("app:roleMenu:export")
    @Log(title = "角色和菜单关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppRoleMenuBo bo, HttpServletResponse response) {
        List<AppRoleMenuVo> list = appRoleMenuService.queryList(bo);
        ExcelUtil.exportExcel(list, "角色和菜单关联", AppRoleMenuVo.class, response);
    }

    /**
     * 获取角色和菜单关联详细信息
     *
     * @param roleId 主键
     */
    @SaCheckPermission("app:roleMenu:query")
    @GetMapping("/{roleId}")
    public R<AppRoleMenuVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long roleId) {
        return R.ok(appRoleMenuService.queryById(roleId));
    }

    /**
     * 新增角色和菜单关联
     */
    @SaCheckPermission("app:roleMenu:add")
    @Log(title = "角色和菜单关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppRoleMenuBo bo) {
        return toAjax(appRoleMenuService.insertByBo(bo));
    }

    /**
     * 修改角色和菜单关联
     */
    @SaCheckPermission("app:roleMenu:edit")
    @Log(title = "角色和菜单关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppRoleMenuBo bo) {
        return toAjax(appRoleMenuService.updateByBo(bo));
    }

    /**
     * 删除角色和菜单关联
     *
     * @param roleIds 主键串
     */
    @SaCheckPermission("app:roleMenu:remove")
    @Log(title = "角色和菜单关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] roleIds) {
        return toAjax(appRoleMenuService.deleteWithValidByIds(List.of(roleIds), true));
    }
}
