package models import "time" // TenantUser 租户用户绑定关系表 yz_system_tenant_user type TenantUser struct { ID uint64 `orm:"column(id);pk;auto" json:"id"` Tid uint64 `orm:"column(tid)" json:"tid"` // 租户ID Uid uint64 `orm:"column(uid)" json:"uid"` // 用户ID Account *string `orm:"column(account);size(64);null" json:"account"` // 用户账号(冗余) Name *string `orm:"column(name);size(64);null" json:"name"` // 用户名称(冗余) Phone *string `orm:"column(phone);size(20);null" json:"phone"` // 手机号(冗余) Email *string `orm:"column(email);size(128);null" json:"email"` // 邮箱(冗余) Password *string `orm:"column(password);size(255);null" json:"password"` // 密码(冗余/可选) IsDefault int8 `orm:"column(is_default);default(0)" json:"is_default"` // 是否默认租户 Status int8 `orm:"column(status);default(1)" json:"status"` // 状态:1启用,0禁用 Remark *string `orm:"column(remark);size(255);null" json:"remark"` CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"create_time"` UpdateTime *time.Time `orm:"column(update_time);auto_now;type(datetime);null" json:"update_time"` DeleteTime *time.Time `orm:"column(delete_time);type(datetime);null" json:"delete_time"` } // TableName 自定义表名 func (m *TenantUser) TableName() string { return "yz_system_tenant_user" } // BindTenantUser 绑定用户到租户(若已存在则更新状态/默认值) func BindTenantUser(tid, uid uint64, account, name, phone, email, password *string, isDefault, status int8, remark *string) (uint64, error) { var existed TenantUser err := Orm.QueryTable(new(TenantUser)). Filter("tid", tid). Filter("uid", uid). One(&existed) if err == nil { update := map[string]interface{}{ "account": account, "name": name, "phone": phone, "email": email, "password": password, "status": status, "is_default": isDefault, "remark": remark, } _, uErr := Orm.QueryTable(new(TenantUser)).Filter("id", existed.ID).Update(update) return existed.ID, uErr } m := &TenantUser{ Tid: tid, Uid: uid, Account: account, Name: name, Phone: phone, Email: email, Password: password, IsDefault: isDefault, Status: status, Remark: remark, } id, iErr := Orm.Insert(m) return uint64(id), iErr } // UnbindTenantUser 删除绑定关系 func UnbindTenantUser(id uint64) error { _, err := Orm.QueryTable(new(TenantUser)).Filter("id", id).Delete() return err } // ListTenantUsersByTid 根据租户ID查询绑定关系 func ListTenantUsersByTid(tid uint64) ([]TenantUser, error) { var rows []TenantUser _, err := Orm.QueryTable(new(TenantUser)). Filter("tid", tid). OrderBy("-is_default", "-id"). All(&rows) return rows, err } // ListTenantBindingsByUid 根据用户ID查询绑定关系 func ListTenantBindingsByUid(uid uint64) ([]TenantUser, error) { var rows []TenantUser _, err := Orm.QueryTable(new(TenantUser)). Filter("uid", uid). OrderBy("-is_default", "-id"). All(&rows) return rows, err } // SetDefaultTenant 设置用户默认租户(同一用户仅一个默认) func SetDefaultTenant(uid, tid uint64) error { _, err := Orm.QueryTable(new(TenantUser)).Filter("uid", uid).Update(map[string]interface{}{ "is_default": 0, }) if err != nil { return err } _, err = Orm.QueryTable(new(TenantUser)). Filter("uid", uid). Filter("tid", tid). Update(map[string]interface{}{"is_default": 1}) return err }