123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package service
- import (
- "fmt"
- "iot_manager_service/app/system/dao"
- "iot_manager_service/util/cache"
- "iot_manager_service/util/common"
- "iot_manager_service/util/logger"
- "time"
- )
- // 租户管理服务
- var TenantService = new(tenantService)
- type tenantService struct{}
- func (s *tenantService) GetTenantByPasskey(passKey string) (*dao.Tenant, error) {
- tenant := &dao.Tenant{
- PassKey: passKey,
- }
- err := tenant.GetTenantByPasskey()
- if err != nil {
- return nil, err
- }
- return tenant, nil
- }
- func (s *tenantService) GetOne(tenantId string) (*dao.Tenant, error) {
- tenant := &dao.Tenant{
- TenantId: tenantId,
- }
- err := tenant.GetTenant()
- if err != nil {
- return nil, err
- }
- return tenant, nil
- }
- func (s *tenantService) Get(id int) (*dao.Tenant, error) {
- tenant := &dao.Tenant{
- ID: id,
- }
- err := tenant.Get()
- if err != nil {
- return nil, err
- }
- return tenant, nil
- }
- func (s *tenantService) GetByDomain(domain string) (*dao.Tenant, error) {
- tenant := &dao.Tenant{
- Domain: domain,
- }
- err := tenant.GetByDomain()
- if err != nil {
- return nil, err
- }
- return tenant, nil
- }
- func (s *tenantService) List(tenantId string, tenantName, linkman string, current, size int) ([]dao.Tenant, int64, error) {
- tenant := &dao.Tenant{}
- offset := (current - 1) * size
- limit := size
- if tenantId == "" {
- tenant.TenantId = tenantId
- }
- if tenantName != "" {
- tenant.TenantName = tenantName
- }
- if linkman != "" {
- tenant.Linkman = linkman
- }
- tenants, counts, err := tenant.GetTenants(offset, limit)
- if err != nil {
- return nil, 0, err
- }
- return tenants, counts, nil
- }
- func (s *tenantService) Submit(req dao.Tenant) *common.Errors {
- tenant := &req
- if tenant.ID == 0 {
- tenant.PassKey = common.RandomString(8)
- if tenant.IsExist() {
- tenant.PassKey = common.RandomString(8)
- }
- err := tenant.Save()
- if err != nil {
- return common.FailResponse(err.Error(), nil)
- }
- return common.SuccessResponse(common.Succeeded, nil)
- }
- err := tenant.Update()
- if err != nil {
- return common.FailResponse(err.Error(), nil)
- }
- return common.SuccessResponse(common.Succeeded, nil)
- }
- func (s *tenantService) GetList() ([]dao.Tenant, error) {
- tenant := &dao.Tenant{}
- tenants, err := tenant.GetAll()
- if err != nil {
- return nil, err
- }
- return tenants, nil
- }
- func (s *tenantService) Remove(userId, id int) *common.Errors {
- tenant := &dao.Tenant{
- ID: id,
- IsDeleted: 1,
- UpdateUser: userId,
- UpdateTime: time.Now(),
- }
- err := tenant.Remove()
- if err != nil {
- return common.FailResponse(err.Error(), nil)
- }
- return nil
- }
- const TenantNameKey = "%d:sys:tenant_name:%d"
- func (s *tenantService) GetTenantName(tenantId string, id string) string {
- var name string
- key := fmt.Sprintf(TenantNameKey, tenantId, id)
- name, err := cache.Redis.Get(key).Result()
- if err != nil {
- tenant := &dao.Tenant{TenantId: id}
- if err := tenant.GetTenant(); err != nil {
- logger.Logger.Errorf("GetTenant err = %s \n", err)
- } else {
- name = tenant.TenantName
- cache.Redis.Set(key, name, -1)
- }
- }
- return name
- }
|