| | |
| | | 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.server.system.entity.Tenant; |
| | | import com.vincent.rsf.framework.common.Cools; |
| | | import com.vincent.rsf.framework.exception.CoolException; |
| | | 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.TenantService; |
| | | 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; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Boolean initTenant(TenantInitParam param) { |
| | | // valid ---------------------------- |
| | | if (null == param) { |
| | | return false; |
| | | } |
| | | if (Cools.isEmpty(param.getName(), param.getFlag(), param.getUsername(), param.getPassword())) { |
| | | return false; |
| | | } |
| | | |
| | | 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"); |
| | | } |
| | | |
| | | if (null != userService.getByUsername(param.getUsername(), null)) { |
| | | throw new CoolException("the username already exist"); |
| | | } |
| | | if (!Cools.isEmpty(param.getEmail())) { |
| | | if (null != userService.getByEmail(param.getEmail(), null)) { |
| | | throw new CoolException("the email 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 true; |
| | | } |
| | | |
| | | } |