/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.auth.controller.manager;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.auth.entity.SysRole;
import com.codvision.auth.entity.vo.SysRoleVO;
import com.codvision.auth.service.ISysRoleService;
import com.codvision.auth.support.UserContext;
import com.codvision.authcore.annotation.RequiresPermissions;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.annotation.ApiSecret;
import com.codvision.webcore.bean.QueryPage;
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.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统角色 管理模块
 *
 * @author lingee
 * @date 2023-11-09
 */
@RestController
@RequestMapping("/v1/manager/sysRole" )
@Api(tags = "平台管理端-系统角色管理模块" )
public class ManagerSysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @ApiOperation(value = "获取系统角色分页列表" , notes = "获取系统角色分页列表接口" )
    @PostMapping("/findAll" )
    @RequiresPermissions("sys_role_list" )
    public ResponseEntity<IPage<SysRoleVO>> findAll(@RequestParam(value = "page" , required = false, defaultValue = "1" ) int page,
                                                    @RequestParam(value = "size" , required = false, defaultValue = "10" ) int size,
                                                    @ApiParam(value = "角色id" ) @RequestParam(value = "roleId" , required = false, defaultValue = "-1" ) Integer roleId) {
        QueryPage<SysRole> queryPage = new QueryPage<>(page, size);
        if (roleId != -1 && !"超级管理员".equals(UserContext.get().getActiveRoleName())) {
            SysRole queryBean = new SysRole();
            queryBean.setId(roleId);
            queryPage.setCondition(queryBean);
        }
        IPage<SysRoleVO> pageResult = sysRoleService.getPageResult(queryPage);
        return new ResponseEntity<IPage<SysRoleVO>>().ok().data(pageResult);
    }

    @PostMapping("/getAll" )
    @ApiOperation(value = "获取所有角色名称和id" , notes = "获取所有角色名称和id接口" )
    public ResponseEntity<List<SysRoleVO>> getAll() {
        ResponseEntity<List<SysRoleVO>> responseEntity = new ResponseEntity<>();
        List<SysRoleVO> list = sysRoleService.listAll(null);
        return responseEntity.ok().data(list);
    }

    @ApiOperation(value = "新增系统角色" , notes = "新增系统角色接口" )
    @PostMapping("/add" )
    @RequiresPermissions("sys_role_add" )
    public ResponseEntity<String> saveSysRole(@Valid @RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return new ResponseEntity<String>().ok().message("添加成功" );
    }

    @ApiOperation(value = "删除系统角色" , notes = "删除系统角色接口" )
    @PostMapping("/del" )
    @RequiresPermissions("sys_role_del" )
    public ResponseEntity<String> delRole(@ApiParam(value = "系统角色ID" , required = true) @RequestParam Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new BusinessException(30002);
        }
        if (id == 1) {
            throw new BusinessException(20001, "默认的超级管理员不支持删除" );
        }

        sysRoleService.logicDel(id);
        return new ResponseEntity<String>().ok().message("删除成功" );
    }

    @ApiOperation(value = "编辑系统角色" , notes = "编辑接系统角色接口" )
    @PostMapping("/update" )
    @RequiresPermissions("sys_role_edit" )
    public ResponseEntity<String> updateSysRole(@RequestBody SysRole sysRole) {
        if (ObjectUtil.isEmpty(sysRole)) {
            throw new BusinessException(30002);
        }
        sysRoleService.updateById(sysRole);
        return new ResponseEntity<String>().ok().message("编辑成功" );
    }

    @ApiSecret(ignore = true)
    @PostMapping("/setRoleMenu" )
    @ApiOperation("设置角色菜单权限" )
    @RequiresPermissions("sys_role_set_menu" )
    public ResponseEntity<String> setRoleMenu(@RequestParam(value = "roleId" ) Integer roleId,
                                              @RequestBody List<Integer> menuIdList) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (ObjectUtil.isEmpty(roleId)) {
            throw new BusinessException(30002);
        }

        sysRoleService.setRoleMenu(roleId, menuIdList);
        return responseEntity.ok();
    }
}
