package com.cyksj.service.user.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.cyksj.common.constant.Constant; import com.cyksj.common.exception.BusinessRuntimeException; import com.cyksj.common.task.GlobalThreadPoolTaskExecutor; import com.cyksj.common.util.Jsons; import com.cyksj.config.WeChatConfig; import com.cyksj.mapper.UserMapper; import com.cyksj.mapper.UserTransferLogMapper; import com.cyksj.mapper.WxSyncLogMapper; import com.cyksj.mapper.channel.ChannelPopularizeMapper; import com.cyksj.model.dto.GzhOAuth2UserInfo; import com.cyksj.model.dto.UserWhoami; import com.cyksj.model.entity.*; import com.cyksj.redis.RedisService; import com.cyksj.service.distribute.DistributeService; import com.cyksj.service.distribute.equipment.EquipmentDistributeService; import com.cyksj.service.mange.coupon.CouponCommonService; import com.cyksj.service.sys.SysConfigService; import com.cyksj.service.user.UserService; import com.ejlchina.searcher.BeanSearcher; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import java.util.List; import java.util.Optional; /** * @author chan * @date 2021/10/10 下午3:27 */ @Slf4j @Service @RequiredArgsConstructor public class UserServiceImpl extends ServiceImpl implements UserService { private final WeChatConfig weChatConfig; private final UserTransferLogMapper userTransferLogMapper; private final WxSyncLogMapper wxSyncLogMapper; private final DistributeService distributeService; private final EquipmentDistributeService equipDistributeService; private final ChannelPopularizeMapper channelPopularizeMapper; private final CouponCommonService couponCommonService; private final BeanSearcher beanSearcher; private final RedisService redisService; private final SysConfigService sysConfigService; private static final GlobalThreadPoolTaskExecutor TASK_POOL = GlobalThreadPoolTaskExecutor.getInstance(); @Override public User saveAuth(GzhOAuth2UserInfo info, Long sharedId, Integer dsType, Long pid) { final String unionid = info.getUnionid(); if (StrUtil.isEmpty(unionid)) { throw BusinessRuntimeException.getInstance("授权操作异常,请刷新页面"); } WxSyncLog wxSyncLog = wxSyncLogMapper.selectOne(Wrappers.lambdaQuery(WxSyncLog.class) .eq(WxSyncLog::getToAppid, weChatConfig.getAppid()) .eq(WxSyncLog::getNewOpenid, info.getOpenid()).last("limit 1")); User user = getSyncUser(info.getOpenid(), info.getUnionid(), wxSyncLog); user = Optional.ofNullable(user).orElseGet(() -> new User() .setUnionid(unionid)); user.setNickname(info.getNickname()) .setSex(info.getSex()) .setCountry(info.getCountry()) .setProvince(info.getProvince()) .setCity(info.getCity()) .setHeadimgurl(info.getHeadimgurl()); if (!user.getUnionid().equals(unionid)) { UserTransferLog log = new UserTransferLog(); if (wxSyncLog != null) { log.setType("公众号迁移"); } else { log.setType("绑定开发者账号"); } log.setOldAppId(weChatConfig.getOldAppid()); log.setNewAppId(weChatConfig.getAppid()); log.setUserId(user.getId()); log.setOldUnionid(user.getUnionid()); log.setNewUnionid(info.getUnionid()); userTransferLogMapper.insert(log); } user.setUnionid(unionid); user.setOpenId(info.getOpenid()); Boolean isNewUser = false; if (user.getId() == null) { isNewUser = true; user.setPopularizeId(pid); if (!redisService.setNx(RedisService.key.REPEAT_USER_KEY.getNameFormat(user.getUnionid()), user.getUnionid(), RedisService.key.REPEAT_USER_KEY.getTimeout())) { return this.getOne(Wrappers.lambdaQuery(User.class) .eq(User::getUnionid, unionid).last("limit 1")); } this.save(user); } else { this.updateById(user); } if (sharedId != null && isNewUser) { if (dsType == null || dsType == 1) { distributeService.saveDistributionInvitation(sharedId, user.getId()); } } Long userId = user.getId(); if (isNewUser && pid != null) { TASK_POOL.execute(() -> { sendFixedCustomerCoupon(pid, userId, unionid); }); } //新旧用户都可实物分销 if (sharedId != null && dsType != null && dsType == 2) { equipDistributeService.saveDistributionInvitation(sharedId, isNewUser, user.getId()); } return user; } @Override public UserWhoami whoami(long userId) { User user = this.getById(userId); Assert.notNull(user,"业务处理失败,请联系客服"); UserWhoami userWhoami = new UserWhoami(); BeanUtil.copyProperties(user, userWhoami); return userWhoami; } @Override public User getSyncUser(String openId, String unionId, WxSyncLog wxSyncLog) { User user; //迁移公众号之后同步授权后信息 //是否是迁移用户 if (wxSyncLog != null) { user = this.getOne(Wrappers.lambdaQuery(User.class) .ne(User::getUnionid, unionId) .eq(User::getOpenId, wxSyncLog.getOriOpenid()).last("limit 1")); if (user != null) { user.setOpenId(wxSyncLog.getNewOpenid()); } else { // 查询用户是否存在 user = Optional.ofNullable( this.getOne(new LambdaQueryWrapper().eq(User::getUnionid, unionId) .orderByAsc(User::getId) .last("limit 1"))) .orElseGet(() -> this.getOne(new LambdaQueryWrapper().eq(User::getOpenId, openId) .orderByAsc(User::getId) .last("limit 1"))); } } else { // 查询用户是否存在 user = Optional.ofNullable( this.getOne(new LambdaQueryWrapper().eq(User::getUnionid, unionId) .orderByAsc(User::getId) .last("limit 1"))) .orElseGet(() -> this.getOne(new LambdaQueryWrapper().eq(User::getOpenId, openId) .orderByAsc(User::getId) .last("limit 1"))); } return user; } @Override public void sendFixedCustomerCoupon(Long popularizeId, Long userId, String unionId) { //若是百度渠道 添加固定客服 注册后直接发放优惠券 ChannelPopularize channelPopularize = channelPopularizeMapper.selectById(popularizeId); if (channelPopularize != null && channelPopularize.getChannelId() == 34) { // SysConfig corpConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class) // .eq(SysConfig::getSysKey, Constant.CORP_FIXED_CUSTOMER) // .last("limit 1")); // if (corpConfig != null) { // Optional.ofNullable(corpConfig.getSysValue()).ifPresent(value -> { // try { // if (StrUtil.isNotBlank(value)) { // List followIds = Jsons.parseList(value, String.class); // //添加固定客服发放优惠券 // CorpUserFollowView corpUserFollowView = beanSearcher.searchFirst(CorpUserFollowView.class, MapUtils.builder() // .field(CorpUserFollowView::getUnionId, unionId) // .field(CorpUserFollowView::getFollowId, followIds).op(Operator.InList).build()); // if (corpUserFollowView != null) { // Constant.FIXED_CUSTOMER_COUPON_IDS.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, 0l, CouponUser.Channel.wxCorp)); // } // } // } catch (Exception e) { // } // }); // // } SysConfig config = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class) .eq(SysConfig::getSysKey, Constant.BAIDU_POPULARIZE_COUPON_IDS).last("limit 1")); if (config != null) { String sysValue = config.getSysValue(); if (StrUtil.isNotBlank(sysValue)) { try { List couponIds = Jsons.parseList(sysValue, Long.class); couponIds.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, popularizeId, CouponUser.Channel.manual)); } catch (Exception e) { log.error("百度渠道优惠券解析错误"); } } } } } }