package com.jy.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.framework.model.annotation.JyLog;
import com.jy.framework.model.base.BaseSelectDto;
import com.jy.framework.model.enums.BusinessType;
import com.jy.framework.model.validation.AddGroup;
import com.jy.framework.model.validation.BatchDeleteGroup;
import com.jy.framework.model.validation.DetailGroup;
import com.jy.framework.model.validation.EditGroup;
import com.jy.system.model.dto.AuthUserDto;
import com.jy.system.model.dto.EditRoleDataScopeDto;
import com.jy.system.model.dto.EditRoleMenuScopeDto;
import com.jy.system.model.dto.SysRoleDto;
import com.jy.system.model.dto.SysRoleSelectDto;
import com.jy.system.model.vo.RoleDataScopeVo;
import com.jy.system.model.vo.RoleMenuScopeVo;
import com.jy.system.model.vo.SysRoleVo;
import com.jy.system.service.SysRoleService;
import com.jy.system.service.SysUserRoleService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 角色列表
     */
    @GetMapping("/getList")
    public List<SysRoleVo> getList(SysRoleSelectDto dto) {
        return sysRoleService.getList(dto);
    }

    /**
     * 角色分页
     */
    @GetMapping("/getPage")
    public Page<SysRoleVo> getPage(SysRoleSelectDto dto) {
        return sysRoleService.getPage(dto);
    }

    /**
     * 角色明细
     */
    @GetMapping("/getDetail")
    public SysRoleVo getDetail(@Validated(DetailGroup.class) BaseSelectDto dto) {
        return sysRoleService.getDetail(dto.getId());
    }

    /**
     * 角色新增
     */
    @JyLog(title = "新增角色", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public void add(@Validated(AddGroup.class) @RequestBody SysRoleDto dto) {
        sysRoleService.add(dto);
    }

    /**
     * 角色编辑
     */
    @JyLog(title = "编辑角色", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public void edit(@Validated(EditGroup.class) @RequestBody SysRoleDto dto) {
        sysRoleService.edit(dto);
    }

    /**
     * 角色删除
     */
    @JyLog(title = "删除角色", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public void delete(@Validated(BatchDeleteGroup.class) @RequestBody BaseSelectDto dto) {
        sysRoleService.delete(dto.getIdList());
    }

    /**
     * 查询菜单权限
     */
    @GetMapping("/getMenuScope")
    public RoleMenuScopeVo getMenuScope(@Validated(DetailGroup.class) BaseSelectDto dto) {
        return sysRoleService.getMenuScope(dto.getId());
    }

    /**
     * 编辑菜单权限
     */
    @JyLog(title = "编辑角色菜单权限", businessType = BusinessType.UPDATE)
    @PostMapping("/editMenuScope")
    public void editMenuScope(@Validated @RequestBody EditRoleMenuScopeDto dto) {
        sysRoleService.editMenuScope(dto);
    }

    /**
     * 查询数据权限
     */
    @GetMapping("/getDataScope")
    public RoleDataScopeVo getDataScope(@Validated(DetailGroup.class) BaseSelectDto dto) {
        return sysRoleService.getDataScope(dto.getId());
    }

    /**
     * 编辑数据权限
     */
    @JyLog(title = "编辑角色数据权限", businessType = BusinessType.UPDATE)
    @PostMapping("/editDataScope")
    public void editDataScope(@Validated(EditGroup.class) @RequestBody EditRoleDataScopeDto dto) {
        sysRoleService.editDataScope(dto);
    }

    /**
     * 授权
     */
    @JyLog(title = "角色授权用户", businessType = BusinessType.UPDATE)
    @PostMapping("/authUser")
    public void authUser(@Validated @RequestBody AuthUserDto dto) {
        sysUserRoleService.authUser(dto);
    }

    /**
     * 取消授权
     */
    @JyLog(title = "角色取消授权用户", businessType = BusinessType.UPDATE)
    @PostMapping("/unAuthUser")
    public void unAuthUser(@Validated @RequestBody AuthUserDto dto) {
        sysUserRoleService.unAuthUser(dto);
    }

}
