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.dto.request.register.RegisterDTO; import com.mzl.flower.dto.request.register.RegisterPartnerDTO; import com.mzl.flower.entity.system.User; import com.mzl.flower.entity.system.UserWechat; import com.mzl.flower.mapper.system.UserMapper; 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.utils.UUIDGenerator; import org.apache.commons.lang3.StringUtils; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @Service @Transactional public class RegisterService { private final UserMapper userMapper; private final PasswordEncoder passwordEncoder; private final PartnerService partnerService; private final CustomerService customerService; private final UserWechatMapper wechatMapper; public RegisterService(UserMapper userMapper, PasswordEncoder passwordEncoder, PartnerService partnerService, CustomerService customerService, UserWechatMapper wechatMapper) { this.userMapper = userMapper; this.passwordEncoder = passwordEncoder; this.partnerService = partnerService; this.customerService = customerService; this.wechatMapper = wechatMapper; } public void registerUser(RegisterDTO dto, String userType) { if(checkUserExist(dto.getTel(),userType)){ throw new ValidationException("该手机号码已经注册"); } 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); } public void registerCustomerUser(RegisterCustomerDTO dto, String userType, String openId, String sessionKey, String unionId) { if(checkUserExist(dto.getTel(),userType)){ throw new ValidationException("该手机号码已经注册"); } 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() .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()); } public void registerPartnerUser(RegisterPartnerDTO dto) { String userType = Constants.USER_TYPE.partner.name(); if(checkUserExist(dto.getTel(),userType)){ throw new ValidationException("该手机号码已经注册"); } 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); dto.getDto().setUserId(user.getId()); partnerService.addOrUpdatePartner(dto.getDto()); } private boolean checkUserExist(String tel, String userType) { List userTypes = new ArrayList<>(); userTypes.add(userType); User user = userMapper.getActiveUser(tel, userTypes); return user != null; } }