
package com.tmerclub.cloud.auth.feign;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.tmerclub.cloud.api.auth.feign.AuthSocialFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthSocialVO;
import com.tmerclub.cloud.api.payment.dto.BindPayAcctDTO;
import com.tmerclub.cloud.api.payment.feign.AllinpayFeignClient;
import com.tmerclub.cloud.auth.config.WxConfig;
import com.tmerclub.cloud.auth.mapper.AuthSocialMapper;
import com.tmerclub.cloud.auth.model.AuthSocial;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tmerclub
 * @date 2020/9/22
 */
@DubboService
public class AuthSocialController implements AuthSocialFeignClient {

    @Autowired
    private AuthSocialMapper authSocialMapper;


    @Autowired
    private WxConfig wxConfig;

    @DubboReference
    private AllinpayFeignClient allinpayFeignClient;

    public static final Integer TYPE = 2;


    @Override
    public ServerResponseEntity<AuthSocialVO> getByTempUid(String tempUid) {
        AuthSocial authSocial = authSocialMapper.getByTempUid(tempUid);
        return ServerResponseEntity.success(BeanUtil.map(authSocial, AuthSocialVO.class));
    }

    @Override
    public ServerResponseEntity<String> getMobileByMaInfo(String sessionKey, String encryptedData, String ivStr) {
        WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxConfig.getWxMaService().getUserService().getPhoneNoInfo(
                sessionKey, encryptedData, ivStr);
        return ServerResponseEntity.success(wxMaPhoneNumberInfo.getPhoneNumber());
    }

    @Override
    public ServerResponseEntity<AuthSocialVO> getByUserIdAndSocialType(String bizUserId, Integer socialType) {
        AuthSocial authSocial = authSocialMapper.getByBizUserIdAndType(bizUserId, socialType);
        return ServerResponseEntity.success(BeanUtil.map(authSocial, AuthSocialVO.class));
    }

    @Override
    public ServerResponseEntity<String> getOpenIdByCode(String code, Integer type) {
        String openId = null;
        try {
            if (type == 1) {
                // 微信公众号获取openId
                WxOAuth2AccessToken wxAccessToken = wxConfig.getWxMpService().getOAuth2Service().getAccessToken(code);
                openId = wxAccessToken.getOpenId();
            } else if (type.equals(TYPE)) {
                // 微信小程序获取openId
                WxMaJscode2SessionResult session = wxConfig.getWxMaService().getUserService().getSessionInfo(code);
                openId = session.getOpenid();
            }
        } catch (WxErrorException e) {
            throw new LuckException("获取openId失败");
        }
        return ServerResponseEntity.success(openId);
    }

    @Override
    public ServerResponseEntity<Void> bindAllinpayPayAcct() {
        // 获取可以绑定的用户支付标识
        List<AuthSocialVO> authSocialVOList = authSocialMapper.listAvailableSocial();
        if (CollectionUtils.isEmpty(authSocialVOList)) {
            return ServerResponseEntity.success();
        }
        List<BindPayAcctDTO> bindPayAcctDTOList = new ArrayList<>(authSocialVOList.size());
        BindPayAcctDTO bindPayAcctDTO;
        for (AuthSocialVO authSocialVO : authSocialVOList) {
            bindPayAcctDTO = new BindPayAcctDTO();
            bindPayAcctDTO.setBizUserId(String.valueOf(authSocialVO.getUserId()));
            bindPayAcctDTO.setSocialType(authSocialVO.getSocialType());
            bindPayAcctDTO.setAcct(authSocialVO.getBizUserId());
            bindPayAcctDTOList.add(bindPayAcctDTO);
        }
        // 通联批量绑定用户支付标识
        allinpayFeignClient.batchBindPayAcct(bindPayAcctDTOList);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<AuthSocialVO> getByUidAndSocialType(Long uid, Integer socialType) {
        AuthSocial authSocial = authSocialMapper.getByUidAndType(uid, socialType);
        return ServerResponseEntity.success(BeanUtil.map(authSocial, AuthSocialVO.class));
    }

}
