role_user_service.go 4.4 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"strconv"
)

type RoleUserService struct {
}

func NewRoleUserService() *RoleUserService {
	newRoleUserService := &RoleUserService{}
	return newRoleUserService
}

// Create 创建
func (rs *RoleUserService) Create(in *command.UserRoleCreateCommand) (interface{}, error) {
	transactionContext, err := factory.ValidateStartTransaction(in)
	if err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})

	// 检测已存在的关联用户
	_, rus, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.RoleId, "companyId": in.CompanyId, "userIds": in.UserIds, "limit": 9999999})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	existUserIds := make(map[int64]int64)
	for i := range rus {
		existUserIds[rus[i].UserId] = rus[i].UserId
	}
	for i := range in.UserIds {
		int64Num, _ := strconv.ParseInt(in.UserIds[i], 10, 64)
		// 不存在关联关系时,新增数据
		if _, ok := existUserIds[int64Num]; !ok {
			newRoleUser := &domain.RoleUser{
				Id:        0,
				RoleId:    in.RoleId,
				UserId:    int64Num,
				CompanyId: in.CompanyId,
			}
			_, err := roleUserRepository.Insert(newRoleUser)
			if 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 nil, nil
}

func (rs *RoleUserService) Remove(in *command.UserRoleDeleteCommand) (interface{}, error) {
	transactionContext, err := factory.ValidateStartTransaction(in)
	if err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})

	// 检测已存在的关联用户
	_, rus, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.RoleId, "companyId": in.CompanyId, "userId": in.UserId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if len(rus) == 0 {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "数据不存在")
	}

	if _, err := roleUserRepository.Remove(rus[0]); 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 rus, nil
}

//func (rs *RoleUserService) ListForUser(in *command.UserRoleQueryCommand) (interface{}, error) {
//	transactionContext, err := factory.StartTransaction()
//	if err != nil {
//		return nil, err
//	}
//	defer func() {
//		transactionContext.RollbackTransaction()
//	}()
//	roleRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
//	userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
//
//	in.PageNumber = 1
//	in.PageSize = 9999999
//
//	conditionMap := tool_funs.SimpleStructToMap(in)
//	_, roles, err := roleRepository.Find(conditionMap)
//	if err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	}
//	if len(roles) == 0 {
//		return nil, application.ThrowError(application.BUSINESS_ERROR, "未找到角色数据")
//	}
//
//	ids := make([]int64, 0)
//	for i := range roles {
//		ids = append(ids, roles[i].Id)
//	}
//
//	_, users, err := userRepository.Find(conditionMap)
//	if err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	}
//
//	//for i := range users {
//	//	users[i].RoleUserIds
//	//}
//
//	//if err := transactionContext.CommitTransaction(); err != nil {
//	//	return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//	//}
//	//groupAdapter := &adapter.RoleUserAdapter{}
//	//newList := groupAdapter.TransformTree(groups)
//	return map[string]interface{}{"list": users}, nil
//}