作者 陈爱青
提交者 yangfu

[add] 用户

@@ -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=
  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 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
  6 + "time"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type CreateUserCommand struct {
  12 + // 1.高管 2.合伙人 4:游客
  13 + UserType int `json:"userType" valid:"Required"`
  14 + // 管理员类型 1.超级管理员 10:企业管理员 100:普通用户
  15 + AdminType int `json:"adminType" valid:"Required"`
  16 + // 状态 1正常 2禁用
  17 + Status int64 `json:"status" valid:"Required"`
  18 + // 是否是公司负责人
  19 + IsPrincipal bool `json:"isPrincipal" valid:"Required"`
  20 + // 统一用户id
  21 + Uid int64 `json:"uid" valid:"Required"`
  22 + // 用户账号
  23 + UserAccount string `json:"userAccount" valid:"Required"`
  24 + // 用户头像URL
  25 + UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
  26 + // 用户名称
  27 + UserName string `json:"userName" valid:"Required"`
  28 + // 邮件地址
  29 + Email string `json:"email" valid:"Required"`
  30 + // 性别
  31 + Gender int `json:"gender" valid:"Required"`
  32 + // 入职时间
  33 + EntryTime time.Time `json:"entryTime" valid:"Required"`
  34 + // 分机
  35 + Extension string `json:"extension" valid:"Required"`
  36 + // 工作地
  37 + Workplace string `json:"workplace" valid:"Required"`
  38 + // 私人电话
  39 + PrivateNumber string `json:"privateNumber" valid:"Required"`
  40 + // 工号
  41 + JobNumber string `json:"jobNumber" valid:"Required"`
  42 + // 合伙人账号
  43 + PartnerAccount string `json:"partnerAccount" valid:"Required"`
  44 + // 合伙人姓名
  45 + PartnerName string `json:"partnerName" valid:"Required"`
  46 + // 区域名称 eg:华南地区
  47 + RegionName string `json:"regionName,omitempty"`
  48 + // 合伙时间
  49 + CooperateTime time.Time `json:"cooperateTime" valid:"Required"`
  50 + // 业务员
  51 + Salesmans []*domain.Salesman `json:"salesmans,omitempty"`
  52 + // 用户信息
  53 + UserInfo *domain.UserInfo `json:"userInfo,omitempty"`
  54 + //合伙人信息
  55 + PartnerInfo *domain.PartnerInfo `json:"partnerInfo"`
  56 + //// 合伙人类型
  57 + PartnerCategorys []*domain.PartnerCategory `json:"partnerCategorys,omitempty"`
  58 + // 可查看合伙人列表
  59 + AccessPartners []int64 `json:"accessPartners,omitempty"`
  60 +}
  61 +
  62 +func (createUserCommand *CreateUserCommand) Valid(validation *validation.Validation) {
  63 + validation.SetError("CustomValid", "未实现的自定义认证")
  64 +}
  65 +
  66 +func (createUserCommand *CreateUserCommand) ValidateCommand() error {
  67 + valid := validation.Validation{}
  68 + b, err := valid.Valid(createUserCommand)
  69 + if err != nil {
  70 + return err
  71 + }
  72 + if !b {
  73 + for _, validErr := range valid.Errors {
  74 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  75 + }
  76 + }
  77 + return nil
  78 +}
  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 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
  6 + "time"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type UpdateUserCommand struct {
  12 + // 用户id
  13 + UserId int64 `json:"userId" valid:"Required"`
  14 + // 1.高管 2.合伙人 4:游客
  15 + UserType int `json:"userType,omitempty"`
  16 + // 管理员类型 1.超级管理员 10:企业管理员 100:普通用户
  17 + AdminType int `json:"adminType,omitempty"`
  18 + // 状态 1正常 2禁用
  19 + Status int64 `json:"status" valid:"Required"`
  20 + // 是否是公司负责人
  21 + IsPrincipal bool `json:"isPrincipal" valid:"Required"`
  22 + // 统一用户id
  23 + Uid int64 `json:"uid" valid:"Required"`
  24 + // 用户账号
  25 + UserAccount string `json:"userAccount" valid:"Required"`
  26 + // 用户头像URL
  27 + UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
  28 + // 用户名称
  29 + UserName string `json:"userName" valid:"Required"`
  30 + // 邮件地址
  31 + Email string `json:"email" valid:"Required"`
  32 + // 性别
  33 + Gender int `json:"gender" valid:"Required"`
  34 + // 入职时间
  35 + EntryTime time.Time `json:"entryTime" valid:"Required"`
  36 + // 分机
  37 + Extension string `json:"extension" valid:"Required"`
  38 + // 工作地
  39 + Workplace string `json:"workplace" valid:"Required"`
  40 + // 私人电话
  41 + PrivateNumber string `json:"privateNumber" valid:"Required"`
  42 + // 工号
  43 + JobNumber string `json:"jobNumber" valid:"Required"`
  44 + // 合伙人账号
  45 + PartnerAccount string `json:"partnerAccount" valid:"Required"`
  46 + // 合伙人姓名
  47 + PartnerName string `json:"partnerName" valid:"Required"`
  48 + // 区域名称 eg:华南地区
  49 + RegionName string `json:"regionName,omitempty"`
  50 + // 合伙时间
  51 + CooperateTime time.Time `json:"cooperateTime" valid:"Required"`
  52 + // 业务员
  53 + Salesmans []*domain.Salesman `json:"salesmans,omitempty"`
  54 + // 合伙人类型
  55 + PartnerCategorys []*domain.PartnerCategory `json:"partnerCategorys,omitempty"`
  56 + // 可查看合伙人列表
  57 + AccessPartners []int64 `json:"accessPartners,omitempty"`
  58 +}
  59 +
  60 +func (updateUserCommand *UpdateUserCommand) Valid(validation *validation.Validation) {
  61 + validation.SetError("CustomValid", "未实现的自定义认证")
  62 +}
  63 +
  64 +func (updateUserCommand *UpdateUserCommand) ValidateCommand() error {
  65 + valid := validation.Validation{}
  66 + b, err := valid.Valid(updateUserCommand)
  67 + if err != nil {
  68 + return err
  69 + }
  70 + if !b {
  71 + for _, validErr := range valid.Errors {
  72 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  73 + }
  74 + }
  75 + return nil
  76 +}
  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 +}
  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" valid:"Required"`
  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 +}
  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 +)
  12 +
  13 +// 用户管理服务
  14 +type UserService struct {
  15 +}
  16 +
  17 +// 用户状态转换(禁用、启用)
  18 +func (userService *UserService) ConvertUserStatus(convertUserStatusCommand *command.ConvertUserStatusCommand) (interface{}, error) {
  19 + if err := convertUserStatusCommand.ValidateCommand(); err != nil {
  20 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  21 + }
  22 + transactionContext, err := factory.CreateTransactionContext(nil)
  23 + if err != nil {
  24 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  25 + }
  26 + if err := transactionContext.StartTransaction(); err != nil {
  27 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  28 + }
  29 + defer func() {
  30 + transactionContext.RollbackTransaction()
  31 + }()
  32 + if err := transactionContext.CommitTransaction(); err != nil {
  33 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  34 + }
  35 + return nil, nil
  36 +}
  37 +
  38 +// 创建
  39 +func (userService *UserService) CreateUser(createUserCommand *command.CreateUserCommand) (interface{}, error) {
  40 + if err := createUserCommand.ValidateCommand(); err != nil {
  41 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  42 + }
  43 + transactionContext, err := factory.CreateTransactionContext(nil)
  44 + if err != nil {
  45 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  46 + }
  47 + if err := transactionContext.StartTransaction(); err != nil {
  48 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  49 + }
  50 + defer func() {
  51 + transactionContext.RollbackTransaction()
  52 + }()
  53 + newUser := &domain.User{
  54 + UserType: createUserCommand.UserType,
  55 + AdminType: createUserCommand.AdminType,
  56 + Status: createUserCommand.Status,
  57 + UserInfo: createUserCommand.UserInfo,
  58 + PartnerInfo: createUserCommand.PartnerInfo,
  59 + AccessPartners: createUserCommand.AccessPartners,
  60 + }
  61 + var userRepository domain.UserRepository
  62 + if value, err := factory.CreateUserRepository(map[string]interface{}{
  63 + "transactionContext": transactionContext,
  64 + }); err != nil {
  65 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  66 + } else {
  67 + userRepository = value
  68 + }
  69 + if user, err := userRepository.Save(newUser); err != nil {
  70 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  71 + } else {
  72 + if err := transactionContext.CommitTransaction(); err != nil {
  73 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  74 + }
  75 + return user, nil
  76 + }
  77 +}
  78 +
  79 +// 返回
  80 +func (userService *UserService) GetUser(getUserQuery *query.GetUserQuery) (interface{}, error) {
  81 + if err := getUserQuery.ValidateQuery(); err != nil {
  82 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  83 + }
  84 + transactionContext, err := factory.CreateTransactionContext(nil)
  85 + if err != nil {
  86 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  87 + }
  88 + if err := transactionContext.StartTransaction(); err != nil {
  89 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  90 + }
  91 + defer func() {
  92 + transactionContext.RollbackTransaction()
  93 + }()
  94 + var userRepository domain.UserRepository
  95 + if value, err := factory.CreateUserRepository(map[string]interface{}{
  96 + "transactionContext": transactionContext,
  97 + }); err != nil {
  98 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  99 + } else {
  100 + userRepository = value
  101 + }
  102 + user, err := userRepository.FindOne(map[string]interface{}{"userId": getUserQuery.UserId})
  103 + if err != nil {
  104 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  105 + }
  106 + if user == nil {
  107 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getUserQuery.UserId)))
  108 + } else {
  109 + if err := transactionContext.CommitTransaction(); err != nil {
  110 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  111 + }
  112 + return user, nil
  113 + }
  114 +}
  115 +
  116 +// 返回列表
  117 +func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (interface{}, error) {
  118 + if err := listUserQuery.ValidateQuery(); err != nil {
  119 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  120 + }
  121 + transactionContext, err := factory.CreateTransactionContext(nil)
  122 + if err != nil {
  123 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  124 + }
  125 + if err := transactionContext.StartTransaction(); err != nil {
  126 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  127 + }
  128 + defer func() {
  129 + transactionContext.RollbackTransaction()
  130 + }()
  131 + var userRepository domain.UserRepository
  132 + if value, err := factory.CreateUserRepository(map[string]interface{}{
  133 + "transactionContext": transactionContext,
  134 + }); err != nil {
  135 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  136 + } else {
  137 + userRepository = value
  138 + }
  139 + if count, users, err := userRepository.Find(tool_funs.SimpleStructToMap(listUserQuery)); err != nil {
  140 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  141 + } else {
  142 + if err := transactionContext.CommitTransaction(); err != nil {
  143 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  144 + }
  145 + return map[string]interface{}{
  146 + "count": count,
  147 + "users": users,
  148 + }, nil
  149 + }
  150 +}
  151 +
  152 +// 移除
  153 +func (userService *UserService) RemoveUser(removeUserCommand *command.RemoveUserCommand) (interface{}, error) {
  154 + if err := removeUserCommand.ValidateCommand(); err != nil {
  155 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  156 + }
  157 + transactionContext, err := factory.CreateTransactionContext(nil)
  158 + if err != nil {
  159 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  160 + }
  161 + if err := transactionContext.StartTransaction(); err != nil {
  162 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  163 + }
  164 + defer func() {
  165 + transactionContext.RollbackTransaction()
  166 + }()
  167 + var userRepository domain.UserRepository
  168 + if value, err := factory.CreateUserRepository(map[string]interface{}{
  169 + "transactionContext": transactionContext,
  170 + }); err != nil {
  171 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  172 + } else {
  173 + userRepository = value
  174 + }
  175 + user, err := userRepository.FindOne(map[string]interface{}{"userId": removeUserCommand.UserId})
  176 + if err != nil {
  177 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  178 + }
  179 + if user == nil {
  180 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeUserCommand.UserId)))
  181 + }
  182 + if user, err := userRepository.Remove(user); err != nil {
  183 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  184 + } else {
  185 + if err := transactionContext.CommitTransaction(); err != nil {
  186 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  187 + }
  188 + return user, nil
  189 + }
  190 +}
  191 +
  192 +// 设置权限
  193 +func (userService *UserService) SetPermission(setPermissionCommand *command.SetPermissionCommand) (interface{}, error) {
  194 + if err := setPermissionCommand.ValidateCommand(); err != nil {
  195 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  196 + }
  197 + transactionContext, err := factory.CreateTransactionContext(nil)
  198 + if err != nil {
  199 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  200 + }
  201 + if err := transactionContext.StartTransaction(); err != nil {
  202 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  203 + }
  204 + defer func() {
  205 + transactionContext.RollbackTransaction()
  206 + }()
  207 + if err := transactionContext.CommitTransaction(); err != nil {
  208 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  209 + }
  210 + return nil, nil
  211 +}
  212 +
  213 +// 更新
  214 +func (userService *UserService) UpdateUser(updateUserCommand *command.UpdateUserCommand) (interface{}, error) {
  215 + if err := updateUserCommand.ValidateCommand(); err != nil {
  216 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  217 + }
  218 + transactionContext, err := factory.CreateTransactionContext(nil)
  219 + if err != nil {
  220 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  221 + }
  222 + if err := transactionContext.StartTransaction(); err != nil {
  223 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  224 + }
  225 + defer func() {
  226 + transactionContext.RollbackTransaction()
  227 + }()
  228 + var userRepository domain.UserRepository
  229 + if value, err := factory.CreateUserRepository(map[string]interface{}{
  230 + "transactionContext": transactionContext,
  231 + }); err != nil {
  232 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  233 + } else {
  234 + userRepository = value
  235 + }
  236 + user, err := userRepository.FindOne(map[string]interface{}{"userId": updateUserCommand.UserId})
  237 + if err != nil {
  238 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  239 + }
  240 + if user == nil {
  241 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateUserCommand.UserId)))
  242 + }
  243 + if err := user.Update(tool_funs.SimpleStructToMap(updateUserCommand)); err != nil {
  244 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  245 + }
  246 + if user, err := userRepository.Save(user); err != nil {
  247 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  248 + } else {
  249 + if err := transactionContext.CommitTransaction(); err != nil {
  250 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  251 + }
  252 + return user, nil
  253 + }
  254 +}
  255 +
  256 +func NewUserService(options map[string]interface{}) *UserService {
  257 + newUserService := &UserService{}
  258 + return newUserService
  259 +}
  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 +}
  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, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
  19 + "testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
  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": "int64",
  28 + "status": "int64",
  29 + }
  30 + httpExpect.POST("/users/convertUserStatus").
  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 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 +)
  10 +
  11 +var _ = Describe("创建", func() {
  12 + Describe("提交数据创建", func() {
  13 + Context("提交正确的新用户实体数据", func() {
  14 + It("返回用户实体数据", func() {
  15 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  16 + body := map[string]interface{}{
  17 + "userType": "int",
  18 + "adminType": "int",
  19 + "status": "int64",
  20 + "isPrincipal": "boolean",
  21 + "uid": "int64",
  22 + "userAccount": "string",
  23 + "userAvatarUrl": "string",
  24 + "userName": "string",
  25 + "email": "string",
  26 + "gender": "int",
  27 + "entryTime": "datetime",
  28 + "extension": "string",
  29 + "workplace": "string",
  30 + "privateNumber": "string",
  31 + "jobNumber": "string",
  32 + "partnerAccount": "string",
  33 + "partnerName": "string",
  34 + "regionName": "string",
  35 + "cooperateTime": "datetime",
  36 + "salesmans": "array",
  37 + "partnerCategorys": "array",
  38 + "accessPartners": "array",
  39 + }
  40 + httpExpect.POST("/users/").
  41 + WithJSON(body).
  42 + Expect().
  43 + Status(http.StatusOK).
  44 + JSON().
  45 + Object().
  46 + ContainsKey("code").ValueEqual("code", 0).
  47 + ContainsKey("msg").ValueEqual("msg", "ok").
  48 + ContainsKey("data").Value("data").Object().
  49 + ContainsKey("userId").ValueNotEqual("userId", BeZero())
  50 + })
  51 + })
  52 + })
  53 + AfterEach(func() {
  54 + //_, err := pG.DB.Exec("DELETE FROM users WHERE true")
  55 + //Expect(err).NotTo(HaveOccurred())
  56 + })
  57 +})
  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, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
  19 + "testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
  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/{userId}").
  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, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
  19 + "testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
  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", "int").
  28 + WithQuery("limit", "int").
  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, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
  19 + "testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
  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/{userId}").
  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, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
  19 + "testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
  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": "int64",
  28 + "permissons": "array",
  29 + }
  30 + httpExpect.POST("/users/setPermission").
  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 + "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, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
  19 + "testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
  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 + "userType": "int",
  28 + "adminType": "int",
  29 + "status": "int64",
  30 + "isPrincipal": "boolean",
  31 + "uid": "int64",
  32 + "userAccount": "string",
  33 + "userAvatarUrl": "string",
  34 + "userName": "string",
  35 + "email": "string",
  36 + "gender": "int",
  37 + "entryTime": "datetime",
  38 + "extension": "string",
  39 + "workplace": "string",
  40 + "privateNumber": "string",
  41 + "jobNumber": "string",
  42 + "partnerAccount": "string",
  43 + "partnerName": "string",
  44 + "regionName": "string",
  45 + "cooperateTime": "datetime",
  46 + "salesmans": "array",
  47 + "partnerCategorys": "array",
  48 + "accessPartners": "array",
  49 + }
  50 + httpExpect.PUT("/users/{userId}").
  51 + WithJSON(body).
  52 + Expect().
  53 + Status(http.StatusOK).
  54 + JSON().
  55 + Object().
  56 + ContainsKey("code").ValueEqual("code", 0).
  57 + ContainsKey("msg").ValueEqual("msg", "ok").
  58 + ContainsKey("data").Value("data").Object().
  59 + ContainsKey("userId").ValueEqual("userId", userId)
  60 + })
  61 + })
  62 + })
  63 + AfterEach(func() {
  64 + //_, err := pG.DB.Exec("DELETE FROM users WHERE true")
  65 + //Expect(err).NotTo(HaveOccurred())
  66 + })
  67 +})
  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 +})