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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.base.SystemException;
import ink.wgink.login.oauth2.client.remote.user.IUserRemoteService;
import ink.wgink.login.oauth2.client.service.user.IUserService;
import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import ink.wgink.pojo.vos.UpdatePasswordVO;
import ink.wgink.properties.ApiPathProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @ClassName: UserServiceImpl
 * @Description: 用户业务
 * @Author: wanggeng
 * @Date: 2021/9/22 5:32 下午
 * @Version: 1.0
 */
@Service
public class UserServiceImpl extends DefaultBaseService implements IUserService {

    @Autowired
    private IUserRemoteService userRemoteService;
    @Autowired
    private ApiPathProperties apiPathProperties;

    @Override
    public String saveDefaultUserAndReturnId(String userUsername, String username, Integer userState, Integer userType) {
        Map<String, Object> params = getHashMap(8);
        params.put("userUsername", userUsername);
        params.put("username", username);
        params.put("userState", userState);
        params.put("userType", userType);
        SuccessResultData<String> successResultData = userRemoteService.saveDefaultUser(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), params);
        return successResultData.getData();
    }

    @Override
    public UserDTO get(String userId) {
        return userRemoteService.get(apiPathProperties.getUserCenter(), userId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

    @Override
    public UserDTO getByUsername(String username) {
        return userRemoteService.getByUsername(apiPathProperties.getUserCenter(), username, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

    @Override
    public List<UserDTO> listByUserIds(List<String> userIds) {
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(userIds);
        return userRemoteService.listByIds(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public List<UserDTO> listByUsernames(List<String> usernames) {
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(usernames);
        return userRemoteService.listByUsernames(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public List<UserDTO> listByUserState(Integer userState) {
        Map<String, Object> params = getHashMap(2);
        params.put("userState", userState);
        return userRemoteService.list(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), params);
    }

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

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

    @Override
    public SuccessResultList<List<UserDTO>> listPageByIds(List<String> userIds, ListPage page) {
        page.getParams().put("userIds", userIds);
        return userRemoteService.listPage(apiPathProperties.getUserCenter(), page.getPage(), page.getRows(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), page.getParams());
    }

    @Override
    public SuccessResultList<List<UserDTO>> listPageByUserState(Integer userState, ListPage page) {
        page.getParams().put("userState", userState);
        return userRemoteService.listPage(apiPathProperties.getUserCenter(), page.getPage(), page.getRows(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), page.getParams());
    }

    @Override
    public List<UserDTO> listByIds(List<String> userIds, Map<String, Object> params) {
        params.put("userIds", userIds);
        return userRemoteService.list(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), params);
    }

    @Override
    public SuccessResultList<List<UserDTO>> listPageByExcludeIds(List<String> excludeUserIds, ListPage page) {
        page.getParams().put("excludeUserIds", excludeUserIds);
        return userRemoteService.listPage(apiPathProperties.getUserCenter(), page.getPage(), page.getRows(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), page.getParams());
    }

    @Override
    public SuccessResultList<List<UserDTO>> listPageByExcludeIdsAndIncludeIds(List<String> excludeUserIds, List<String> includeUserIds, ListPage page) {
        page.getParams().put("excludeUserIds", excludeUserIds);
        page.getParams().put("userIds", includeUserIds);
        return userRemoteService.listPage(apiPathProperties.getUserCenter(), page.getPage(), page.getRows(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), page.getParams());
    }

    @Override
    public List<UserDTO> listByExcludeIds(List<String> excludeUserIds, Map<String, Object> params) {
        params.put("excludeUserIds", excludeUserIds);
        return userRemoteService.list(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), params);
    }

    @Override
    public List<UserDTO> listByExcludeIds(List<String> excludeUserIds, List<String> includeUserIds, Map<String, Object> params) {
        params.put("excludeUserIds", excludeUserIds);
        params.put("userIds", excludeUserIds);
        return userRemoteService.list(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), params);
    }

    @Override
    public int countDateRange(String startDate, String endDate) {
        SuccessResultData<Integer> successResultData = userRemoteService.countDateRange(apiPathProperties.getUserCenter(), startDate, endDate, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
        return successResultData.getData();
    }

    @Override
    public int count() {
        SuccessResultData<Integer> successResultData = userRemoteService.count(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
        return successResultData.getData();
    }

    @Override
    public int countType(int userType) {
        SuccessResultData<Integer> successResultData = userRemoteService.countType(apiPathProperties.getUserCenter(), userType, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
        return successResultData.getData();
    }

    @Override
    public int countErrorType() {
        SuccessResultData<Integer> successResultData = userRemoteService.countErrorType(apiPathProperties.getUserCenter(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
        return successResultData.getData();
    }

    @Override
    public Integer countState(int userState) {
        SuccessResultData<Integer> successResultData = userRemoteService.countState(apiPathProperties.getUserCenter(), userState, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
        return successResultData.getData();
    }

    @Override
    public SuccessResultData<String> getPasswordStatus(String userId) {
        return userRemoteService.getPasswordStatus(apiPathProperties.getUserCenter(), userId, OAuth2ClientTokenManager.getInstance().getToken().getAccessToken());
    }

    @Override
    public List<UserDTO> listByKeywords(String keywords) {
        Map<String, Object> params = getHashMap(2);
        try {
            params.put("keywords", URLEncoder.encode(keywords, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new SystemException("编码异常");
        }
        return list(params);
    }

    @Override
    public List<String> listUserIds(List<UserDTO> userDTOs) {
        if (userDTOs.isEmpty()) {
            return new ArrayList<>();
        }
        Set<String> userIdSet = new HashSet<>();
        for (UserDTO userDTO : userDTOs) {
            userIdSet.add(userDTO.getUserId());
        }
        return new ArrayList<>(userIdSet);
    }

    @Override
    public List<UserDTO> listByUserPhones(List<String> phones) {
        if (phones.isEmpty()) {
            return new ArrayList<>();
        }
        IdsVO idsVO = new IdsVO();
        idsVO.setIds(phones);
        return userRemoteService.listByUserPhones(apiPathProperties.getUserCenter(), securityComponent.getCurrentUser().getUserId(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), idsVO);
    }

    @Override
    public void updatePassword(UpdatePasswordVO updatePasswordVO) {
        userRemoteService.updatePassword(apiPathProperties.getUserCenter(), securityComponent.getCurrentUser().getUserId(), OAuth2ClientTokenManager.getInstance().getToken().getAccessToken(), updatePasswordVO);
    }
}
