package org.dromara.member.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.member.domain.bo.AppRoleBo;
import org.dromara.member.domain.vo.AppRoleVo;
import org.dromara.member.service.AppRoleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * App端角色信息
 *
 * @author pengles
 */
@Tag(name = "App端角色管理", description = "App端角色信息的增删改查操作")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/role")
public class AppRoleController extends BaseController {

    private final AppRoleService appRoleService;

    /**
     * 查询App端角色信息列表
     */
    @Operation(summary = "查询App端角色信息列表", description = "分页查询App端角色信息列表")
    @SaCheckPermission("app:role:list")
    @GetMapping("/list")
    public TableDataInfo<AppRoleVo> list(
            @Parameter(description = "角色查询条件") @Valid AppRoleBo bo,
            @Parameter(description = "分页查询参数") @Valid PageQuery pageQuery) {
        return appRoleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出App端角色信息列表
     */
    @Operation(summary = "导出App端角色信息列表", description = "导出App端角色信息到Excel文件")
    @SaCheckPermission("app:role:export")
    @Log(title = "App端角色信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(
            @Parameter(description = "角色查询条件") @Valid AppRoleBo bo,
            HttpServletResponse response) {
        List<AppRoleVo> list = appRoleService.queryList(bo);
        ExcelUtil.exportExcel(list, "App端角色信息", AppRoleVo.class, response);
    }

    /**
     * 获取App端角色信息详细信息
     */
    @Operation(summary = "获取App端角色信息详细信息", description = "根据角色ID获取App端角色详细信息")
    @SaCheckPermission("app:role:query")
    @GetMapping("/{roleId}")
    public R<AppRoleVo> getInfo(
            @Parameter(description = "角色ID", required = true) @PathVariable @NotNull(message = "角色ID不能为空") @Positive(message = "角色ID必须为正数") Long roleId) {
        return R.ok(appRoleService.queryById(roleId));
    }

    /**
     * 新增App端角色信息
     */
    @Operation(summary = "新增App端角色信息", description = "新增App端角色信息")
    @SaCheckPermission("app:role:add")
    @Log(title = "App端角色信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(
            @Parameter(description = "角色信息", required = true) @Validated(AddGroup.class) @RequestBody @NotNull(message = "角色信息不能为空") AppRoleBo bo) {
        return toAjax(appRoleService.insertByBo(bo));
    }

    /**
     * 修改App端角色信息
     */
    @Operation(summary = "修改App端角色信息", description = "修改App端角色信息")
    @SaCheckPermission("app:role:edit")
    @Log(title = "App端角色信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(
            @Parameter(description = "角色信息", required = true) @Validated(EditGroup.class) @RequestBody @NotNull(message = "角色信息不能为空") AppRoleBo bo) {
        return toAjax(appRoleService.updateByBo(bo));
    }

    /**
     * 删除App端角色信息
     */
    @Operation(summary = "删除App端角色信息", description = "批量删除App端角色信息")
    @SaCheckPermission("app:role:remove")
    @Log(title = "App端角色信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public R<Void> remove(
            @Parameter(description = "角色ID数组", required = true) @PathVariable @NotEmpty(message = "角色ID不能为空") Long[] roleIds) {
        return toAjax(appRoleService.deleteWithValidByIds(List.of(roleIds), true));
    }

    /**
     * 获取角色选择框列表
     */
    @Operation(summary = "获取角色选择框列表", description = "获取所有角色的选择框列表")
    @SaCheckPermission("app:role:query")
    @GetMapping("/optionselect")
    public R<List<AppRoleVo>> optionselect() {
        return R.ok(appRoleService.selectRoleAll());
    }

    /**
     * 查询已分配用户角色列表
     */
    @Operation(summary = "查询已分配用户角色列表", description = "分页查询已分配用户的角色列表")
    @SaCheckPermission("app:role:list")
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo<AppRoleVo> allocatedList(
            @Parameter(description = "角色查询条件") @Valid AppRoleBo bo,
            @Parameter(description = "分页查询参数") @Valid PageQuery pageQuery) {
        return appRoleService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询未分配用户角色列表
     */
    @Operation(summary = "查询未分配用户角色列表", description = "分页查询未分配用户的角色列表")
    @SaCheckPermission("app:role:list")
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo<AppRoleVo> unallocatedList(
            @Parameter(description = "角色查询条件") @Valid AppRoleBo bo,
            @Parameter(description = "分页查询参数") @Valid PageQuery pageQuery) {
        return appRoleService.queryPageList(bo, pageQuery);
    }

    /**
     * 取消授权用户
     */
    @Operation(summary = "取消授权用户", description = "取消用户的角色授权")
    @SaCheckPermission("app:role:edit")
    @Log(title = "App端角色信息", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public R<Void> cancelAuthUser(
            @Parameter(description = "角色信息", required = true) @RequestBody @NotNull(message = "角色信息不能为空") @Valid AppRoleBo bo) {
        return toAjax(appRoleService.deleteAuthUser(bo.getUserId(), bo.getRoleId()));
    }

    /**
     * 批量取消授权用户
     */
    @Operation(summary = "批量取消授权用户", description = "批量取消用户的角色授权")
    @SaCheckPermission("app:role:edit")
    @Log(title = "App端角色信息", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public R<Void> cancelAuthUserAll(
            @Parameter(description = "角色ID", required = true) @NotNull(message = "角色ID不能为空") @Positive(message = "角色ID必须为正数") Long roleId,
            @Parameter(description = "用户ID数组", required = true) @NotEmpty(message = "用户ID不能为空") Long[] userIds) {
        return toAjax(appRoleService.deleteAuthUsers(roleId, userIds));
    }

    /**
     * 批量选择用户授权
     */
    @Operation(summary = "批量选择用户授权", description = "批量为用户分配角色授权")
    @SaCheckPermission("app:role:edit")
    @Log(title = "App端角色信息", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public R<Void> selectAuthUserAll(
            @Parameter(description = "角色ID", required = true) @NotNull(message = "角色ID不能为空") @Positive(message = "角色ID必须为正数") Long roleId,
            @Parameter(description = "用户ID数组", required = true) @NotEmpty(message = "用户ID不能为空") Long[] userIds) {
        return toAjax(appRoleService.insertAuthUsers(roleId, userIds));
    }

}