|  |  |  | 
|---|
|  |  |  | package com.mzl.flower.service.register; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.mzl.flower.config.exception.ValidationException; | 
|---|
|  |  |  | import com.mzl.flower.constant.Constants; | 
|---|
|  |  |  | import com.mzl.flower.dto.request.register.RegisterCustomerDTO; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.mzl.flower.mapper.system.UserWechatMapper; | 
|---|
|  |  |  | import com.mzl.flower.service.customer.CustomerService; | 
|---|
|  |  |  | import com.mzl.flower.service.partner.PartnerService; | 
|---|
|  |  |  | import com.mzl.flower.service.payment.RedisLockService; | 
|---|
|  |  |  | import com.mzl.flower.utils.UUIDGenerator; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.springframework.security.crypto.password.PasswordEncoder; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final UserWechatMapper wechatMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final RedisLockService lockService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public RegisterService(UserMapper userMapper, | 
|---|
|  |  |  | PasswordEncoder passwordEncoder, | 
|---|
|  |  |  | PartnerService partnerService, | 
|---|
|  |  |  | CustomerService customerService, UserWechatMapper wechatMapper) { | 
|---|
|  |  |  | CustomerService customerService, UserWechatMapper wechatMapper, | 
|---|
|  |  |  | RedisLockService lockService) { | 
|---|
|  |  |  | this.userMapper = userMapper; | 
|---|
|  |  |  | this.passwordEncoder = passwordEncoder; | 
|---|
|  |  |  | this.partnerService = partnerService; | 
|---|
|  |  |  | this.customerService = customerService; | 
|---|
|  |  |  | this.wechatMapper = wechatMapper; | 
|---|
|  |  |  | this.lockService = lockService; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void registerUser(RegisterDTO dto, String userType) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void registerCustomerUser(RegisterCustomerDTO dto, String userType, String openId, String sessionKey, String unionId) { | 
|---|
|  |  |  | if(checkUserExist(dto.getTel(),userType)){ | 
|---|
|  |  |  | throw new ValidationException("该手机号码已经注册"); | 
|---|
|  |  |  | String key = dto.getTel() + "_" + userType; | 
|---|
|  |  |  | boolean lock = lockService.getObjectLock(RedisLockService.LOCK_KEY_CART_, key); | 
|---|
|  |  |  | if(!lock){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | User user = new User(); | 
|---|
|  |  |  | user.setId(UUIDGenerator.getUUID()); | 
|---|
|  |  |  | user.setLoginName(dto.getTel()); | 
|---|
|  |  |  | user.setTel(dto.getTel()); | 
|---|
|  |  |  | user.setNickName(dto.getTel()); | 
|---|
|  |  |  | user.setPassword(passwordEncoder.encode(dto.getPassword())); | 
|---|
|  |  |  | user.setType(userType); | 
|---|
|  |  |  | user.setStatus(Constants.STATUS_ACTIVE); | 
|---|
|  |  |  | user.setIsSys(Constants.N); | 
|---|
|  |  |  | user.create(); | 
|---|
|  |  |  | userMapper.insert(user); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(StringUtils.isNotBlank(openId)){ | 
|---|
|  |  |  | UserWechat wechat = wechatMapper.selectOne(new LambdaQueryWrapper<UserWechat>() | 
|---|
|  |  |  | .eq(UserWechat::getUserId,user.getId()) | 
|---|
|  |  |  | .eq(UserWechat::getOpenId,openId)); | 
|---|
|  |  |  | if(wechat != null){ | 
|---|
|  |  |  | throw new ValidationException("该微信用户已经注册"); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(checkUserExist(dto.getTel(),userType)){ | 
|---|
|  |  |  | throw new ValidationException("该手机号码已经注册"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | wechat = new UserWechat(); | 
|---|
|  |  |  | wechat.setUserId(user.getId()); | 
|---|
|  |  |  | wechat.setId(UUIDGenerator.getUUID()); | 
|---|
|  |  |  | wechat.setOpenId(openId); | 
|---|
|  |  |  | wechat.setUnionId(unionId); | 
|---|
|  |  |  | wechat.setSessionKey(sessionKey); | 
|---|
|  |  |  | wechat.create(user.getId()); | 
|---|
|  |  |  | wechatMapper.insert(wechat); | 
|---|
|  |  |  | User user = new User(); | 
|---|
|  |  |  | user.setId(UUIDGenerator.getUUID()); | 
|---|
|  |  |  | user.setLoginName(dto.getTel()); | 
|---|
|  |  |  | user.setTel(dto.getTel()); | 
|---|
|  |  |  | user.setNickName(dto.getTel()); | 
|---|
|  |  |  | user.setPassword(passwordEncoder.encode(dto.getPassword())); | 
|---|
|  |  |  | user.setType(userType); | 
|---|
|  |  |  | user.setStatus(Constants.STATUS_ACTIVE); | 
|---|
|  |  |  | user.setIsSys(Constants.N); | 
|---|
|  |  |  | user.create(); | 
|---|
|  |  |  | userMapper.insert(user); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(StringUtils.isNotBlank(openId)){ | 
|---|
|  |  |  | UserWechat wechat = wechatMapper.selectOne(new LambdaQueryWrapper<UserWechat>() | 
|---|
|  |  |  | .eq(UserWechat::getUserId,user.getId()) | 
|---|
|  |  |  | .eq(UserWechat::getOpenId,openId)); | 
|---|
|  |  |  | if(wechat != null){ | 
|---|
|  |  |  | throw new ValidationException("该微信用户已经注册"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | wechat = new UserWechat(); | 
|---|
|  |  |  | wechat.setUserId(user.getId()); | 
|---|
|  |  |  | wechat.setId(UUIDGenerator.getUUID()); | 
|---|
|  |  |  | wechat.setOpenId(openId); | 
|---|
|  |  |  | wechat.setUnionId(unionId); | 
|---|
|  |  |  | wechat.setSessionKey(sessionKey); | 
|---|
|  |  |  | wechat.create(user.getId()); | 
|---|
|  |  |  | wechatMapper.insert(wechat); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | dto.getDto().setUserId(user.getId()); | 
|---|
|  |  |  | customerService.addOrUpdateCustomer(dto.getDto()); | 
|---|
|  |  |  | }finally { | 
|---|
|  |  |  | lockService.releaseObjectLock(RedisLockService.LOCK_KEY_CART_, key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | dto.getDto().setUserId(user.getId()); | 
|---|
|  |  |  | customerService.addOrUpdateCustomer(dto.getDto()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|