tenant.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dao
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // 租户
  7. type Tenant struct {
  8. ID int64 `gorm:"primary_key" json:"id"` //编号
  9. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户ID
  10. TenantName string `gorm:"type:varchar(50)" json:"tenantName"` //租户名称
  11. Domain string `gorm:"type:varchar(255)" json:"domain"` //域名地址
  12. BackgroundUrl string `gorm:"type:varchar(1000)" json:"backgroundUrl"` //系统背景
  13. SysLogoUrl string `gorm:"type:varchar(1000)" json:"sysLogoUrl"` //logo图表
  14. Linkman string `gorm:"type:varchar(20)" json:"linkman"` //联系人
  15. ContactNumber string `gorm:"type:varchar(20)" json:"contactNumber"` //联系电话
  16. Address string `gorm:"type:varchar(255)" json:"address"` //联系地址
  17. AccountNumber int `gorm:"type:int;default -1" json:"accountNumber"` //邮箱
  18. ExpireTime time.Time `gorm:"type:datetime" json:"expireTime"` //过期时间
  19. DatasourceId int64 `gorm:"type:bigint" json:"datasourceId"` //数据源
  20. LicenseKey string `gorm:"type:varchar(1000)" json:"licenseKey"` //授权码
  21. BigScreenName string `gorm:"type:varchar(60)" json:"bigScreenName"` //大屏端显示名称
  22. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //创建人
  23. CreateDept int64 `gorm:"type:bigint" json:"createDept"` //创建部门
  24. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  25. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改人
  26. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  27. Status int `gorm:"type:int " json:"status"` //状态
  28. IsDeleted int `gorm:"type:int" json:"isDeleted"` //是否删除 0=未删除,1=删除
  29. PassKey string `gorm:"type:varchar(10)" json:"passKey"` //租户登录Key
  30. LoginDisplayName string `gorm:"type:varchar(60)" json:"loginDisplayName"` //登录页面显示名称
  31. ContactEmail string `gorm:"type:varchar(255)" json:"contactEmail"` //联系邮箱
  32. CityCode string `gorm:"type:varchar(6)" json:"cityCode"` //城市行政编码
  33. CityName string `gorm:"type:varchar(255)" json:"cityName"` //城市名称
  34. CityLng float64 `gorm:"type:double(18, 14)" json:"cityLng"` //经度
  35. CityLat float64 `gorm:"type:double(18, 14)" json:"cityLat"` //经度
  36. }
  37. func (Tenant) TableName() string {
  38. return "tenant"
  39. }
  40. func (c *Tenant) Get() error {
  41. return Db.Debug().Model(&c).Where("id = ? and is_deleted = 0", c.ID).Find(&c).Error
  42. }
  43. func (c *Tenant) GetByDomain() error {
  44. return Db.Debug().Model(&c).Where("domain = ? and is_deleted = 0", c.Domain).Find(&c).Error
  45. }
  46. func (c *Tenant) GetTenant() error {
  47. return Db.Debug().Model(&c).Where("tenant_id = ? and is_deleted = 0", c.TenantId).First(&c).Error
  48. }
  49. func (c *Tenant) GetTenantByPasskey() error {
  50. return Db.Debug().Model(&c).Where("pass_key = ? and is_deleted = 0", c.PassKey).Find(&c).Error
  51. }
  52. func (c *Tenant) IsExist() bool {
  53. var count int64
  54. _ = Db.Debug().Model(&c).Where("pass_key = ? and is_deleted = 0", c.PassKey).Count(&count).Error
  55. return count > 0
  56. }
  57. func (c *Tenant) GetTenants(offset, limit int) ([]Tenant, int64, error) {
  58. var tenants []Tenant
  59. var counts int64
  60. db := Db.Debug().Model(&c)
  61. db.Count(&counts)
  62. if c.TenantId == "" {
  63. db = db.Where("tenant_id = ?", c.TenantId)
  64. }
  65. if c.TenantName != "" {
  66. db = db.Where("tenant_name like ?", "%"+c.TenantName+"%")
  67. }
  68. if c.Linkman != "" {
  69. db = db.Where("linkman like ?", "%"+c.Linkman+"%")
  70. }
  71. err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&tenants).Error
  72. return tenants, counts, err
  73. }
  74. func (c *Tenant) Save() error {
  75. return Db.Debug().Model(&c).Save(&c).Error
  76. }
  77. func (c *Tenant) Update() error {
  78. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  79. }
  80. func (c *Tenant) GetAll() ([]Tenant, error) {
  81. var tenants []Tenant
  82. err := Db.Debug().Model(&c).Where("is_deleted = 0").Find(&tenants).Error
  83. fmt.Printf("[]Tenant:%v\n", tenants)
  84. return tenants, err
  85. }
  86. func (c *Tenant) Remove() error {
  87. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.
  88. UpdateTime, "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  89. }