46 lines
984 B
Go
46 lines
984 B
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"taotie-api/common"
|
|
"taotie-api/core"
|
|
"taotie-api/model/do/tenantdo"
|
|
"taotie-api/model/po"
|
|
"taotie-api/repo"
|
|
|
|
"github.com/gookit/goutil/errorx"
|
|
)
|
|
|
|
type TenantService struct {
|
|
cfg *core.Configuration
|
|
tenantRepo *repo.TenantRepo[*po.TTenant]
|
|
}
|
|
|
|
func NewTenantService(cfg *core.Configuration, tenantRepo *repo.TenantRepo[*po.TTenant]) *TenantService {
|
|
return &TenantService{cfg, tenantRepo}
|
|
}
|
|
|
|
func (rp *TenantService) Create(ctx context.Context, in *tenantdo.CreateIn) (*tenantdo.CreateOut, error) {
|
|
// 查重
|
|
tenant, err := rp.tenantRepo.FindOneByTenantName(ctx, in.TenantName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if tenant != nil {
|
|
return nil, errorx.With(common.ErrSysDuplicate, "租户名称重复")
|
|
}
|
|
|
|
// 创建租户
|
|
tenant = &po.TTenant{
|
|
TenantName: in.TenantName,
|
|
}
|
|
tenantId, err := rp.tenantRepo.Create(ctx, tenant)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &tenantdo.CreateOut{
|
|
Id: tenantId,
|
|
}, nil
|
|
}
|