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

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.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import ink.wgink.pojo.vos.SaveAndDeleteIdsVO;
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.Arrays;
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_SYSTEM_PREFIX + "角色用户")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/role/user")
public class RoleUserController extends DefaultBaseController {

    @Autowired
    private IRoleUserService roleUserService;

    @ApiOperation(value = "保存角色用户列表", notes = "保存角色用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save/{roleId}")
    public synchronized SuccessResult save(@PathVariable("roleId") String roleId,
                                           @RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("用户ID列表不能为空");
        }
        roleUserService.save(roleId, idsVO.getIds());
        return new SuccessResult();
    }

    @ApiOperation(value = "更新角色用户列表", notes = "更新角色用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{roleId}")
    public synchronized SuccessResult update(@PathVariable("roleId") String roleId,
                                             @RequestBody SaveAndDeleteIdsVO saveAndDeleteIdsVO) {
        roleUserService.update(roleId, saveAndDeleteIdsVO.getSaveIds(), saveAndDeleteIdsVO.getDeleteIds());
        return new SuccessResult();
    }

    @ApiOperation(value = "删除角色用户", notes = "删除角色用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "path"),
            @ApiImplicitParam(name = "ids", value = "人员ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{roleId}/{ids}")
    public synchronized SuccessResult remove(@PathVariable("roleId") String roleId,
                                             @PathVariable("ids") String ids) {
        roleUserService.delete(roleId, Arrays.asList(ids.split("_")));
        return new SuccessResult();
    }

    @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("listpage/{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 = "角色用户ID列表", notes = "角色用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "用户角色ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listuserid/{roleId}")
    public List<String> listUserId(@PathVariable("roleId") String roleId) {
        return roleUserService.listUserId(roleId);
    }

    @ApiOperation(value = "角色用户列表", notes = "角色用户列表接口")
    @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) {
        Map<String, Object> requestParams = requestParams();
        requestParams.put("roleId", roleId);
        return roleUserService.listUser(requestParams);
    }

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

    @ApiOperation(value = "角色用户列表", notes = "角色用户列表接口")
    @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) {
        Map<String, Object> requestParams = requestParams();
        requestParams.put("roleId", roleId);
        page.setParams(requestParams);
        return roleUserService.listPageUser(page);
    }

    @ApiOperation(value = "不在角色的用户列表", notes = "不在角色的用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-user/exclude-role-id/{roleId}")
    public SuccessResultList<List<UserDTO>> listPageUserByExcludeRoleId(@PathVariable("roleId") String roleId, ListPage page) {
        Map<String, Object> requestParams = requestParams();
        requestParams.put("excludeRoleId", roleId);
        page.setParams(requestParams);
        return roleUserService.listPageUser(page);
    }

}
