package com.lantone.security.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lantone.common.api.CommonResult;
import com.lantone.security.dto.GetCreateRoleDTO;
import com.lantone.security.dto.GetRoleDTO;
import com.lantone.security.dto.GetRolePageDTO;
import com.lantone.security.dto.UserMenuResourceTreeDTO;
import com.lantone.security.vo.AddRoleVO;
import com.lantone.security.vo.GetRolePageVO;
import com.lantone.security.vo.UpdateRoleVO;
import com.lantone.security.facade.RoleManagementFacade;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.Transactional;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @Description: 角色管理API
 * @author: songxl
 * @time: 2021/7/20 12:39
 */
@RestController
@Api(value = "角色管理API", tags = { "角色管理API" })
@RequestMapping("/roleManage")
@EnableAsync
public class RoleManagementController {
    @Autowired
    private RoleManagementFacade roleManagementFacade;

    @ApiOperation(value = "获取角色列表 [by:songxl]",
            notes = "")
    @PostMapping("/getRolePage")
    public CommonResult<IPage<GetRolePageDTO>> getRolePage(@RequestBody @Valid GetRolePageVO getRolePageVO) {
        return CommonResult.success(roleManagementFacade.getRolePage(getRolePageVO));
    }

    @ApiOperation(value = "添加角色 [by:songxl]")
    @PostMapping("/addRole")
    @Transactional
    public CommonResult<Boolean> addRole(@RequestBody @Valid AddRoleVO addRoleVO) {
        return CommonResult.success(roleManagementFacade.addRole(addRoleVO));
    }

    @ApiOperation(value = "修改角色 [by:songxl]")
    @PostMapping("/updateRole")
    @Transactional
    public CommonResult<Boolean> updateRole(@RequestBody @Valid UpdateRoleVO updateRoleVO) {
        return CommonResult.success(roleManagementFacade.updateRole(updateRoleVO));
    }

    @ApiOperation(value = "删除角色 [by:songxl]")
    @PostMapping("/deleteRole")
    @Transactional
    public CommonResult<Boolean> deleteRole(@RequestParam @Valid @NotNull(message = "角色编号为空") Long roleId) {
        return CommonResult.success(roleManagementFacade.deleteRole(roleId));
    }


    @ApiOperation(value = "禁用|启用角色 [by:songxl]")
    @PostMapping("/disableRole")
    @Transactional
    public CommonResult<Boolean> disableRole(@RequestParam @Valid @NotNull(message = "角色编号为空") Long roleId, @RequestParam @Valid @NotBlank(message = "启用禁用status为空") String status) {
        return CommonResult.success(roleManagementFacade.disableRole(roleId, status));
    }

    @ApiOperation(value = "查看功能 [by:songxl]")
    @PostMapping("/getRoleById")
    public CommonResult<GetRoleDTO> getRoleById(@RequestParam @Valid @NotNull(message = "角色编号为空") Long roleId) {
        //创建角色时type为1（新创建的角色非管理角色，不会有管理菜单）
        return CommonResult.success(roleManagementFacade.getRoleById(roleId,"1"));
    }

    @ApiOperation(value = "获取当前登录用户管理的的功能权限树 [by:songxl]",
            notes = "type:新增组织时查询type为0,添加角色时查询type为1，必填<br>")
    @PostMapping("/getUserMenuResourceTree")
    public CommonResult<List<UserMenuResourceTreeDTO>> getUserMenuResourceTree(@RequestParam @Valid @NotNull(message = "类型不能为空")String type) {
        return CommonResult.success(roleManagementFacade.getUserMenuResourceTree(type));
    }

    @ApiOperation(value = "获取当前登录用户管理的角色列表 [by:songxl]",
            notes = "softwareId:系统id 添加用户时获取的角色softwareId为空；添加数据权限角色的softwareId为选择的系统id")
    @PostMapping("/getCreateRoles")
    public CommonResult<List<GetCreateRoleDTO>> getCreateRoles(@RequestParam Long softwareId) {
        return CommonResult.success(roleManagementFacade.getCreateRoles(softwareId));
    }

    @ApiOperation(value = "获取当前登录用户的显示的菜单列表 [by:songxl]")
    @PostMapping("/getUserShowMemuTree")
    public CommonResult<Map<String, Object>> getUserShowMemuTree() {
        return CommonResult.success(roleManagementFacade.getUserShowMemuTree());
    }
}
