UserServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.common.util.StringUtil;
  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.dao.DuplicateKeyException;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.util.Assert;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import java.util.Optional;
  33. /**
  34. * @author chan
  35. * @date 2021/10/10 下午3:27
  36. */
  37. @Slf4j
  38. @Service
  39. @RequiredArgsConstructor
  40. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  41. private final DistributeService distributeService;
  42. private final EquipmentDistributeService equipDistributeService;
  43. private final ChannelPopularizeMapper channelPopularizeMapper;
  44. private final CouponCommonService couponCommonService;
  45. private final RedisService redisService;
  46. private final SysConfigService sysConfigService;
  47. private final UserBindDetailMapper userBindDetailMapper;
  48. private final UserDistributePopularizeMapper distributePopularizeMapper;
  49. private final UserMapper userMapper;
  50. private static final GlobalThreadPoolTaskExecutor TASK_POOL = GlobalThreadPoolTaskExecutor.getInstance();
  51. @Override
  52. public User saveAuth(GzhOAuth2UserInfo info, Long sharedId, Integer dsType, Long pid, User.RegisterEnv registerEnv, String dsCode) {
  53. final String unionid = info.getUnionid();
  54. if (StrUtil.isEmpty(unionid)) {
  55. throw BusinessRuntimeException.getInstance("授权操作异常,请刷新页面");
  56. }
  57. User user = getSyncUser(info.getOpenid(), info.getUnionid(), null);
  58. user = Optional.ofNullable(user).orElseGet(() -> new User()
  59. .setUnionid(unionid));
  60. user.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. if (StrUtil.isEmpty(user.getNickname())) {
  70. user.setNickname(info.getNickname());
  71. }
  72. user.setUnionid(unionid);
  73. user.setOpenId(info.getOpenid());
  74. Boolean isNewUser = false;
  75. if (user.getId() == null) {
  76. isNewUser = true;
  77. user.setPopularizeId(pid);
  78. user.setRegisterEnv(registerEnv.name());
  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. if (StrUtil.isEmpty(user.getRegisterEnv())) {
  86. user.setRegisterEnv(registerEnv.name());
  87. }
  88. this.updateById(user);
  89. }
  90. Long dsPopularizeId = null;
  91. if (StrUtil.isNotBlank(dsCode)) {
  92. dsCode = dsCode.trim();
  93. User sharedUser = getOne(Wrappers.lambdaQuery(User.class)
  94. .eq(User::getShowId, dsCode).last("limit 1"));
  95. if (sharedUser != null) {
  96. sharedId = sharedUser.getId();
  97. } else {
  98. UserDistributePopularize distributePopularize = distributePopularizeMapper.selectOne(Wrappers.lambdaQuery(UserDistributePopularize.class)
  99. .eq(UserDistributePopularize::getCode, dsCode)
  100. .select(UserDistributePopularize::getUserId, UserDistributePopularize::getId).last("limit 1"));
  101. if (distributePopularize != null) {
  102. log.info("渠道sharedId:{}设置固定推广链接用户分销", distributePopularize.getUserId());
  103. sharedId = distributePopularize.getUserId();
  104. dsPopularizeId = distributePopularize.getId();
  105. }
  106. }
  107. }
  108. if (sharedId != null && isNewUser) {
  109. if (dsType == null || dsType == 1) {
  110. distributeService.saveDistributionInvitation(sharedId, user.getId(), dsPopularizeId);
  111. }
  112. }
  113. Long userId = user.getId();
  114. if (isNewUser && pid != null) {
  115. TASK_POOL.execute(() -> {
  116. sendFixedCustomerCoupon(pid, userId, unionid);
  117. });
  118. }
  119. //新旧用户都可实物分销
  120. if (sharedId != null && dsType != null && dsType == 2) {
  121. equipDistributeService.saveDistributionInvitation(sharedId, isNewUser, user.getId());
  122. }
  123. return user;
  124. }
  125. @Override
  126. public UserWhoami whoami(long userId) {
  127. User user = this.getById(userId);
  128. Assert.notNull(user, "业务处理失败,请联系客服");
  129. UserWhoami userWhoami = new UserWhoami();
  130. if (StrUtil.isEmpty(user.getShowId())) {
  131. user.setShowId(getUserShowId(userId));
  132. }
  133. BeanUtil.copyProperties(user, userWhoami);
  134. return userWhoami;
  135. }
  136. @Override
  137. public User getSyncUser(String openId, String unionId, WxSyncLog wxSyncLog) {
  138. User user;
  139. //迁移公众号之后同步授权后信息
  140. //是否是迁移用户
  141. if (wxSyncLog != null) {
  142. user = this.getOne(Wrappers.lambdaQuery(User.class)
  143. .ne(User::getUnionid, unionId)
  144. .eq(User::getOpenId, wxSyncLog.getOriOpenid()).last("limit 1"));
  145. if (user != null) {
  146. user.setOpenId(wxSyncLog.getNewOpenid());
  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. } else {
  158. // 查询用户是否存在
  159. user = Optional.ofNullable(
  160. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  161. .orderByAsc(User::getId)
  162. .last("limit 1")))
  163. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  164. .orderByAsc(User::getId)
  165. .last("limit 1")));
  166. }
  167. return user;
  168. }
  169. @Override
  170. public void sendFixedCustomerCoupon(Long popularizeId, Long userId, String unionId) {
  171. //若是百度渠道 添加固定客服 注册后直接发放优惠券
  172. ChannelPopularize channelPopularize = channelPopularizeMapper.selectById(popularizeId);
  173. if (channelPopularize != null && channelPopularize.getChannelId() == 34) {
  174. // SysConfig corpConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  175. // .eq(SysConfig::getSysKey, Constant.CORP_FIXED_CUSTOMER)
  176. // .last("limit 1"));
  177. // if (corpConfig != null) {
  178. // Optional.ofNullable(corpConfig.getSysValue()).ifPresent(value -> {
  179. // try {
  180. // if (StrUtil.isNotBlank(value)) {
  181. // List<String> followIds = Jsons.parseList(value, String.class);
  182. // //添加固定客服发放优惠券
  183. // CorpUserFollowView corpUserFollowView = beanSearcher.searchFirst(CorpUserFollowView.class, MapUtils.builder()
  184. // .field(CorpUserFollowView::getUnionId, unionId)
  185. // .field(CorpUserFollowView::getFollowId, followIds).op(Operator.InList).build());
  186. // if (corpUserFollowView != null) {
  187. // Constant.FIXED_CUSTOMER_COUPON_IDS.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, 0l, CouponUser.Channel.wxCorp));
  188. // }
  189. // }
  190. // } catch (Exception e) {
  191. // }
  192. // });
  193. //
  194. // }
  195. SysConfig config = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  196. .eq(SysConfig::getSysKey, Constant.BAIDU_POPULARIZE_COUPON_IDS).last("limit 1"));
  197. if (config != null) {
  198. String sysValue = config.getSysValue();
  199. if (StrUtil.isNotBlank(sysValue)) {
  200. try {
  201. List<Long> couponIds = Jsons.parseList(sysValue, Long.class);
  202. couponIds.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, popularizeId, CouponUser.Channel.manual));
  203. } catch (Exception e) {
  204. log.error("百度渠道优惠券解析错误");
  205. }
  206. }
  207. }
  208. }
  209. }
  210. @Override
  211. public List<Long> getRelationUserIdList(Long userId, User selectUser) {
  212. List<Long> userIds = new ArrayList<>();
  213. userIds.add(userId);
  214. UserBindDetail userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  215. .eq(UserBindDetail::getUserId, userId).last("limit 1"));
  216. if (userBindDetail == null) {
  217. User user = Optional.ofNullable(selectUser).orElse(this.getById(userId));
  218. if (StrUtil.isEmpty(user.getOpenId()) && StrUtil.isNotEmpty(user.getLoginPhone())) {
  219. userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  220. .eq(UserBindDetail::getPhone, user.getLoginPhone()).last("limit 1"));
  221. if (userBindDetail != null) {
  222. userIds.add(userBindDetail.getUserId());
  223. }
  224. }
  225. } else {
  226. if (StrUtil.isNotBlank(userBindDetail.getPhone())) {
  227. User otherUser = this.getOne(Wrappers.lambdaQuery(User.class)
  228. .eq(User::getLoginPhone, userBindDetail.getPhone()).last("limit 1"));
  229. if (otherUser != null) {
  230. userIds.add(otherUser.getId());
  231. }
  232. }
  233. }
  234. return userIds;
  235. }
  236. @Override
  237. public List<Long> getUserIdsByNickname(String nickname) {
  238. return userMapper.selectListByNickname(nickname);
  239. }
  240. @Override
  241. public String getUserShowId(Long userId) {
  242. Integer update = 0;
  243. String showId = null;
  244. while (update != 1) {
  245. showId = StringUtil.randomString(5);
  246. try {
  247. update = userMapper.update(null, Wrappers.lambdaUpdate(User.class)
  248. .set(User::getShowId, showId)
  249. .eq(User::getId, userId));
  250. } catch (DuplicateKeyException e) {
  251. update = 0;
  252. }
  253. }
  254. return showId;
  255. }
  256. @Override
  257. public Long getUserIdByShowId(String showId) {
  258. return userMapper.getUserIdByShowId(showId);
  259. }
  260. }