package com.qiaoba.module.system.controller;

import com.qiaoba.api.system.entity.SysRole;
import com.qiaoba.api.system.entity.dto.DataScopeDto;
import com.qiaoba.api.system.entity.dto.SysRoleDto;
import com.qiaoba.api.system.entity.param.SysRoleParam;
import com.qiaoba.api.system.entity.param.SysUserParam;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.common.poi.util.ExcelUtil;
import com.qiaoba.module.system.service.SysRoleService;
import com.qiaoba.module.system.service.SysUserRoleService;
import com.qiaoba.module.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 角色管理 web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 04:20:28
 */
@Validated
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
@Tag(name = "角色管理")
public class SysRoleController {

    private final SysRoleService sysRoleService;
    private final SysUserService sysUserService;
    private final SysUserRoleService sysUserRoleService;

    @PreAuthorize("hasAuthority('system:role:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public TableDataInfo<SysRole> list(@Validated SysRoleParam param, PageQuery pageQuery) {
        return sysRoleService.selectPageList(param, pageQuery);
    }

    @PreAuthorize("hasAuthority('system:role:export')")
    @PostMapping("/export")
    @Operation(summary = "导出列表")
    public void export(HttpServletResponse response, @Validated SysRoleParam param) {
        List<SysRole> list = sysRoleService.selectList(param);
        ExcelUtil.exportExcel(list, SysRole.class, "角色数据", response);
    }

    @PreAuthorize("hasAuthority('system:role:query')")
    @GetMapping(value = "/{roleId}")
    @Operation(summary = "获取详情")
    public AjaxResult getInfo(@Size(min = 19, max = 20, message = "ID不合法") @PathVariable String roleId) {
        return AjaxResult.success(sysRoleService.selectById(roleId));
    }

    @PreAuthorize("hasAuthority('system:role:add')")
    @PostMapping
    @Operation(summary = "添加角色")
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody SysRoleDto roleDto) {
        return AjaxResult.toAjax(sysRoleService.insert(roleDto));

    }

    @PreAuthorize("hasAuthority('system:role:edit')")
    @PutMapping
    @Operation(summary = "修改角色")
    public AjaxResult edit(@Validated(EditGroup.class) @RequestBody SysRoleDto roleDto) {
        return AjaxResult.toAjax(sysRoleService.updateById(roleDto));

    }

    @PreAuthorize("hasAuthority('system:role:edit')")
    @PutMapping("/changeStatus")
    @Operation(summary = "更改状态")
    public AjaxResult changeStatus(@RequestBody SysRoleDto dto) {
        return AjaxResult.toAjax(sysRoleService.changeStatus(dto.getRoleId(), dto.getStatus()));
    }

    @PreAuthorize("hasAuthority('system:role:remove')")
    @DeleteMapping("/{roleIds}")
    @Operation(summary = "删除角色")
    public AjaxResult remove(@PathVariable List<String> roleIds) {
        return AjaxResult.toAjax(sysRoleService.deleteByIds(roleIds));
    }

    @PreAuthorize("hasAuthority('system:role:edit')")
    @PutMapping("/dataScope")
    @Operation(summary = "数据权限")
    public AjaxResult dataScope(@RequestBody DataScopeDto dto) {
        return AjaxResult.toAjax(sysRoleService.authDataScope(dto));
    }

    @PreAuthorize("hasAuthority('system:role:list')")
    @GetMapping("/authUser/allocatedList")
    @Operation(summary = "已绑定用户")
    public TableDataInfo allocatedList(SysUserParam param, PageQuery pageQuery) {
        return sysUserService.selectAllocatedList(param, pageQuery);
    }

    @PreAuthorize("hasAuthority('system:role:edit')")
    @PutMapping("/authUser/cancel")
    @Operation(summary = "解绑用户")
    public AjaxResult cancelAuthUser(@RequestParam("roleId") String roleId, @RequestParam("userIds") List<String> userIds) {
        sysUserRoleService.deleteByRoleIdAndUserIds(roleId, userIds);
        return AjaxResult.success();
    }

    @PreAuthorize("hasAuthority('system:role:list')")
    @GetMapping("/authUser/unallocatedList")
    @Operation(summary = "未绑定用户")
    public TableDataInfo unallocatedList(SysUserParam param, PageQuery pageQuery) {
        return sysUserService.selectUnAllocatedList(param, pageQuery);
    }

    @PreAuthorize("hasAuthority('system:role:edit')")
    @PutMapping("/authUser/insert")
    @Operation(summary = "绑定用户")
    public AjaxResult insertAuthUsers(@RequestParam("roleId") String roleId, @RequestParam("userIds") List<String> userIds) {
        sysUserRoleService.insertAuthUsers(roleId, userIds);
        return AjaxResult.success();
    }

}
