package com.ruoyi.predict.controller;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author hlf
 * @date 2022/12/9 15:12
 * 文件说明：角色管理
 */
@Api(value = "角色管理Controller", tags = {"角色管理操作接口"})
@Controller
@RequestMapping("/predict/api/rmc")
public class RoleManagementController extends BaseController {

    @Autowired
    private ISysRoleService roleService;

    @ApiOperation("获取角色信息列表")
    @PostMapping("/getRoleList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称"),
            @ApiImplicitParam(name = "menus", value = "菜单组"),
    })
    public TableDataInfo getRoleList(SysRole role) {
        startPage();
        List<SysRole> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }

    @ApiOperation("新增角色信息")
    @PostMapping("/addRole")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称", required = true),
            @ApiImplicitParam(name = "mIds", value = "菜单组", required = true),
    })
    public AjaxResult addRole(SysRole role) {
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        role.setMenuIds(Convert.toLongArray(role.getmIds()));
        role.setRoleKey(RandomUtil.randomStringWithoutStr(8, ""));
        role.setRoleSort("0");
        role.setStatus("0");
        role.setCreateBy("admin");
        return toAjax(roleService.insertRole(role));
    }

    @ApiOperation("修改角色信息")
    @PostMapping("/editRole")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色编号", required = true),
            @ApiImplicitParam(name = "roleName", value = "角色名称"),
            @ApiImplicitParam(name = "mIds", value = "菜单组"),
    })
    public AjaxResult editRole(SysRole role) {
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        role.setMenuIds(Convert.toLongArray(role.getmIds()));
        role.setUpdateBy("admin");
        return toAjax(roleService.updateRole(role));
    }

    @ApiOperation("删除角色信息")
    @PostMapping("/delRole")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色编号", required = true),
    })
    public AjaxResult delRole(@RequestParam(value = "roleId") String roleId) {
        return toAjax(roleService.deleteRoleByIds(roleId));
    }
}
