tenant.go 4.3 KB

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