陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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<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());
    }
 
 
    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<String> userTypes = new ArrayList<>();
        userTypes.add(userType);
        User user = userMapper.getActiveUser(tel, userTypes);
        return user != null;
    }
}