package ink.wgink.login.oauth2.client.service.role.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.login.oauth2.client.remote.role.IRoleUserRemoteService;
import ink.wgink.login.oauth2.client.service.role.IRoleUserService;
import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.role.RoleSimpleDTO;
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.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import ink.wgink.properties.ApiPathProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName: RoleUserServiceImpl
 * @Description: 角色用户
 * @Author: wanggeng
 * @Date: 2021/9/22 10:35 下午
 * @Version: 1.0
 */
@Service
public class RoleUserServiceImpl extends DefaultBaseService implements IRoleUserService {

    @Autowired
    private IRoleUserRemoteService roleUserRemoteService;
    @Autowired
    private ApiPathProperties apiPathProperties;

    @Override
    public List<String> listUserId(String roleId) {
        return roleUserRemoteService.listUserIdByRoleId(apiPathProperties.getUserCenter(), roleId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

    @Override
    public List<String> listUserId(List<String> roleIds) {
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(roleIds);
        return roleUserRemoteService.listUserIdByRoleIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public SuccessResultList<List<RoleUserDTO>> listPage(String roleId, ListPage page) {
        return roleUserRemoteService.listPage(apiPathProperties.getUserCenter(), roleId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), page.getPage(), page.getRows(), page.getParams());
    }

    @Override
    public List<RolePO> listRolePOByUserId(String userId) {
        return roleUserRemoteService.listRolePOByUserId(apiPathProperties.getUserCenter(), userId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

    @Override
    public List<RolePO> listRolePOByRoleIds(List<String> roleIds) {
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(roleIds);
        return roleUserRemoteService.listRolePOByRoleIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public List<RoleSimpleDTO> listSimple(List<RolePO> rolePOs) {
        if (rolePOs == null || rolePOs.isEmpty()) {
            return new ArrayList<>();
        }
        List<RoleSimpleDTO> roleSimpleDTOs = new ArrayList<>();
        rolePOs.forEach(rolePO -> {
            RoleSimpleDTO roleSimpleDTO = new RoleSimpleDTO();
            roleSimpleDTO.setRoleId(rolePO.getRoleId());
            roleSimpleDTO.setRoleParentId(rolePO.getRoleParentId());
            roleSimpleDTO.setRoleName(rolePO.getRoleName());
            roleSimpleDTO.setRoleCode(rolePO.getRoleCode());
            roleSimpleDTO.setRoleDataRight(rolePO.getRoleDataRight());
            roleSimpleDTOs.add(roleSimpleDTO);
        });
        return roleSimpleDTOs;
    }

    @Override
    public List<String> listUserIdByRoleIdAndUserIds(String roleId, List<String> userIds) {
        if (StringUtils.isBlank(roleId)) {
            return new ArrayList<>();
        }
        if (userIds == null || userIds.isEmpty()) {
            return new ArrayList<>();
        }
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(userIds);
        return roleUserRemoteService.listUserIdByRoleIdAndUserIds(apiPathProperties.getUserCenter(), roleId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public List<String> listUserIdByRoleIdsAndUserIds(List<String> roleIds, List<String> userIds) {
        if (roleIds == null || roleIds.isEmpty()) {
            return new ArrayList<>();
        }
        if (userIds == null || userIds.isEmpty()) {
            return new ArrayList<>();
        }
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(roleIds);
        idsVO.setIds2(userIds);
        return roleUserRemoteService.listUserIdByRoleIdsAndUserIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public List<String> listRoleIdByUserId(String userId) {
        return roleUserRemoteService.listRoleIdByUserId(apiPathProperties.getUserCenter(), userId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

    @Override
    public List<UserDTO> listUserByRoleId(String roleId) {
        return roleUserRemoteService.listUserByRoleId(apiPathProperties.getUserCenter(), roleId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

    @Override
    public SuccessResultList<List<UserDTO>> listPageUserByRoleId(String roleId, ListPage page) {
        return roleUserRemoteService.listPageUserByRoleId(apiPathProperties.getUserCenter(), roleId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), page.getPage(), page.getRows(), page.getParams());
    }

    @Override
    public List<UserDTO> listUserNoDepartmentAndNoRole(Map<String, Object> params) {
        params = params == null ? getHashMap(2) : params;
        return roleUserRemoteService.listUserNoDepartmentAndNoRole(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), params);
    }

    @Override
    public SuccessResultList<List<UserDTO>> listPageUserNoDepartmentAndNoRole(ListPage page) {
        return roleUserRemoteService.listPageUserNoDepartmentAndNoRole(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), page.getPage(), page.getRows(), page.getParams());
    }

}
