package com.njzscloud.supervisory.role.controller;

import cn.hutool.core.lang.Assert;
import com.njzscloud.supervisory.role.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.njzscloud.supervisory.role.pojo.RoleBindResourceParam;
import com.njzscloud.supervisory.role.pojo.RoleAddParam;
import com.njzscloud.supervisory.role.pojo.RoleModifyParam;
import com.njzscloud.supervisory.role.pojo.RoleQueryParam;
import com.njzscloud.supervisory.menu.pojo.MenuDetailResult;
import com.njzscloud.supervisory.role.pojo.RoleDetailResult;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 角色
 */
@Slf4j
@RestController
@RequestMapping("/sys_role")
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService sysRoleService;

    /**
     * 绑定菜单
     */
    @PostMapping("/bind_menu")
    public R<?> bindMenu(@RequestBody RoleBindResourceParam roleBindResourceParam) {
        sysRoleService.bindMenu(roleBindResourceParam);
        return R.success();
    }

    /**
     * 查询角色拥有的菜单
     *
     * @param roleId 角色 Id
     * @return 菜单列表
     */
    @GetMapping("/owned_menu")
    public R<List<MenuDetailResult>> ownedMenu(@RequestParam Long roleId) {
        return R.success(sysRoleService.ownedMenu(roleId));
    }

    /**
     * 新增
     *
     * @param roleAddParam 数据
     * @return Long
     */
    @PostMapping("/add")
    public R<Long> add(@RequestBody @Validated RoleAddParam roleAddParam) {
        return R.success(sysRoleService.add(roleAddParam));
    }

    /**
     * 修改
     *
     * @param roleModifyParam 数据
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody RoleModifyParam roleModifyParam) {
        sysRoleService.modify(roleModifyParam);
        return R.success();
    }

    /**
     * 删除
     *
     * @param ids Id
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        Assert.notEmpty(ids, "未指定要删除的数据");
        sysRoleService.del(ids);
        return R.success();
    }

    /**
     * 详情
     *
     * @param id Id
     * @return RoleDetailResult 结果
     */
    @GetMapping("/detail")
    public R<RoleDetailResult> detail(@RequestParam Long id) {
        return R.success(sysRoleService.detail(id));
    }

    /**
     * 分页查询
     *
     * @param roleQueryParam 筛选条件
     * @param page           分页参数
     * @return PageResult&lt;RoleDetailResult&gt; 分页结果
     */
    @GetMapping("/paging")
    public R<PageResult<RoleDetailResult>> paging(PageParam page, RoleQueryParam roleQueryParam) {
        return R.success(sysRoleService.paging(page.toPage(), roleQueryParam));
    }

    @GetMapping("/user_role")
    public R<Set<String>> listUserRole(@RequestParam Long userId) {
        return R.success(sysRoleService.listUserRole(userId));
    }

}
