package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ss.menuRole.domain.MenuRole;
import com.ruoyi.ss.menuRole.domain.MenuRoleQuery;
import com.ruoyi.ss.menuRole.domain.MenuRoleVO;
import com.ruoyi.ss.menuRole.service.IMenuRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 角色和菜单关联Controller
 *
 * @author qzz
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/system/menuRole")
public class MenuRoleController extends BaseController
{
    @Autowired
    private IMenuRoleService menuRoleService;

    /**
     * 查询角色和菜单关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(MenuRoleQuery query)
    {
        startPage();
        startOrderBy();
        List<MenuRoleVO> list = menuRoleService.selectMenuRoleList(query);
        return getDataTable(list);
    }

    /**
     * 导出角色和菜单关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:export')")
    @Log(title = "角色和菜单关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenuRoleQuery query)
    {
        List<MenuRoleVO> list = menuRoleService.selectMenuRoleList(query);
        ExcelUtil<MenuRoleVO> util = new ExcelUtil<MenuRoleVO>(MenuRoleVO.class);
        util.exportExcel(response, list, "角色和菜单关联数据");
    }

    /**
     * 获取角色和菜单关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable("roleId") String roleId)
    {
        return success(menuRoleService.selectMenuRoleByRoleId(roleId));
    }

    /**
     * 新增角色和菜单关联
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @Log(title = "角色和菜单关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MenuRole menuRole)
    {
        return toAjax(menuRoleService.insertMenuRole(menuRole));
    }

    /**
     * 修改角色和菜单关联
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色和菜单关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MenuRole menuRole)
    {
        return toAjax(menuRoleService.updateMenuRole(menuRole));
    }

    /**
     * 删除角色和菜单关联
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @Log(title = "角色和菜单关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable String[] roleIds)
    {
        return toAjax(menuRoleService.deleteMenuRoleByRoleIds(roleIds));
    }
}
