Please enter the commit message for your changes. Lines starting

with '#' will be ignored, and an empty message aborts the commit.

On branch develop
Your branch is ahead of 'goflyapp/develop' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
	new file:   zlt-business/user-center/src/main/java/com/central/user/config/TenantConfiguration.java
	new file:   zlt-business/user-center/src/main/java/com/central/user/controller/SysTenantController.java
	modified:   zlt-business/user-center/src/main/java/com/central/user/mapper/SysTenantMapper.java
	modified:   zlt-business/user-center/src/main/java/com/central/user/model/SysTenant.java
	new file:   zlt-business/user-center/src/main/java/com/central/user/service/ISysTenantService.java
	new file:   zlt-business/user-center/src/main/java/com/central/user/service/impl/SysTenantServiceImpl.java
	new file:   zlt-business/user-center/src/main/java/com/central/user/service/impl/TenantInitServiceImpl.java
	modified:   zlt-business/user-center/src/main/resources/application.yml
	new file:   zlt-business/user-center/src/main/resources/mapper/SysTenantMapper.xml
	modified:   zlt-commons/zlt-common-core/src/main/java/com/central/common/properties/TenantProperties.java
	modified:   zlt-web/react-web/src/main/frontend/config/routes.ts
	new file:   zlt-web/react-web/src/main/frontend/src/pages/orders/Refund/components/UpdateForm.less
	modified:   zlt-web/react-web/src/main/frontend/src/pages/orders/Refund/components/UpdateForm.tsx
	modified:   zlt-web/react-web/src/main/frontend/src/pages/orders/Refund/constants.ts
	modified:   zlt-web/react-web/src/main/frontend/src/pages/orders/Refund/index.less
	modified:   zlt-web/react-web/src/main/frontend/src/pages/orders/Refund/index.tsx
	modified:   zlt-web/react-web/src/main/frontend/src/pages/orders/Refund/types.ts
	new file:   zlt-web/react-web/src/main/frontend/src/pages/system/Tenant/components/CreateForm.tsx
	new file:   zlt-web/react-web/src/main/frontend/src/pages/system/Tenant/components/UpdateForm.tsx
	new file:   zlt-web/react-web/src/main/frontend/src/pages/system/Tenant/index.tsx
	modified:   zlt-web/react-web/src/main/frontend/src/services/system/api.ts
	modified:   zlt-web/react-web/src/main/frontend/src/services/system/typings.d.ts
This commit is contained in:
2025-04-10 17:16:01 +08:00
parent 4859929595
commit 946e033545
22 changed files with 1179 additions and 182 deletions
@@ -0,0 +1,50 @@
package com.central.user.config;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.central.common.context.TenantContextHolder;
import com.central.common.filter.TenantFilter;
import com.central.common.properties.TenantProperties;
import lombok.RequiredArgsConstructor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(TenantProperties.class)
@RequiredArgsConstructor
public class TenantConfiguration {
private final TenantProperties tenantProperties;
@Bean
public TenantLineHandler tenantLineHandler() {
return new TenantLineHandler() {
@Override
public String getTenantIdColumn() {
return "tenant_id";
}
@Override
public boolean ignoreTable(String tableName) {
return tenantProperties.getIgnoreTables().stream().anyMatch(
(t) -> t.equalsIgnoreCase(tableName)
);
}
@Override
public Expression getTenantId() {
String tenant = TenantContextHolder.getTenant();
if (tenant != null) {
return new StringValue(tenant);
}
return null;
}
};
}
@Bean
public TenantFilter tenantFilter() {
return new TenantFilter();
}
}
@@ -0,0 +1,59 @@
package com.central.user.controller;
import com.central.common.context.TenantContextHolder;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.user.model.SysTenant;
import com.central.user.service.ISysTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/tenant")
@Tag(name = "租户管理")
@RequiredArgsConstructor
public class SysTenantController {
private final ISysTenantService sysTenantService;
@Operation(summary = "查询租户列表")
@GetMapping("/list")
public PageResult<SysTenant> list(@RequestParam Map<String, Object> params) {
return sysTenantService.findList(params);
}
@Operation(summary = "查询租户详情")
@GetMapping("/{id}")
public Result<SysTenant> get(@PathVariable String id) {
return Result.succeed(sysTenantService.getById(id));
}
@Operation(summary = "新增/更新租户")
@PostMapping("/save")
public Result save(@RequestBody SysTenant tenant) {
return sysTenantService.saveOrUpdateTenant(tenant);
}
@Operation(summary = "删除租户")
@DeleteMapping("/{id}")
public Result delete(@PathVariable String id) {
return sysTenantService.deleteTenant(id);
}
@Operation(summary = "更新租户状态")
@PutMapping("/status/{id}/{status}")
public Result updateStatus(@PathVariable String id, @PathVariable Integer status) {
return sysTenantService.updateStatus(id, status);
}
@Operation(summary = "查询当前租户")
@GetMapping("/current")
public Result<String> getCurrentTenant() {
return Result.succeed(TenantContextHolder.getTenant());
}
}
@@ -1,7 +1,12 @@
package com.central.user.mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.db.mapper.SuperMapper;
import com.central.user.model.SysTenant;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
@Mapper
@@ -1,9 +1,27 @@
package com.central.user.model;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.central.common.model.SuperEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("sys_tenant")
public class SysTenant extends SuperEntity {
private static final long serialVersionUID = 1L;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
/**
* 租户名称
*/
@@ -20,37 +38,28 @@ public class SysTenant extends SuperEntity {
private Integer status;
/**
* 租户类型:1-免费版,2-标准版,3-企业版
* 租户类型
*/
private Integer type;
/**
* 开始时间
* 租户域名
*/
private Date startTime;
private String domain;
/**
* 结束时间
* 租户数据源名称
*/
private Date endTime;
private String datasourceConfigId;
/**
* 联系人
* 是否删除
*/
private String contactName;
/**
* 联系电话
*/
private String contactPhone;
/**
* 联系邮箱
*/
private String contactEmail;
@TableLogic
private Boolean isDeleted;
/**
* 备注
*/
private String remark;
private String remarks;
}
@@ -0,0 +1,15 @@
package com.central.user.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.user.model.SysTenant;
import java.util.Map;
public interface ISysTenantService extends IService<SysTenant> {
PageResult<SysTenant> findList(Map<String, Object> params);
Result saveOrUpdateTenant(SysTenant tenant);
Result deleteTenant(String id);
Result updateStatus(String id, Integer status);
}
@@ -0,0 +1,130 @@
package com.central.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.common.constant.CommonConstant;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.user.mapper.SysTenantMapper;
import com.central.user.model.SysTenant;
import com.central.user.service.ISysTenantService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.MapUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.central.user.config.TenantProperties;
import com.central.user.context.TenantContextHolder;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
@RequiredArgsConstructor
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements ISysTenantService {
private final RedisTemplate<String, Object> redisTemplate;
private final TenantProperties tenantProperties;
@Override
public PageResult<SysTenant> findList(Map<String, Object> params) {
// 清除租户上下文,查询所有租户
TenantContextHolder.clear();
try {
Page<SysTenant> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
LambdaQueryWrapper<SysTenant> wrapper = new LambdaQueryWrapper<>();
// 查询条件
if (StrUtil.isNotEmpty(MapUtils.getString(params, "name"))) {
wrapper.like(SysTenant::getName, MapUtils.getString(params, "name"));
}
if (StrUtil.isNotEmpty(MapUtils.getString(params, "code"))) {
wrapper.eq(SysTenant::getCode, MapUtils.getString(params, "code"));
}
if (params.get("status") != null) {
wrapper.eq(SysTenant::getStatus, MapUtils.getInteger(params, "status"));
}
wrapper.orderByDesc(SysTenant::getCreateTime);
IPage<SysTenant> list = baseMapper.selectPage(page, wrapper);
return PageResult.<SysTenant>builder().data(list.getRecords()).count(list.getTotal()).build();
} finally {
// 恢复租户上下文
TenantContextHolder.setTenant(MapUtils.getString(params, "tenantId"));
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public Result saveOrUpdateTenant(SysTenant tenant) {
// 清除租户上下文
TenantContextHolder.clear();
try {
if (tenant.getId() == null) {
// 新增租户
tenant.setCode(generateTenantCode());
tenant.setStatus(CommonConstant.STATUS_NORMAL);
// 检查租户编码是否重复
if (checkTenantCodeExists(tenant.getCode())) {
return Result.failed("租户编码已存在");
}
}
boolean result = saveOrUpdate(tenant);
if (result) {
refreshTenantCache(tenant);
return Result.succeed("操作成功");
}
return Result.failed("操作失败");
} finally {
// 恢复租户上下文
TenantContextHolder.setTenant(tenant.getCode());
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public Result deleteTenant(String id) {
// 清除租户上下文
TenantContextHolder.clear();
try {
SysTenant tenant = getById(id);
if (tenant == null) {
return Result.failed("租户不存在");
}
// 逻辑删除租户
boolean result = removeById(id);
if (result) {
clearTenantCache(tenant.getCode());
return Result.succeed("删除成功");
}
return Result.failed("删除失败");
} finally {
// 恢复租户上下文
TenantContextHolder.setTenant(getById(id).getCode());
}
}
private boolean checkTenantCodeExists(String code) {
LambdaQueryWrapper<SysTenant> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysTenant::getCode, code);
return baseMapper.selectCount(wrapper) > 0;
}
private String generateTenantCode() {
return "T" + IdUtil.getSnowflake().nextIdStr();
}
private void refreshTenantCache(SysTenant tenant) {
String key = CommonConstant.TENANT_PREFIX + tenant.getCode();
redisTemplate.opsForValue().set(key, tenant);
}
private void clearTenantCache(String tenantCode) {
String key = CommonConstant.TENANT_PREFIX + tenantCode;
redisTemplate.delete(key);
}
}
@@ -0,0 +1,49 @@
package com.central.user.service.impl;
import com.central.common.model.Result;
import com.central.user.service.ISysTenantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
@RequiredArgsConstructor
public class TenantInitServiceImpl {
private final ISysTenantService tenantService;
private final DataSourceInitializer dataSourceInitializer;
@Transactional(rollbackFor = Exception.class)
public Result initTenantData(String tenantId) {
try {
// 1. 初始化租户数据源
dataSourceInitializer.initTenantDataSource(tenantId);
// 2. 初始化租户基础数据
initBasicData(tenantId);
// 3. 初始化租户配置
initTenantConfig(tenantId);
return Result.succeed("租户初始化成功");
} catch (Exception e) {
log.error("租户初始化失败", e);
return Result.failed("租户初始化失败");
}
}
private void initBasicData(String tenantId) {
// 初始化基础数据
// 1. 初始化管理员账号
// 2. 初始化基础角色
// 3. 初始化基础权限
}
private void initTenantConfig(String tenantId) {
// 初始化租户配置
// 1. 初始化系统配置
// 2. 初始化业务配置
}
}
@@ -39,11 +39,14 @@ zlt:
- sys_user
- sys_role_user
- sys_role_menu
- sys_tenant
ignoreSqls:
# 用户关联角色时,显示所有角色
- com.central.user.mapper.SysRoleMapper.findAll
# 用户列表显示用户所关联的所有角色
- com.central.user.mapper.SysUserRoleMapper.findRolesByUserIds
# 租户列表
- com.central.user.mapper.SysTenantMapper.findList
# 数据权限
datascope:
enabled: false
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.central.user.mapper.SysTenantMapper">
<select id="findList" resultType="com.central.user.model.SysTenant">
select * from sys_tenant t
<where>
<if test="p.name != null and p.name != ''">
and t.name like concat('%', #{p.name}, '%')
</if>
<if test="p.code != null and p.code != ''">
and t.code = #{p.code}
</if>
<if test="p.status != null">
and t.status = #{p.status}
</if>
and t.is_deleted = 0
</where>
order by t.create_time desc
</select>
</mapper>