package cn.ezhizao.project.system.controller;

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

import cn.ezhizao.project.system.domain.SysMenu;
import cn.ezhizao.project.system.domain.SysRoleMenuInit;
import cn.ezhizao.project.system.service.ISysMenuService;
import cn.ezhizao.project.system.service.ISysRoleMenuInitService;
import org.springframework.security.access.prepost.PreAuthorize;
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 cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.web.page.TableDataInfo;

/**
 * menuRoleController
 *
 * @author ruoyi
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/system/menuInit")
public class SysRoleMenuInitController extends BaseController
{
    @Resource
    private ISysRoleMenuInitService sysRoleMenuInitService;

    @Resource
    private ISysMenuService menuService;

    /**
     * 查询menuRole列表
     */
    @PreAuthorize("@ss.hasPermi('business:init:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRoleMenuInit sysRoleMenuInit) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(sysRoleMenuInit);
        startPage();
        List<SysRoleMenuInit> list = sysRoleMenuInitService.getList(sysRoleMenuInit);
        return getDataTable(list);
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId)
    {
        List<SysMenu> menus = menuService.selectMenuList(getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", menuService.selectMenuInItListByRoleId(roleId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ajax;
    }

    /**
     * 导出menuRole列表
     */
    @PreAuthorize("@ss.hasPermi('business:init:export')")
    @Log(title = "menuRole", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRoleMenuInit sysRoleMenuInit) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(sysRoleMenuInit);
        List<SysRoleMenuInit> list = sysRoleMenuInitService.getList(sysRoleMenuInit);
        ExcelUtil<SysRoleMenuInit> util = new ExcelUtil<SysRoleMenuInit>(SysRoleMenuInit.class);
        util.exportExcel(response, list, "menuRole数据");
    }

    /**
     * 获取menuRole详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:init:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysRoleMenuInitService.getById(id));
    }

    /**
     * 新增menuRole
     */
    @PreAuthorize("@ss.hasPermi('business:init:add')")
    @Log(title = "menuRole", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRoleMenuInit sysRoleMenuInit) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(sysRoleMenuInit);
        return toAjax(sysRoleMenuInitService.save(sysRoleMenuInit));
    }

    /**
     * 修改menuRole
     */
    @PreAuthorize("@ss.hasPermi('business:init:edit')")
    @Log(title = "menuRole", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRoleMenuInit sysRoleMenuInit) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(sysRoleMenuInit);
        return toAjax(sysRoleMenuInitService.updateById(sysRoleMenuInit));
    }

    /**
     * 删除menuRole
     */
    @PreAuthorize("@ss.hasPermi('business:init:remove')")
    @Log(title = "menuRole", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(sysRoleMenuInitService.removeBatchByIds(ids));
    }
}
