package dao

import (
	"fmt"
	"time"
)

// 租户
type Tenant struct {
	ID               int64     `gorm:"primary_key" json:"id"`                    //编号
	TenantId         string    `gorm:"type:varchar(12)" json:"tenantId"`         //租户ID
	TenantName       string    `gorm:"type:varchar(50)" json:"tenantName"`       //租户名称
	Domain           string    `gorm:"type:varchar(255)" json:"domain"`          //域名地址
	BackgroundUrl    string    `gorm:"type:varchar(1000)" json:"backgroundUrl"`  //系统背景
	SysLogoUrl       string    `gorm:"type:varchar(1000)" json:"sysLogoUrl"`     //logo图表
	Linkman          string    `gorm:"type:varchar(20)" json:"linkman"`          //联系人
	ContactNumber    string    `gorm:"type:varchar(20)" json:"contactNumber"`    //联系电话
	Address          string    `gorm:"type:varchar(255)" json:"address"`         //联系地址
	AccountNumber    int       `gorm:"type:int;default -1" json:"accountNumber"` //邮箱
	ExpireTime       time.Time `gorm:"type:datetime" json:"expireTime"`          //过期时间
	DatasourceId     int64     `gorm:"type:bigint" json:"datasourceId"`          //数据源
	LicenseKey       string    `gorm:"type:varchar(1000)" json:"licenseKey"`     //授权码
	BigScreenName    string    `gorm:"type:varchar(60)" json:"bigScreenName"`    //大屏端显示名称
	CreateUser       int64     `gorm:"type:bigint" json:"createUser"`            //创建人
	CreateDept       int64     `gorm:"type:bigint" json:"createDept"`            //创建部门
	CreateTime       time.Time `gorm:"type:datetime" json:"createTime"`          //新增时间
	UpdateUser       int64     `gorm:"type:bigint" json:"updateUser"`            //修改人
	UpdateTime       time.Time `gorm:"type:datetime" json:"updateTime"`          //修改时间
	Status           int       `gorm:"type:int  " json:"status"`                 //状态
	IsDeleted        int       `gorm:"type:int" json:"isDeleted"`                //是否删除 0=未删除,1=删除
	PassKey          string    `gorm:"type:varchar(10)" json:"passKey"`          //租户登录Key
	LoginDisplayName string    `gorm:"type:varchar(60)" json:"loginDisplayName"` //登录页面显示名称
	ContactEmail     string    `gorm:"type:varchar(255)" json:"contactEmail"`    //联系邮箱
	CityCode         string    `gorm:"type:varchar(6)" json:"cityCode"`          //城市行政编码
	CityName         string    `gorm:"type:varchar(255)" json:"cityName"`        //城市名称
	CityLng          float64   `gorm:"type:double(18, 14)" json:"cityLng"`       //经度
	CityLat          float64   `gorm:"type:double(18, 14)" json:"cityLat"`       //经度
}

func (Tenant) TableName() string {
	return "tenant"
}

func (c *Tenant) Get() error {
	return Db.Debug().Model(&c).Where("id = ? and is_deleted = 0", c.ID).Find(&c).Error
}

func (c *Tenant) GetByDomain() error {
	return Db.Debug().Model(&c).Where("domain = ? and is_deleted = 0", c.Domain).Find(&c).Error
}

func (c *Tenant) GetTenant() error {
	return Db.Debug().Model(&c).Where("tenant_id = ? and is_deleted = 0", c.TenantId).First(&c).Error
}

func (c *Tenant) GetTenantByPasskey() error {
	return Db.Debug().Model(&c).Where("pass_key = ? and is_deleted = 0", c.PassKey).Find(&c).Error
}

func (c *Tenant) IsExist() bool {
	var count int64
	_ = Db.Debug().Model(&c).Where("pass_key = ? and is_deleted = 0", c.PassKey).Count(&count).Error
	return count > 0
}

func (c *Tenant) GetTenants(offset, limit int) ([]Tenant, int64, error) {
	var tenants []Tenant
	var counts int64
	db := Db.Debug().Model(&c)
	if c.TenantId == "" {
		//db = db.Where("tenant_id = ?", c.TenantId)
	}
	if c.TenantName != "" {
		db = db.Where("tenant_name like ?", "%"+c.TenantName+"%")
	}
	if c.Linkman != "" {
		db = db.Where("linkman like ?", "%"+c.Linkman+"%")
	}
	db.Count(&counts)
	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&tenants).Error
	return tenants, counts, err
}

func (c *Tenant) Save() error {
	return Db.Debug().Model(&c).Save(&c).Error
}

func (c *Tenant) Update() error {
	return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
}

func (c *Tenant) GetAll() ([]Tenant, error) {
	var tenants []Tenant
	err := Db.Debug().Model(&c).Where("is_deleted = 0").Find(&tenants).Error
	fmt.Printf("[]Tenant:%v\n", tenants)
	return tenants, err
}

func (c *Tenant) Remove() error {
	return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.
		UpdateTime, "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
}
