package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName: RoleController
 * Package: com.atguigu.ssyx.acl.controller
 * 角色管理 前端控制器
 *
 * @author yovinchen
 * @Create 2023/9/14 09:09
 */
@RestController
@RequestMapping("/admin/acl/role")
@Api(tags = "角色管理")

@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "获取角色分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@ApiParam(name = "page", value = "当前页码", required = true)
                        @PathVariable Long page, @ApiParam(name = "limit", value = "每页记录数", required = true)
                        @PathVariable Long limit, @ApiParam(name = "roleQueryVo", value = "查询对象", required = false)
                        RoleQueryVo roleQueryVo) {
        try {
            Page<Role> pageParam = new Page<>(page, limit);
            IPage<Role> pageModel = roleService.selectPage(pageParam, roleQueryVo);
            return Result.ok(pageModel);
        } catch (Exception e) {
            // 处理异常，例如记录日志或者抛出自定义异常
            throw new RuntimeException("获取角色分页列表异常", e);
        }
    }

    @ApiOperation(value = "获取角色")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        try {
            Role role = roleService.getById(id);
            return Result.ok(role);
        } catch (Exception e) {
            throw new RuntimeException("获取角色异常", e);
        }
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("save")
    public Result save(@RequestBody Role role) {
        try {
            boolean result = roleService.save(role);
            return result ? Result.ok(null) : Result.fail(null);
        } catch (Exception e) {
            // 处理异常，例如记录日志或者抛出自定义异常
            throw new RuntimeException("新增角色异常" + role.toString(), e);
        }
    }

    @ApiOperation(value = "修改角色")
    @PutMapping("update")
    public Result updateById(@RequestBody Role role) {
        try {
            boolean result = roleService.updateById(role);
            return result ? Result.ok(null) : Result.fail(null);
        } catch (Exception e) {
            // 处理异常，例如记录日志或者抛出自定义异常
            throw new RuntimeException("修改角色异常" + role.toString(), e);
        }
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        try {
            boolean result = roleService.removeById(id);
            return result ? Result.ok(null) : Result.fail(null);
        } catch (Exception e) {
            // 处理异常，例如记录日志或者抛出自定义异常
            throw new RuntimeException("删除角色异常", e);
        }
    }

    @ApiOperation(value = "根据id列表删除角色")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        //        TODO
        try {
            boolean result = roleService.removeByIds(idList);
            return result ? Result.ok(null) : Result.fail(null);
        } catch (Exception e) {
            // 处理异常，例如记录日志或者抛出自定义异常
            throw new RuntimeException("根据id列表删除角色异常", e);
        }
    }

    @ApiOperation(value = "获取一个角色的所有权限列表")
    @GetMapping("toAssign/{id}")
    public Result toAssign(@PathVariable Long id) {
//        TODO
        try {
            Map<String, Object> result = permissionService.getPermissionById(id);
            return Result.ok(result);
        } catch (Exception e) {
            // 处理异常，例如记录日志或者抛出自定义异常
            throw new RuntimeException("获取一个角色的所有权限列表异常", e);
        }
    }
}
