合并分支 'test' 到 'master'
用户基础数据修改 查看合并请求 !3
正在显示
5 个修改的文件
包含
122 行增加
和
2 行删除
@@ -55,6 +55,14 @@ func CreatePgUpdateUserService(options map[string]interface{}) (service.PgUpdate | @@ -55,6 +55,14 @@ func CreatePgUpdateUserService(options map[string]interface{}) (service.PgUpdate | ||
55 | return domainService.NewPgUpdateUserService(transactionContext) | 55 | return domainService.NewPgUpdateUserService(transactionContext) |
56 | } | 56 | } |
57 | 57 | ||
58 | +func CreatePgUpdateUserBaseService(options map[string]interface{}) (service.PgUpdateUserBaseService, error) { | ||
59 | + var transactionContext *pgTransaction.TransactionContext | ||
60 | + if value, ok := options["transactionContext"]; ok { | ||
61 | + transactionContext = value.(*pgTransaction.TransactionContext) | ||
62 | + } | ||
63 | + return domainService.NewPgUpdateUserBaseService(transactionContext) | ||
64 | +} | ||
65 | + | ||
58 | func CreatePgCreateRoleService(options map[string]interface{}) (service.PgCreateRoleService, error) { | 66 | func CreatePgCreateRoleService(options map[string]interface{}) (service.PgCreateRoleService, error) { |
59 | var transactionContext *pgTransaction.TransactionContext | 67 | var transactionContext *pgTransaction.TransactionContext |
60 | if value, ok := options["transactionContext"]; ok { | 68 | if value, ok := options["transactionContext"]; ok { |
@@ -12,7 +12,9 @@ import ( | @@ -12,7 +12,9 @@ import ( | ||
12 | type UpdateUsersBaseCommand struct { | 12 | type UpdateUsersBaseCommand struct { |
13 | OperateInfo *domain.OperateInfo `json:"-"` | 13 | OperateInfo *domain.OperateInfo `json:"-"` |
14 | // 用户Id 用户唯一标识 | 14 | // 用户Id 用户唯一标识 |
15 | - UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string" valid:"Required"` | 15 | + UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string"` |
16 | + // 用户基础Id 用户唯一标识 | ||
17 | + UserBaseId int64 `cname:"用户Id 用户唯一标识" json:"userBaseId" valid:"Required"` | ||
16 | // 用户姓名 | 18 | // 用户姓名 |
17 | UserName string `cname:"用户姓名" json:"userName" valid:"Required"` | 19 | UserName string `cname:"用户姓名" json:"userName" valid:"Required"` |
18 | // 头像 | 20 | // 头像 |
@@ -741,7 +741,7 @@ func (userService *UserService) UpdateUser(updateUserCommand *command.UpdateUser | @@ -741,7 +741,7 @@ func (userService *UserService) UpdateUser(updateUserCommand *command.UpdateUser | ||
741 | } | 741 | } |
742 | 742 | ||
743 | // 更新用户基础信息数据 | 743 | // 更新用户基础信息数据 |
744 | -func (userService *UserService) UpdateUsersBase(updateUsersBaseCommand *command.UpdateUsersBaseCommand) (interface{}, error) { | 744 | +func (userService *UserService) UpdateUsersBaseOld(updateUsersBaseCommand *command.UpdateUsersBaseCommand) (interface{}, error) { |
745 | if err := updateUsersBaseCommand.ValidateCommand(); err != nil { | 745 | if err := updateUsersBaseCommand.ValidateCommand(); err != nil { |
746 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | 746 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) |
747 | } | 747 | } |
@@ -778,6 +778,40 @@ func (userService *UserService) UpdateUsersBase(updateUsersBaseCommand *command. | @@ -778,6 +778,40 @@ func (userService *UserService) UpdateUsersBase(updateUsersBaseCommand *command. | ||
778 | return struct{}{}, nil | 778 | return struct{}{}, nil |
779 | } | 779 | } |
780 | 780 | ||
781 | +// 更新用户基础信息数据 | ||
782 | +func (userService *UserService) UpdateUsersBase(updateUsersBaseCommand *command.UpdateUsersBaseCommand) (interface{}, error) { | ||
783 | + if err := updateUsersBaseCommand.ValidateCommand(); err != nil { | ||
784 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
785 | + } | ||
786 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
787 | + if err != nil { | ||
788 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
789 | + } | ||
790 | + if err := transactionContext.StartTransaction(); err != nil { | ||
791 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
792 | + } | ||
793 | + defer func() { | ||
794 | + transactionContext.RollbackTransaction() | ||
795 | + }() | ||
796 | + | ||
797 | + userInfo := &domain.UserInfo{ | ||
798 | + UserName: updateUsersBaseCommand.UserName, | ||
799 | + Phone: updateUsersBaseCommand.Phone, | ||
800 | + Avatar: updateUsersBaseCommand.Avatar, | ||
801 | + Email: updateUsersBaseCommand.Email, | ||
802 | + } | ||
803 | + updateUserService, _ := factory.CreatePgUpdateUserBaseService(map[string]interface{}{ | ||
804 | + "transactionContext": transactionContext, | ||
805 | + }) | ||
806 | + if _, err = updateUserService.UpdateUserBase(nil, updateUsersBaseCommand.UserBaseId, userInfo); err != nil { | ||
807 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
808 | + } | ||
809 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
810 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
811 | + } | ||
812 | + return struct{}{}, nil | ||
813 | +} | ||
814 | + | ||
781 | func NewUserService(options map[string]interface{}) *UserService { | 815 | func NewUserService(options map[string]interface{}) *UserService { |
782 | newUserService := &UserService{} | 816 | newUserService := &UserService{} |
783 | return newUserService | 817 | return newUserService |
1 | +package service | ||
2 | + | ||
3 | +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | ||
4 | + | ||
5 | +// PgUpdateUserBaseService 用户基础更新服务 | ||
6 | +type PgUpdateUserBaseService interface { | ||
7 | + UpdateUserBase(optUser *domain.OperateInfo, userBaseId int64, userInfo *domain.UserInfo) (*domain.UserBase, error) | ||
8 | +} |
1 | +package domainService | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository" | ||
8 | + "strings" | ||
9 | + "time" | ||
10 | +) | ||
11 | + | ||
12 | +// PgUpdateUserBaseService 用户基础更新服务 | ||
13 | +type PgUpdateUserBaseService struct { | ||
14 | + transactionContext *pgTransaction.TransactionContext | ||
15 | +} | ||
16 | + | ||
17 | +func (ptr *PgUpdateUserService) UpdateUserBase(optUser *domain.OperateInfo, userBaseId int64, userInfo *domain.UserInfo) (*domain.UserBase, error) { | ||
18 | + var err error | ||
19 | + | ||
20 | + //1.更新用户信息 | ||
21 | + userBaseRepository, _ := repository.NewUserBaseRepository(ptr.transactionContext) | ||
22 | + var userBase *domain.UserBase | ||
23 | + if userBase, err = userBaseRepository.FindOne(map[string]interface{}{"userBaseId": userBaseId}); err != nil { | ||
24 | + return nil, err | ||
25 | + } | ||
26 | + if userBase.Account != strings.TrimSpace(userInfo.Phone) && len(userInfo.Phone) > 0 { // 修改了手机号 | ||
27 | + if _, err = userBaseRepository.FindOne(map[string]interface{}{"account": strings.TrimSpace(userInfo.Phone)}); err == nil { | ||
28 | + return nil, fmt.Errorf("手机号已存在") | ||
29 | + } | ||
30 | + if err = userBase.ResetPhone(userBase.Account, userInfo.Phone); err != nil { | ||
31 | + return nil, err | ||
32 | + } | ||
33 | + } | ||
34 | + if err = userBase.UpdateUserInfo(userInfo); err != nil { | ||
35 | + return nil, err | ||
36 | + } | ||
37 | + if userBase, err = userBaseRepository.Save(userBase); err != nil { | ||
38 | + return nil, err | ||
39 | + } | ||
40 | + | ||
41 | + //2.更新用户、冗余信息 | ||
42 | + var users []*domain.User | ||
43 | + userRepository, _ := repository.NewUserRepository(ptr.transactionContext) | ||
44 | + if _, users, err = userRepository.Find(map[string]interface{}{"userBaseId": userBaseId}); err != nil { | ||
45 | + return nil, err | ||
46 | + } | ||
47 | + for i := range users { | ||
48 | + user := users[i] | ||
49 | + user.Ext.Phone = userBase.UserInfo.Phone | ||
50 | + user.Ext.UserName = userBase.UserInfo.UserName | ||
51 | + user.UpdatedAt = time.Now() | ||
52 | + if user, err = userRepository.Save(user); err != nil { | ||
53 | + return nil, err | ||
54 | + } | ||
55 | + } | ||
56 | + | ||
57 | + return userBase, nil | ||
58 | +} | ||
59 | + | ||
60 | +func NewPgUpdateUserBaseService(transactionContext *pgTransaction.TransactionContext) (*PgUpdateUserService, error) { | ||
61 | + if transactionContext == nil { | ||
62 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
63 | + } else { | ||
64 | + return &PgUpdateUserService{ | ||
65 | + transactionContext: transactionContext, | ||
66 | + }, nil | ||
67 | + } | ||
68 | +} |
-
请 注册 或 登录 后发表评论