作者 Administrator

合并分支 'test' 到 'master'

用户基础数据修改



查看合并请求 !3
... ... @@ -55,6 +55,14 @@ func CreatePgUpdateUserService(options map[string]interface{}) (service.PgUpdate
return domainService.NewPgUpdateUserService(transactionContext)
}
func CreatePgUpdateUserBaseService(options map[string]interface{}) (service.PgUpdateUserBaseService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgUpdateUserBaseService(transactionContext)
}
func CreatePgCreateRoleService(options map[string]interface{}) (service.PgCreateRoleService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
... ...
... ... @@ -12,7 +12,9 @@ import (
type UpdateUsersBaseCommand struct {
OperateInfo *domain.OperateInfo `json:"-"`
// 用户Id 用户唯一标识
UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string" valid:"Required"`
UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string"`
// 用户基础Id 用户唯一标识
UserBaseId int64 `cname:"用户Id 用户唯一标识" json:"userBaseId" valid:"Required"`
// 用户姓名
UserName string `cname:"用户姓名" json:"userName" valid:"Required"`
// 头像
... ...
... ... @@ -741,7 +741,7 @@ func (userService *UserService) UpdateUser(updateUserCommand *command.UpdateUser
}
// 更新用户基础信息数据
func (userService *UserService) UpdateUsersBase(updateUsersBaseCommand *command.UpdateUsersBaseCommand) (interface{}, error) {
func (userService *UserService) UpdateUsersBaseOld(updateUsersBaseCommand *command.UpdateUsersBaseCommand) (interface{}, error) {
if err := updateUsersBaseCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
... ... @@ -778,6 +778,40 @@ func (userService *UserService) UpdateUsersBase(updateUsersBaseCommand *command.
return struct{}{}, nil
}
// 更新用户基础信息数据
func (userService *UserService) UpdateUsersBase(updateUsersBaseCommand *command.UpdateUsersBaseCommand) (interface{}, error) {
if err := updateUsersBaseCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
userInfo := &domain.UserInfo{
UserName: updateUsersBaseCommand.UserName,
Phone: updateUsersBaseCommand.Phone,
Avatar: updateUsersBaseCommand.Avatar,
Email: updateUsersBaseCommand.Email,
}
updateUserService, _ := factory.CreatePgUpdateUserBaseService(map[string]interface{}{
"transactionContext": transactionContext,
})
if _, err = updateUserService.UpdateUserBase(nil, updateUsersBaseCommand.UserBaseId, userInfo); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
}
func NewUserService(options map[string]interface{}) *UserService {
newUserService := &UserService{}
return newUserService
... ...
package service
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgUpdateUserBaseService 用户基础更新服务
type PgUpdateUserBaseService interface {
UpdateUserBase(optUser *domain.OperateInfo, userBaseId int64, userInfo *domain.UserInfo) (*domain.UserBase, error)
}
... ...
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
"strings"
"time"
)
// PgUpdateUserBaseService 用户基础更新服务
type PgUpdateUserBaseService struct {
transactionContext *pgTransaction.TransactionContext
}
func (ptr *PgUpdateUserService) UpdateUserBase(optUser *domain.OperateInfo, userBaseId int64, userInfo *domain.UserInfo) (*domain.UserBase, error) {
var err error
//1.更新用户信息
userBaseRepository, _ := repository.NewUserBaseRepository(ptr.transactionContext)
var userBase *domain.UserBase
if userBase, err = userBaseRepository.FindOne(map[string]interface{}{"userBaseId": userBaseId}); err != nil {
return nil, err
}
if userBase.Account != strings.TrimSpace(userInfo.Phone) && len(userInfo.Phone) > 0 { // 修改了手机号
if _, err = userBaseRepository.FindOne(map[string]interface{}{"account": strings.TrimSpace(userInfo.Phone)}); err == nil {
return nil, fmt.Errorf("手机号已存在")
}
if err = userBase.ResetPhone(userBase.Account, userInfo.Phone); err != nil {
return nil, err
}
}
if err = userBase.UpdateUserInfo(userInfo); err != nil {
return nil, err
}
if userBase, err = userBaseRepository.Save(userBase); err != nil {
return nil, err
}
//2.更新用户、冗余信息
var users []*domain.User
userRepository, _ := repository.NewUserRepository(ptr.transactionContext)
if _, users, err = userRepository.Find(map[string]interface{}{"userBaseId": userBaseId}); err != nil {
return nil, err
}
for i := range users {
user := users[i]
user.Ext.Phone = userBase.UserInfo.Phone
user.Ext.UserName = userBase.UserInfo.UserName
user.UpdatedAt = time.Now()
if user, err = userRepository.Save(user); err != nil {
return nil, err
}
}
return userBase, nil
}
func NewPgUpdateUserBaseService(transactionContext *pgTransaction.TransactionContext) (*PgUpdateUserService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgUpdateUserService{
transactionContext: transactionContext,
}, nil
}
}
... ...