package com.ruoyi.web.controller.system;

import com.mybatisflex.core.paginate.Page;
import com.ruoyi.common.constants.UserConstant;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.common.web.controller.BaseController;
import com.ruoyi.common.web.domain.PageData;
import com.ruoyi.common.web.domain.entity.SysRole;
import com.ruoyi.framework.annotation.RyDataScope;
import com.ruoyi.framework.annotation.RyLog;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysRoleService;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.Result;
import org.noear.solon.data.annotation.Transaction;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 角色 操作处理
 * @author ruoyi
 */
@Controller
@Mapping("system/role")
public class SysRoleController extends BaseController {

    @Inject
    ISysRoleService sysRoleService;

    /**
     * 查询角色列表
     */
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT)
    @AuthPermissions("system:role:list")
    @Get
    @Mapping("list")
    public PageData<SysRole> list(Page<SysRole> page, SysRole sysRole) {
        startPage(page);
        List<SysRole> list = sysRoleService.selectRoleList(sysRole);
        return getPageData(list);
    }

    /**
     * 导出角色列表
     */
    @RyLog(title = "角色", businessType = BusinessType.EXPORT)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT)
    @AuthPermissions("system:role:export")
    @Post
    @Mapping("export")
    public void export(SysRole sysRole) {
        List<SysRole> list = sysRoleService.selectRoleList(sysRole);
        ExcelUtil.export(list);
    }

    /**
     * 查询角色详情
     */
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT)
    @AuthPermissions("system:role:query")
    @Get
    @Mapping("{id}")
    public SysRole info(@Path Long id, SysRole sysRole) {
        return sysRoleService.info(id, sysRole);
    }

    /**
     * 新增角色
     */
    @RyLog(title = "角色", businessType = BusinessType.INSERT)
    @AuthPermissions("system:role:add")
    @Transaction
    @Post
    @Mapping
    public Result<Void> add(@Body @Validated SysRole sysRole) {
        return toResult(sysRoleService.add(sysRole));
    }

    /**
     * 修改角色
     */
    @RyLog(title = "角色", businessType = BusinessType.UPDATE)
    @AuthPermissions("system:role:edit")
    @Transaction
    @Put
    @Mapping
    public Result<Void> edit(@Body @Validated SysRole sysRole) {
        return toResult(sysRoleService.edit(sysRole));
    }

    /**
     * 修改保存数据权限
     */
    @RyLog(title = "角色", businessType = BusinessType.UPDATE)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT)
    @AuthPermissions("system:role:edit")
    @Transaction
    @Put
    @Mapping("dataScope")
    public Result<Void> dataScope(@Body SysRole sysRole) {
        return toResult(sysRoleService.dataScope(sysRole));
    }

    /**
     * 状态修改
     */
    @RyLog(title = "角色", businessType = BusinessType.UPDATE)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT)
    @AuthPermissions("system:role:edit")
    @Put
    @Mapping("changeStatus")
    public Result<Void> changeStatus(@Body SysRole sysRole) {
        return toResult(sysRoleService.changeStatus(sysRole));
    }

    /**
     * 删除角色
     */
    @RyLog(title = "角色", businessType = BusinessType.DELETE)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT)
    @AuthPermissions("system:role:remove")
    @Transaction
    @Delete
    @Mapping("{roleIds}")
    public Result<Void> delete(@Path Long[] roleIds, SysRole sysRole) {
        return toResult(sysRoleService.delete(roleIds, sysRole));
    }

    /**
     * 获取角色选择框列表
     */
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT)
    @AuthPermissions("system:role:query")
    @Get
    @Mapping("optionselect")
    public List<SysRole> optionselect(SysRole sysRole) {
        return sysRoleService.selectRoleList(sysRole);
    }

    /**
     * 取消授权用户
     */
    @RyLog(title = "角色", businessType = BusinessType.GRANT)
    @AuthPermissions("system:role:edit")
    @Put
    @Mapping("authUser/cancel")
    public Result<Void> cancelAuthUser(@Body SysUserRole sysUserRole) {
        return toResult(sysRoleService.cancelAuthUser(sysUserRole));
    }

    /**
     * 批量取消授权用户
     */
    @RyLog(title = "角色", businessType = BusinessType.GRANT)
    @AuthPermissions("system:role:edit")
    @Put
    @Mapping("authUser/cancelAll")
    public Result<Void> cancelAuthUserAll(Long roleId, Long[] userIds) {
        return toResult(sysRoleService.cancelAuthUsers(roleId, userIds));
    }

    /**
     * 批量选择用户授权
     */
    @RyLog(title = "角色", businessType = BusinessType.GRANT)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT)
    @AuthPermissions("system:role:edit")
    @Put
    @Mapping("authUser/selectAll")
    public Result<Void> selectAuthUserAll(Long roleId, Long[] userIds, SysRole sysRole) {
        return toResult(sysRoleService.insertAuthUsers(roleId, userIds, sysRole));
    }

}
