Files
EdgeAPI/internal/db/models/user_dao.go

425 lines
10 KiB
Go
Raw Normal View History

2020-07-22 22:17:53 +08:00
package models
import (
"encoding/json"
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
2021-07-11 18:05:57 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
2021-11-28 20:11:36 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
2020-07-22 22:17:53 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string"
2021-07-11 18:05:57 +08:00
timeutil "github.com/iwind/TeaGo/utils/time"
2020-07-22 22:17:53 +08:00
)
const (
UserStateEnabled = 1 // 已启用
UserStateDisabled = 0 // 已禁用
)
type UserDAO dbs.DAO
func NewUserDAO() *UserDAO {
return dbs.NewDAO(&UserDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeUsers",
Model: new(User),
PkName: "id",
},
}).(*UserDAO)
}
2020-10-13 20:05:13 +08:00
var SharedUserDAO *UserDAO
func init() {
dbs.OnReady(func() {
SharedUserDAO = NewUserDAO()
})
}
2020-07-22 22:17:53 +08:00
2021-05-27 17:09:07 +08:00
// EnableUser 启用条目
func (this *UserDAO) EnableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
return this.Query(tx).
2020-07-22 22:17:53 +08:00
Pk(id).
Set("state", UserStateEnabled).
Update()
}
2021-05-27 17:09:07 +08:00
// DisableUser 禁用条目
func (this *UserDAO) DisableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
return this.Query(tx).
2020-07-22 22:17:53 +08:00
Pk(id).
Set("state", UserStateDisabled).
Update()
}
// FindEnabledUser 查找启用的用户
2021-11-11 14:16:42 +08:00
func (this *UserDAO) FindEnabledUser(tx *dbs.Tx, userId int64, cacheMap *utils.CacheMap) (*User, error) {
if cacheMap == nil {
2021-11-11 14:16:42 +08:00
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":FindEnabledUser:" + types.String(userId)
2021-11-11 14:16:42 +08:00
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*User), nil
}
result, err := this.Query(tx).
Pk(userId).
2020-07-22 22:17:53 +08:00
Attr("state", UserStateEnabled).
Find()
if result == nil {
return nil, err
}
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
2020-07-22 22:17:53 +08:00
return result.(*User), err
}
2020-11-10 20:30:55 +08:00
2021-05-27 17:09:07 +08:00
// FindEnabledBasicUser 查找用户基本信息
func (this *UserDAO) FindEnabledBasicUser(tx *dbs.Tx, id int64) (*User, error) {
result, err := this.Query(tx).
2020-12-07 11:46:02 +08:00
Pk(id).
Attr("state", UserStateEnabled).
Result("id", "fullname", "username").
Find()
if result == nil {
return nil, err
}
return result.(*User), err
2022-01-23 19:16:52 +08:00
}
// FindBasicUserWithoutState 查找用户基本信息,并忽略状态
func (this *UserDAO) FindBasicUserWithoutState(tx *dbs.Tx, id int64) (*User, error) {
result, err := this.Query(tx).
Pk(id).
Result("id", "fullname", "username").
Find()
if result == nil {
return nil, err
}
return result.(*User), err
2020-12-07 11:46:02 +08:00
}
2021-05-27 17:09:07 +08:00
// FindUserFullname 获取管理员名称
func (this *UserDAO) FindUserFullname(tx *dbs.Tx, userId int64) (string, error) {
return this.Query(tx).
2020-11-10 20:30:55 +08:00
Pk(userId).
Result("fullname").
FindStringCol("")
}
2021-05-27 17:09:07 +08:00
// CreateUser 创建用户
2022-01-05 10:45:19 +08:00
func (this *UserDAO) CreateUser(tx *dbs.Tx, username string,
password string,
fullname string,
mobile string,
tel string,
email string,
remark string,
source string,
clusterId int64,
features []string,
registeredIP string,
isVerified bool) (int64, error) {
var op = NewUserOperator()
op.Username = username
op.Password = stringutil.Md5(password)
op.Fullname = fullname
op.Mobile = mobile
op.Tel = tel
op.Email = email
2022-01-05 10:45:19 +08:00
op.EmailIsVerified = false
op.Remark = remark
op.Source = source
op.ClusterId = clusterId
2021-07-11 18:05:57 +08:00
op.Day = timeutil.Format("Ymd")
2022-01-05 10:45:19 +08:00
op.IsVerified = isVerified
op.RegisteredIP = registeredIP
// features
if len(features) == 0 {
op.Features = "[]"
} else {
featuresJSON, err := json.Marshal(features)
if err != nil {
return 0, err
}
op.Features = featuresJSON
}
op.IsOn = true
op.State = UserStateEnabled
err := this.Save(tx, op)
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}
2021-05-27 17:09:07 +08:00
// UpdateUser 修改用户
func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, password string, fullname string, mobile string, tel string, email string, remark string, isOn bool, nodeClusterId int64) error {
if userId <= 0 {
return errors.New("invalid userId")
}
var op = NewUserOperator()
op.Id = userId
op.Username = username
if len(password) > 0 {
op.Password = stringutil.Md5(password)
}
op.Fullname = fullname
op.Mobile = mobile
op.Tel = tel
op.Email = email
op.Remark = remark
op.ClusterId = nodeClusterId
op.IsOn = isOn
err := this.Save(tx, op)
return err
}
2021-05-27 17:09:07 +08:00
// UpdateUserInfo 修改用户基本信息
2022-01-05 10:45:19 +08:00
func (this *UserDAO) UpdateUserInfo(tx *dbs.Tx, userId int64, fullname string, mobile string, email string) error {
2020-12-15 11:52:57 +08:00
if userId <= 0 {
return errors.New("invalid userId")
}
var op = NewUserOperator()
2020-12-15 11:52:57 +08:00
op.Id = userId
op.Fullname = fullname
2022-01-05 10:45:19 +08:00
op.Mobile = mobile
op.Email = email
return this.Save(tx, op)
2020-12-15 11:52:57 +08:00
}
2021-05-27 17:09:07 +08:00
// UpdateUserLogin 修改用户登录信息
func (this *UserDAO) UpdateUserLogin(tx *dbs.Tx, userId int64, username string, password string) error {
2020-12-15 11:52:57 +08:00
if userId <= 0 {
return errors.New("invalid userId")
}
var op = NewUserOperator()
2020-12-15 11:52:57 +08:00
op.Id = userId
op.Username = username
if len(password) > 0 {
op.Password = stringutil.Md5(password)
}
err := this.Save(tx, op)
2020-12-15 11:52:57 +08:00
return err
}
2021-05-27 17:09:07 +08:00
// CountAllEnabledUsers 计算用户数量
func (this *UserDAO) CountAllEnabledUsers(tx *dbs.Tx, clusterId int64, keyword string, isVerifying bool) (int64, error) {
query := this.Query(tx)
query.State(UserStateEnabled)
2021-07-05 11:37:22 +08:00
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}
if len(keyword) > 0 {
query.Where("(username LIKE :keyword OR fullname LIKE :keyword OR mobile LIKE :keyword OR email LIKE :keyword OR tel LIKE :keyword OR remark LIKE :keyword)").
Param("keyword", dbutils.QuoteLike(keyword))
}
if isVerifying {
query.Attr("isVerified", 0)
}
return query.Count()
}
// CountAllVerifyingUsers 获取等待审核的用户数
func (this *UserDAO) CountAllVerifyingUsers(tx *dbs.Tx) (int64, error) {
query := this.Query(tx)
query.State(UserStateEnabled)
query.Attr("isVerified", 0)
return query.Count()
}
2021-05-27 17:09:07 +08:00
// ListEnabledUsers 列出单页用户
func (this *UserDAO) ListEnabledUsers(tx *dbs.Tx, clusterId int64, keyword string, isVerifying bool, offset int64, size int64) (result []*User, err error) {
query := this.Query(tx)
query.State(UserStateEnabled)
2021-07-05 11:37:22 +08:00
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}
if len(keyword) > 0 {
query.Where("(username LIKE :keyword OR fullname LIKE :keyword OR mobile LIKE :keyword OR email LIKE :keyword OR tel LIKE :keyword OR remark LIKE :keyword)").
Param("keyword", dbutils.QuoteLike(keyword))
}
if isVerifying {
query.Attr("isVerified", 0)
}
_, err = query.
DescPk().
2021-05-27 17:09:07 +08:00
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
2021-05-27 17:09:07 +08:00
// ExistUser 检查用户名是否存在
func (this *UserDAO) ExistUser(tx *dbs.Tx, userId int64, username string) (bool, error) {
return this.Query(tx).
State(UserStateEnabled).
Attr("username", username).
Neq("id", userId).
Exist()
}
2021-05-27 17:09:07 +08:00
// ListEnabledUserIds 列出单页的用户ID
func (this *UserDAO) ListEnabledUserIds(tx *dbs.Tx, offset, size int64) ([]int64, error) {
ones, _, err := this.Query(tx).
ResultPk().
State(UserStateEnabled).
Offset(offset).
Limit(size).
AscPk().
FindOnes()
if err != nil {
return nil, err
}
result := []int64{}
for _, one := range ones {
result = append(result, one.GetInt64("id"))
}
return result, nil
}
2020-12-14 21:25:11 +08:00
2021-05-27 17:09:07 +08:00
// CheckUserPassword 检查用户名、密码
func (this *UserDAO) CheckUserPassword(tx *dbs.Tx, username string, encryptedPassword string) (int64, error) {
2020-12-14 21:25:11 +08:00
if len(username) == 0 || len(encryptedPassword) == 0 {
return 0, nil
}
return this.Query(tx).
2020-12-14 21:25:11 +08:00
Attr("username", username).
Attr("password", encryptedPassword).
Attr("state", UserStateEnabled).
Attr("isOn", true).
ResultPk().
FindInt64Col(0)
}
2020-12-18 21:18:53 +08:00
2021-05-27 17:09:07 +08:00
// FindUserClusterId 查找用户所在集群
func (this *UserDAO) FindUserClusterId(tx *dbs.Tx, userId int64) (int64, error) {
return this.Query(tx).
2020-12-18 21:18:53 +08:00
Pk(userId).
Result("clusterId").
FindInt64Col(0)
}
2021-05-27 17:09:07 +08:00
// UpdateUserFeatures 更新用户Features
func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON []byte) error {
if userId <= 0 {
return errors.New("invalid userId")
}
if len(featuresJSON) == 0 {
featuresJSON = []byte("[]")
}
_, err := this.Query(tx).
Pk(userId).
Set("features", featuresJSON).
Update()
if err != nil {
return err
}
return nil
}
2021-05-27 17:09:07 +08:00
// FindUserFeatures 查找用户Features
2021-11-28 20:11:36 +08:00
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*userconfigs.UserFeature, error) {
featuresJSON, err := this.Query(tx).
Pk(userId).
Result("features").
FindStringCol("")
if err != nil {
return nil, err
}
if len(featuresJSON) == 0 {
return nil, nil
}
featureCodes := []string{}
err = json.Unmarshal([]byte(featuresJSON), &featureCodes)
if err != nil {
return nil, err
}
// 检查是否还存在以及设置名称
2021-11-28 20:11:36 +08:00
result := []*userconfigs.UserFeature{}
if len(featureCodes) > 0 {
for _, featureCode := range featureCodes {
2021-11-28 20:11:36 +08:00
f := userconfigs.FindUserFeature(featureCode)
if f != nil {
2021-11-28 20:11:36 +08:00
result = append(result, &userconfigs.UserFeature{Name: f.Name, Code: f.Code, Description: f.Description})
}
}
}
return result, nil
}
2021-07-11 18:05:57 +08:00
// SumDailyUsers 获取当天用户数量
func (this *UserDAO) SumDailyUsers(tx *dbs.Tx, dayFrom string, dayTo string) (int64, error) {
return this.Query(tx).
Between("day", dayFrom, dayTo).
State(UserStateEnabled).
Count()
}
// CountDailyUsers 计算每天用户数
func (this *UserDAO) CountDailyUsers(tx *dbs.Tx, dayFrom string, dayTo string) ([]*pb.ComposeUserGlobalBoardResponse_DailyStat, error) {
ones, _, err := this.Query(tx).
Result("COUNT(*) AS count", "day").
Between("day", dayFrom, dayTo).
State(UserStateEnabled).
Group("day").
FindOnes()
if err != nil {
return nil, err
}
var m = map[string]*pb.ComposeUserGlobalBoardResponse_DailyStat{} // day => Stat
for _, one := range ones {
m[one.GetString("day")] = &pb.ComposeUserGlobalBoardResponse_DailyStat{
Day: one.GetString("day"),
Count: one.GetInt64("count"),
}
}
var result = []*pb.ComposeUserGlobalBoardResponse_DailyStat{}
days, err := utils.RangeDays(dayFrom, dayTo)
if err != nil {
return nil, err
}
for _, day := range days {
stat, ok := m[day]
if ok {
result = append(result, stat)
} else {
result = append(result, &pb.ComposeUserGlobalBoardResponse_DailyStat{
Day: day,
Count: 0,
})
}
}
return result, nil
}
2022-01-05 10:45:19 +08:00
// UpdateUserIsVerified 审核用户
func (this *UserDAO) UpdateUserIsVerified(tx *dbs.Tx, userId int64, isRejected bool, rejectReason string) error {
if userId <= 0 {
return errors.New("invalid userId")
}
var op = NewUserOperator()
op.Id = userId
op.IsRejected = isRejected
op.RejectReason = rejectReason
op.IsVerified = true
return this.Save(tx, op)
}