userbase.go 4.5 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
)

type UserBaseService struct {
}

// 返回列表
func (userService *UserBaseService) SearchUser(listUserQuery *query.ListUserQuery) (interface{}, error) {
	if err := listUserQuery.ValidateQuery(); 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()
	}()
	var count int64
	var userBases []*domain.UserBase
	userBaseRepository, _, _ := factory.FastPgUserBase(transactionContext, 0)
	userRepository, _, _ := factory.FastPgUser(transactionContext, 0)
	_, users, err := userRepository.Find(map[string]interface{}{
		"limit":                listUserQuery.Limit,
		"offset":               listUserQuery.Offset,
		"orgName":              listUserQuery.OrgName,
		"userName":             listUserQuery.UserName,
		"distinctOnUserBaseId": true,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var result = make([]*dto.UserBaseDto, 0)
	if len(users) > 0 {
		var userBaseIds []int64
		for i := range users {
			userBaseIds = append(userBaseIds, users[i].UserBaseId)
		}
		listUserQuery.InUserBaseIds = userBaseIds
		queryOptions := utils.ObjectToMap(listUserQuery)
		count, userBases, err = userBaseRepository.Find(queryOptions)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}

		for i := range userBases {
			u := userBases[i]
			var relateUsers []*domain.User
			if listUserQuery.FetchOrgBelong && len(u.RelatedUsers) > 0 {
				for i := range u.RelatedUsers {
					relateUser, _ := userRepository.FindOne(map[string]interface{}{"userId": u.RelatedUsers[i]})
					if relateUser != nil {
						relateUsers = append(relateUsers, relateUser)
					}
				}
			}
			userBaseDto := &dto.UserBaseDto{}
			userBaseDto.LoadDto(u, relateUsers)
			result = append(result, userBaseDto)
		}
	}

	return map[string]interface{}{
		"count": count,
		"users": result,
	}, nil
}

// 批量修改启用状态
func (userService *UserBaseService) BatchEnable(batchEnableCommand *command.BatchEnableCommand) (interface{}, error) {
	if err := batchEnableCommand.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()
	}()
	userRepository, _, err := factory.FastPgUserBase(transactionContext, 0)
	if err != nil {
		return nil, err
	}

	for i := 0; i < len(batchEnableCommand.UserBaseIds); i++ {
		if user, err := userRepository.FindOne(map[string]interface{}{"userBaseId": batchEnableCommand.UserBaseIds[i]}); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		} else {
			if err := user.SetEnableStatus(batchEnableCommand.EnableStatus); err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
			if _, err := userRepository.Save(user); err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
		}
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return struct{}{}, nil
}

func NewUserBaseService(options map[string]interface{}) *UserBaseService {
	newUserService := &UserBaseService{}
	return newUserService
}