-
-
Notifications
You must be signed in to change notification settings - Fork 264
/
update_user_logic.go
90 lines (72 loc) · 2.54 KB
/
update_user_logic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package user
import (
"context"
"github.com/suyuan32/simple-admin-common/enum/common"
"github.com/suyuan32/simple-admin-common/utils/encrypt"
"github.com/suyuan32/simple-admin-common/utils/pointy"
"github.com/suyuan32/simple-admin-common/utils/uuidx"
"github.com/suyuan32/simple-admin-core/rpc/internal/logic/token"
"github.com/suyuan32/simple-admin-core/rpc/internal/utils/entx"
"github.com/suyuan32/simple-admin-core/rpc/ent"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/internal/utils/dberrorhandler"
"github.com/suyuan32/simple-admin-core/rpc/types/core"
"github.com/zeromicro/go-zero/core/logx"
"github.com/suyuan32/simple-admin-common/i18n"
)
type UpdateUserLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
return &UpdateUserLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdateUserLogic) UpdateUser(in *core.UserInfo) (*core.BaseResp, error) {
err := entx.WithTx(l.ctx, l.svcCtx.DB, func(tx *ent.Tx) error {
updateQuery := tx.User.UpdateOneID(uuidx.ParseUUIDString(*in.Id)).
SetNotNilUsername(in.Username).
SetNotNilNickname(in.Nickname).
SetNotNilEmail(in.Email).
SetNotNilMobile(in.Mobile).
SetNotNilAvatar(in.Avatar).
SetNotNilHomePath(in.HomePath).
SetNotNilDescription(in.Description).
SetNotNilDepartmentID(in.DepartmentId).
SetNotNilStatus(pointy.GetStatusPointer(in.Status))
if in.Password != nil {
updateQuery = updateQuery.SetNotNilPassword(pointy.GetPointer(encrypt.BcryptEncrypt(*in.Password)))
}
if in.RoleIds != nil {
err := tx.User.UpdateOneID(uuidx.ParseUUIDString(*in.Id)).ClearRoles().Exec(l.ctx)
if err != nil {
return err
}
updateQuery = updateQuery.AddRoleIDs(in.RoleIds...)
}
if in.PositionIds != nil {
err := tx.User.UpdateOneID(uuidx.ParseUUIDString(*in.Id)).ClearPositions().Exec(l.ctx)
if err != nil {
return err
}
updateQuery = updateQuery.AddPositionIDs(in.PositionIds...)
}
if in.Password != nil || in.RoleIds != nil || in.PositionIds != nil || (in.Status != nil && uint8(*in.Status) != common.StatusNormal) {
_, err := token.NewBlockUserAllTokenLogic(l.ctx, l.svcCtx).BlockUserAllToken(&core.UUIDReq{Id: *in.Id})
if err != nil {
return err
}
}
return updateQuery.Exec(l.ctx)
})
if err != nil {
return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
}
return &core.BaseResp{
Msg: i18n.Success,
}, nil
}