Blame view

fw-valhalla-rpc/src/main/java/cn/fw/valhalla/rpc/member/MemberRpcService.java 7.24 KB
3f17473a   张志伟   init
1
2
  package cn.fw.valhalla.rpc.member;
  
e237bcd9   张志伟   电话号码归属地查询
3
  import cn.fw.common.util.MobileUtil;
3f17473a   张志伟   init
4
  import cn.fw.data.base.domain.common.Message;
e237bcd9   张志伟   电话号码归属地查询
5
  import cn.fw.member.sdk.api.FunctionApi;
3f17473a   张志伟   init
6
  import cn.fw.member.sdk.api.MemberApi;
3f17473a   张志伟   init
7
  import cn.fw.member.sdk.vo.BatchUserParam;
e237bcd9   张志伟   电话号码归属地查询
8
  import cn.fw.member.sdk.vo.MobileLocation;
3f17473a   张志伟   init
9
10
  import cn.fw.member.sdk.vo.UserBaseInfoVO;
  import cn.fw.member.sdk.vo.UserRegistryVO;
5b538595   张志伟   feature(*): 新增查询线...
11
  import cn.fw.valhalla.common.utils.ThreadPoolUtil;
3f17473a   张志伟   init
12
  import cn.fw.valhalla.rpc.member.dto.MemberUserDTO;
3f17473a   张志伟   init
13
14
15
16
  import lombok.extern.slf4j.Slf4j;
  import org.apache.commons.lang3.StringUtils;
  import org.springframework.beans.BeanUtils;
  import org.springframework.beans.factory.annotation.Autowired;
e237bcd9   张志伟   电话号码归属地查询
17
  import org.springframework.cache.annotation.Cacheable;
3f17473a   张志伟   init
18
19
20
21
22
23
24
  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;
5b538595   张志伟   feature(*): 新增查询线...
25
  import java.util.concurrent.CompletableFuture;
3f17473a   张志伟   init
26
  
3f17473a   张志伟   init
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  import static org.apache.commons.lang3.Validate.isTrue;
  import static org.apache.commons.lang3.Validate.notNull;
  
  /**
   * 会员服务
   *
   * @author kurisu
   */
  @Slf4j
  @Service
  public class MemberRpcService {
      /**
       * 会员服务
       */
      private final MemberApi memberApi;
e237bcd9   张志伟   电话号码归属地查询
42
      private final FunctionApi functionApi;
3f17473a   张志伟   init
43
44
45
46
47
  
      /**
       * 默认构造器
       */
      @Autowired
e237bcd9   张志伟   电话号码归属地查询
48
49
      public MemberRpcService(final MemberApi memberApi,
                              final FunctionApi functionApi) {
3f17473a   张志伟   init
50
          this.memberApi = memberApi;
e237bcd9   张志伟   电话号码归属地查询
51
          this.functionApi = functionApi;
3f17473a   张志伟   init
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
      }
  
      /**
       * 根据会员ID获取会员信息
       *
       * @param userId 会员ID
       * @return 会员信息
       */
      public MemberUserDTO user(final Long userId) {
          if (userId == null) {
              return null;
          }
          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);
                  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()) {
36f2c44d   张志伟   :art:
97
                  log.warn("调用Member[根据会员ID集合[{}]获取会员列表]系统失败, 原因:{}", userIds, msg.getResult());
3f17473a   张志伟   init
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
                  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;
          }
          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);
              return userDTO;
          } catch (Exception e) {
              log.error("调用Member系统根据会员手机号[{}]获取会员信息失败,原因:{}", mobile, e);
          }
          return null;
3f17473a   张志伟   init
140
141
142
      }
  
      /**
3f17473a   张志伟   init
143
144
145
146
147
148
149
150
151
152
153
       * 注册会员
       *
       * @param memberUser 注册信息
       * @return 会员
       */
      public MemberUserDTO register(final MemberUserDTO memberUser) {
          notNull(memberUser, "会员注册信息不能为空");
          UserRegistryVO vo = new UserRegistryVO();
          BeanUtils.copyProperties(memberUser, vo);
          vo.setNickName(StringUtils.isNotBlank(memberUser.getNickName()) ? memberUser.getNickName() : memberUser.getRealName());
          final Message<UserBaseInfoVO> msg = memberApi.register(vo);
e237bcd9   张志伟   电话号码归属地查询
154
          isTrue(msg.isSuccess(), String.format("调用Member[注册会员[%s]]系统失败", vo));
3f17473a   张志伟   init
155
          final UserBaseInfoVO user = msg.getData();
e237bcd9   张志伟   电话号码归属地查询
156
          notNull(user, String.format("调用Member[注册会员[%s]]系统失败,返回会员信息为空", vo));
3f17473a   张志伟   init
157
158
159
160
161
          final MemberUserDTO userDTO = new MemberUserDTO();
          BeanUtils.copyProperties(user, userDTO);
          return userDTO;
      }
  
e237bcd9   张志伟   电话号码归属地查询
162
163
164
165
166
167
      /**
       * 查询手机号归属地
       *
       * @param mobile
       * @return
       */
5b538595   张志伟   feature(*): 新增查询线...
168
      @Cacheable(cacheNames = "mobile:attribution", key = "#mobile", unless = "#result.isEmpty()")
e237bcd9   张志伟   电话号码归属地查询
169
170
171
172
173
      public String attribution(final String mobile) {
          if (StringUtils.isBlank(mobile)) {
              return "";
          }
          try {
5b538595   张志伟   feature(*): 新增查询线...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
              CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
                  MobileUtil.Result result = MobileUtil.attribution(mobile);
                  String att = "";
                  if (Objects.nonNull(result)) {
                      att = result.getProvince().concat(" ").concat(result.getCity());
                  }
                  return att;
              }, ThreadPoolUtil.getInstance().getExecutor());
  
              CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
                  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);
                  }
e237bcd9   张志伟   电话号码归属地查询
194
                  return "";
5b538595   张志伟   feature(*): 新增查询线...
195
196
197
198
              }, ThreadPoolUtil.getInstance().getExecutor());
              return future1.applyToEither(future2, String::trim).join();
          } catch (Exception e) {
              log.warn("查询手机号归属地失败:", e);
e237bcd9   张志伟   电话号码归属地查询
199
200
201
202
          }
          return "";
      }
  
3f17473a   张志伟   init
203
  }