credit_account.go 11.7 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/creditAccount/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/creditAccount/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
)

// 账期结算单服务
type CreditAccountService struct {
}

// 创建账期结算单服务
func (creditAccountService *CreditAccountService) CreateCreditAccount(createCreditAccountCommand *command.CreateCreditAccountCommand) (interface{}, error) {
	if err := createCreditAccountCommand.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()
	}()
	newCreditAccount := &domain.CreditAccount{
		//CompanyId: createCreditAccountCommand.CompanyId,
		//OrgId:     createCreditAccountCommand.OrgId,
		//UserId:    createCreditAccountCommand.UserId,
	}
	var creditAccountRepository domain.CreditAccountRepository
	if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		creditAccountRepository = value
	}
	if creditAccount, err := creditAccountRepository.Save(newCreditAccount); 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 creditAccount, nil
	}
}

// 账期结算单排名
func (creditAccountService *CreditAccountService) CreditAccountRanking(creditAccountRankingQuery *query.CreditAccountRankingQuery) (interface{}, error) {
	if err := creditAccountRankingQuery.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 返回账期结算单服务
func (creditAccountService *CreditAccountService) GetCreditAccount(getCreditAccountQuery *query.GetCreditAccountQuery) (interface{}, error) {
	if err := getCreditAccountQuery.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 creditAccountRepository domain.CreditAccountRepository
	if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		creditAccountRepository = value
	}
	creditAccount, err := creditAccountRepository.FindOne(map[string]interface{}{"creditAccountId": getCreditAccountQuery.CreditAccountId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if creditAccount == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getCreditAccountQuery.CreditAccountId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return creditAccount, nil
	}
}

// 返回账期结算单服务列表
func (creditAccountService *CreditAccountService) ListCreditAccount(listCreditAccountQuery *query.ListCreditAccountQuery) (interface{}, error) {
	if err := listCreditAccountQuery.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 creditAccountRepository domain.CreditAccountRepository
	if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		creditAccountRepository = value
	}
	if count, creditAccounts, err := creditAccountRepository.Find(tool_funs.SimpleStructToMap(listCreditAccountQuery)); 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,
			"creditAccounts": creditAccounts,
		}, nil
	}
}

// 支付账期结算(支付分红)
func (creditAccountService *CreditAccountService) PayCreditAccount(payCreditAccountCommand *command.PayCreditAccountCommand) (interface{}, error) {
	if err := payCreditAccountCommand.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 移除账期结算单服务
func (creditAccountService *CreditAccountService) RemoveCreditAccount(removeCreditAccountCommand *command.RemoveCreditAccountCommand) (interface{}, error) {
	if err := removeCreditAccountCommand.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 creditAccountRepository domain.CreditAccountRepository
	if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		creditAccountRepository = value
	}
	creditAccount, err := creditAccountRepository.FindOne(map[string]interface{}{"creditAccountId": removeCreditAccountCommand.CreditAccountId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if creditAccount == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeCreditAccountCommand.CreditAccountId)))
	}
	if creditAccount, err := creditAccountRepository.Remove(creditAccount); 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 creditAccount, nil
	}
}

// 查询账期结算单
func (creditAccountService *CreditAccountService) SearchCreditAccount(searchCreditAccountQuery *query.SearchCreditAccountQuery) (interface{}, error) {
	if err := searchCreditAccountQuery.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// 更新账期结算单服务
func (creditAccountService *CreditAccountService) UpdateCreditAccount(updateCreditAccountCommand *command.UpdateCreditAccountCommand) (interface{}, error) {
	if err := updateCreditAccountCommand.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 creditAccountRepository domain.CreditAccountRepository
	if value, err := factory.CreateCreditAccountRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		creditAccountRepository = value
	}
	creditAccount, err := creditAccountRepository.FindOne(map[string]interface{}{"creditAccountId": updateCreditAccountCommand.CreditAccountId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if creditAccount == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateCreditAccountCommand.CreditAccountId)))
	}
	if err := creditAccount.Update(tool_funs.SimpleStructToMap(updateCreditAccountCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if creditAccount, err := creditAccountRepository.Save(creditAccount); 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 creditAccount, nil
	}
}

func NewCreditAccountService(options map[string]interface{}) *CreditAccountService {
	newCreditAccountService := &CreditAccountService{}
	return newCreditAccountService
}