合并分支 'chenaq' 到 'dev'
Chenaq 查看合并请求 !14
正在显示
26 个修改的文件
包含
1189 行增加
和
53 行删除
| @@ -13,6 +13,7 @@ require ( | @@ -13,6 +13,7 @@ require ( | ||
| 13 | github.com/google/go-querystring v1.1.0 // indirect | 13 | github.com/google/go-querystring v1.1.0 // indirect |
| 14 | github.com/gorilla/websocket v1.4.2 // indirect | 14 | github.com/gorilla/websocket v1.4.2 // indirect |
| 15 | github.com/imkira/go-interpol v1.1.0 // indirect | 15 | github.com/imkira/go-interpol v1.1.0 // indirect |
| 16 | + github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect | ||
| 16 | github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7 | 17 | github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7 |
| 17 | github.com/mattn/go-colorable v0.1.8 // indirect | 18 | github.com/mattn/go-colorable v0.1.8 // indirect |
| 18 | github.com/moul/http2curl v1.0.0 // indirect | 19 | github.com/moul/http2curl v1.0.0 // indirect |
| @@ -132,6 +132,8 @@ github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ | @@ -132,6 +132,8 @@ github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ | ||
| 132 | github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= | 132 | github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= |
| 133 | github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= | 133 | github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= |
| 134 | github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= | 134 | github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= |
| 135 | +github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM= | ||
| 136 | +github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= | ||
| 135 | github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= | 137 | github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= |
| 136 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= | 138 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= |
| 137 | github.com/klauspost/compress v1.11.8 h1:difgzQsp5mdAz9v8lm3P/I+EpDKMU/6uTMw1y1FObuo= | 139 | github.com/klauspost/compress v1.11.8 h1:difgzQsp5mdAz9v8lm3P/I+EpDKMU/6uTMw1y1FObuo= |
| @@ -39,6 +39,7 @@ func CreateReturnGoodsService(options map[string]interface{}) (service.ReturnGoo | @@ -39,6 +39,7 @@ func CreateReturnGoodsService(options map[string]interface{}) (service.ReturnGoo | ||
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | func CreateUserCreateService(options map[string]interface{}) (service.UserCreateService, error) { | 41 | func CreateUserCreateService(options map[string]interface{}) (service.UserCreateService, error) { |
| 42 | + | ||
| 42 | var transactionContext *pgTransaction.TransactionContext | 43 | var transactionContext *pgTransaction.TransactionContext |
| 43 | if value, ok := options["transactionContext"]; ok { | 44 | if value, ok := options["transactionContext"]; ok { |
| 44 | transactionContext = value.(*pgTransaction.TransactionContext) | 45 | transactionContext = value.(*pgTransaction.TransactionContext) |
| @@ -53,3 +54,11 @@ func CreatePhoneAuthService(options map[string]interface{}) (service.PhoneAuthSe | @@ -53,3 +54,11 @@ func CreatePhoneAuthService(options map[string]interface{}) (service.PhoneAuthSe | ||
| 53 | } | 54 | } |
| 54 | return domainService.NewPhoneAuthenticationService(transactionContext) | 55 | return domainService.NewPhoneAuthenticationService(transactionContext) |
| 55 | } | 56 | } |
| 57 | + | ||
| 58 | +func CreateAddUserService(options map[string]interface{}) (service.AddUserService, error) { | ||
| 59 | + var transactionContext *pgTransaction.TransactionContext | ||
| 60 | + if value, ok := options["transactionContext"]; ok { | ||
| 61 | + transactionContext = value.(*pgTransaction.TransactionContext) | ||
| 62 | + } | ||
| 63 | + return domainService.NewAddUserService(transactionContext) | ||
| 64 | +} |
| 1 | +package command | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + | ||
| 6 | + "github.com/beego/beego/v2/core/validation" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +type ConvertUserStatusCommand struct { | ||
| 10 | + // 用户id | ||
| 11 | + UserId int64 `json:"userId,omitempty"` | ||
| 12 | + // 状态 1正常 2禁用 | ||
| 13 | + Status int64 `json:"status" valid:"Required"` | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +func (convertUserStatusCommand *ConvertUserStatusCommand) Valid(validation *validation.Validation) { | ||
| 17 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +func (convertUserStatusCommand *ConvertUserStatusCommand) ValidateCommand() error { | ||
| 21 | + valid := validation.Validation{} | ||
| 22 | + b, err := valid.Valid(convertUserStatusCommand) | ||
| 23 | + if err != nil { | ||
| 24 | + return err | ||
| 25 | + } | ||
| 26 | + if !b { | ||
| 27 | + for _, validErr := range valid.Errors { | ||
| 28 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + return nil | ||
| 32 | +} |
pkg/application/user/command/create_user.go
0 → 100644
| 1 | +package command | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "github.com/beego/beego/v2/core/validation" | ||
| 6 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +type CreateUserCommand struct { | ||
| 10 | + //按位与 1.高管 001 右第0位高管 ; 2.合伙人 010 右第1位合伙人 ;3.高管,合伙人 011; | ||
| 11 | + // 1.高管 2.合伙人 3高管,合伙人 4:游客 | ||
| 12 | + UserType int `json:"userType" valid:"Required"` | ||
| 13 | + // 管理员类型 1.超级管理员 10:企业管理员 100:普通用户 | ||
| 14 | + AdminType int `json:"adminType" valid:"Required"` | ||
| 15 | + // 状态 1正常 2禁用 | ||
| 16 | + Status int64 `json:"status" valid:"Required"` | ||
| 17 | + //用户账号(手机号) | ||
| 18 | + UserAccount string `json:"userAccount" valid:"Required"` | ||
| 19 | + // 业务员 | ||
| 20 | + Salesmans []*domain.Salesman `json:"salesmans,omitempty"` | ||
| 21 | + // 用户信息 | ||
| 22 | + UserInfo *domain.UserInfo `json:"userInfo,omitempty"` | ||
| 23 | + //合伙人信息 | ||
| 24 | + PartnerInfo *domain.PartnerInfo `json:"partnerInfo"` | ||
| 25 | + //// 合伙人类型 | ||
| 26 | + PartnerCategorys []*domain.PartnerCategory `json:"partnerCategorys,omitempty"` | ||
| 27 | + // 可查看合伙人列表 | ||
| 28 | + AccessPartners []int64 `json:"accessPartners,omitempty"` | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +func (createUserCommand *CreateUserCommand) Valid(validation *validation.Validation) { | ||
| 32 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +func (createUserCommand *CreateUserCommand) ValidateCommand() error { | ||
| 36 | + valid := validation.Validation{} | ||
| 37 | + b, err := valid.Valid(createUserCommand) | ||
| 38 | + if err != nil { | ||
| 39 | + return err | ||
| 40 | + } | ||
| 41 | + if !b { | ||
| 42 | + for _, validErr := range valid.Errors { | ||
| 43 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + return nil | ||
| 47 | +} |
pkg/application/user/command/remove_user.go
0 → 100644
| 1 | +package command | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + | ||
| 6 | + "github.com/beego/beego/v2/core/validation" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +type RemoveUserCommand struct { | ||
| 10 | + // 用户id | ||
| 11 | + UserId int64 `json:"userId" valid:"Required"` | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +func (removeUserCommand *RemoveUserCommand) Valid(validation *validation.Validation) { | ||
| 15 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +func (removeUserCommand *RemoveUserCommand) ValidateCommand() error { | ||
| 19 | + valid := validation.Validation{} | ||
| 20 | + b, err := valid.Valid(removeUserCommand) | ||
| 21 | + if err != nil { | ||
| 22 | + return err | ||
| 23 | + } | ||
| 24 | + if !b { | ||
| 25 | + for _, validErr := range valid.Errors { | ||
| 26 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + return nil | ||
| 30 | +} |
| 1 | +package command | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + | ||
| 6 | + "github.com/beego/beego/v2/core/validation" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +type SetPermissionCommand struct { | ||
| 10 | + // 用户id | ||
| 11 | + UserId int64 `json:"userId" valid:"Required"` | ||
| 12 | + // 权限编码列表 | ||
| 13 | + Permissons []int `json:"permissons,omitempty"` | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +func (setPermissionCommand *SetPermissionCommand) Valid(validation *validation.Validation) { | ||
| 17 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +func (setPermissionCommand *SetPermissionCommand) ValidateCommand() error { | ||
| 21 | + valid := validation.Validation{} | ||
| 22 | + b, err := valid.Valid(setPermissionCommand) | ||
| 23 | + if err != nil { | ||
| 24 | + return err | ||
| 25 | + } | ||
| 26 | + if !b { | ||
| 27 | + for _, validErr := range valid.Errors { | ||
| 28 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + return nil | ||
| 32 | +} |
pkg/application/user/command/update_user.go
0 → 100644
| 1 | +package command | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "github.com/beego/beego/v2/core/validation" | ||
| 6 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +type UpdateUserCommand struct { | ||
| 10 | + // 用户id | ||
| 11 | + UserId int64 `json:"userId" valid:"Required"` | ||
| 12 | + // 1.高管 2.合伙人 4:游客 | ||
| 13 | + UserType int `json:"userType,omitempty"` | ||
| 14 | + // 管理员类型 1.超级管理员 10:企业管理员 100:普通用户 | ||
| 15 | + AdminType int `json:"adminType,omitempty"` | ||
| 16 | + // 状态 1正常 2禁用 | ||
| 17 | + Status int64 `json:"status,omitempty"` | ||
| 18 | + // 是否是公司负责人 | ||
| 19 | + UserInfo *domain.UserInfo `json:"userInfo,omitempty"` | ||
| 20 | + // 业务员 | ||
| 21 | + Salesmans []*domain.Salesman `json:"salesmans,omitempty"` | ||
| 22 | + // 合伙人类型 | ||
| 23 | + PartnerCategorys []*domain.PartnerCategory `json:"partnerCategorys,omitempty"` | ||
| 24 | + // 可查看合伙人列表 | ||
| 25 | + AccessPartners []int64 `json:"accessPartners,omitempty"` | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +func (updateUserCommand *UpdateUserCommand) Valid(validation *validation.Validation) { | ||
| 29 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +func (updateUserCommand *UpdateUserCommand) ValidateCommand() error { | ||
| 33 | + valid := validation.Validation{} | ||
| 34 | + b, err := valid.Valid(updateUserCommand) | ||
| 35 | + if err != nil { | ||
| 36 | + return err | ||
| 37 | + } | ||
| 38 | + if !b { | ||
| 39 | + for _, validErr := range valid.Errors { | ||
| 40 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + return nil | ||
| 44 | +} |
pkg/application/user/query/get_user.go
0 → 100644
| 1 | +package query | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + | ||
| 6 | + "github.com/beego/beego/v2/core/validation" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +type GetUserQuery struct { | ||
| 10 | + // 用户id | ||
| 11 | + UserId int64 `json:"userId" valid:"Required"` | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +func (getUserQuery *GetUserQuery) Valid(validation *validation.Validation) { | ||
| 15 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +func (getUserQuery *GetUserQuery) ValidateQuery() error { | ||
| 19 | + valid := validation.Validation{} | ||
| 20 | + b, err := valid.Valid(getUserQuery) | ||
| 21 | + if err != nil { | ||
| 22 | + return err | ||
| 23 | + } | ||
| 24 | + if !b { | ||
| 25 | + for _, validErr := range valid.Errors { | ||
| 26 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + return nil | ||
| 30 | +} |
pkg/application/user/query/list_user.go
0 → 100644
| 1 | +package query | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + | ||
| 6 | + "github.com/beego/beego/v2/core/validation" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +type ListUserQuery struct { | ||
| 10 | + // 查询偏离量 | ||
| 11 | + Offset int `json:"offset"` | ||
| 12 | + // 查询限制 | ||
| 13 | + Limit int `json:"limit" valid:"Required"` | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +func (listUserQuery *ListUserQuery) Valid(validation *validation.Validation) { | ||
| 17 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +func (listUserQuery *ListUserQuery) ValidateQuery() error { | ||
| 21 | + valid := validation.Validation{} | ||
| 22 | + b, err := valid.Valid(listUserQuery) | ||
| 23 | + if err != nil { | ||
| 24 | + return err | ||
| 25 | + } | ||
| 26 | + if !b { | ||
| 27 | + for _, validErr := range valid.Errors { | ||
| 28 | + return fmt.Errorf("%s %s", validErr.Key, validErr.Message) | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + return nil | ||
| 32 | +} |
pkg/application/user/service/user.go
0 → 100644
| 1 | +package service | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "github.com/linmadan/egglib-go/core/application" | ||
| 6 | + "github.com/linmadan/egglib-go/utils/tool_funs" | ||
| 7 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory" | ||
| 8 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/command" | ||
| 9 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/query" | ||
| 10 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain" | ||
| 11 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain/service" | ||
| 12 | +) | ||
| 13 | + | ||
| 14 | +// 用户管理服务 | ||
| 15 | +type UserService struct { | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +// 用户状态转换(禁用、启用) | ||
| 19 | +func (userService *UserService) ConvertUserStatus(convertUserStatusCommand *command.ConvertUserStatusCommand) (interface{}, error) { | ||
| 20 | + if err := convertUserStatusCommand.ValidateCommand(); err != nil { | ||
| 21 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 22 | + } | ||
| 23 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 24 | + if err != nil { | ||
| 25 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 26 | + } | ||
| 27 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 28 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 29 | + } | ||
| 30 | + defer func() { | ||
| 31 | + transactionContext.RollbackTransaction() | ||
| 32 | + }() | ||
| 33 | + | ||
| 34 | + var userRepository domain.UserRepository | ||
| 35 | + if value, err := factory.CreateUserRepository(map[string]interface{}{ | ||
| 36 | + "transactionContext": transactionContext, | ||
| 37 | + }); err != nil { | ||
| 38 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 39 | + } else { | ||
| 40 | + userRepository = value | ||
| 41 | + } | ||
| 42 | + user, err := userRepository.FindOne(map[string]interface{}{"userId": convertUserStatusCommand.UserId}) | ||
| 43 | + if err != nil { | ||
| 44 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 45 | + } | ||
| 46 | + if user == nil { | ||
| 47 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(convertUserStatusCommand.UserId))) | ||
| 48 | + } | ||
| 49 | + if err := user.CovertUserStatus(convertUserStatusCommand.Status); err != nil { | ||
| 50 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
| 51 | + } | ||
| 52 | + if _, err := userRepository.Save(user); err != nil { | ||
| 53 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 57 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 58 | + } | ||
| 59 | + return nil, nil | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +// 创建 | ||
| 63 | +func (userService *UserService) CreateUser(createUserCommand *command.CreateUserCommand) (interface{}, error) { | ||
| 64 | + if err := createUserCommand.ValidateCommand(); err != nil { | ||
| 65 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 66 | + } | ||
| 67 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 68 | + if err != nil { | ||
| 69 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 70 | + } | ||
| 71 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 72 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 73 | + } | ||
| 74 | + defer func() { | ||
| 75 | + transactionContext.RollbackTransaction() | ||
| 76 | + }() | ||
| 77 | + newUser := &domain.User{ | ||
| 78 | + UserType: createUserCommand.UserType, | ||
| 79 | + AdminType: createUserCommand.AdminType, | ||
| 80 | + UserAccount: createUserCommand.UserAccount, | ||
| 81 | + Status: createUserCommand.Status, | ||
| 82 | + UserInfo: createUserCommand.UserInfo, | ||
| 83 | + PartnerInfo: createUserCommand.PartnerInfo, | ||
| 84 | + AccessPartners: createUserCommand.AccessPartners, | ||
| 85 | + } | ||
| 86 | + var addUserService service.UserCreateService | ||
| 87 | + if value, err := factory.CreateUserCreateService(map[string]interface{}{ | ||
| 88 | + "transactionContext": transactionContext, | ||
| 89 | + }); err != nil { | ||
| 90 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 91 | + } else { | ||
| 92 | + addUserService = value | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + if user, err := addUserService.CreateUser(0, newUser); err != nil { | ||
| 96 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 97 | + } else { | ||
| 98 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 99 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 100 | + } | ||
| 101 | + return user, nil | ||
| 102 | + } | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +// 返回 | ||
| 106 | +func (userService *UserService) GetUser(getUserQuery *query.GetUserQuery) (interface{}, error) { | ||
| 107 | + if err := getUserQuery.ValidateQuery(); err != nil { | ||
| 108 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 109 | + } | ||
| 110 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 111 | + if err != nil { | ||
| 112 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 113 | + } | ||
| 114 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 115 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 116 | + } | ||
| 117 | + defer func() { | ||
| 118 | + transactionContext.RollbackTransaction() | ||
| 119 | + }() | ||
| 120 | + var userRepository domain.UserRepository | ||
| 121 | + if value, err := factory.CreateUserRepository(map[string]interface{}{ | ||
| 122 | + "transactionContext": transactionContext, | ||
| 123 | + }); err != nil { | ||
| 124 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 125 | + } else { | ||
| 126 | + userRepository = value | ||
| 127 | + } | ||
| 128 | + user, err := userRepository.FindOne(map[string]interface{}{"userId": getUserQuery.UserId}) | ||
| 129 | + if err != nil { | ||
| 130 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 131 | + } | ||
| 132 | + if user == nil { | ||
| 133 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getUserQuery.UserId))) | ||
| 134 | + } else { | ||
| 135 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 136 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 137 | + } | ||
| 138 | + return user, nil | ||
| 139 | + } | ||
| 140 | +} | ||
| 141 | + | ||
| 142 | +// 返回列表 | ||
| 143 | +func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (interface{}, error) { | ||
| 144 | + if err := listUserQuery.ValidateQuery(); err != nil { | ||
| 145 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 146 | + } | ||
| 147 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 148 | + if err != nil { | ||
| 149 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 150 | + } | ||
| 151 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 152 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 153 | + } | ||
| 154 | + defer func() { | ||
| 155 | + transactionContext.RollbackTransaction() | ||
| 156 | + }() | ||
| 157 | + var userRepository domain.UserRepository | ||
| 158 | + if value, err := factory.CreateUserRepository(map[string]interface{}{ | ||
| 159 | + "transactionContext": transactionContext, | ||
| 160 | + }); err != nil { | ||
| 161 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 162 | + } else { | ||
| 163 | + userRepository = value | ||
| 164 | + } | ||
| 165 | + if count, users, err := userRepository.Find(tool_funs.SimpleStructToMap(listUserQuery)); err != nil { | ||
| 166 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 167 | + } else { | ||
| 168 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 169 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 170 | + } | ||
| 171 | + return map[string]interface{}{ | ||
| 172 | + "count": count, | ||
| 173 | + "users": users, | ||
| 174 | + }, nil | ||
| 175 | + } | ||
| 176 | +} | ||
| 177 | + | ||
| 178 | +// 移除 | ||
| 179 | +func (userService *UserService) RemoveUser(removeUserCommand *command.RemoveUserCommand) (interface{}, error) { | ||
| 180 | + if err := removeUserCommand.ValidateCommand(); err != nil { | ||
| 181 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 182 | + } | ||
| 183 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 184 | + if err != nil { | ||
| 185 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 186 | + } | ||
| 187 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 188 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 189 | + } | ||
| 190 | + defer func() { | ||
| 191 | + transactionContext.RollbackTransaction() | ||
| 192 | + }() | ||
| 193 | + var userRepository domain.UserRepository | ||
| 194 | + if value, err := factory.CreateUserRepository(map[string]interface{}{ | ||
| 195 | + "transactionContext": transactionContext, | ||
| 196 | + }); err != nil { | ||
| 197 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 198 | + } else { | ||
| 199 | + userRepository = value | ||
| 200 | + } | ||
| 201 | + user, err := userRepository.FindOne(map[string]interface{}{"userId": removeUserCommand.UserId}) | ||
| 202 | + if err != nil { | ||
| 203 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 204 | + } | ||
| 205 | + if user == nil { | ||
| 206 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeUserCommand.UserId))) | ||
| 207 | + } | ||
| 208 | + if user, err := userRepository.Remove(user); err != nil { | ||
| 209 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 210 | + } else { | ||
| 211 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 212 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 213 | + } | ||
| 214 | + return user, nil | ||
| 215 | + } | ||
| 216 | +} | ||
| 217 | + | ||
| 218 | +// 设置权限 | ||
| 219 | +func (userService *UserService) SetPermission(setPermissionCommand *command.SetPermissionCommand) (interface{}, error) { | ||
| 220 | + if err := setPermissionCommand.ValidateCommand(); err != nil { | ||
| 221 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 222 | + } | ||
| 223 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 224 | + if err != nil { | ||
| 225 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 226 | + } | ||
| 227 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 228 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 229 | + } | ||
| 230 | + defer func() { | ||
| 231 | + transactionContext.RollbackTransaction() | ||
| 232 | + }() | ||
| 233 | + | ||
| 234 | + var userRepository domain.UserRepository | ||
| 235 | + if value, err := factory.CreateUserRepository(map[string]interface{}{ | ||
| 236 | + "transactionContext": transactionContext, | ||
| 237 | + }); err != nil { | ||
| 238 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 239 | + } else { | ||
| 240 | + userRepository = value | ||
| 241 | + } | ||
| 242 | + user, err := userRepository.FindOne(map[string]interface{}{"userId": setPermissionCommand.UserId}) | ||
| 243 | + if err != nil { | ||
| 244 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 245 | + } | ||
| 246 | + if user == nil { | ||
| 247 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(setPermissionCommand.UserId))) | ||
| 248 | + } | ||
| 249 | + if err := user.SetPermission(setPermissionCommand.Permissons); err != nil { | ||
| 250 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
| 251 | + } | ||
| 252 | + if _, err := userRepository.Save(user); err != nil { | ||
| 253 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 257 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 258 | + } | ||
| 259 | + return nil, nil | ||
| 260 | +} | ||
| 261 | + | ||
| 262 | +// 更新 | ||
| 263 | +func (userService *UserService) UpdateUser(updateUserCommand *command.UpdateUserCommand) (interface{}, error) { | ||
| 264 | + if err := updateUserCommand.ValidateCommand(); err != nil { | ||
| 265 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 266 | + } | ||
| 267 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 268 | + if err != nil { | ||
| 269 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 270 | + } | ||
| 271 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 272 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 273 | + } | ||
| 274 | + defer func() { | ||
| 275 | + transactionContext.RollbackTransaction() | ||
| 276 | + }() | ||
| 277 | + var userRepository domain.UserRepository | ||
| 278 | + if value, err := factory.CreateUserRepository(map[string]interface{}{ | ||
| 279 | + "transactionContext": transactionContext, | ||
| 280 | + }); err != nil { | ||
| 281 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 282 | + } else { | ||
| 283 | + userRepository = value | ||
| 284 | + } | ||
| 285 | + user, err := userRepository.FindOne(map[string]interface{}{"userId": updateUserCommand.UserId}) | ||
| 286 | + if err != nil { | ||
| 287 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 288 | + } | ||
| 289 | + if user == nil { | ||
| 290 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateUserCommand.UserId))) | ||
| 291 | + } | ||
| 292 | + if err := user.Update(tool_funs.SimpleStructToMap(updateUserCommand)); err != nil { | ||
| 293 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
| 294 | + } | ||
| 295 | + if user, err := userRepository.Save(user); err != nil { | ||
| 296 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 297 | + } else { | ||
| 298 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 299 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 300 | + } | ||
| 301 | + return user, nil | ||
| 302 | + } | ||
| 303 | +} | ||
| 304 | + | ||
| 305 | +func NewUserService(options map[string]interface{}) *UserService { | ||
| 306 | + newUserService := &UserService{} | ||
| 307 | + return newUserService | ||
| 308 | +} |
pkg/domain/service/add_user.go
0 → 100644
| 1 | package domain | 1 | package domain |
| 2 | 2 | ||
| 3 | -import "time" | 3 | +import ( |
| 4 | + "fmt" | ||
| 5 | + "time" | ||
| 6 | +) | ||
| 4 | 7 | ||
| 5 | const ( | 8 | const ( |
| 6 | - StatusEnable int64 = 1 | ||
| 7 | - StatusDisable int64 = 2 | 9 | + StatusEnable int64 = 1 //启用 |
| 10 | + StatusDisable int64 = 2 //禁用 | ||
| 8 | ) | 11 | ) |
| 12 | + | ||
| 13 | +// 管理员类型 1.超级管理员 10:企业管理员 100:普通用户 | ||
| 9 | const ( | 14 | const ( |
| 10 | - SuperAdministrator = 1 | ||
| 11 | - EnterpriseAdministrator = 10 | ||
| 12 | - NormalUser = 100 | 15 | + SuperAdministrator = 1 //超级管理员 |
| 16 | + EnterpriseAdministrator = 10 //企业管理员 | ||
| 17 | + NormalUser = 100 //普通用户 | ||
| 13 | ) | 18 | ) |
| 14 | 19 | ||
| 15 | // UserType | 20 | // UserType |
| 16 | const ( | 21 | const ( |
| 17 | - Manager = 1 | ||
| 18 | - Partner = 2 | ||
| 19 | - Guest = 3 | 22 | + Manager = 1 //高管 |
| 23 | + Partner = 2 //合伙人 | ||
| 24 | + Guest = 3 //游客 | ||
| 20 | ) | 25 | ) |
| 21 | 26 | ||
| 22 | // 用户实体 | 27 | // 用户实体 |
| @@ -25,6 +30,8 @@ type User struct { | @@ -25,6 +30,8 @@ type User struct { | ||
| 25 | UserId int64 `json:"userId"` | 30 | UserId int64 `json:"userId"` |
| 26 | // 1.高管 2.合伙人 4:游客 | 31 | // 1.高管 2.合伙人 4:游客 |
| 27 | UserType int `json:"userType"` | 32 | UserType int `json:"userType"` |
| 33 | + //用户账号(手机号) | ||
| 34 | + UserAccount string `json:"userAccount"` | ||
| 28 | // 用户权限 | 35 | // 用户权限 |
| 29 | Permissions []int `json:"permissions"` | 36 | Permissions []int `json:"permissions"` |
| 30 | // 公司Id | 37 | // 公司Id |
| @@ -61,66 +68,84 @@ func (user *User) Identify() interface{} { | @@ -61,66 +68,84 @@ func (user *User) Identify() interface{} { | ||
| 61 | return user.UserId | 68 | return user.UserId |
| 62 | } | 69 | } |
| 63 | 70 | ||
| 64 | -func (user *User) Update(data map[string]interface{}) error { | ||
| 65 | - if userId, ok := data["userId"]; ok { | ||
| 66 | - user.UserId = userId.(int64) | 71 | +func (user *User) SetPermission(permissions []int) error { |
| 72 | + user.Permissions = permissions | ||
| 73 | + return nil | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +func (user *User) CovertUserStatus(status int64) error { | ||
| 77 | + if !(status == StatusEnable || status == StatusDisable) { | ||
| 78 | + return fmt.Errorf("status options value (%v,%v) , invalid %v", StatusEnable, StatusDisable, status) | ||
| 67 | } | 79 | } |
| 80 | + user.Status = status | ||
| 81 | + return nil | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +func (user *User) Update(data map[string]interface{}) error { | ||
| 68 | if userType, ok := data["userType"]; ok { | 85 | if userType, ok := data["userType"]; ok { |
| 69 | user.UserType = userType.(int) | 86 | user.UserType = userType.(int) |
| 70 | } | 87 | } |
| 71 | if permissions, ok := data["permissions"]; ok { | 88 | if permissions, ok := data["permissions"]; ok { |
| 72 | user.Permissions = permissions.([]int) | 89 | user.Permissions = permissions.([]int) |
| 73 | } | 90 | } |
| 74 | - if companyId, ok := data["companyId"]; ok { | ||
| 75 | - user.CompanyId = companyId.(int64) | ||
| 76 | - } | ||
| 77 | - if isPrincipal, ok := data["isPrincipal"]; ok { | ||
| 78 | - user.UserInfo.IsPrincipal = isPrincipal.(bool) | ||
| 79 | - } | ||
| 80 | - if uid, ok := data["uid"]; ok { | ||
| 81 | - user.UserInfo.Uid = uid.(int64) | 91 | + if userInfo, ok := data["userInfo"]; ok { |
| 92 | + user.UserInfo = userInfo.(*UserInfo) | ||
| 82 | } | 93 | } |
| 83 | - if userAccount, ok := data["userAccount"]; ok { | ||
| 84 | - user.UserInfo.UserAccount = userAccount.(string) | 94 | + if partnerAccount, ok := data["partnerAccount"]; ok { |
| 95 | + user.PartnerInfo.PartnerAccount = partnerAccount.(string) | ||
| 85 | } | 96 | } |
| 86 | - if userAvatarUrl, ok := data["userAvatarUrl"]; ok { | ||
| 87 | - user.UserInfo.UserAvatarUrl = userAvatarUrl.(string) | 97 | + if partnerName, ok := data["partnerName"]; ok { |
| 98 | + user.PartnerInfo.PartnerName = partnerName.(string) | ||
| 88 | } | 99 | } |
| 89 | - if userName, ok := data["userName"]; ok { | ||
| 90 | - user.UserInfo.UserName = userName.(string) | 100 | + if regionName, ok := data["regionName"]; ok { |
| 101 | + user.PartnerInfo.RegionInfo.RegionName = regionName.(string) | ||
| 91 | } | 102 | } |
| 92 | - if email, ok := data["email"]; ok { | ||
| 93 | - user.UserInfo.Email = email.(string) | 103 | + if status, ok := data["status"]; ok { |
| 104 | + user.PartnerInfo.Status = status.(int64) | ||
| 94 | } | 105 | } |
| 95 | - if gender, ok := data["gender"]; ok { | ||
| 96 | - user.UserInfo.Gender = gender.(int) | 106 | + user.UpdateAt = time.Now() |
| 107 | + return nil | ||
| 108 | +} | ||
| 109 | + | ||
| 110 | +//添加/编辑高管 | ||
| 111 | + | ||
| 112 | +func CheckAdminUserType(dm *User) (*User, error) { | ||
| 113 | + if dm.UserInfo.Uid == int64(0) { | ||
| 114 | + return nil, fmt.Errorf("高管的uid不能为空") | ||
| 97 | } | 115 | } |
| 98 | - if entryTime, ok := data["entryTime"]; ok { | ||
| 99 | - user.UserInfo.EntryTime = entryTime.(time.Time) | 116 | + if dm.UserInfo.Email == "" { |
| 117 | + return nil, fmt.Errorf("高管的Email不能为空") | ||
| 100 | } | 118 | } |
| 101 | - if extension, ok := data["extension"]; ok { | ||
| 102 | - user.UserInfo.Extension = extension.(string) | 119 | + if dm.UserInfo.EntryTime.IsZero() { |
| 120 | + return nil, fmt.Errorf("高管的入职时间不能为空") | ||
| 103 | } | 121 | } |
| 104 | - if workplace, ok := data["workplace"]; ok { | ||
| 105 | - user.UserInfo.Workplace = workplace.(string) | 122 | + if dm.UserInfo.Workplace == "" { |
| 123 | + return nil, fmt.Errorf("高管的工作地不能为空") | ||
| 106 | } | 124 | } |
| 107 | - if privateNumber, ok := data["privateNumber"]; ok { | ||
| 108 | - user.UserInfo.PrivateNumber = privateNumber.(string) | 125 | + if dm.UserInfo.JobNumber == "" { |
| 126 | + return nil, fmt.Errorf("高管的工号不能为空") | ||
| 109 | } | 127 | } |
| 110 | - if jobNumber, ok := data["jobNumber"]; ok { | ||
| 111 | - user.UserInfo.JobNumber = jobNumber.(string) | 128 | + return dm, nil |
| 129 | +} | ||
| 130 | + | ||
| 131 | +//添加/编辑合伙人 | ||
| 132 | +type PartnerUserType struct{} | ||
| 133 | + | ||
| 134 | +func CheckPartnerUserType(dm *User) (*User, error) { | ||
| 135 | + if dm.PartnerInfo.Status == 0 { | ||
| 136 | + return nil, fmt.Errorf("合伙人的状态不能为空") | ||
| 112 | } | 137 | } |
| 113 | - if partnerAccount, ok := data["partnerAccount"]; ok { | ||
| 114 | - user.PartnerInfo.PartnerAccount = partnerAccount.(string) | 138 | + if dm.PartnerInfo.PartnerAccount == "" { |
| 139 | + return nil, fmt.Errorf("合伙人账号不能为空") | ||
| 115 | } | 140 | } |
| 116 | - if partnerName, ok := data["partnerName"]; ok { | ||
| 117 | - user.PartnerInfo.PartnerName = partnerName.(string) | 141 | + if dm.PartnerInfo.PartnerName == "" { |
| 142 | + return nil, fmt.Errorf("合伙人姓名不能为空") | ||
| 118 | } | 143 | } |
| 119 | - if regionName, ok := data["regionName"]; ok { | ||
| 120 | - user.PartnerInfo.RegionInfo.RegionName = regionName.(string) | 144 | + if len(dm.PartnerInfo.PartnerCategorys) == 0 { |
| 145 | + return nil, fmt.Errorf("合伙人类别不能为空") | ||
| 121 | } | 146 | } |
| 122 | - if status, ok := data["status"]; ok { | ||
| 123 | - user.PartnerInfo.Status = status.(int64) | 147 | + if len(dm.PartnerInfo.Salesmans) == 0 { |
| 148 | + return nil, fmt.Errorf("合伙人查看合伙人账号不能为空") | ||
| 124 | } | 149 | } |
| 125 | - return nil | 150 | + return dm, nil |
| 126 | } | 151 | } |
| 1 | +package domainService | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + coreDomain "github.com/linmadan/egglib-go/core/domain" | ||
| 6 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain" | ||
| 7 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/repository" | ||
| 8 | + | ||
| 9 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
| 10 | +) | ||
| 11 | + | ||
| 12 | +type AddUserService struct { | ||
| 13 | + coreDomain.BaseEventPublisher | ||
| 14 | + transactionContext *pgTransaction.TransactionContext | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +func (service *AddUserService) AddUser(user *domain.User) (*domain.User, error) { | ||
| 18 | + | ||
| 19 | + if user.UserType&1 > 0 { | ||
| 20 | + if _, err := domain.CheckAdminUserType(user); err != nil { | ||
| 21 | + return nil, err | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + if user.UserType&2 > 0 { | ||
| 26 | + if _, err := domain.CheckPartnerUserType(user); err != nil { | ||
| 27 | + return nil, err | ||
| 28 | + } | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + var userRepository domain.UserRepository | ||
| 32 | + if repository, err := repository.NewUserRepository(service.transactionContext); err != nil { | ||
| 33 | + return nil, err | ||
| 34 | + } else { | ||
| 35 | + userRepository = repository | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + count, existUser, err := userRepository.Find(map[string]interface{}{ | ||
| 39 | + "userAccount": user.UserAccount, | ||
| 40 | + "companyId": user.CompanyId, | ||
| 41 | + }) | ||
| 42 | + | ||
| 43 | + if count != 0 { | ||
| 44 | + if existUser[0].UserType^user.UserType == 0 { | ||
| 45 | + return nil, fmt.Errorf("用户已存在") | ||
| 46 | + } | ||
| 47 | + if existUser[0].UserType&1 > 0 { | ||
| 48 | + user.UserInfo = existUser[0].UserInfo | ||
| 49 | + } | ||
| 50 | + if existUser[0].UserType&2 > 0 { | ||
| 51 | + user.PartnerInfo = existUser[0].PartnerInfo | ||
| 52 | + } | ||
| 53 | + user.UserType = existUser[0].UserType + user.UserType | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + user, err = userRepository.Save(user) | ||
| 57 | + | ||
| 58 | + if err != nil { | ||
| 59 | + return nil, err | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + return user, nil | ||
| 63 | +} | ||
| 64 | + | ||
| 65 | +func NewAddUserService(transactionContext *pgTransaction.TransactionContext) (*AddUserService, error) { | ||
| 66 | + if transactionContext == nil { | ||
| 67 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
| 68 | + } else { | ||
| 69 | + return &AddUserService{ | ||
| 70 | + transactionContext: transactionContext, | ||
| 71 | + }, nil | ||
| 72 | + } | ||
| 73 | +} |
| @@ -13,6 +13,8 @@ type User struct { | @@ -13,6 +13,8 @@ type User struct { | ||
| 13 | UserType int | 13 | UserType int |
| 14 | // 用户权限 | 14 | // 用户权限 |
| 15 | Permissions []int `pg:",array"` | 15 | Permissions []int `pg:",array"` |
| 16 | + //用户账号(手机号) | ||
| 17 | + UserAccount string | ||
| 16 | // 公司Id | 18 | // 公司Id |
| 17 | CompanyId int64 | 19 | CompanyId int64 |
| 18 | // 用户信息 | 20 | // 用户信息 |
| @@ -30,6 +30,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | @@ -30,6 +30,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | ||
| 30 | "user_type", | 30 | "user_type", |
| 31 | "permissions", | 31 | "permissions", |
| 32 | "company_id", | 32 | "company_id", |
| 33 | + "user_account", | ||
| 33 | "user_info", | 34 | "user_info", |
| 34 | "partner_info", | 35 | "partner_info", |
| 35 | "status", | 36 | "status", |
| @@ -42,7 +43,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | @@ -42,7 +43,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | ||
| 42 | insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) | 43 | insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) |
| 43 | insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields) | 44 | insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields) |
| 44 | returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) | 45 | returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) |
| 45 | - updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "user_id") | 46 | + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "user_id", "delete_at", "create_at") |
| 46 | updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields) | 47 | updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields) |
| 47 | tx := repository.transactionContext.PgTx | 48 | tx := repository.transactionContext.PgTx |
| 48 | if user.Identify() == nil { | 49 | if user.Identify() == nil { |
| @@ -58,6 +59,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | @@ -58,6 +59,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | ||
| 58 | &user.UserType, | 59 | &user.UserType, |
| 59 | pg.Array(&user.Permissions), | 60 | pg.Array(&user.Permissions), |
| 60 | &user.CompanyId, | 61 | &user.CompanyId, |
| 62 | + &user.UserAccount, | ||
| 61 | &user.UserInfo, | 63 | &user.UserInfo, |
| 62 | &user.PartnerInfo, | 64 | &user.PartnerInfo, |
| 63 | &user.Status, | 65 | &user.Status, |
| @@ -72,6 +74,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | @@ -72,6 +74,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | ||
| 72 | user.UserType, | 74 | user.UserType, |
| 73 | pg.Array(user.Permissions), | 75 | pg.Array(user.Permissions), |
| 74 | user.CompanyId, | 76 | user.CompanyId, |
| 77 | + user.UserAccount, | ||
| 75 | user.UserInfo, | 78 | user.UserInfo, |
| 76 | user.PartnerInfo, | 79 | user.PartnerInfo, |
| 77 | user.Status, | 80 | user.Status, |
| @@ -90,6 +93,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | @@ -90,6 +93,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | ||
| 90 | &user.UserType, | 93 | &user.UserType, |
| 91 | pg.Array(&user.Permissions), | 94 | pg.Array(&user.Permissions), |
| 92 | &user.CompanyId, | 95 | &user.CompanyId, |
| 96 | + &user.UserAccount, | ||
| 93 | &user.UserInfo, | 97 | &user.UserInfo, |
| 94 | &user.PartnerInfo, | 98 | &user.PartnerInfo, |
| 95 | &user.Status, | 99 | &user.Status, |
| @@ -100,18 +104,16 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | @@ -100,18 +104,16 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | ||
| 100 | &user.DeleteAt, | 104 | &user.DeleteAt, |
| 101 | ), | 105 | ), |
| 102 | fmt.Sprintf("UPDATE users SET %s WHERE user_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | 106 | fmt.Sprintf("UPDATE users SET %s WHERE user_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), |
| 103 | - user.UserId, | ||
| 104 | user.UserType, | 107 | user.UserType, |
| 105 | pg.Array(user.Permissions), | 108 | pg.Array(user.Permissions), |
| 106 | user.CompanyId, | 109 | user.CompanyId, |
| 110 | + user.UserAccount, | ||
| 107 | user.UserInfo, | 111 | user.UserInfo, |
| 108 | user.PartnerInfo, | 112 | user.PartnerInfo, |
| 109 | user.Status, | 113 | user.Status, |
| 110 | user.AdminType, | 114 | user.AdminType, |
| 111 | pg.Array(user.AccessPartners), | 115 | pg.Array(user.AccessPartners), |
| 112 | - user.CreateAt, | ||
| 113 | user.UpdateAt, | 116 | user.UpdateAt, |
| 114 | - user.DeleteAt, | ||
| 115 | user.Identify(), | 117 | user.Identify(), |
| 116 | ); err != nil { | 118 | ); err != nil { |
| 117 | return user, err | 119 | return user, err |
| 1 | +package controllers | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/linmadan/egglib-go/web/beego" | ||
| 5 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/command" | ||
| 6 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/query" | ||
| 7 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/service" | ||
| 8 | +) | ||
| 9 | + | ||
| 10 | +type UserController struct { | ||
| 11 | + beego.BaseController | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +func (controller *UserController) CreateUser() { | ||
| 15 | + userService := service.NewUserService(nil) | ||
| 16 | + createUserCommand := &command.CreateUserCommand{} | ||
| 17 | + controller.Unmarshal(createUserCommand) | ||
| 18 | + data, err := userService.CreateUser(createUserCommand) | ||
| 19 | + controller.Response(data, err) | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +func (controller *UserController) UpdateUser() { | ||
| 23 | + userService := service.NewUserService(nil) | ||
| 24 | + updateUserCommand := &command.UpdateUserCommand{} | ||
| 25 | + controller.Unmarshal(updateUserCommand) | ||
| 26 | + userId, _ := controller.GetInt64(":userId") | ||
| 27 | + updateUserCommand.UserId = userId | ||
| 28 | + data, err := userService.UpdateUser(updateUserCommand) | ||
| 29 | + controller.Response(data, err) | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +func (controller *UserController) GetUser() { | ||
| 33 | + userService := service.NewUserService(nil) | ||
| 34 | + getUserQuery := &query.GetUserQuery{} | ||
| 35 | + userId, _ := controller.GetInt64(":userId") | ||
| 36 | + getUserQuery.UserId = userId | ||
| 37 | + data, err := userService.GetUser(getUserQuery) | ||
| 38 | + controller.Response(data, err) | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +func (controller *UserController) RemoveUser() { | ||
| 42 | + userService := service.NewUserService(nil) | ||
| 43 | + removeUserCommand := &command.RemoveUserCommand{} | ||
| 44 | + controller.Unmarshal(removeUserCommand) | ||
| 45 | + userId, _ := controller.GetInt64(":userId") | ||
| 46 | + removeUserCommand.UserId = userId | ||
| 47 | + data, err := userService.RemoveUser(removeUserCommand) | ||
| 48 | + controller.Response(data, err) | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +func (controller *UserController) ListUser() { | ||
| 52 | + userService := service.NewUserService(nil) | ||
| 53 | + listUserQuery := &query.ListUserQuery{} | ||
| 54 | + offset, _ := controller.GetInt("offset") | ||
| 55 | + listUserQuery.Offset = offset | ||
| 56 | + limit, _ := controller.GetInt("limit") | ||
| 57 | + listUserQuery.Limit = limit | ||
| 58 | + data, err := userService.ListUser(listUserQuery) | ||
| 59 | + controller.Response(data, err) | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +func (controller *UserController) ConvertUserStatus() { | ||
| 63 | + userService := service.NewUserService(nil) | ||
| 64 | + convertUserStatusCommand := &command.ConvertUserStatusCommand{} | ||
| 65 | + controller.Unmarshal(convertUserStatusCommand) | ||
| 66 | + data, err := userService.ConvertUserStatus(convertUserStatusCommand) | ||
| 67 | + controller.Response(data, err) | ||
| 68 | +} | ||
| 69 | + | ||
| 70 | +func (controller *UserController) SetPermission() { | ||
| 71 | + userService := service.NewUserService(nil) | ||
| 72 | + setPermissionCommand := &command.SetPermissionCommand{} | ||
| 73 | + controller.Unmarshal(setPermissionCommand) | ||
| 74 | + data, err := userService.SetPermission(setPermissionCommand) | ||
| 75 | + controller.Response(data, err) | ||
| 76 | +} |
pkg/port/beego/routers/user_router.go
0 → 100644
| 1 | +package routers | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/beego/beego/v2/server/web" | ||
| 5 | + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego/controllers" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +func init() { | ||
| 9 | + web.Router("/users/", &controllers.UserController{}, "Post:CreateUser") | ||
| 10 | + web.Router("/users/:userId", &controllers.UserController{}, "Put:UpdateUser") | ||
| 11 | + web.Router("/users/:userId", &controllers.UserController{}, "Get:GetUser") | ||
| 12 | + web.Router("/users/:userId", &controllers.UserController{}, "Delete:RemoveUser") | ||
| 13 | + web.Router("/users/", &controllers.UserController{}, "Get:ListUser") | ||
| 14 | + web.Router("/users/convert-user-status", &controllers.UserController{}, "Post:ConvertUserStatus") | ||
| 15 | + web.Router("/users/set-permission", &controllers.UserController{}, "Post:SetPermission") | ||
| 16 | +} |
| 1 | +package user | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("用户状态转换(禁用、启用)", func() { | ||
| 14 | + var userId int64 | ||
| 15 | + BeforeEach(func() { | ||
| 16 | + _, err := pG.DB.QueryOne( | ||
| 17 | + pg.Scan(&userId), | ||
| 18 | + "INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id", | ||
| 19 | + 1, 1, pg.Array([]int{}), 1, `{}`, `{}`, 1, 1) | ||
| 20 | + Expect(err).NotTo(HaveOccurred()) | ||
| 21 | + }) | ||
| 22 | + Describe("用户状态转换(禁用、启用)", func() { | ||
| 23 | + Context("", func() { | ||
| 24 | + It("", func() { | ||
| 25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 26 | + body := map[string]interface{}{ | ||
| 27 | + "userId": 1, | ||
| 28 | + "status": 2, | ||
| 29 | + } | ||
| 30 | + httpExpect.POST("/users/convert-user-status"). | ||
| 31 | + WithJSON(body). | ||
| 32 | + Expect(). | ||
| 33 | + Status(http.StatusOK). | ||
| 34 | + JSON(). | ||
| 35 | + Object(). | ||
| 36 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 37 | + ContainsKey("msg").ValueEqual("msg", "ok") | ||
| 38 | + //ContainsKey("data").Value("data").Object() | ||
| 39 | + }) | ||
| 40 | + }) | ||
| 41 | + }) | ||
| 42 | + AfterEach(func() { | ||
| 43 | + _, err := pG.DB.Exec("DELETE FROM users WHERE true") | ||
| 44 | + Expect(err).NotTo(HaveOccurred()) | ||
| 45 | + }) | ||
| 46 | +}) |
| 1 | +package user | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | +) | ||
| 11 | + | ||
| 12 | +var _ = Describe("创建", func() { | ||
| 13 | + Describe("提交数据创建", func() { | ||
| 14 | + Context("提交正确的新用户实体数据", func() { | ||
| 15 | + It("返回用户实体数据", func() { | ||
| 16 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 17 | + body := map[string]interface{}{ | ||
| 18 | + "userType": 1, | ||
| 19 | + "adminType": 1, | ||
| 20 | + "status": 1, | ||
| 21 | + "userAccount": "13800000000", | ||
| 22 | + "userInfo": map[string]interface{}{ | ||
| 23 | + "isPrincipal": false, | ||
| 24 | + "uid": 5678664411598702, | ||
| 25 | + "userAccount": "13800000000", | ||
| 26 | + "userAvatarUrl": "www.baidu.com", | ||
| 27 | + "userName": "彭于晏", | ||
| 28 | + "email": "gfghf@qq.com", | ||
| 29 | + "gender": 1, | ||
| 30 | + "extension": "1", | ||
| 31 | + "workplace": "2", | ||
| 32 | + "privateNumber": "3", | ||
| 33 | + "jobNumber": "4", | ||
| 34 | + }, | ||
| 35 | + "partnerCategorys": "[]", | ||
| 36 | + "accessPartners": "[]", | ||
| 37 | + } | ||
| 38 | + httpExpect.POST("/users/"). | ||
| 39 | + WithJSON(body). | ||
| 40 | + Expect(). | ||
| 41 | + Status(http.StatusOK). | ||
| 42 | + JSON(). | ||
| 43 | + Object(). | ||
| 44 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 45 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 46 | + ContainsKey("data").Value("data").Object(). | ||
| 47 | + ContainsKey("userId").ValueNotEqual("userId", BeZero()) | ||
| 48 | + }) | ||
| 49 | + }) | ||
| 50 | + }) | ||
| 51 | + AfterEach(func() { | ||
| 52 | + _, err := pG.DB.Exec("DELETE FROM users WHERE true") | ||
| 53 | + _, err = pG.DB.Exec("DELETE FROM user_auth WHERE true") | ||
| 54 | + Expect(err).NotTo(HaveOccurred()) | ||
| 55 | + }) | ||
| 56 | +}) |
test/integration/beego/user/get_user_test.go
0 → 100644
| 1 | +package user | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("返回", func() { | ||
| 14 | + var userId int64 | ||
| 15 | + BeforeEach(func() { | ||
| 16 | + _, err := pG.DB.QueryOne( | ||
| 17 | + pg.Scan(&userId), | ||
| 18 | + "INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id", | ||
| 19 | + 1, 1, pg.Array([]int{}), 1, `{}`, `{}`, 1, 1) | ||
| 20 | + Expect(err).NotTo(HaveOccurred()) | ||
| 21 | + }) | ||
| 22 | + Describe("根据userId参数返回用户实体", func() { | ||
| 23 | + Context("传入有效的userId", func() { | ||
| 24 | + It("返回用户实体数据", func() { | ||
| 25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 26 | + httpExpect.GET("/users/1"). | ||
| 27 | + Expect(). | ||
| 28 | + Status(http.StatusOK). | ||
| 29 | + JSON(). | ||
| 30 | + Object(). | ||
| 31 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 32 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 33 | + ContainsKey("data").Value("data").Object() | ||
| 34 | + }) | ||
| 35 | + }) | ||
| 36 | + }) | ||
| 37 | + AfterEach(func() { | ||
| 38 | + _, err := pG.DB.Exec("DELETE FROM users WHERE true") | ||
| 39 | + Expect(err).NotTo(HaveOccurred()) | ||
| 40 | + }) | ||
| 41 | +}) |
| 1 | +package user | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("返回列表", func() { | ||
| 14 | + var userId int64 | ||
| 15 | + BeforeEach(func() { | ||
| 16 | + _, err := pG.DB.QueryOne( | ||
| 17 | + pg.Scan(&userId), | ||
| 18 | + "INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id", | ||
| 19 | + 1, 1, pg.Array([]int{}), 1, `{}`, `{}`, 1, 1) | ||
| 20 | + Expect(err).NotTo(HaveOccurred()) | ||
| 21 | + }) | ||
| 22 | + Describe("根据参数返回用户实体列表", func() { | ||
| 23 | + Context("传入有效的参数", func() { | ||
| 24 | + It("返回用户实体数据列表", func() { | ||
| 25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 26 | + httpExpect.GET("/users/"). | ||
| 27 | + WithQuery("offset", 0). | ||
| 28 | + WithQuery("limit", 10). | ||
| 29 | + Expect(). | ||
| 30 | + Status(http.StatusOK). | ||
| 31 | + JSON(). | ||
| 32 | + Object(). | ||
| 33 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 34 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 35 | + ContainsKey("data").Value("data").Object(). | ||
| 36 | + ContainsKey("count").ValueEqual("count", 1). | ||
| 37 | + ContainsKey("users").Value("users").Array() | ||
| 38 | + }) | ||
| 39 | + }) | ||
| 40 | + }) | ||
| 41 | + AfterEach(func() { | ||
| 42 | + _, err := pG.DB.Exec("DELETE FROM users WHERE true") | ||
| 43 | + Expect(err).NotTo(HaveOccurred()) | ||
| 44 | + }) | ||
| 45 | +}) |
| 1 | +package user | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("移除", func() { | ||
| 14 | + var userId int64 | ||
| 15 | + BeforeEach(func() { | ||
| 16 | + _, err := pG.DB.QueryOne( | ||
| 17 | + pg.Scan(&userId), | ||
| 18 | + "INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id", | ||
| 19 | + 1, 1, pg.Array([]int{}), 1, `{}`, `{}`, 1, 1) | ||
| 20 | + Expect(err).NotTo(HaveOccurred()) | ||
| 21 | + }) | ||
| 22 | + Describe("根据参数移除", func() { | ||
| 23 | + Context("传入有效的userId", func() { | ||
| 24 | + It("返回被移除用户实体的数据", func() { | ||
| 25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 26 | + httpExpect.DELETE("/users/1"). | ||
| 27 | + Expect(). | ||
| 28 | + Status(http.StatusOK). | ||
| 29 | + JSON(). | ||
| 30 | + Object(). | ||
| 31 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 32 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 33 | + ContainsKey("data").Value("data").Object() | ||
| 34 | + }) | ||
| 35 | + }) | ||
| 36 | + }) | ||
| 37 | + AfterEach(func() { | ||
| 38 | + _, err := pG.DB.Exec("DELETE FROM users WHERE true") | ||
| 39 | + Expect(err).NotTo(HaveOccurred()) | ||
| 40 | + }) | ||
| 41 | +}) |
| 1 | +package user | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("设置权限", func() { | ||
| 14 | + return | ||
| 15 | + var userId int64 | ||
| 16 | + BeforeEach(func() { | ||
| 17 | + _, err := pG.DB.QueryOne( | ||
| 18 | + pg.Scan(&userId), | ||
| 19 | + "INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id", | ||
| 20 | + 1, 1, pg.Array([]int{}), 1, `{}`, `{}`, 1, 1) | ||
| 21 | + Expect(err).NotTo(HaveOccurred()) | ||
| 22 | + }) | ||
| 23 | + Describe("设置权限", func() { | ||
| 24 | + Context("", func() { | ||
| 25 | + It("", func() { | ||
| 26 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 27 | + body := map[string]interface{}{ | ||
| 28 | + "userId": 1, | ||
| 29 | + "permissons": []int{1, 2, 3}, | ||
| 30 | + } | ||
| 31 | + httpExpect.POST("/users/setPermission"). | ||
| 32 | + WithJSON(body). | ||
| 33 | + Expect(). | ||
| 34 | + Status(http.StatusOK). | ||
| 35 | + JSON(). | ||
| 36 | + Object(). | ||
| 37 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 38 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 39 | + ContainsKey("data").Value("data").Object() | ||
| 40 | + }) | ||
| 41 | + }) | ||
| 42 | + }) | ||
| 43 | + AfterEach(func() { | ||
| 44 | + _, err := pG.DB.Exec("DELETE FROM users WHERE true") | ||
| 45 | + Expect(err).NotTo(HaveOccurred()) | ||
| 46 | + }) | ||
| 47 | +}) |
| 1 | +package user | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("更新", func() { | ||
| 14 | + var userId int64 | ||
| 15 | + BeforeEach(func() { | ||
| 16 | + _, err := pG.DB.QueryOne( | ||
| 17 | + pg.Scan(&userId), | ||
| 18 | + "INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id", | ||
| 19 | + 1, 1, pg.Array([]int{}), 1, `{}`, `{}`, 1, 1) | ||
| 20 | + Expect(err).NotTo(HaveOccurred()) | ||
| 21 | + }) | ||
| 22 | + Describe("提交数据更新", func() { | ||
| 23 | + Context("提交正确的用户实体数据", func() { | ||
| 24 | + It("返回更新后的用户实体数据", func() { | ||
| 25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 26 | + body := map[string]interface{}{ | ||
| 27 | + "userInfo": map[string]interface{}{ | ||
| 28 | + "isPrincipal": false, | ||
| 29 | + "uid": 5678664411598702, | ||
| 30 | + "userAccount": "13800000000", | ||
| 31 | + "userAvatarUrl": "www.baidu.com", | ||
| 32 | + "userName": "彭于晏01", | ||
| 33 | + "email": "gfghf@qq.com", | ||
| 34 | + "gender": 1, | ||
| 35 | + "extension": "1", | ||
| 36 | + "workplace": "2", | ||
| 37 | + "privateNumber": "3", | ||
| 38 | + "jobNumber": "4", | ||
| 39 | + }, | ||
| 40 | + } | ||
| 41 | + httpExpect.PUT("/users/1"). | ||
| 42 | + WithJSON(body). | ||
| 43 | + Expect(). | ||
| 44 | + Status(http.StatusOK). | ||
| 45 | + JSON(). | ||
| 46 | + Object(). | ||
| 47 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 48 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 49 | + ContainsKey("data").Value("data").Object(). | ||
| 50 | + ContainsKey("userId").ValueEqual("userId", userId) | ||
| 51 | + }) | ||
| 52 | + }) | ||
| 53 | + }) | ||
| 54 | + AfterEach(func() { | ||
| 55 | + _, err := pG.DB.Exec("DELETE FROM users WHERE true") | ||
| 56 | + Expect(err).NotTo(HaveOccurred()) | ||
| 57 | + }) | ||
| 58 | +}) |
| 1 | +package user | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "net/http" | ||
| 5 | + "net/http/httptest" | ||
| 6 | + "testing" | ||
| 7 | + | ||
| 8 | + "github.com/beego/beego/v2/server/web" | ||
| 9 | + . "github.com/onsi/ginkgo" | ||
| 10 | + . "github.com/onsi/gomega" | ||
| 11 | + _ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg" | ||
| 12 | + _ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego" | ||
| 13 | +) | ||
| 14 | + | ||
| 15 | +func TestUser(t *testing.T) { | ||
| 16 | + RegisterFailHandler(Fail) | ||
| 17 | + RunSpecs(t, "Beego Port User Correlations Test Case Suite") | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +var handler http.Handler | ||
| 21 | +var server *httptest.Server | ||
| 22 | + | ||
| 23 | +var _ = BeforeSuite(func() { | ||
| 24 | + handler = web.BeeApp.Handlers | ||
| 25 | + server = httptest.NewServer(handler) | ||
| 26 | +}) | ||
| 27 | + | ||
| 28 | +var _ = AfterSuite(func() { | ||
| 29 | + server.Close() | ||
| 30 | +}) |
-
请 注册 或 登录 后发表评论