|
@@ -0,0 +1,157 @@
|
|
|
+package com.fjhx.tenant.service.tenant.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.tenant.entity.tenant.dto.TenantInfoDto;
|
|
|
+import com.fjhx.tenant.entity.tenant.dto.TenantInfoSelectDto;
|
|
|
+import com.fjhx.tenant.entity.tenant.po.TenantInfo;
|
|
|
+import com.fjhx.tenant.entity.tenant.vo.TenantInfoVo;
|
|
|
+import com.fjhx.tenant.mapper.tenant.TenantInfoMapper;
|
|
|
+import com.fjhx.tenant.service.tenant.TenantInfoService;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysDept;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysRole;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
+import com.ruoyi.common.utils.wrapper.SqlField;
|
|
|
+import com.ruoyi.system.service.ISysDeptService;
|
|
|
+import com.ruoyi.system.service.ISysRoleService;
|
|
|
+import com.ruoyi.system.service.ISysUserService;
|
|
|
+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;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 租户表 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author zlj
|
|
|
+ * @since 2023-03-15
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoMapper, TenantInfo> implements TenantInfoService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService sysUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysDeptService sysDeptService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysRoleService sysRoleService;
|
|
|
+
|
|
|
+ public Page<TenantInfoVo> getPage(TenantInfoSelectDto dto) {
|
|
|
+ IWrapper<TenantInfo> wrapper = getWrapper();
|
|
|
+ wrapper.orderByDesc("ti", TenantInfo::getId)
|
|
|
+ .eq("ti", TenantInfo::getStatus, dto.getStatus())
|
|
|
+ .eq("ti", TenantInfo::getFlowStatus, dto.getFlowStatus())
|
|
|
+ .like("ti", TenantInfo::getTenantId, dto.getTenantId())
|
|
|
+ .like("ti", TenantInfo::getEnterpriseName, dto.getEnterpriseName())
|
|
|
+ .keyword(dto,
|
|
|
+ new SqlField("ti", TenantInfo::getEnterpriseName),
|
|
|
+ new SqlField("ti", TenantInfo::getTenantId)
|
|
|
+ );
|
|
|
+
|
|
|
+ Page<TenantInfoVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
|
|
|
+ List<TenantInfoVo> records = page.getRecords();
|
|
|
+
|
|
|
+ if (records.size() == 0) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> tenantIdList = records.stream().map(TenantInfo::getTenantId).collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 赋值创建账号数
|
|
|
+ List<Map<String, Object>> list = sysUserService.listMaps(Wrappers.<SysUser>query()
|
|
|
+ .select("count(0) count", "tenant_id tenantId")
|
|
|
+ .in("tenant_id", tenantIdList)
|
|
|
+ .groupBy("tenant_id")
|
|
|
+ );
|
|
|
+ Map<String, Long> tenantCountMap = list.stream().collect(Collectors.toMap(
|
|
|
+ item -> (String) item.get("tenantId"),
|
|
|
+ item -> (Long) item.get("count")
|
|
|
+ ));
|
|
|
+ for (TenantInfoVo record : records) {
|
|
|
+ String tenantId = record.getTenantId();
|
|
|
+ Long count = ObjectUtil.defaultIfNull(tenantCountMap.get(tenantId), 0L);
|
|
|
+ record.setAccountCount(count);
|
|
|
+ }
|
|
|
+
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ public TenantInfoVo detail(Long id) {
|
|
|
+ TenantInfo TenantInfo = this.getById(id);
|
|
|
+ TenantInfoVo result = BeanUtil.toBean(TenantInfo, TenantInfoVo.class);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void add(TenantInfoDto tenantInfoDto) {
|
|
|
+ TenantInfo tenantInfo = getOne(q -> q.eq(TenantInfo::getTenantId, tenantInfoDto.getTenantId()));
|
|
|
+ if (tenantInfo != null) {
|
|
|
+ throw new ServiceException("租户已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO 审核通过
|
|
|
+ tenantInfoDto.setFlowStatus(2);
|
|
|
+ this.save(tenantInfoDto);
|
|
|
+
|
|
|
+ SysDept sysDept = new SysDept();
|
|
|
+ sysDept.setParentId(0L);
|
|
|
+ sysDept.setDeptName(tenantInfoDto.getEnterpriseName());
|
|
|
+ sysDept.setOrderNum(1);
|
|
|
+ sysDept.setStatus("0");
|
|
|
+ sysDept.setTenantId(tenantInfoDto.getTenantId());
|
|
|
+ sysDept.setCreateBy(SecurityUtils.getUsername());
|
|
|
+ sysDept.setCreateTime(new Date());
|
|
|
+ sysDeptService.save(sysDept);
|
|
|
+
|
|
|
+ SysUser sysUser = new SysUser();
|
|
|
+ sysUser.setDeptId(sysDept.getDeptId());
|
|
|
+ sysUser.setTenantId(tenantInfoDto.getTenantId());
|
|
|
+ sysUser.setUserName("admin");
|
|
|
+ sysUser.setNickName("管理员");
|
|
|
+ sysUser.setPassword(SecurityUtils.encryptPassword(tenantInfoDto.getPassword()));
|
|
|
+ sysUser.setStatus("0");
|
|
|
+ sysUser.setCreateBy(SecurityUtils.getUsername());
|
|
|
+ sysUser.setCreateTime(new Date());
|
|
|
+ sysUserService.save(sysUser);
|
|
|
+
|
|
|
+ SysRole sysRole = new SysRole();
|
|
|
+ sysRole.setRoleName("管理员");
|
|
|
+ sysRole.setRoleKey("admin");
|
|
|
+ sysRole.setRoleSort(1);
|
|
|
+ sysRole.setDataScope("1");
|
|
|
+ sysRole.setStatus("0");
|
|
|
+ sysRole.setTenantId(tenantInfoDto.getTenantId());
|
|
|
+ sysRole.setCreateBy(SecurityUtils.getUsername());
|
|
|
+ sysRole.setCreateTime(new Date());
|
|
|
+ sysRoleService.save(sysRole);
|
|
|
+
|
|
|
+ sysRoleService.insertAuthUsers(sysRole.getRoleId(), new Long[]{sysUser.getUserId()});
|
|
|
+ }
|
|
|
+
|
|
|
+ public void edit(TenantInfoDto tenantInfoDto) {
|
|
|
+ // 租户id不允许修改
|
|
|
+ tenantInfoDto.setTenantId(null);
|
|
|
+
|
|
|
+ this.updateById(tenantInfoDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void delete(Long id) {
|
|
|
+ this.removeById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|