user.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package dao
  2. import (
  3. "time"
  4. )
  5. // User 用户
  6. type User struct {
  7. ID int `gorm:"primary_key;type:int" json:"id"` //编号
  8. UserName string `gorm:"type:varchar(12)" json:"userName"` //姓名
  9. Sex int `gorm:"type:smallint;default:1" json:"sex"` //性别 //用户编号
  10. Account string `gorm:"type:varchar(45)" json:"account"` //账号
  11. Password string `gorm:"type:text" json:"password"` //密码
  12. Avatar string `gorm:"type:varchar(500)" json:"avatar"` //头像
  13. Email string `gorm:"type:varchar(45)" json:"email"` //邮箱
  14. Phone string `gorm:"type:varchar(45)" json:"phone"` //手机
  15. RoleName string `gorm:"type:varchar(12)" json:"roleName"` //角色名
  16. Birthday time.Time `gorm:"type:datetime" json:"birthday"` //生日
  17. DeptId int `gorm:"type:int" json:"deptId"` //部门id
  18. Status int `gorm:"type:int;default:0" json:"status"` //状态 0=可用,1禁用
  19. IsDeleted int `gorm:"type:int" json:"isDeleted"` //是否删除 0=未删除,1=删除
  20. CreateTime time.Time `gorm:"autoCreateTime;column:create_time;type:datetime" json:"createTime"` //新增时间
  21. AuthId string `gorm:"type:varchar(12);default:'100'" json:"authId"` //权限id
  22. }
  23. func (*User) TableName() string {
  24. return "user"
  25. }
  26. func (c *User) LoginFindAccount(acc string) (*User, error) {
  27. var user User
  28. err := Db.Debug().Model(&user).Where("account = ? and status = 0", acc).First(&user).Error
  29. return &user, err
  30. }
  31. func (c *User) GetUsers() ([]User, error) {
  32. var users []User
  33. err := Db.Model(&c).Find(&users).Error
  34. return users, err
  35. }
  36. func (c *User) GetNowUser(id int) (*User, error) {
  37. err := Db.Model(&c).Where("id = ?", id).First(&c).Error
  38. return c, err
  39. }
  40. func (c *User) GetDeptsUsers(depts []int) ([]User, error) {
  41. var users []User
  42. err := Db.Model(&c).Where("dept_id in ?", depts).Find(&users).Error
  43. return users, err
  44. }
  45. //func (c *User) GetUsers1(id int) ([]User, error) {
  46. // var users []User
  47. // err := Db.Model(&c).Where("dept_id = ?", id).Find(&users).Error
  48. // return users, err
  49. //}
  50. //func (c *User) GetUser() error {
  51. // return Db.Model(&c).Where(" is_deleted = 0").Find(&c).Error
  52. //}
  53. //
  54. //func (c *User) GetUserByTenantId() error {
  55. // return Db.Model(&c).Where("tenant_id = ? and is_deleted = 0", c.TenantId).Find(&c).Error
  56. //}
  57. //
  58. //func (c *User) GetUserByPwd() error {
  59. // return Db.Model(&c).Where("tenant_id = ? and account = ? and password = ? and is_deleted = 0", c.TenantId, c.Account, c.Password).First(&c).Error
  60. //}
  61. //
  62. //func (c *User) GetUsers(offset, limit int) ([]User, int, error) {
  63. // var users []User
  64. // var counts int64
  65. // db := Db.Model(&c)
  66. //
  67. // if c.Account != "" {
  68. // db = db.Where("account like ?", "%"+c.Account+"%")
  69. // }
  70. // if c.RealName != "" {
  71. // db = db.Where("real_name like ?", "%"+c.RealName+"%")
  72. // }
  73. // err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&users).Error
  74. // db1 := Db.Model(&c)
  75. // db1.Where("is_deleted = 0").Count(&counts)
  76. // return users, int(counts), err
  77. //}
  78. //
  79. //func (c *User) Save() error {
  80. // return Db.Model(&c).Save(&c).Error
  81. //}
  82. //
  83. //func (c *User) Update() error {
  84. // return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  85. //}
  86. //
  87. //func (c *User) Remove() error {
  88. // return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime, "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  89. //}
  90. //
  91. //func (c *User) UpdatePwd(pwd string) error {
  92. // return Db.Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"password": pwd}).Error
  93. //}
  94. //
  95. //func (c *User) GetAll() ([]User, error) {
  96. // var users []User
  97. // err := Db.Model(&c).Where("is_deleted = 0").Find(&users).Error
  98. // return users, err
  99. //}
  100. //
  101. //func (c *User) UpdateRoles(userIds []string, roleIds string) error {
  102. // err := Db.Model(&c).Where("id in ?", userIds).Updates(map[string]interface{}{"role_id": roleIds}).Error
  103. // return err
  104. //}
  105. //
  106. //// IsExist :account是否已存在 true为存在,false不存在
  107. //func (c *User) IsExist() bool {
  108. // var s string
  109. // Db.Model(&c).Select("account").Where("account = ?", c.Account).First(&s)
  110. // return s != ""
  111. //}
  112. //
  113. //// GetAvatar 查 avatar
  114. //func (c *User) GetAvatar() {
  115. // tx := Db.Model(&c)
  116. // tx.Select("avatar").Where("tenant_id = ?", c.TenantId).Find(&c)
  117. //}