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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.user.IUserBaseService;
import ink.wgink.login.oauth2.client.remote.department.IDepartmentUserRemoteService;
import ink.wgink.login.oauth2.client.service.department.IDepartmentUserService;
import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.department.DepartmentContainUserDTO;
import ink.wgink.pojo.dtos.department.DepartmentDTO;
import ink.wgink.pojo.dtos.department.DepartmentSimpleDTO;
import ink.wgink.pojo.dtos.department.DepartmentUserDTO;
import ink.wgink.pojo.dtos.user.UserDTO;
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.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: DepartmentUserServiceImpl
 * @Description: 组织机构用户
 * @Author: wanggeng
 * @Date: 2021/9/22 10:33 下午
 * @Version: 1.0
 */
@Service
public class DepartmentUserServiceImpl extends DefaultBaseService implements IDepartmentUserService {

    @Autowired
    private IUserBaseService userBaseService;
    @Autowired
    private IDepartmentUserRemoteService departmentUserRemoteService;
    @Autowired
    private ApiPathProperties apiPathProperties;

    @Override
    public List<DepartmentUserDTO> list(Map<String, Object> params) {
        return departmentUserRemoteService.list(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), params);
    }

    @Override
    public List<DepartmentUserDTO> list(String departmentId) {
        return departmentUserRemoteService.listByDepartmentId(apiPathProperties.getUserCenter(), departmentId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

    @Override
    public List<DepartmentUserDTO> list(List<String> departmentIds) {
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(departmentIds);
        return departmentUserRemoteService.listByDepartmentIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public List<DepartmentUserDTO> listByUserIds(List<String> userIds) {
        if (userIds.isEmpty()) {
            return new ArrayList<>();
        }
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(userIds);
        return departmentUserRemoteService.listByUserIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

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

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

    @Override
    public List<String> listUserId(String departmentId, List<String> userIds) {
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(userIds);
        return departmentUserRemoteService.listUserIdByDepartmentIdAndUserIds(apiPathProperties.getUserCenter(), departmentId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public List<String> listUserId(List<String> departmentIds, List<String> userIds) {
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(departmentIds);
        idsVO.setIds2(userIds);
        return departmentUserRemoteService.listUserIdByDepartmentIdsAndUserIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

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

    @Override
    public List<DepartmentUserDTO> list(String departmentId, Map<String, Object> params) {
        return departmentUserRemoteService.list(apiPathProperties.getUserCenter(), departmentId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), params);
    }

    @Override
    public void setUser(List<DepartmentUserDTO> departmentUserDTOs) {
        if (departmentUserDTOs.isEmpty()) {
            return;
        }
        Set<String> userIdSet = new HashSet<>();
        for (DepartmentUserDTO hasDepartmentUserId : departmentUserDTOs) {
            userIdSet.add(hasDepartmentUserId.getUserId());
        }
        List<UserDTO> userDTOs = userBaseService.listByUserIds(new ArrayList<>(userIdSet));
        for (DepartmentUserDTO departmentUserDTO : departmentUserDTOs) {
            for (UserDTO userDTO : userDTOs) {
                if (StringUtils.equals(departmentUserDTO.getUserId(), userDTO.getUserId())) {
                    departmentUserDTO.setUser(userDTO);
                    break;
                }
            }
        }
    }

    @Override
    public List<DepartmentDTO> listDepartmentBySelf() {
        List<DepartmentDTO> departmentDTOs = new ArrayList<>();
        List<DepartmentSimpleDTO> departmentSimpleDTOs = securityComponent.getCurrentUser().getDepartments();
        departmentSimpleDTOs.forEach(departmentSimpleDTO -> {
            DepartmentDTO departmentDTO = new DepartmentDTO();
            BeanUtils.copyProperties(departmentSimpleDTO, departmentDTO);
            departmentDTOs.add(departmentDTO);
        });
        return departmentDTOs;
    }

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

    @Override
    public List<UserDTO> listUserByDepartmentId(String departmentId) {
        return departmentUserRemoteService.listUserByDepartmentId(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), departmentId);
    }

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

    @Override
    public List<String> listDepartmentIdByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return new ArrayList<>();
        }
        return departmentUserRemoteService.listDepartmentIdByUserId(apiPathProperties.getUserCenter(), userId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

    @Override
    public List<String> listDepartmentIdByUserIds(List<String> userIds) {
        if (userIds == null && userIds.isEmpty()) {
            return new ArrayList<>();
        }
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(userIds);
        return departmentUserRemoteService.listDepartmentIdByUserIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public List<String> listSameDepartmentUserIdByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return new ArrayList<>();
        }
        return departmentUserRemoteService.listSameDepartmentUserIdByUserId(apiPathProperties.getUserCenter(), userId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

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

    @Override
    public List<UserDTO> listUserByExcludeDepartmentId(String excludeDepartmentId) {
        if (StringUtils.isBlank(excludeDepartmentId)) {
            return new ArrayList<>();
        }
        return departmentUserRemoteService.listUserByExcludeDepartmentId(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), excludeDepartmentId);
    }

    @Override
    public List<UserDTO> listUserByExcludeDepartmentIds(List<String> excludeDepartmentIds) {
        if (excludeDepartmentIds == null || excludeDepartmentIds.isEmpty()) {
            return new ArrayList<>();
        }
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(excludeDepartmentIds);
        return departmentUserRemoteService.listUserByExcludeDepartmentIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public SuccessResultList<List<UserDTO>> listPageUserByExcludeDepartmentId(String excludeDepartmentId, ListPage page) {
        if (StringUtils.isBlank(excludeDepartmentId)) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        return departmentUserRemoteService.listPageUserByExcludeDepartmentId(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), excludeDepartmentId, page.getPage(), page.getRows(), page.getParams());
    }

    @Override
    public SuccessResultList<List<UserDTO>> listPageUserByExcludeDepartmentId(List<String> excludeDepartmentIds, ListPage page) {
        if (excludeDepartmentIds == null || excludeDepartmentIds.isEmpty()) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(excludeDepartmentIds);
        return departmentUserRemoteService.listPageUserByExcludeDepartmentIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO, page.getPage(), page.getRows(), page.getParams());
    }

    @Override
    public DepartmentContainUserDTO getDepartmentContainUserAndSubByDepartmentId(String departmentId) {
        if (StringUtils.isBlank(departmentId)) {
            return null;
        }
        return departmentUserRemoteService.getDepartmentContainUserAndSubByDepartmentId(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), departmentId);
    }

}
