package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"github.com/tiptok/godevp/pkg/application/factory"
	"github.com/tiptok/godevp/pkg/application/role/command"
	"github.com/tiptok/godevp/pkg/application/role/query"
	"github.com/tiptok/godevp/pkg/domain/role"
)

// 角色名称
type RoleService struct {
}

// 创建
func (roleService *RoleService) CreateRole(createRoleCommand *command.CreateRoleCommand) (interface{}, error) {
	if err := createRoleCommand.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()
	}()
	newRole := &role.Role{
		Id:       createRoleCommand.Id,
		RoleName: createRoleCommand.RoleName,
	}
	var roleRepository role.RoleRepository
	if value, err := factory.CreateRoleRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		roleRepository = value
	}
	if role, err := roleRepository.Save(newRole); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return role, nil
	}
}

// 返回
func (roleService *RoleService) GetRole(getRoleQuery *query.GetRoleQuery) (interface{}, error) {
	if err := getRoleQuery.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 roleRepository role.RoleRepository
	if value, err := factory.CreateRoleRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		roleRepository = value
	}
	role, err := roleRepository.FindOne(map[string]interface{}{"roleId": getRoleQuery.RoleId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if role == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getRoleQuery.RoleId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return role, nil
	}
}

// 返回列表
func (roleService *RoleService) ListRole(listRoleQuery *query.ListRoleQuery) (interface{}, error) {
	if err := listRoleQuery.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 roleRepository role.RoleRepository
	if value, err := factory.CreateRoleRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		roleRepository = value
	}
	if count, roles, err := roleRepository.Find(tool_funs.SimpleStructToMap(listRoleQuery)); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return map[string]interface{}{
			"count": count,
			"roles": roles,
		}, nil
	}
}

// 移除
func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRoleCommand) (interface{}, error) {
	if err := removeRoleCommand.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()
	}()
	var roleRepository role.RoleRepository
	if value, err := factory.CreateRoleRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		roleRepository = value
	}
	role, err := roleRepository.FindOne(map[string]interface{}{"roleId": removeRoleCommand.RoleId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if role == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeRoleCommand.RoleId)))
	}
	if role, err := roleRepository.Remove(role); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return role, nil
	}
}

// 更新
func (roleService *RoleService) UpdateRole(updateRoleCommand *command.UpdateRoleCommand) (interface{}, error) {
	if err := updateRoleCommand.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()
	}()
	var roleRepository role.RoleRepository
	if value, err := factory.CreateRoleRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		roleRepository = value
	}
	role, err := roleRepository.FindOne(map[string]interface{}{"roleId": updateRoleCommand.RoleId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if role == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateRoleCommand.RoleId)))
	}
	if err := role.Update(tool_funs.SimpleStructToMap(updateRoleCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if role, err := roleRepository.Save(role); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return role, nil
	}
}

func NewRoleService(options map[string]interface{}) *RoleService {
	newRoleService := &RoleService{}
	return newRoleService
}