package com.cpop.system.business.controller.backstage;

import com.cpop.core.base.R;
import com.cpop.system.business.bo.MenuListBo;
import com.cpop.system.business.bo.RoleBo;
import com.cpop.system.business.bo.RolePageBo;
import com.cpop.system.business.bo.RoleStatusBo;
import com.cpop.system.business.service.MenuService;
import com.cpop.system.business.service.RoleService;
import com.cpop.system.business.vo.MenuVo;
import com.cpop.system.business.vo.RolePageVo;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色表 控制层。
 *
 * @author DB
 * @since 2023-10-19
 */
@RestController
@Api(tags = "系统角色表接口")
@RequestMapping("/backstage/role")
public class BackstageRoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    /**
     * @descriptions 查询系统角色分页列表
     * @author DB
     * @date 2023/09/10 16:51
     * @param bo 请求参数
     * @return R<PageVo<SysRolePageVo>>
     */
    @PreAuthorize("@aps.hasPermission('system:role:list')")
    @ApiOperation("查询系统角色分页列表")
    @GetMapping("/getSysRolePageList")
    public R<Page<RolePageVo>> getSysRolePageList(RolePageBo bo) {
        Page<RolePageVo> pageVo = roleService.getSysRolePageList(bo);
        return R.ok(pageVo);
    }

    /**
     * @descriptions 新增角色
     * @author DB
     * @date 2023/10/12 10:48
     * @param bo 请求参数
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PreAuthorize("@aps.hasPermission('system:role:insert')")
    @ApiOperation("新增角色")
    @PostMapping("/insertSysRole")
    public R<Void> insertSysRole(@RequestBody @Validated RoleBo bo) {
        roleService.insertSysRole(bo);
        return R.ok();
    }

    /**
     * @Description: 获取系统菜单树列表
     * @return: R<List<SysMenuVo>>
     * @Author: DB
     * @Date: 2023/5/10 14:39
     **/
    @ApiOperation("获取菜单树列表")
    @GetMapping("/getSysMenuTreeList")
    public R<List<MenuVo>> getSysMenuTreeList() {
        MenuListBo menuListBo = new MenuListBo();
        menuListBo.setStatus(true);
        List<MenuVo> list = menuService.getSysMenuTreeList(menuListBo);
        //过滤掉没有权限的数据
        return R.ok(list);
    }

    /**
     * @descriptions 修改角色
     * @author DB
     * @date 2023/09/10 17:45
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @PreAuthorize("@aps.hasPermission('system:role:update')")
    @ApiOperation("修改角色")
    @PutMapping("/updateSysRole")
    public R<Void> updateSysRole(@RequestBody @Validated RoleBo bo) {
        roleService.updateSysRole(bo);
        return R.ok();
    }

    /**
     * 删除系统角色表
     */
    @PreAuthorize("@aps.hasPermission('system:role:remove')")
    @ApiOperation("删除系统角色表")
    @DeleteMapping("/removeSysRole/{id}")
    public R<Void> removeSysRole(@PathVariable String id) {
        roleService.removeSysRole(id);
        return R.ok();
    }

    /**
     * @Description: 设置角色状态
     * @param bo 请求参数
     * @return: R<Void>
     * @Author: DB
     * @Date: 2023/5/9 14:13
     **/
    @PreAuthorize("@aps.hasPermission('system:role:update')")
    @ApiOperation("设置角色状态")
    @PutMapping("/setSysRoleStatus")
    public R<Void> setSysRoleStatus(@RequestBody @Validated RoleStatusBo bo) {
        roleService.setSysRoleStatus(bo);
        return R.ok();
    }

}
