MemberRpcService.java 6.32 KB
package cn.fw.shirasawa.rpc.member;

import cn.fw.data.base.domain.common.Message;
import cn.fw.member.sdk.api.FunctionApi;
import cn.fw.member.sdk.api.MemberApi;
import cn.fw.member.sdk.vo.BatchUserParam;
import cn.fw.member.sdk.vo.MobileLocation;
import cn.fw.member.sdk.vo.UserBaseInfoVO;
import cn.fw.shirasawa.common.utils.MobileUtilKt;
import cn.fw.shirasawa.rpc.AbsBaseRpcService;
import cn.fw.shirasawa.rpc.member.dto.MemberUserDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static org.apache.commons.lang3.Validate.isTrue;

/**
 * 会员服务
 *
 * @author kurisu
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MemberRpcService extends AbsBaseRpcService {
    /**
     * 会员服务
     */
    private final MemberApi memberApi;
    private final FunctionApi functionApi;

    @Value("${spring.cache.custom.global-prefix}:rpc:member")
    private String keyPrefix;

    /**
     * 根据会员ID获取会员信息
     *
     * @param userId 会员ID
     * @return 会员信息
     */
    public MemberUserDTO user(final Long userId) {
        if (userId == null || userId <= 0) {
            return null;
        }
        String key = String.format("%s:member-info:%s", getKeyPrefix(), userId);
        MemberUserDTO memberUserDTO = getFromCache(key, MemberUserDTO.class);
        if (Objects.nonNull(memberUserDTO)) {
            return memberUserDTO;
        }
        try {
            final Message<UserBaseInfoVO> msg = memberApi.queryUserByuserId(userId);
            if (!msg.isSuccess()) {
                log.warn("调用Member[根据会员ID[{}]获取会员信息]系统失败, 原因:{}", userId, msg.getResult());
                return null;
            }
            final UserBaseInfoVO user = msg.getData();
            if (user != null) {
                final MemberUserDTO userDTO = new MemberUserDTO();
                BeanUtils.copyProperties(user, userDTO);
                setToCache(key, JSONObject.toJSONString(userDTO), 60 * 3);
                return userDTO;
            }
        } catch (Exception e) {
            log.error("调用Member[根据会员ID[{}]获取会员信息]系统失败", userId, e);
        }
        return null;
    }

    /**
     * 根据会员ID集合获取会员列表
     *
     * @param userIds 会员ID集合
     * @return 会员列表
     */
    public List<MemberUserDTO> users(final List<Long> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return Collections.emptyList();
        }
        try {
            final BatchUserParam param = new BatchUserParam();
            param.setUserIdList(userIds);
            final Message<List<UserBaseInfoVO>> msg = memberApi.batchUserByUserId(param);
            if (!msg.isSuccess()) {
                log.warn("调用Member[根据会员ID集合[{}]获取会员列表]系统失败, 原因:{}", userIds, msg.getResult());
                return Collections.emptyList();
            }
            final List<UserBaseInfoVO> users = msg.getData();
            if (!CollectionUtils.isEmpty(users)) {
                final List<MemberUserDTO> members = new ArrayList<>();
                users.forEach(item -> {
                    final MemberUserDTO userDTO = new MemberUserDTO();
                    BeanUtils.copyProperties(item, userDTO);
                    members.add(userDTO);
                });
                return members;
            }
        } catch (Exception e) {
            log.error("调用Member[根据会员ID集合[{}]获取会员列表]系统失败", userIds, e);
        }
        return Collections.emptyList();
    }

    /**
     * 根据手机号查询会员信息
     *
     * @param mobile 手机号
     * @return 会员信息
     */
    public MemberUserDTO queryByMobile(final String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return null;
        }
        String key = String.format("%s:info:%s", getKeyPrefix(), mobile);
        MemberUserDTO memberUserDTO = getFromCache(key, MemberUserDTO.class);
        if (Objects.nonNull(memberUserDTO)) {
            return memberUserDTO;
        }
        try {
            final Message<UserBaseInfoVO> msg = memberApi.queryUserByMobile(mobile);
            isTrue(msg.isSuccess(), String.format("调用Member系统失败: [根据会员手机号[%s]获取会员信息]", mobile));
            final UserBaseInfoVO user = msg.getData();
            if (Objects.isNull(user)) {
                return null;
            }
            final MemberUserDTO userDTO = new MemberUserDTO();
            BeanUtils.copyProperties(user, userDTO);
            setToCache(key, JSONObject.toJSONString(userDTO), 60 * 3);
            return userDTO;
        } catch (Exception e) {
            log.error("调用Member系统根据会员手机号[{}]获取会员信息失败,原因:{}", mobile, e);
        }
        return null;
    }

    /**
     * 查询手机号归属地
     *
     * @param mobile
     * @return
     */
    @Cacheable(cacheNames = "mobile:attribution", key = "#mobile", unless = "#result.isEmpty()")
    public String attribution(final String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return "";
        }
        try {
            String attribution = MobileUtilKt.attribution(mobile);
            if (StringUtils.isNotBlank(attribution)) {
                return attribution;
            }
            Message<MobileLocation> msg = functionApi.queryMobileLocation(mobile);
            if (!msg.isSuccess()) {
                return "";
            }
            MobileLocation data = msg.getData();
            if (data != null) {
                String province = data.getProvince();
                String city = data.getCity();
                return province.concat(" ").concat(city);
            }
        } catch (Exception ignored) {
        }
        return "";
    }

    @Override
    public String getKeyPrefix() {
        return this.keyPrefix;
    }
}