package com.cm.common.dingding.service.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.dingding.config.properties.DingDingAppProperties;
import com.cm.common.dingding.manager.app.DingDingAppManager;
import com.cm.common.dingding.service.IDingDingAppUserService;
import com.cm.common.exception.SearchException;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserGetByMobileRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetUseridByUnionidRequest;
import com.dingtalk.api.response.OapiUserGetByMobileResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetUseridByUnionidResponse;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DingDingAppUserServiceImpl
 * @Description: 钉钉App用户业务
 * @Author: WangGeng
 * @Date: 2020/8/31 20:19
 * @Version: 1.0
 **/
@Service
public class DingDingAppUserServiceImpl extends AbstractService implements IDingDingAppUserService {

    @Autowired
    private DingDingAppProperties dingDingAppProperties;

    @Override
    public String getUserIdByUnionId(String unionId) throws SearchException {
        DingTalkClient dingTalkClient = new DefaultDingTalkClient(dingDingAppProperties.getUseridByUnionidUrl());
        OapiUserGetUseridByUnionidRequest oapiUserGetUseridByUnionidRequest = new OapiUserGetUseridByUnionidRequest();
        oapiUserGetUseridByUnionidRequest.setUnionid(unionId);
        oapiUserGetUseridByUnionidRequest.setHttpMethod("GET");
        try {
            OapiUserGetUseridByUnionidResponse oapiUserGetUseridByUnionidResponse = dingTalkClient.execute(oapiUserGetUseridByUnionidRequest, DingDingAppManager.getInstance().getAccessToken());
            if (oapiUserGetUseridByUnionidResponse.getErrcode() != 0) {
                throw new SearchException(oapiUserGetUseridByUnionidResponse.getErrmsg());
            }
            return oapiUserGetUseridByUnionidResponse.getUserid();
        } catch (ApiException e) {
            LOG.error(e.getMessage(), e);
            throw new SearchException("获取钉钉用户失败");
        }
    }

    @Override
    public OapiUserGetResponse getUserByUserId(String userId) throws SearchException {
        try {
            DingTalkClient dingTalkClient = new DefaultDingTalkClient(dingDingAppProperties.getUserinfoByIdUrl());
            OapiUserGetRequest oapiUserGetRequest = new OapiUserGetRequest();
            oapiUserGetRequest.setUserid(userId);
            oapiUserGetRequest.setHttpMethod("GET");
            OapiUserGetResponse oapiUserGetResponse = dingTalkClient.execute(oapiUserGetRequest, DingDingAppManager.getInstance().getAccessToken());
            if (oapiUserGetResponse.getErrcode() != 0) {
                throw new SearchException(oapiUserGetResponse.getErrmsg());
            }
            return oapiUserGetResponse;
        } catch (ApiException e) {
            LOG.error(e.getMessage(), e);
            throw new SearchException("获取钉钉用户详情失败");
        }
    }

    @Override
    public String getUserIdByPhone(String phone) {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
            OapiUserGetByMobileRequest oapiUserGetByMobileRequest = new OapiUserGetByMobileRequest();
            oapiUserGetByMobileRequest.setMobile(phone);
            OapiUserGetByMobileResponse oapiUserGetByMobileResponse = client.execute(oapiUserGetByMobileRequest, DingDingAppManager.getInstance().getAccessToken());
            if (oapiUserGetByMobileResponse.getErrcode() != 0) {
                throw new SearchException(oapiUserGetByMobileResponse.getErrmsg());
            }
            return oapiUserGetByMobileResponse.getUserid();
        } catch (ApiException e) {
            LOG.error(e.getMessage(), e);
            return null;
        }
    }

}
