fix: unique tenancy and name check

This commit is contained in:
2024-06-25 10:55:07 +08:00
parent 4120483b17
commit e763c50aeb
5 changed files with 97 additions and 25 deletions

View File

@@ -131,13 +131,23 @@ func (s *SysTenantController) Add(c *gin.Context) {
body.Ancestors = "0"
}
// 检查同级下名称唯一
uniqueTenantName := s.sysTenantService.CheckUniqueTenantName(body.TenantName, body.ParentID, "")
if !uniqueTenantName {
// 租户新增【%s】失败租户名称已存在
msg := i18n.TTemplate(language, "tenant.errNameExists", map[string]any{"name": body.TenantName})
c.JSON(200, result.ErrMsg(msg))
return
// 检查租户名称唯一
if body.ParentID == "1" {
uniqueTenantName := s.sysTenantService.CheckUniqueTenantName(body.TenantName, body.ParentID, "")
if !uniqueTenantName {
// 租户新增【%s】失败租户名称已存在
msg := i18n.TTemplate(language, "tenant.errNameExists", map[string]any{"name": body.TenantName})
c.JSON(200, result.ErrMsg(msg))
return
}
} else {
uniqueTenancy := s.sysTenantService.IsUniqueTenancy(body.TenancyType, body.TenancyKey)
if !uniqueTenancy {
// 租赁对象添加失败,租赁对象已存在
msg := i18n.TTemplate(language, "Tenancy object is exist", map[string]any{"key": body.TenancyKey})
c.JSON(200, result.ErrMsg(msg))
return
}
}
body.CreateBy = ctx.LoginUserToUserName(c)
@@ -186,13 +196,23 @@ func (s *SysTenantController) Edit(c *gin.Context) {
}
}
// 检查同级下名称唯一
uniqueTenantName := s.sysTenantService.CheckUniqueTenantName(body.TenantName, body.ParentID, body.TenantID)
if !uniqueTenantName {
// 租户修改【%s】失败租户名称已存在
msg := i18n.TTemplate(language, "tenant.errNameExists", map[string]any{"name": body.TenantName})
c.JSON(200, result.ErrMsg(msg))
return
// 检查租户名称唯一
if body.ParentID == "1" {
uniqueTenantName := s.sysTenantService.CheckUniqueTenantName(body.TenantName, body.ParentID, body.TenantID)
if !uniqueTenantName {
// 租户修改【%s】失败租户名称已存在
msg := i18n.TTemplate(language, "tenant.errNameExists", map[string]any{"name": body.TenantName})
c.JSON(200, result.ErrMsg(msg))
return
}
} else {
uniqueTenancy := s.sysTenantService.IsUniqueTenancy(body.TenancyType, body.TenancyKey)
if !uniqueTenancy {
// 租赁对象添加失败,租赁对象已存在
msg := i18n.TTemplate(language, "Tenancy object is exist", map[string]any{"key": body.TenancyKey})
c.JSON(200, result.ErrMsg(msg))
return
}
}
// 上级停用需要检查下级是否有在使用

View File

@@ -25,6 +25,9 @@ type ISysTenant interface {
// CheckUniqueTenant 校验租户是否唯一
CheckUniqueTenant(sysTenant model.SysTenant) string
// 校验租赁对象是否唯一
IsUniqueTenancy(sysTenant model.SysTenant) bool
// InsertTenant 新增租户信息
InsertTenant(sysTenant model.SysTenant) string

View File

@@ -37,7 +37,7 @@ var NewSysTenantImpl = &SysTenantImpl{
},
}
// SysTenantImpl 部门表 数据层处理
// SysTenantImpl 租户表 数据层处理
type SysTenantImpl struct {
// 查询视图对象SQL
selectSql string
@@ -60,7 +60,7 @@ func (r *SysTenantImpl) convertResultRows(rows []map[string]any) []model.SysTena
return arr
}
// SelectTenantList 查询部门管理数据
// SelectTenantList 查询租户管理数据
func (r *SysTenantImpl) SelectTenantList(sysTenant model.SysTenant, dataScopeSQL string) []model.SysTenant {
// 查询条件拼接
var conditions []string
@@ -101,7 +101,7 @@ func (r *SysTenantImpl) SelectTenantList(sysTenant model.SysTenant, dataScopeSQL
return r.convertResultRows(results)
}
// SelectTenantListByRoleId 根据角色ID查询部门树信息
// SelectTenantListByRoleId 根据角色ID查询租户树信息
func (r *SysTenantImpl) SelectTenantListByRoleId(roleId string, tenantCheckStrictly bool) []string {
querySql := `select t.tenant_id as 'str' from sys_tenant d
left join sys_role_tenant rd on t.tenant_id = rt.tenant_id
@@ -135,7 +135,7 @@ func (r *SysTenantImpl) SelectTenantListByRoleId(roleId string, tenantCheckStric
return []string{}
}
// SelectTenantById 根据部门ID查询信息
// SelectTenantById 根据租户ID查询信息
func (r *SysTenantImpl) SelectTenantById(tenantId string) model.SysTenant {
querySql := `select t.tenant_id, t.parent_id, t.ancestors,
t.tenant_name, t.order_num, t.tenancy_type, t.tenancy_key, t.status,
@@ -154,7 +154,7 @@ func (r *SysTenantImpl) SelectTenantById(tenantId string) model.SysTenant {
return model.SysTenant{}
}
// SelectChildrenTenantById 根据ID查询所有子部门
// SelectChildrenTenantById 根据ID查询所有子租户
func (r *SysTenantImpl) SelectChildrenTenantById(tenantId string) []model.SysTenant {
querySql := r.selectSql + " where find_in_set(?, t.ancestors)"
results, err := datasource.RawDB("", querySql, []any{tenantId})
@@ -181,7 +181,7 @@ func (r *SysTenantImpl) HasChildByTenantId(tenantId string) int64 {
return 0
}
// CheckTenantExistUser 查询部门是否存在用户
// CheckTenantExistUser 查询租户是否存在用户
func (r *SysTenantImpl) CheckTenantExistUser(tenantId string) int64 {
querySql := "select count(1) as 'total' from sys_user where tenant_id = ? and del_flag = '0'"
results, err := datasource.RawDB("", querySql, []any{tenantId})
@@ -195,7 +195,7 @@ func (r *SysTenantImpl) CheckTenantExistUser(tenantId string) int64 {
return 0
}
// CheckUniqueTenant 校验部门是否唯一
// CheckUniqueTenant 校验租户是否唯一
func (r *SysTenantImpl) CheckUniqueTenant(sysTenant model.SysTenant) string {
// 查询条件拼接
var conditions []string
@@ -231,7 +231,43 @@ func (r *SysTenantImpl) CheckUniqueTenant(sysTenant model.SysTenant) string {
return ""
}
// InsertTenant 新增部门信息
// 校验租赁对象是否唯一
func (r *SysTenantImpl) IsUniqueTenancy(sysTenant model.SysTenant) bool {
// 查询条件拼接
var conditions []string
var params []any
if sysTenant.TenancyType != "" {
conditions = append(conditions, "tenancy_type = ?")
params = append(params, sysTenant.TenancyType)
}
if sysTenant.TenancyKey != "" {
conditions = append(conditions, "? like tenancy_key")
params = append(params, sysTenant.TenancyKey)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
if whereSql == "" {
return true
}
// 查询数据
querySql := "select 1 from sys_tenant " + whereSql + " and status = '1' and del_flag = '0' limit 1"
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err %v", err)
return true
}
if len(results) > 0 {
return false
}
return true
}
// InsertTenant 新增租户信息
func (r *SysTenantImpl) InsertTenant(sysTenant model.SysTenant) string {
// 参数拼接
params := make(map[string]any)
@@ -243,6 +279,8 @@ func (r *SysTenantImpl) InsertTenant(sysTenant model.SysTenant) string {
}
if sysTenant.TenantName != "" {
params["tenant_name"] = sysTenant.TenantName
} else {
params["tenant_name"] = sysTenant.TenantName
}
if sysTenant.Ancestors != "" {
params["ancestors"] = sysTenant.Ancestors
@@ -291,7 +329,7 @@ func (r *SysTenantImpl) InsertTenant(sysTenant model.SysTenant) string {
return insertedID
}
// UpdateTenant 修改部门信息
// UpdateTenant 修改租户信息
func (r *SysTenantImpl) UpdateTenant(sysTenant model.SysTenant) int64 {
// 参数拼接
params := make(map[string]any)
@@ -335,7 +373,7 @@ func (r *SysTenantImpl) UpdateTenant(sysTenant model.SysTenant) int64 {
return rows
}
// UpdateTenantStatusNormal 修改所在部门正常状态
// UpdateTenantStatusNormal 修改所在租户正常状态
func (r *SysTenantImpl) UpdateTenantStatusNormal(tenantIds []string) int64 {
placeholder := repo.KeyPlaceholderByQuery(len(tenantIds))
sql := "update sys_tenant set status = '1' where tenant_id in (" + placeholder + ")"
@@ -375,7 +413,7 @@ func (r *SysTenantImpl) UpdateTenantChildren(sysTenants []model.SysTenant) int64
return results
}
// DeleteTenantById 删除部门管理信息
// DeleteTenantById 删除租户管理信息
func (r *SysTenantImpl) DeleteTenantById(tenantId string) int64 {
sql := "update sys_tenant set status = '0', del_flag = '1' where tenant_id = ?"
results, err := datasource.ExecDB("", sql, []any{tenantId})

View File

@@ -25,6 +25,9 @@ type ISysTenant interface {
// CheckUniqueTenantName 校验同级租户名称是否唯一
CheckUniqueTenantName(tenantName, parentId, tenantId string) bool
// check unique tenancy_type and tenancy_key
IsUniqueTenancy(tenancyType, tenancyKey string) bool
// InsertTenant 新增租户信息
InsertTenant(sysTenant model.SysTenant) string

View File

@@ -73,6 +73,14 @@ func (r *SysTenantImpl) CheckUniqueTenantName(tenantName, parentId, tenantId str
return uniqueId == ""
}
// CheckUniqueTenantName 校验同级部门名称是否唯一
func (r *SysTenantImpl) IsUniqueTenancy(tenancyType, tenancyKey string) bool {
return r.sysTenantRepository.IsUniqueTenancy(model.SysTenant{
TenancyType: tenancyType,
TenancyKey: tenancyKey,
})
}
// InsertTenant 新增部门信息
func (r *SysTenantImpl) InsertTenant(sysTenant model.SysTenant) string {
return r.sysTenantRepository.InsertTenant(sysTenant)