user.go 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package dao
  2. import (
  3. "time"
  4. )
  5. // User 用户
  6. type User struct {
  7. ID int64 `gorm:"primary_key" json:"id"` //编号
  8. TenantId string `gorm:"type:varchar(12);default '000000'" json:"tenantId"` //租户id
  9. Code string `gorm:"type:varchar(12)" json:"code"` //用户编号
  10. Account string `gorm:"type:varchar(45)" json:"account"` //账号
  11. Password string `gorm:"type:varchar(45)" json:"password"` //密码
  12. Name string `gorm:"type:varchar(20)" json:"name"` //昵称
  13. RealName string `gorm:"type:varchar(12)" json:"realName"` //真名
  14. Avatar string `gorm:"type:varchar(500)" json:"avatar"` //头像
  15. Email string `gorm:"type:varchar(45)" json:"email"` //邮箱
  16. Phone string `gorm:"type:varchar(45)" json:"phone"` //手机
  17. Birthday string `gorm:"type:datetime" json:"birthday"` //生日
  18. Sex int `gorm:"type:smallint" json:"sex"` //生日
  19. RoleId string `gorm:"type:varchar(1000)" json:"roleId"` //角色id 数组,分隔
  20. DeptId string `gorm:"type:varchar(1000)" json:"deptId"` //部门id 数组,分隔
  21. PostId string `gorm:"type:varchar(1000)" json:"postId"` //岗位id 数组,分隔
  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. GroupId int `gorm:"type:int" json:"groupId"` //用户分组id
  30. BigScreenIndexCameraIds string `gorm:"type:varchar(255)" json:"bigScreenIndexCameraIds"` //数据大屏中摄像头保存位置
  31. SecuritySixScreen string `gorm:"type:varchar(255)" json:"securitySixScreen"` //安防页面六分屏
  32. }
  33. func (User) TableName() string {
  34. return "user"
  35. }
  36. func (c *User) GetUser() error {
  37. return Db.Model(&c).Where("id = ?", c.ID).Find(&c).Error
  38. }
  39. func (c *User) GetUserByPwd() error {
  40. return Db.Model(&c).Where("tenant_id = ? and account = ? and password = ?", c.TenantId, c.Account,
  41. c.Password).Find(&c).Error
  42. }
  43. func (c User) GetUsers(offset, limit int) ([]User, error) {
  44. var users []User
  45. db := Db.Model(&c)
  46. if c.Account != "" {
  47. db = db.Where("account like ?", "%"+c.Account+"%")
  48. }
  49. if c.RealName != "" {
  50. db = db.Where("real_name like ?", "%"+c.RealName+"%")
  51. }
  52. err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&users).Error
  53. return users, err
  54. }
  55. func (c *User) Save() error {
  56. return Db.Model(&c).Save(&c).Error
  57. }
  58. func (c *User) Update() error {
  59. return Db.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
  60. }
  61. func (c *User) Remove() error {
  62. return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  63. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  64. }
  65. func (c *User) UpdatePwd(pwd string) error {
  66. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"password": pwd}).Error
  67. }
  68. func (c *User) GetAll() ([]User, error) {
  69. var users []User
  70. err := Db.Model(&c).Find(&users).Error
  71. return users, err
  72. }
  73. func (c *User) UpdateRoles(userIds []string, roleIds string) error {
  74. err := Db.Model(&c).Where("id in ?", userIds).Updates(map[string]interface{}{"role_id": roleIds}).Error
  75. return err
  76. }