UserServiceImpl.java 8.3 KB

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