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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.login.oauth2.client.remote.position.IPositionUserRemoteService;
import ink.wgink.login.oauth2.client.service.position.IPositionUserService;
import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.position.PositionSimpleDTO;
import ink.wgink.pojo.dtos.position.PositionUserDTO;
import ink.wgink.pojo.pos.PositionPO;
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;

/**
 * @ClassName: PositionUserServiceImpl
 * @Description: 职位用户
 * @Author: wanggeng
 * @Date: 2021/9/22 10:37 下午
 * @Version: 1.0
 */
@Service
public class PositionUserServiceImpl extends DefaultBaseService implements IPositionUserService {

    @Autowired
    private IPositionUserRemoteService positionUserRemoteService;
    @Autowired
    private ApiPathProperties apiPathProperties;

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

    @Override
    public List<String> listUserId(List<String> positionIds) {
        if (positionIds.isEmpty()) {
            return new ArrayList<>();
        }
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(positionIds);
        return positionUserRemoteService.listUserIdByPositionIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

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

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

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

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

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

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

    @Override
    public List<PositionPO> listPositionPOByUserIds(List<String> userIds) {
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(userIds);
        return positionUserRemoteService.listPositionPOByUserIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public List<PositionSimpleDTO> listSimple(List<PositionPO> positionPOs) {
        if (positionPOs == null || positionPOs.isEmpty()) {
            return new ArrayList<>();
        }
        List<PositionSimpleDTO> positionSimpleDTOs = new ArrayList<>();
        positionPOs.forEach(positionPO -> {
            PositionSimpleDTO positionSimpleDTO = new PositionSimpleDTO();
            positionSimpleDTO.setPositionId(positionPO.getPositionId());
            positionSimpleDTO.setPositionParentId(positionPO.getPositionParentId());
            positionSimpleDTO.setPositionName(positionPO.getPositionName());
            positionSimpleDTO.setPositionCode(positionPO.getPositionCode());
            positionSimpleDTOs.add(positionSimpleDTO);
        });
        return positionSimpleDTOs;
    }
}
