UserServiceImpl.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.exception.BusinessRuntimeException;
  8. import com.cyksj.config.WeChatConfig;
  9. import com.cyksj.mapper.UserMapper;
  10. import com.cyksj.mapper.UserTransferLogMapper;
  11. import com.cyksj.mapper.WxSyncLogMapper;
  12. import com.cyksj.model.dto.GzhOAuth2UserInfo;
  13. import com.cyksj.model.dto.UserWhoami;
  14. import com.cyksj.model.entity.User;
  15. import com.cyksj.model.entity.UserTransferLog;
  16. import com.cyksj.model.entity.WxSyncLog;
  17. import com.cyksj.service.distribute.DistributeService;
  18. import com.cyksj.service.distribute.equipment.EquipmentDistributeService;
  19. import com.cyksj.service.user.UserService;
  20. import lombok.RequiredArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.util.Assert;
  24. import java.util.Optional;
  25. /**
  26. * @author chan
  27. * @date 2021/10/10 下午3:27
  28. */
  29. @Slf4j
  30. @Service
  31. @RequiredArgsConstructor
  32. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  33. private final WeChatConfig weChatConfig;
  34. private final UserTransferLogMapper userTransferLogMapper;
  35. private final WxSyncLogMapper wxSyncLogMapper;
  36. private final DistributeService distributeService;
  37. private final EquipmentDistributeService equipDistributeService;
  38. @Override
  39. public User saveAuth(GzhOAuth2UserInfo info, Long sharedId, Integer dsType) {
  40. final String unionid = info.getUnionid();
  41. if (StrUtil.isEmpty(unionid)) {
  42. throw BusinessRuntimeException.getInstance("授权操作异常,请刷新页面");
  43. }
  44. WxSyncLog wxSyncLog = wxSyncLogMapper.selectOne(Wrappers.lambdaQuery(WxSyncLog.class)
  45. .eq(WxSyncLog::getToAppid, weChatConfig.getAppid())
  46. .eq(WxSyncLog::getNewOpenid, info.getOpenid()).last("limit 1"));
  47. User user = getSyncUser(info.getOpenid(), info.getUnionid(), wxSyncLog);
  48. user = Optional.ofNullable(user).orElseGet(() -> new User()
  49. .setUnionid(unionid));
  50. user.setNickname(info.getNickname())
  51. .setSex(info.getSex())
  52. .setCountry(info.getCountry())
  53. .setProvince(info.getProvince())
  54. .setCity(info.getCity())
  55. .setHeadimgurl(info.getHeadimgurl());
  56. if (!user.getUnionid().equals(unionid)) {
  57. UserTransferLog log = new UserTransferLog();
  58. if (wxSyncLog != null) {
  59. log.setType("公众号迁移");
  60. } else {
  61. log.setType("绑定开发者账号");
  62. }
  63. log.setOldAppId(weChatConfig.getOldAppid());
  64. log.setNewAppId(weChatConfig.getAppid());
  65. log.setUserId(user.getId());
  66. log.setOldUnionid(user.getUnionid());
  67. log.setNewUnionid(info.getUnionid());
  68. userTransferLogMapper.insert(log);
  69. }
  70. user.setUnionid(unionid);
  71. user.setOpenId(info.getOpenid());
  72. Boolean isNewUser = false;
  73. if (user.getId() == null) {
  74. isNewUser = true;
  75. }
  76. this.saveOrUpdate(user);
  77. if (sharedId != null && isNewUser) {
  78. if (dsType == null || dsType == 1) {
  79. distributeService.saveDistributionInvitation(sharedId, user.getId());
  80. }
  81. }
  82. //新旧用户都可实物分销
  83. if (sharedId != null && dsType != null && dsType == 2) {
  84. equipDistributeService.saveDistributionInvitation(sharedId, isNewUser, user.getId());
  85. }
  86. return user;
  87. }
  88. @Override
  89. public UserWhoami whoami(long userId) {
  90. User user = this.getById(userId);
  91. Assert.notNull(user,"业务处理失败,请联系客服");
  92. UserWhoami userWhoami = new UserWhoami();
  93. BeanUtil.copyProperties(user, userWhoami);
  94. return userWhoami;
  95. }
  96. @Override
  97. public User getSyncUser(String openId, String unionId, WxSyncLog wxSyncLog) {
  98. User user;
  99. //迁移公众号之后同步授权后信息
  100. //是否是迁移用户
  101. if (wxSyncLog != null) {
  102. user = this.getOne(Wrappers.lambdaQuery(User.class)
  103. .ne(User::getUnionid, unionId)
  104. .eq(User::getOpenId, wxSyncLog.getOriOpenid()).last("limit 1"));
  105. if (user != null) {
  106. user.setOpenId(wxSyncLog.getNewOpenid());
  107. } else {
  108. // 查询用户是否存在
  109. user = Optional.ofNullable(
  110. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  111. .orderByAsc(User::getId)
  112. .last("limit 1")))
  113. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  114. .orderByAsc(User::getId)
  115. .last("limit 1")));
  116. }
  117. } else {
  118. // 查询用户是否存在
  119. user = Optional.ofNullable(
  120. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  121. .orderByAsc(User::getId)
  122. .last("limit 1")))
  123. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  124. .orderByAsc(User::getId)
  125. .last("limit 1")));
  126. }
  127. return user;
  128. }
  129. }