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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.login.oauth2.client.remote.miniapp.IMiniappUserRemoteService;
import ink.wgink.login.oauth2.client.service.miniapp.IMiniappUserService;
import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
import ink.wgink.pojo.dtos.miniapp.MiniappUserDTO;
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.Arrays;
import java.util.List;

/**
 * @ClassName: IMiniappUserServiceImpl
 * @Description: 小程序用户
 * @Author: wanggeng
 * @Date: 2022/6/22 14:59
 * @Version: 1.0
 */
@Service
public class IMiniappUserServiceImpl extends DefaultBaseService implements IMiniappUserService {

    @Autowired
    private IMiniappUserRemoteService miniappUserRemoteService;
    @Autowired
    private ApiPathProperties apiPathProperties;


    @Override
    public void delete(String userId) {
        if (StringUtils.isBlank(userId)) {
            return;
        }
        delete(Arrays.asList(userId));
    }

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

    @Override
    public MiniappUserDTO getByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        List<MiniappUserDTO> miniappUserDTOS = listByUserIds(Arrays.asList(userId));
        if (miniappUserDTOS.isEmpty()) {
            return null;
        }
        return miniappUserDTOS.get(0);
    }

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

    @Override
    public MiniappUserDTO getByUserCode(String userCode) {
        if (StringUtils.isBlank(userCode)) {
            return null;
        }
        List<MiniappUserDTO> miniappUserDTOS = listByUserCodes(Arrays.asList(userCode));
        if (miniappUserDTOS.isEmpty()) {
            return null;
        }
        return miniappUserDTOS.get(0);
    }

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