package cn.com.poc.user.wecom.service;

import cn.com.poc.user.wecom.api.WecomServiceApi;
import cn.com.poc.user.wecom.domain.WecomLoginInfoResponse;
import cn.com.poc.user.wecom.domain.WecomUserInfo3rd;
import cn.com.yict.framemax.core.i18n.I18nMessageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WecomUserService {

    private static final Logger logger = LoggerFactory.getLogger(WecomUserService.class);

    @Resource
    private WecomServiceApi wecomServiceApi;

    public WecomLoginInfoResponse getLoginInfo(String providerAccessToken, String authCode) {
        WecomLoginInfoResponse response = wecomServiceApi.getLoginInfo(providerAccessToken, authCode);
        if (response != null && response.isSuccess()) {
            return response;
        }
        logger.error("wecom login info error: {}", response == null ? "null response" : response.getErrmsg());
        throw new I18nMessageException("exception/system.error");
    }

    public WecomUserInfo3rd getUserInfo3rd(String suiteAccessToken, String code) {
        WecomUserInfo3rd response = wecomServiceApi.getUserInfo3rd(suiteAccessToken, code);
        if (response != null && response.isSuccess()) {
            return response;
        }
        logger.error("wecom user info error: {}", response == null ? "null response" : response.getErrmsg());
        throw new I18nMessageException("exception/system.error");
    }

    public WecomUserInfo3rd getUserDetail3rd(String suiteAccessToken, String userTicket) {
        WecomUserInfo3rd response = wecomServiceApi.getUserDetail3rd(suiteAccessToken, userTicket);
        if (response != null && response.isSuccess()) {
            return response;
        }
        logger.error("wecom user detail error: {}", response == null ? "null response" : response.getErrmsg());
        throw new I18nMessageException("exception/system.error");
    }
}
