package dao import ( "time" ) // User 用户 type User struct { ID int64 `gorm:"primary_key" json:"id"` //编号 TenantId int `gorm:"type:int" json:"tenantId"` //租户ID Code string `gorm:"type:varchar(12)" json:"code"` //用户编号 Account string `gorm:"type:varchar(45)" json:"account"` //账号 Password string `gorm:"type:varchar(45)" json:"password"` //密码 Name string `gorm:"type:varchar(20)" json:"name"` //昵称 RealName string `gorm:"type:varchar(12)" json:"realName"` //真名 Avatar string `gorm:"type:varchar(500)" json:"avatar"` //头像 Email string `gorm:"type:varchar(45)" json:"email"` //邮箱 Phone string `gorm:"type:varchar(45)" json:"phone"` //手机 Birthday string `gorm:"type:datetime" json:"birthday"` //生日 Sex int `gorm:"type:smallint" json:"sex"` //生日 RoleId int64 `gorm:"type:bigint" json:"roleId"` //角色id 数组,分隔 CreateUser int64 `gorm:"type:bigint" json:"createUser"` //创建人 CreateDept int64 `gorm:"type:bigint" json:"createDept"` //创建部门 CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间 UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改人 UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间 Status int `gorm:"type:int " json:"status"` //状态 IsDeleted int `gorm:"type:int" json:"isDeleted"` //是否删除 0=未删除,1=删除 GroupId int `gorm:"type:int" json:"groupId"` //用户分组id BigScreenIndexCameraIds string `gorm:"type:varchar(255)" json:"bigScreenIndexCameraIds"` //数据大屏中摄像头保存位置 SecuritySixScreen string `gorm:"type:varchar(255)" json:"securitySixScreen"` //安防页面六分屏 } func (User) TableName() string { return "user" } func (c *User) GetUser() error { return Db.Debug().Model(&c).Where("id = ? and is_deleted = 0", c.ID).Find(&c).Error } func (c *User) GetUserByTenantId() error { return Db.Debug().Model(&c).Where("tenant_id = ? and is_deleted = 0", c.TenantId).Find(&c).Error } func (c *User) GetUserByPwd() error { return Db.Debug().Model(&c).Where("tenant_id = ? and account = ? and password = ? and is_deleted = 0", c.TenantId, c.Account, c.Password).Find(&c).Error } func (c User) GetUsers(offset, limit int) ([]User, error) { var users []User db := Db.Debug().Model(&c) if c.Account != "" { db = db.Where("account like ?", "%"+c.Account+"%") } if c.RealName != "" { db = db.Where("real_name like ?", "%"+c.RealName+"%") } err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&users).Error return users, err } func (c *User) Save() error { return Db.Debug().Model(&c).Save(&c).Error } func (c *User) Update() error { return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error } func (c *User) Remove() error { return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime, "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error } func (c *User) UpdatePwd(pwd string) error { return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"password": pwd}).Error } func (c *User) GetAll() ([]User, error) { var users []User err := Db.Debug().Model(&c).Where("is_deleted = 0").Find(&users).Error return users, err } func (c *User) UpdateRoles(userIds []string, roleIds string) error { err := Db.Debug().Model(&c).Where("id in ?", userIds).Updates(map[string]interface{}{"role_id": roleIds}).Error return err }