package com.microservices.system.controller;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.page.TableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.common.security.annotation.Logical;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.system.api.domain.SysRole;
import com.microservices.system.domain.vo.SysRoleInputVo;
import com.microservices.system.domain.vo.SysRoleUpdateVo;
import com.microservices.system.service.ISysDeptRoleService;
import com.microservices.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 组织下可选角色Controller
 *
 * @author otto
 * @date 2023-04-12
 */

@Api(tags = "组织下可选角色")
@RestController
@RequestMapping("/SysDeptRole")
public class SysDeptRoleController extends BaseController {
    @Autowired
    private ISysDeptRoleService sysDeptRoleService;
    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询所有可选角色列表
     */
    @RequiresPermissions("system:SysDeptRole:list")
    @GetMapping("/list")
    @ApiOperation("查询所有可选角色列表")
    public TableDataInfo list() {
        List<SysRole> list = sysDeptRoleService.selectSysRoleList();
        return getDataTable(list);
    }

    /**
     * 查询组织下可选角色列表
     */
    @RequiresPermissions(value = {"system:SysDeptRole:list", "system:SysDeptRole:manage", "pms:pmsEnterprise:showMember"}, logical = Logical.OR)
    @GetMapping("/dept/{currentDeptId}/list")
    @ApiOperation("查询组织下可选角色列表")
    public TableDataInfo list(
            @ApiParam(name = "currentDeptId", value = "组织Id", required = true) @PathVariable("currentDeptId") Long currentDeptId
            , @ApiParam("根据角色名称模糊搜索") String roleName) {
        List<SysRole> list = sysDeptRoleService.selectSysRoleListByDept(currentDeptId, roleName);
        return getDataTable(list);
    }

    /**
     * 批量增加/更新组织下可选角色
     */
    @RequiresPermissions("system:SysDeptRole:add")
    @Log(title = "组织下可选角色", businessType = BusinessType.INSERT)
    @PostMapping("/dept/{currentDeptId}/{roleIds}")
    @ApiOperation("批量增加/更新组织下可选角色")
    public AjaxResult addForSysDept(@ApiParam(name = "currentDeptId", value = "组织Id", required = true) @PathVariable("currentDeptId") Long currentDeptId,
                                    @ApiParam(name = "roleIds", value = "角色id列表", required = true) @PathVariable("roleIds") Long[] roleIds) {
        return toAjax(sysDeptRoleService.addSysRoleListForSysDept(currentDeptId, roleIds));
    }

    /**
     * 批量删除组织下可选角色
     */
    @RequiresPermissions("system:SysDeptRole:remove")
    @Log(title = "组织下可选角色", businessType = BusinessType.DELETE)
    @DeleteMapping("/dept/{currentDeptId}/{roleIds}")
    @ApiOperation("批量删除组织下可选角色")
    public AjaxResult removeForSysDept(@ApiParam(name = "currentDeptId", value = "组织Id", required = true) @PathVariable("currentDeptId") Long currentDeptId,
                                       @ApiParam(name = "roleIds", value = "角色id列表", required = true) @PathVariable("roleIds") Long[] roleIds) {
        return toAjax(sysDeptRoleService.deleteSysRoleListForSysDept(currentDeptId, roleIds));
    }


    /**
     * 获取角色信息
     */
    @RequiresPermissions(value = {"system:SysDeptRole:manage", "pms:pmsEnterprise:showMember"}, logical = Logical.OR)
    @GetMapping(value = "/dept/{currentDeptId}/role/{roleId}")
    @ApiOperation("获取角色信息")
    public AjaxResult getRoleInfo(@PathVariable Long currentDeptId,
                                  @ApiParam(value = "角色Id") @PathVariable Long roleId) {
        SysRole sysRole = roleService.selectRoleById(roleId);
        sysRole.setIsDefault(sysDeptRoleService.isDefaultRole(sysRole.getRoleKey()));
        return success(sysRole);
    }


    /**
     * 加载对应角色菜单列表树
     */
    @RequiresPermissions(value = {"system:SysDeptRole:manage", "pms:pmsEnterprise:showMember"}, logical = Logical.OR)
    @GetMapping(value = "/dept/{currentDeptId}/roleMenuTreeSelect")
    @ApiOperation("查看角色对应菜单列表树")
    public AjaxResult roleMenuTreeSelect(@PathVariable Long currentDeptId,
                                         @ApiParam(value = "角色Id") Long roleId) {
        return sysDeptRoleService.roleMenuTreeSelect(currentDeptId, roleId);
    }

    /**
     * 修改保存角色
     */
    @RequiresPermissions("system:SysDeptRole:manage")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dept/{currentDeptId}/role/{roleId}")
    @ApiOperation("修改保存角色")
    public AjaxResult edit(@PathVariable Long currentDeptId, @PathVariable Long roleId, @Validated @RequestBody SysRoleUpdateVo roleUpdateVo) {
        return toAjax(sysDeptRoleService.updateSysRole(currentDeptId, roleUpdateVo.toSysRole(roleId)));
    }


    /**
     * 新增角色
     */
    @RequiresPermissions("system:SysDeptRole:manage")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping("/dept/{currentDeptId}/role")
    @ApiOperation("新增角色")
    public AjaxResult add(@PathVariable Long currentDeptId, @Validated @RequestBody SysRoleInputVo roleInput) {
        return toAjax(sysDeptRoleService.addSysRole(currentDeptId, roleInput));
    }


    /**
     * 删除角色
     */
    @RequiresPermissions("system:SysDeptRole:manage")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/dept/{currentDeptId}/role/{roleIds}")
    @ApiOperation("删除角色")
    public AjaxResult remove(@PathVariable Long currentDeptId, @PathVariable Long[] roleIds) {
        for (Long roleId : roleIds) {
            SysRole oldRole = roleService.selectRoleById(roleId);
            if (sysDeptRoleService.isDefaultRole(oldRole.getRoleKey())) {
                throw new ServiceException("默认角色不允许删除");
            }
        }
        sysDeptRoleService.deleteSysRoleListForSysDept(currentDeptId, roleIds);
        return toAjax(roleService.deleteRoleByIds(roleIds));
    }
}
