2020-07-22 22:17:53 +08:00
package models
import (
2020-12-30 22:01:01 +08:00
"encoding/json"
2022-03-27 12:22:47 +08:00
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
2020-12-04 16:01:01 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/errors"
2021-07-11 18:05:57 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
2022-10-23 16:22:20 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2021-07-11 18:05:57 +08:00
"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"
2022-08-28 17:01:09 +08:00
"github.com/iwind/TeaGo/lists"
2023-01-13 18:51:50 +08:00
"github.com/iwind/TeaGo/rands"
2020-12-04 16:01:01 +08:00
"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 启用条目
2022-07-24 17:13:05 +08:00
func ( this * UserDAO ) EnableUser ( tx * dbs . Tx , userId int64 ) error {
if userId <= 0 {
return errors . New ( "invalid 'userId'" )
}
_ , err := this . Query ( tx ) .
Pk ( userId ) .
2020-07-22 22:17:53 +08:00
Set ( "state" , UserStateEnabled ) .
Update ( )
2022-10-23 16:22:20 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , userId )
2020-07-22 22:17:53 +08:00
}
2021-05-27 17:09:07 +08:00
// DisableUser 禁用条目
2022-07-24 17:13:05 +08:00
func ( this * UserDAO ) DisableUser ( tx * dbs . Tx , userId int64 ) error {
if userId <= 0 {
return errors . New ( "invalid 'userId'" )
}
2023-01-13 18:51:50 +08:00
// 处理以往同用户名用户
username , err := this . Query ( tx ) .
Pk ( userId ) .
Result ( "username" ) .
FindStringCol ( "" )
if err != nil {
return err
}
if len ( username ) > 0 {
err = this . Query ( tx ) .
Attr ( "username" , username ) .
Attr ( "state" , UserStateDisabled ) .
Set ( "username" , username + "_" + rands . HexString ( 8 ) ) .
UpdateQuickly ( )
if err != nil {
return err
}
}
// 禁止当前
_ , err = this . Query ( tx ) .
2022-07-24 17:13:05 +08:00
Pk ( userId ) .
2020-07-22 22:17:53 +08:00
Set ( "state" , UserStateDisabled ) .
Update ( )
2022-10-23 16:22:20 +08:00
if err != nil {
return err
}
2022-12-02 17:33:45 +08:00
err = SharedAPIAccessTokenDAO . DeleteAccessTokens ( tx , 0 , userId )
if err != nil {
return err
}
2022-10-23 16:22:20 +08:00
return this . NotifyUpdate ( tx , userId )
2020-07-22 22:17:53 +08:00
}
2021-11-08 20:52:15 +08:00
// FindEnabledUser 查找启用的用户
2021-11-11 14:16:42 +08:00
func ( this * UserDAO ) FindEnabledUser ( tx * dbs . Tx , userId int64 , cacheMap * utils . CacheMap ) ( * User , error ) {
2021-11-08 20:52:15 +08:00
if cacheMap == nil {
2021-11-11 14:16:42 +08:00
cacheMap = utils . NewCacheMap ( )
2021-11-08 20:52:15 +08:00
}
var cacheKey = this . Table + ":FindEnabledUser:" + types . String ( userId )
2021-11-11 14:16:42 +08:00
cache , ok := cacheMap . Get ( cacheKey )
2021-11-08 20:52:15 +08:00
if ok {
return cache . ( * User ) , nil
}
2021-01-01 23:31:30 +08:00
result , err := this . Query ( tx ) .
2021-11-08 20:52:15 +08:00
Pk ( userId ) .
2020-07-22 22:17:53 +08:00
Attr ( "state" , UserStateEnabled ) .
Find ( )
if result == nil {
return nil , err
}
2021-11-08 20:52:15 +08:00
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cacheMap . Put ( cacheKey , result )
}
2021-11-08 20:52:15 +08:00
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 查找用户基本信息
2021-01-01 23:31:30 +08:00
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 ) .
2023-02-22 17:34:05 +08:00
Result ( "id" , "fullname" , "username" , "isOn" ) .
2020-12-07 11:46:02 +08:00
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
}
2022-07-24 16:14:56 +08:00
// FindEnabledUserIdWithUsername 根据用户名查找用户ID
func ( this * UserDAO ) FindEnabledUserIdWithUsername ( tx * dbs . Tx , username string ) ( int64 , error ) {
return this . Query ( tx ) .
ResultPk ( ) .
State ( UserStateEnabled ) .
Attr ( "username" , username ) .
FindInt64Col ( 0 )
}
2022-12-10 15:57:17 +08:00
// FindUserId 根据多个条件查找用户ID
func ( this * UserDAO ) FindUserId ( tx * dbs . Tx , verifiedEmail string , verifiedMobile string ) ( int64 , error ) {
var query = this . Query ( tx ) .
State ( UserStateEnabled ) .
ResultPk ( )
if len ( verifiedEmail ) > 0 {
query . Attr ( "verifiedEmail" , verifiedEmail )
} else if len ( verifiedMobile ) > 0 {
query . Attr ( "verifiedMobile" , verifiedMobile )
} else {
return 0 , nil
}
return query . FindInt64Col ( 0 )
}
// FindUserFullname 获取用户名称
2021-01-01 23:31:30 +08:00
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 ( "" )
}
2020-12-04 16:01:01 +08:00
2022-12-10 15:57:17 +08:00
// FindUserVerifiedEmail 查询用户已绑定邮箱
func ( this * UserDAO ) FindUserVerifiedEmail ( tx * dbs . Tx , userId int64 ) ( string , error ) {
return this . Query ( tx ) .
Pk ( userId ) .
Result ( "verifiedEmail" ) .
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 ) {
2022-07-24 09:56:27 +08:00
var op = NewUserOperator ( )
2020-12-04 16:01:01 +08:00
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
2020-12-04 16:01:01 +08:00
op . Remark = remark
op . Source = source
2020-12-17 15:51:02 +08:00
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
}
2020-12-04 16:01:01 +08:00
op . IsOn = true
op . State = UserStateEnabled
2021-01-01 23:31:30 +08:00
err := this . Save ( tx , op )
2020-12-04 16:01:01 +08:00
if err != nil {
return 0 , err
}
return types . Int64 ( op . Id ) , nil
}
2021-05-27 17:09:07 +08:00
// UpdateUser 修改用户
2021-06-19 22:19:12 +08:00
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 {
2020-12-04 16:01:01 +08:00
if userId <= 0 {
return errors . New ( "invalid userId" )
}
2022-07-24 17:13:05 +08:00
2022-05-25 11:44:18 +08:00
var op = NewUserOperator ( )
2020-12-04 16:01:01 +08:00
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
2021-06-19 22:19:12 +08:00
op . ClusterId = nodeClusterId
2020-12-04 16:01:01 +08:00
op . IsOn = isOn
2022-10-23 16:22:20 +08:00
err := this . Save ( tx , op )
2022-07-24 17:13:05 +08:00
if err != nil {
return err
}
2022-12-02 17:33:45 +08:00
// 删除AccessTokens
if ! isOn {
err = SharedAPIAccessTokenDAO . DeleteAccessTokens ( tx , 0 , userId )
if err != nil {
return err
}
}
2022-10-23 16:22:20 +08:00
return this . NotifyUpdate ( tx , userId )
2020-12-04 16:01:01 +08:00
}
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" )
}
2022-07-24 09:56:27 +08:00
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
2021-01-01 23:31:30 +08:00
return this . Save ( tx , op )
2020-12-15 11:52:57 +08:00
}
2021-05-27 17:09:07 +08:00
// UpdateUserLogin 修改用户登录信息
2021-01-01 23:31:30 +08:00
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" )
}
2022-07-24 09:56:27 +08:00
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 )
}
2022-12-10 15:57:17 +08:00
return this . Save ( tx , op )
}
// UpdateUserPassword 修改用户密码
func ( this * UserDAO ) UpdateUserPassword ( tx * dbs . Tx , userId int64 , password string ) error {
if userId <= 0 {
return errors . New ( "invalid userId" )
}
var op = NewUserOperator ( )
op . Id = userId
if len ( password ) > 0 {
op . Password = stringutil . Md5 ( password )
}
return this . Save ( tx , op )
2020-12-15 11:52:57 +08:00
}
2021-05-27 17:09:07 +08:00
// CountAllEnabledUsers 计算用户数量
2022-01-05 11:12:24 +08:00
func ( this * UserDAO ) CountAllEnabledUsers ( tx * dbs . Tx , clusterId int64 , keyword string , isVerifying bool ) ( int64 , error ) {
2021-01-01 23:31:30 +08:00
query := this . Query ( tx )
2020-12-04 16:01:01 +08:00
query . State ( UserStateEnabled )
2021-07-05 11:37:22 +08:00
if clusterId > 0 {
query . Attr ( "clusterId" , clusterId )
}
2020-12-04 16:01:01 +08:00
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)" ) .
2022-03-27 12:22:47 +08:00
Param ( "keyword" , dbutils . QuoteLike ( keyword ) )
2020-12-04 16:01:01 +08:00
}
2022-01-05 11:12:24 +08:00
if isVerifying {
2022-07-24 11:57:42 +08:00
query . Where ( "(isVerified=0 OR (id IN (SELECT userId FROM " + SharedUserIdentityDAO . Table + " WHERE status=:identityStatus AND state=1)))" )
query . Param ( "identityStatus" , userconfigs . UserIdentityStatusSubmitted )
2022-01-05 11:12:24 +08:00
}
return query . Count ( )
}
// CountAllVerifyingUsers 获取等待审核的用户数
func ( this * UserDAO ) CountAllVerifyingUsers ( tx * dbs . Tx ) ( int64 , error ) {
query := this . Query ( tx )
query . State ( UserStateEnabled )
2022-07-24 11:57:42 +08:00
query . Where ( "(isVerified=0 OR (id IN (SELECT userId FROM " + SharedUserIdentityDAO . Table + " WHERE status=:identityStatus AND state=1)))" )
query . Param ( "identityStatus" , userconfigs . UserIdentityStatusSubmitted )
2020-12-04 16:01:01 +08:00
return query . Count ( )
}
2021-05-27 17:09:07 +08:00
// ListEnabledUsers 列出单页用户
2022-01-05 11:12:24 +08:00
func ( this * UserDAO ) ListEnabledUsers ( tx * dbs . Tx , clusterId int64 , keyword string , isVerifying bool , offset int64 , size int64 ) ( result [ ] * User , err error ) {
2021-01-01 23:31:30 +08:00
query := this . Query ( tx )
2020-12-04 16:01:01 +08:00
query . State ( UserStateEnabled )
2021-07-05 11:37:22 +08:00
if clusterId > 0 {
query . Attr ( "clusterId" , clusterId )
}
2020-12-04 16:01:01 +08:00
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)" ) .
2022-03-27 12:22:47 +08:00
Param ( "keyword" , dbutils . QuoteLike ( keyword ) )
2020-12-04 16:01:01 +08:00
}
2022-01-05 11:12:24 +08:00
if isVerifying {
2022-07-24 11:57:42 +08:00
query . Where ( "(isVerified=0 OR (id IN (SELECT userId FROM " + SharedUserIdentityDAO . Table + " WHERE status=:identityStatus AND state=1)))" )
query . Param ( "identityStatus" , userconfigs . UserIdentityStatusSubmitted )
2022-01-05 11:12:24 +08:00
}
2020-12-04 16:01:01 +08:00
_ , err = query .
DescPk ( ) .
2021-05-27 17:09:07 +08:00
Offset ( offset ) .
Limit ( size ) .
2020-12-04 16:01:01 +08:00
Slice ( & result ) .
FindAll ( )
return
}
2021-05-27 17:09:07 +08:00
// ExistUser 检查用户名是否存在
2021-01-01 23:31:30 +08:00
func ( this * UserDAO ) ExistUser ( tx * dbs . Tx , userId int64 , username string ) ( bool , error ) {
return this . Query ( tx ) .
2020-12-04 16:01:01 +08:00
State ( UserStateEnabled ) .
Attr ( "username" , username ) .
Neq ( "id" , userId ) .
Exist ( )
}
2020-12-11 21:39:10 +08:00
2021-05-27 17:09:07 +08:00
// ListEnabledUserIds 列出单页的用户ID
2021-01-01 23:31:30 +08:00
func ( this * UserDAO ) ListEnabledUserIds ( tx * dbs . Tx , offset , size int64 ) ( [ ] int64 , error ) {
ones , _ , err := this . Query ( tx ) .
2020-12-11 21:39:10 +08:00
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
2022-12-08 20:25:46 +08:00
// CheckUserPassword 检查用户名+密码
2021-01-01 23:31:30 +08:00
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
}
2021-01-01 23:31:30 +08:00
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
2022-12-08 20:25:46 +08:00
// CheckUserEmailPassword 检查邮箱+密码
func ( this * UserDAO ) CheckUserEmailPassword ( tx * dbs . Tx , verifiedEmail string , encryptedPassword string ) ( int64 , error ) {
if len ( verifiedEmail ) == 0 || len ( encryptedPassword ) == 0 {
return 0 , nil
}
return this . Query ( tx ) .
Attr ( "verifiedEmail" , verifiedEmail ) .
Attr ( "password" , encryptedPassword ) .
Attr ( "state" , UserStateEnabled ) .
Attr ( "isOn" , true ) .
ResultPk ( ) .
FindInt64Col ( 0 )
}
2021-05-27 17:09:07 +08:00
// FindUserClusterId 查找用户所在集群
2021-01-01 23:31:30 +08:00
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 )
}
2020-12-30 22:01:01 +08:00
2022-08-28 17:01:09 +08:00
// UpdateUserFeatures 更新单个用户Features
2021-01-01 23:31:30 +08:00
func ( this * UserDAO ) UpdateUserFeatures ( tx * dbs . Tx , userId int64 , featuresJSON [ ] byte ) error {
2020-12-30 22:01:01 +08:00
if userId <= 0 {
return errors . New ( "invalid userId" )
}
if len ( featuresJSON ) == 0 {
featuresJSON = [ ] byte ( "[]" )
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-12-30 22:01:01 +08:00
Pk ( userId ) .
Set ( "features" , featuresJSON ) .
Update ( )
if err != nil {
return err
}
return nil
}
2022-08-28 17:01:09 +08:00
// UpdateUsersFeatures 更新所有用户的Features
func ( this * UserDAO ) UpdateUsersFeatures ( tx * dbs . Tx , featureCodes [ ] string , overwrite bool ) error {
if featureCodes == nil {
featureCodes = [ ] string { }
}
if overwrite {
featureCodesJSON , err := json . Marshal ( featureCodes )
if err != nil {
return err
}
err = this . Query ( tx ) .
State ( UserStateEnabled ) .
Set ( "features" , featureCodesJSON ) .
UpdateQuickly ( )
return err
}
var lastId int64
const size = 1000
for {
ones , _ , err := this . Query ( tx ) .
Result ( "id" , "features" ) .
State ( UserStateEnabled ) .
Gt ( "id" , lastId ) .
Limit ( size ) .
AscPk ( ) .
FindOnes ( )
if err != nil {
return err
}
for _ , one := range ones {
var userId = one . GetInt64 ( "id" )
var userFeaturesJSON = one . GetBytes ( "features" )
var userFeatures = [ ] string { }
if len ( userFeaturesJSON ) > 0 {
err = json . Unmarshal ( userFeaturesJSON , & userFeatures )
if err != nil {
return err
}
}
for _ , featureCode := range featureCodes {
if ! lists . ContainsString ( userFeatures , featureCode ) {
userFeatures = append ( userFeatures , featureCode )
}
}
userFeaturesJSON , err = json . Marshal ( userFeatures )
if err != nil {
return err
}
err = this . Query ( tx ) .
Pk ( userId ) .
Set ( "features" , userFeaturesJSON ) .
UpdateQuickly ( )
if err != nil {
return err
}
}
if len ( ones ) < size {
break
}
lastId += size
}
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 ) {
2021-01-01 23:31:30 +08:00
featuresJSON , err := this . Query ( tx ) .
2020-12-30 22:01:01 +08:00
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 { }
2020-12-30 22:01:01 +08:00
if len ( featureCodes ) > 0 {
for _ , featureCode := range featureCodes {
2021-11-28 20:11:36 +08:00
f := userconfigs . FindUserFeature ( featureCode )
2020-12-30 22:01:01 +08:00
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 } )
2020-12-30 22:01:01 +08:00
}
}
}
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
2022-10-23 16:22:20 +08:00
err := this . Save ( tx , op )
if err != nil {
return err
}
return this . NotifyUpdate ( tx , userId )
}
// RenewUserServersState 更新用户服务状态
2022-10-23 20:12:28 +08:00
func ( this * UserDAO ) RenewUserServersState ( tx * dbs . Tx , userId int64 ) ( bool , error ) {
2022-10-23 16:22:20 +08:00
oldServersEnabled , err := this . Query ( tx ) .
Pk ( userId ) .
Result ( "serversEnabled" ) .
FindBoolCol ( )
if err != nil {
2022-10-23 20:12:28 +08:00
return false , err
2022-10-23 16:22:20 +08:00
}
newServersEnabled , err := this . CheckUserServersEnabled ( tx , userId )
if err != nil {
2022-10-23 20:12:28 +08:00
return false , err
2022-10-23 16:22:20 +08:00
}
if oldServersEnabled != newServersEnabled {
err = this . Query ( tx ) .
Pk ( userId ) .
Set ( "serversEnabled" , newServersEnabled ) .
UpdateQuickly ( )
if err != nil {
2022-10-23 20:12:28 +08:00
return false , err
2022-10-23 16:22:20 +08:00
}
// 创建变更通知
clusterIds , err := SharedServerDAO . FindUserServerClusterIds ( tx , userId )
if err != nil {
2022-10-23 20:12:28 +08:00
return false , err
2022-10-23 16:22:20 +08:00
}
for _ , clusterId := range clusterIds {
err = SharedNodeTaskDAO . CreateClusterTask ( tx , nodeconfigs . NodeRoleNode , clusterId , userId , 0 , NodeTaskTypeUserServersStateChanged )
if err != nil {
2022-10-23 20:12:28 +08:00
return false , err
2022-10-23 16:22:20 +08:00
}
}
}
2022-10-23 20:12:28 +08:00
return newServersEnabled , nil
2022-10-23 16:22:20 +08:00
}
2022-12-08 20:25:46 +08:00
// FindUserIdWithVerifiedEmail 使用验证后Email查找用户ID
func ( this * UserDAO ) FindUserIdWithVerifiedEmail ( tx * dbs . Tx , verifiedEmail string ) ( int64 , error ) {
if len ( verifiedEmail ) == 0 {
}
return this . Query ( tx ) .
ResultPk ( ) .
State ( UserStateEnabled ) .
Attr ( "verifiedEmail" , verifiedEmail ) .
FindInt64Col ( 0 )
}
// UpdateUserVerifiedEmail 修改已激活邮箱
func ( this * UserDAO ) UpdateUserVerifiedEmail ( tx * dbs . Tx , userId int64 , verifiedEmail string ) error {
if userId <= 0 {
return nil
}
return this . Query ( tx ) .
Pk ( userId ) .
Set ( "verifiedEmail" , verifiedEmail ) .
Set ( "emailIsVerified" , true ) .
UpdateQuickly ( )
}
2022-10-23 16:22:20 +08:00
// NotifyUpdate 用户变更通知
func ( this * UserDAO ) NotifyUpdate ( tx * dbs . Tx , userId int64 ) error {
if userId <= 0 {
return nil
}
// 更新用户服务状态
2022-10-23 20:12:28 +08:00
_ , err := this . RenewUserServersState ( tx , userId )
2022-10-23 16:22:20 +08:00
if err != nil {
return err
}
return nil
2022-01-05 10:45:19 +08:00
}