UserServiceImpl.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.cyksj.service.user.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.cyksj.common.constant.Constant;
  8. import com.cyksj.common.exception.BusinessRuntimeException;
  9. import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
  10. import com.cyksj.common.util.Jsons;
  11. import com.cyksj.config.WeChatConfig;
  12. import com.cyksj.mapper.UserMapper;
  13. import com.cyksj.mapper.UserTransferLogMapper;
  14. import com.cyksj.mapper.WxSyncLogMapper;
  15. import com.cyksj.mapper.channel.ChannelPopularizeMapper;
  16. import com.cyksj.mapper.market.task.UserBindDetailMapper;
  17. import com.cyksj.model.dto.GzhOAuth2UserInfo;
  18. import com.cyksj.model.dto.UserWhoami;
  19. import com.cyksj.model.entity.*;
  20. import com.cyksj.redis.RedisService;
  21. import com.cyksj.service.distribute.DistributeService;
  22. import com.cyksj.service.distribute.equipment.EquipmentDistributeService;
  23. import com.cyksj.service.mange.coupon.CouponCommonService;
  24. import com.cyksj.service.sys.SysConfigService;
  25. import com.cyksj.service.user.UserService;
  26. import com.ejlchina.searcher.BeanSearcher;
  27. import lombok.RequiredArgsConstructor;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.util.Assert;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Optional;
  34. /**
  35. * @author chan
  36. * @date 2021/10/10 下午3:27
  37. */
  38. @Slf4j
  39. @Service
  40. @RequiredArgsConstructor
  41. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  42. private final WeChatConfig weChatConfig;
  43. private final UserTransferLogMapper userTransferLogMapper;
  44. private final WxSyncLogMapper wxSyncLogMapper;
  45. private final DistributeService distributeService;
  46. private final EquipmentDistributeService equipDistributeService;
  47. private final ChannelPopularizeMapper channelPopularizeMapper;
  48. private final CouponCommonService couponCommonService;
  49. private final BeanSearcher beanSearcher;
  50. private final RedisService redisService;
  51. private final SysConfigService sysConfigService;
  52. private final UserBindDetailMapper userBindDetailMapper;
  53. private static final GlobalThreadPoolTaskExecutor TASK_POOL = GlobalThreadPoolTaskExecutor.getInstance();
  54. @Override
  55. public User saveAuth(GzhOAuth2UserInfo info, Long sharedId, Integer dsType, Long pid) {
  56. final String unionid = info.getUnionid();
  57. if (StrUtil.isEmpty(unionid)) {
  58. throw BusinessRuntimeException.getInstance("授权操作异常,请刷新页面");
  59. }
  60. User user = getSyncUser(info.getOpenid(), info.getUnionid(), null);
  61. user = Optional.ofNullable(user).orElseGet(() -> new User()
  62. .setUnionid(unionid));
  63. user.setNickname(info.getNickname())
  64. .setCountry(info.getCountry())
  65. .setProvince(info.getProvince())
  66. .setCity(info.getCity());
  67. if (user.getSex() == null) {
  68. user.setSex(info.getSex());
  69. }
  70. if (StrUtil.isEmpty(user.getHeadimgurl())) {
  71. user.setHeadimgurl(info.getHeadimgurl());
  72. }
  73. user.setUnionid(unionid);
  74. user.setOpenId(info.getOpenid());
  75. Boolean isNewUser = false;
  76. if (user.getId() == null) {
  77. isNewUser = true;
  78. user.setPopularizeId(pid);
  79. if (!redisService.setNx(RedisService.key.REPEAT_USER_KEY.getNameFormat(user.getUnionid()), user.getUnionid(), RedisService.key.REPEAT_USER_KEY.getTimeout())) {
  80. return this.getOne(Wrappers.lambdaQuery(User.class)
  81. .eq(User::getUnionid, unionid).last("limit 1"));
  82. }
  83. this.save(user);
  84. } else {
  85. this.updateById(user);
  86. }
  87. if (sharedId != null && isNewUser) {
  88. if (dsType == null || dsType == 1) {
  89. distributeService.saveDistributionInvitation(sharedId, user.getId());
  90. }
  91. }
  92. Long userId = user.getId();
  93. if (isNewUser && pid != null) {
  94. TASK_POOL.execute(() -> {
  95. sendFixedCustomerCoupon(pid, userId, unionid);
  96. });
  97. }
  98. //新旧用户都可实物分销
  99. if (sharedId != null && dsType != null && dsType == 2) {
  100. equipDistributeService.saveDistributionInvitation(sharedId, isNewUser, user.getId());
  101. }
  102. return user;
  103. }
  104. @Override
  105. public UserWhoami whoami(long userId) {
  106. User user = this.getById(userId);
  107. Assert.notNull(user,"业务处理失败,请联系客服");
  108. UserWhoami userWhoami = new UserWhoami();
  109. BeanUtil.copyProperties(user, userWhoami);
  110. return userWhoami;
  111. }
  112. @Override
  113. public User getSyncUser(String openId, String unionId, WxSyncLog wxSyncLog) {
  114. User user;
  115. //迁移公众号之后同步授权后信息
  116. //是否是迁移用户
  117. if (wxSyncLog != null) {
  118. user = this.getOne(Wrappers.lambdaQuery(User.class)
  119. .ne(User::getUnionid, unionId)
  120. .eq(User::getOpenId, wxSyncLog.getOriOpenid()).last("limit 1"));
  121. if (user != null) {
  122. user.setOpenId(wxSyncLog.getNewOpenid());
  123. } else {
  124. // 查询用户是否存在
  125. user = Optional.ofNullable(
  126. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  127. .orderByAsc(User::getId)
  128. .last("limit 1")))
  129. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  130. .orderByAsc(User::getId)
  131. .last("limit 1")));
  132. }
  133. } else {
  134. // 查询用户是否存在
  135. user = Optional.ofNullable(
  136. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  137. .orderByAsc(User::getId)
  138. .last("limit 1")))
  139. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  140. .orderByAsc(User::getId)
  141. .last("limit 1")));
  142. }
  143. return user;
  144. }
  145. @Override
  146. public void sendFixedCustomerCoupon(Long popularizeId, Long userId, String unionId) {
  147. //若是百度渠道 添加固定客服 注册后直接发放优惠券
  148. ChannelPopularize channelPopularize = channelPopularizeMapper.selectById(popularizeId);
  149. if (channelPopularize != null && channelPopularize.getChannelId() == 34) {
  150. // SysConfig corpConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  151. // .eq(SysConfig::getSysKey, Constant.CORP_FIXED_CUSTOMER)
  152. // .last("limit 1"));
  153. // if (corpConfig != null) {
  154. // Optional.ofNullable(corpConfig.getSysValue()).ifPresent(value -> {
  155. // try {
  156. // if (StrUtil.isNotBlank(value)) {
  157. // List<String> followIds = Jsons.parseList(value, String.class);
  158. // //添加固定客服发放优惠券
  159. // CorpUserFollowView corpUserFollowView = beanSearcher.searchFirst(CorpUserFollowView.class, MapUtils.builder()
  160. // .field(CorpUserFollowView::getUnionId, unionId)
  161. // .field(CorpUserFollowView::getFollowId, followIds).op(Operator.InList).build());
  162. // if (corpUserFollowView != null) {
  163. // Constant.FIXED_CUSTOMER_COUPON_IDS.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, 0l, CouponUser.Channel.wxCorp));
  164. // }
  165. // }
  166. // } catch (Exception e) {
  167. // }
  168. // });
  169. //
  170. // }
  171. SysConfig config = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  172. .eq(SysConfig::getSysKey, Constant.BAIDU_POPULARIZE_COUPON_IDS).last("limit 1"));
  173. if (config != null) {
  174. String sysValue = config.getSysValue();
  175. if (StrUtil.isNotBlank(sysValue)) {
  176. try {
  177. List<Long> couponIds = Jsons.parseList(sysValue, Long.class);
  178. couponIds.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, popularizeId, CouponUser.Channel.manual));
  179. } catch (Exception e) {
  180. log.error("百度渠道优惠券解析错误");
  181. }
  182. }
  183. }
  184. }
  185. }
  186. @Override
  187. public List<Long> getRelationUserIdList(Long userId, User selectUser) {
  188. List<Long> userIds = new ArrayList<>();
  189. userIds.add(userId);
  190. UserBindDetail userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  191. .eq(UserBindDetail::getUserId, userId).last("limit 1"));
  192. if (userBindDetail == null) {
  193. User user = Optional.ofNullable(selectUser).orElse(this.getById(userId));
  194. if (StrUtil.isEmpty(user.getOpenId()) && StrUtil.isNotEmpty(user.getLoginPhone())) {
  195. userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  196. .eq(UserBindDetail::getPhone, user.getLoginPhone()).last("limit 1"));
  197. if (userBindDetail != null) {
  198. userIds.add(userBindDetail.getUserId());
  199. }
  200. }
  201. } else {
  202. if (StrUtil.isNotBlank(userBindDetail.getPhone())) {
  203. User otherUser = this.getOne(Wrappers.lambdaQuery(User.class)
  204. .eq(User::getLoginPhone, userBindDetail.getPhone()).last("limit 1"));
  205. if (otherUser != null) {
  206. userIds.add(otherUser.getId());
  207. }
  208. }
  209. }
  210. return userIds;
  211. }
  212. }