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 int64) (*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 int64) *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
}
