zjj
2 天以前 d5540413a0163383b53d33aea1be93390ea9149c
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
132
133
134
135
136
137
138
139
package com.vincent.rsf.server.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.common.domain.BusinessRes;
import com.vincent.rsf.server.common.exception.BusinessException;
import com.vincent.rsf.server.common.service.EmailService;
import com.vincent.rsf.server.system.controller.param.TenantInitParam;
import com.vincent.rsf.server.system.entity.*;
import com.vincent.rsf.server.system.enums.StatusType;
import com.vincent.rsf.server.system.mapper.TenantMapper;
import com.vincent.rsf.server.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.List;
 
/**
 * Created by vincent on 8/30/2024
 */
@Service("tenantService")
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements TenantService {
 
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private EmailService emailService;
 
    @Override
    @Transactional
    public Long initTenant(TenantInitParam param) {
        // valid ----------------------------
        if (null == param) {
            return null;
        }
        if (Cools.isEmpty(param.getName(), param.getFlag(), param.getUsername(), param.getPassword())) {
            return null;
        }
 
        if (null != userService.getByUsername(param.getUsername(), null)) {
            throw new BusinessException(BusinessRes.USERNAME_EXIST);
        }
        if (!Cools.isEmpty(param.getEmail())) {
            if (!emailService.isValid(param.getEmail())) {
                throw new CoolException("Please enter a valid email address");
            }
            if (null != userService.getByEmail(param.getEmail(), null)) {
                throw new CoolException("the email already exist");
            }
        }
 
        if (0 < this.count(new LambdaQueryWrapper<Tenant>().eq(Tenant::getName, param.getName()))) {
            throw new CoolException("tenant name already exist");
        }
        if (0 < this.count(new LambdaQueryWrapper<Tenant>().eq(Tenant::getFlag, param.getFlag()))) {
            throw new CoolException("tenant flag already exist");
        }
 
        Date now = new Date();
        // save tenant
        Tenant tenant = new Tenant();
        tenant.setName(param.getName());
        tenant.setFlag(param.getFlag());
        tenant.setRoot(0L);
        tenant.setStatus(StatusType.ENABLE.val);
        tenant.setCreateTime(now);
        tenant.setUpdateTime(now);
        tenant.setMemo(param.getMemo());
        if (!this.save(tenant)) {
            throw new CoolException("failed to create tenant");
        }
 
        // save user
        User user = new User();
        user.setTenantId(tenant.getId());
        user.setUsername(param.getUsername());
        user.setNickname(param.getUsername());
        user.setPassword(userService.encodePassword(param.getPassword()));
        user.setEmail(param.getEmail());
        user.setCreateTime(now);
        user.setUpdateTime(now);
        user.setStatus(StatusType.ENABLE.val);
        if (!userService.save(user)) {
            throw new CoolException("failed to create root user");
        }
 
        // update tenant
        tenant.setRoot(user.getId());
        if (!this.updateById(tenant)) {
            throw new CoolException("failed to create tenant");
        }
 
        // save role
        Role role = new Role();
        role.setTenantId(tenant.getId());
        role.setName("admin");
        role.setCode("admin");
        role.setStatus(StatusType.ENABLE.val);
        role.setCreateTime(now);
        role.setUpdateTime(now);
        if (!roleService.save(role)) {
            throw new CoolException("failed to create role");
        }
 
        // save userRole
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(role.getId());
        if (!userRoleService.save(userRole)) {
            throw new CoolException("failed to create userRole");
        }
 
        // save roleMenu
        List<Menu> menuList = menuService.list();
        for (Menu menu : menuList) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(menu.getId());
            if (!roleMenuService.save(roleMenu)) {
                throw new CoolException("failed to create roleMenu");
            }
        }
 
        return tenant.getId();
    }
 
}