UserServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package com.cyksj.service.user.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.RandomUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.cyksj.common.constant.Constant;
  9. import com.cyksj.common.exception.BusinessRuntimeException;
  10. import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
  11. import com.cyksj.common.util.Jsons;
  12. import com.cyksj.mapper.UserMapper;
  13. import com.cyksj.mapper.channel.ChannelPopularizeMapper;
  14. import com.cyksj.mapper.manage.cps.UserDistributePopularizeMapper;
  15. import com.cyksj.mapper.market.task.UserBindDetailMapper;
  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 lombok.RequiredArgsConstructor;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.util.Assert;
  29. import java.util.ArrayList;
  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 DistributeService distributeService;
  41. private final EquipmentDistributeService equipDistributeService;
  42. private final ChannelPopularizeMapper channelPopularizeMapper;
  43. private final CouponCommonService couponCommonService;
  44. private final RedisService redisService;
  45. private final SysConfigService sysConfigService;
  46. private final UserBindDetailMapper userBindDetailMapper;
  47. private final UserDistributePopularizeMapper distributePopularizeMapper;
  48. private final UserMapper userMapper;
  49. private static final GlobalThreadPoolTaskExecutor TASK_POOL = GlobalThreadPoolTaskExecutor.getInstance();
  50. @Override
  51. public User saveAuth(GzhOAuth2UserInfo info, Long sharedId, Integer dsType, Long pid, User.RegisterEnv registerEnv, String dsCode) {
  52. final String unionid = info.getUnionid();
  53. if (StrUtil.isEmpty(unionid)) {
  54. throw BusinessRuntimeException.getInstance("授权操作异常,请刷新页面");
  55. }
  56. User user = getSyncUser(info.getOpenid(), info.getUnionid(), null);
  57. user = Optional.ofNullable(user).orElseGet(() -> new User()
  58. .setUnionid(unionid));
  59. user.setNickname(info.getNickname())
  60. .setCountry(info.getCountry())
  61. .setProvince(info.getProvince())
  62. .setCity(info.getCity());
  63. if (user.getSex() == null) {
  64. user.setSex(info.getSex());
  65. }
  66. if (StrUtil.isEmpty(user.getHeadimgurl())) {
  67. user.setHeadimgurl(info.getHeadimgurl());
  68. }
  69. user.setUnionid(unionid);
  70. user.setOpenId(info.getOpenid());
  71. Boolean isNewUser = false;
  72. if (user.getId() == null) {
  73. isNewUser = true;
  74. user.setPopularizeId(pid);
  75. user.setRegisterEnv(registerEnv.name());
  76. if (!redisService.setNx(RedisService.key.REPEAT_USER_KEY.getNameFormat(user.getUnionid()), user.getUnionid(), RedisService.key.REPEAT_USER_KEY.getTimeout())) {
  77. return this.getOne(Wrappers.lambdaQuery(User.class)
  78. .eq(User::getUnionid, unionid).last("limit 1"));
  79. }
  80. this.save(user);
  81. } else {
  82. if (StrUtil.isEmpty(user.getRegisterEnv())) {
  83. user.setRegisterEnv(registerEnv.name());
  84. }
  85. this.updateById(user);
  86. }
  87. Long dsPopularizeId = null;
  88. if (StrUtil.isNotBlank(dsCode)) {
  89. UserDistributePopularize distributePopularize = distributePopularizeMapper.selectOne(Wrappers.lambdaQuery(UserDistributePopularize.class)
  90. .eq(UserDistributePopularize::getCode, dsCode)
  91. .select(UserDistributePopularize::getUserId, UserDistributePopularize::getId).last("limit 1"));
  92. if (distributePopularize != null) {
  93. log.info("渠道sharedId:{}设置固定推广链接用户分销", distributePopularize.getUserId());
  94. sharedId = distributePopularize.getUserId();
  95. dsPopularizeId = distributePopularize.getId();
  96. }
  97. }
  98. if (sharedId != null && isNewUser) {
  99. if (dsType == null || dsType == 1) {
  100. distributeService.saveDistributionInvitation(sharedId, user.getId(), dsPopularizeId);
  101. }
  102. }
  103. Long userId = user.getId();
  104. if (isNewUser && pid != null) {
  105. TASK_POOL.execute(() -> {
  106. sendFixedCustomerCoupon(pid, userId, unionid);
  107. });
  108. }
  109. //新旧用户都可实物分销
  110. if (sharedId != null && dsType != null && dsType == 2) {
  111. equipDistributeService.saveDistributionInvitation(sharedId, isNewUser, user.getId());
  112. }
  113. return user;
  114. }
  115. @Override
  116. public UserWhoami whoami(long userId) {
  117. User user = this.getById(userId);
  118. Assert.notNull(user, "业务处理失败,请联系客服");
  119. UserWhoami userWhoami = new UserWhoami();
  120. if (StrUtil.isEmpty(user.getShowId())) {
  121. user.setShowId(getUserShowId(userId));
  122. }
  123. BeanUtil.copyProperties(user, userWhoami);
  124. return userWhoami;
  125. }
  126. @Override
  127. public User getSyncUser(String openId, String unionId, WxSyncLog wxSyncLog) {
  128. User user;
  129. //迁移公众号之后同步授权后信息
  130. //是否是迁移用户
  131. if (wxSyncLog != null) {
  132. user = this.getOne(Wrappers.lambdaQuery(User.class)
  133. .ne(User::getUnionid, unionId)
  134. .eq(User::getOpenId, wxSyncLog.getOriOpenid()).last("limit 1"));
  135. if (user != null) {
  136. user.setOpenId(wxSyncLog.getNewOpenid());
  137. } else {
  138. // 查询用户是否存在
  139. user = Optional.ofNullable(
  140. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  141. .orderByAsc(User::getId)
  142. .last("limit 1")))
  143. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  144. .orderByAsc(User::getId)
  145. .last("limit 1")));
  146. }
  147. } else {
  148. // 查询用户是否存在
  149. user = Optional.ofNullable(
  150. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  151. .orderByAsc(User::getId)
  152. .last("limit 1")))
  153. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  154. .orderByAsc(User::getId)
  155. .last("limit 1")));
  156. }
  157. return user;
  158. }
  159. @Override
  160. public void sendFixedCustomerCoupon(Long popularizeId, Long userId, String unionId) {
  161. //若是百度渠道 添加固定客服 注册后直接发放优惠券
  162. ChannelPopularize channelPopularize = channelPopularizeMapper.selectById(popularizeId);
  163. if (channelPopularize != null && channelPopularize.getChannelId() == 34) {
  164. // SysConfig corpConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  165. // .eq(SysConfig::getSysKey, Constant.CORP_FIXED_CUSTOMER)
  166. // .last("limit 1"));
  167. // if (corpConfig != null) {
  168. // Optional.ofNullable(corpConfig.getSysValue()).ifPresent(value -> {
  169. // try {
  170. // if (StrUtil.isNotBlank(value)) {
  171. // List<String> followIds = Jsons.parseList(value, String.class);
  172. // //添加固定客服发放优惠券
  173. // CorpUserFollowView corpUserFollowView = beanSearcher.searchFirst(CorpUserFollowView.class, MapUtils.builder()
  174. // .field(CorpUserFollowView::getUnionId, unionId)
  175. // .field(CorpUserFollowView::getFollowId, followIds).op(Operator.InList).build());
  176. // if (corpUserFollowView != null) {
  177. // Constant.FIXED_CUSTOMER_COUPON_IDS.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, 0l, CouponUser.Channel.wxCorp));
  178. // }
  179. // }
  180. // } catch (Exception e) {
  181. // }
  182. // });
  183. //
  184. // }
  185. SysConfig config = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  186. .eq(SysConfig::getSysKey, Constant.BAIDU_POPULARIZE_COUPON_IDS).last("limit 1"));
  187. if (config != null) {
  188. String sysValue = config.getSysValue();
  189. if (StrUtil.isNotBlank(sysValue)) {
  190. try {
  191. List<Long> couponIds = Jsons.parseList(sysValue, Long.class);
  192. couponIds.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, popularizeId, CouponUser.Channel.manual));
  193. } catch (Exception e) {
  194. log.error("百度渠道优惠券解析错误");
  195. }
  196. }
  197. }
  198. }
  199. }
  200. @Override
  201. public List<Long> getRelationUserIdList(Long userId, User selectUser) {
  202. List<Long> userIds = new ArrayList<>();
  203. userIds.add(userId);
  204. UserBindDetail userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  205. .eq(UserBindDetail::getUserId, userId).last("limit 1"));
  206. if (userBindDetail == null) {
  207. User user = Optional.ofNullable(selectUser).orElse(this.getById(userId));
  208. if (StrUtil.isEmpty(user.getOpenId()) && StrUtil.isNotEmpty(user.getLoginPhone())) {
  209. userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  210. .eq(UserBindDetail::getPhone, user.getLoginPhone()).last("limit 1"));
  211. if (userBindDetail != null) {
  212. userIds.add(userBindDetail.getUserId());
  213. }
  214. }
  215. } else {
  216. if (StrUtil.isNotBlank(userBindDetail.getPhone())) {
  217. User otherUser = this.getOne(Wrappers.lambdaQuery(User.class)
  218. .eq(User::getLoginPhone, userBindDetail.getPhone()).last("limit 1"));
  219. if (otherUser != null) {
  220. userIds.add(otherUser.getId());
  221. }
  222. }
  223. }
  224. return userIds;
  225. }
  226. @Override
  227. public List<Long> getUserIdsByNickname(String nickname) {
  228. return userMapper.selectListByNickname(nickname);
  229. }
  230. @Override
  231. public String getUserShowId(Long userId) {
  232. Integer update = 0;
  233. String showId = null;
  234. while (update != 1) {
  235. showId = RandomUtil.randomString(6);
  236. update = userMapper.update(null, Wrappers.lambdaUpdate(User.class)
  237. .set(User::getShowId, showId)
  238. .eq(User::getId, userId));
  239. }
  240. return showId;
  241. }
  242. @Override
  243. public Long getUserIdByShowId(String showId) {
  244. return userMapper.getUserIdByShowId(showId);
  245. }
  246. }