package ink.wgink.service.role.controller.resources;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.role.RoleUserDTO;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.pos.RolePO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import ink.wgink.service.role.service.IRoleUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: RoleUserController
 * @Description: 角色用户
 * @Author: WangGeng
 * @Date: 2021/2/18 11:19
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_RESOURCE_PREFIX + "角色用户")
@RestController
@RequestMapping(ISystemConstant.RESOURCE_PREFIX + "/role/user")
public class RoleUserResourceController extends DefaultBaseController {

    @Autowired
    private IRoleUserService roleUserService;

    @ApiOperation(value = "角色用户ID列表", notes = "通过角色ID获取角色用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "用户角色ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-userid/role-id/{roleId}")
    public List<String> listUserIdByRoleId(@PathVariable("roleId") String roleId) {
        return roleUserService.listUserId(roleId);
    }

    @ApiOperation(value = "角色用户ID列表", notes = "通过角色ID列表获取角色用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "用户角色ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-userid/role-ids")
    public List<String> listUserIdByRoleIds(@RequestBody IdsVO idsVO) {
        return roleUserService.listUserId(idsVO.getIds());
    }

    @ApiOperation(value = "角色用户分页列表", notes = "角色用户分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-page/{roleId}")
    public SuccessResultList<List<RoleUserDTO>> listPage(@PathVariable("roleId") String roleId, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return roleUserService.listPage(roleId, page);
    }

    @ApiOperation(value = "角色列表", notes = "通过用户ID获取角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path", dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-page/userid/{userId}")
    public List<RolePO> listRolePOByUserId(@PathVariable("userId") String userId) {
        return roleUserService.listRolePOByUserId(userId);
    }

    @ApiOperation(value = "用户ID列表", notes = "通过角色ID和用户ID列表获取用户ID列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "path", dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-user-id/role-id/{roleId}/user-ids")
    public List<String> listUserIdByRoleIdAndUserIds(@PathVariable("roleId") String roleId, @RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("id列表不能为空");
        }
        return roleUserService.listUserIdByRoleIdAndUserIds(roleId, idsVO.getIds());
    }

    @ApiOperation(value = "用户ID列表", notes = "通过角色ID列表和用户ID列表获取用户ID列表")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-user-id/role-ids/user-ids")
    public List<String> listUserIdByRoleIdAndUserIds(@RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("id列表不能为空");
        }
        if (idsVO.getIds2().isEmpty()) {
            throw new ParamsException("id2列表不能为空");
        }
        return roleUserService.listUserIdByRoleIdsAndUserIds(idsVO.getIds(), idsVO.getIds2());
    }

    @ApiOperation(value = "角色ID列表", notes = "通过用户ID列表获取角色ID列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path", dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-role-id/user-id/{userId}")
    public List<String> listRoleIdByUserId(@PathVariable("userId") String userId) {
        return roleUserService.listRoleIdByUserId(userId);
    }

    @ApiOperation(value = "用户列表", notes = "通过角色ID获取用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "用户角色ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-user/role-id/{roleId}")
    public List<UserDTO> listUserByRoleId(@PathVariable("roleId") String roleId) {
        return roleUserService.listUserByRoleId(roleId);
    }

    @ApiOperation(value = "用户分页列表", notes = "通过角色ID获取用户分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "用户角色ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-user/role-id/{roleId}")
    public SuccessResultList<List<UserDTO>> listPageUserByRoleId(@PathVariable("roleId") String roleId, ListPage page) {
        return roleUserService.listPageUserByRoleId(roleId, page);
    }

    @ApiOperation(value = "无部门无角色用户列表", notes = "无部门无角色用户列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-user/no-department/no-role")
    public List<UserDTO> listUserNoDepartmentAndNoRole() {
        Map<String, Object> params = requestParams();
        return roleUserService.listUserNoDepartmentAndNoRole(params);
    }

    @ApiOperation(value = "无部门无角色用户分页列表", notes = "无部门无角色用户分页列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-user/no-department/no-role")
    public SuccessResultList<List<UserDTO>> listPageUserNoDepartmentAndNoRole(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return roleUserService.listPageUserNoDepartmentAndNoRole(page);
    }

}
