package com.agileboot.system.role.controller;

import com.agileboot.common.core.core.R;
import com.agileboot.common.core.utils.poi.CustomExcelUtil;
import com.agileboot.common.mybatis.core.page.PageR;
import com.agileboot.system.role.dto.*;
import com.agileboot.system.role.service.ISysRoleService;
import com.agileboot.system.role.vo.RoleVO;
import com.agileboot.system.user.dto.UserInfo;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 */
@RestController
@RequestMapping("/system/role")
@Validated
@RequiredArgsConstructor
public class SysRoleController {

    private final ISysRoleService sysRoleService;

    /**
     * 角色列表
     */
    @GetMapping("/list")
    public PageR<RoleVO> list(RoleQuery query) {
        PageR<RoleVO> page = sysRoleService.getRoleList(query);
        return page;
    }

    /**
     * 角色列表导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, RoleQuery query) {
        PageR<RoleVO> page = sysRoleService.getRoleList(query);
        CustomExcelUtil.writeToResponse(page.getRows(), RoleVO.class, response);
    }

    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping(value = "/{roleId}")
    public R<RoleVO> getInfo(@PathVariable("roleId") @NotNull Long roleId) {
        RoleVO roleInfo = sysRoleService.getRoleInfo(roleId);
        return R.ok(roleInfo);
    }

    /**
     * 新增角色
     */
    @PostMapping("/create")
    public R<Void> add(@RequestBody AddRoleDTO addDTO) {
        sysRoleService.addRole(addDTO);
        return R.ok();
    }

    /**
     * 移除角色
     */
    @PostMapping(value = "/del/{roleId}")
    public R<Void> remove(@PathVariable("roleId") List<Long> roleIds) {
        sysRoleService.deleteRole(roleIds);
        return R.ok();
    }

    /**
     * 修改保存角色
     */
    @PostMapping("/update")
    public R<Void> edit(@Validated @RequestBody UpdateRoleDTO updateDTO) {
        sysRoleService.updateRole(updateDTO);
        return R.ok();
    }

    /**
     * 修改保存数据权限
     */
    @PostMapping("/{roleId}/dataScope")
    public R<Void> dataScope(@PathVariable("roleId") Long roleId,
                             @RequestBody UpdateDataScopeDTO updateDTO) {
        updateDTO.setRoleId(roleId);
        sysRoleService.updateDataScope(updateDTO);
        return R.ok();
    }

    /**
     * 角色状态修改
     */
    @PostMapping("/status/{roleId}")
    public R<Void> changeStatus(@PathVariable("roleId") Long roleId,
                                @RequestBody UpdateStatusDTO updateDTO) {
        updateDTO.setRoleId(roleId);

        sysRoleService.updateStatus(updateDTO);
        return R.ok();
    }


    /**
     * 查询已分配用户角色列表
     */
    @GetMapping("/{roleId}/allocated/list")
    public PageR<UserInfo> allocatedUserList(@PathVariable("roleId") Long roleId,
                                             AllocatedRoleQuery query) {
        query.setRoleId(roleId);
        PageR<UserInfo> page = sysRoleService.getAllocatedUserList(query);
        return page;
    }

    /**
     * 查询未分配用户角色列表
     */
    @GetMapping("/{roleId}/unallocated/list")
    public PageR<UserInfo> unallocatedUserList(@PathVariable("roleId") Long roleId,
                                               UnallocatedRoleQuery query) {
        query.setRoleId(roleId);
        PageR<UserInfo> page = sysRoleService.getUnallocatedUserList(query);
        return page;
    }


    /**
     * 批量取消授权用户
     */
    @PostMapping("/users/{userIds}/grant/bulk")
    public R<Void> deleteRoleOfUserByBulk(@PathVariable("userIds") List<Long> userIds) {
        sysRoleService.deleteRoleOfUserByUserIds(userIds);
        return R.ok();
    }

    /**
     * 批量选择用户授权
     */
    @PostMapping("/{roleId}/users/{userIds}/grant/bulk")
    public R<Void> addRoleForUserByBulk(@PathVariable("roleId") Long roleId,
                                        @PathVariable("userIds") List<Long> userIds) {
        sysRoleService.addRoleOfUserByByUserIds(roleId, userIds);
        return R.ok();
    }
}
