credit_account.go 14.2 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"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
	"strconv"
	"time"
)

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

// CreateCreditAccount 创建账期结算单服务
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()
	}()

	// 公司REST服务初始化
	var companyService service.CompanyService
	if value, err := factory.CreateCompanyService(map[string]interface{}{}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		companyService = value
	}

	// 获取公司信息
	var company *domain.Company
	if data, err := companyService.CompanyFrom(createCreditAccountCommand.CompanyId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		company = data
	}

	// 组织机构REST服务初始化
	var organizationService service.OrgService
	if value, err := factory.CreateOrganizationService(map[string]interface{}{}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		organizationService = value
	}

	// 获取组织机构信息
	var organization *domain.Org
	if data, err := organizationService.OrgFrom(createCreditAccountCommand.CompanyId, createCreditAccountCommand.OrgId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		organization = data
	}

	// 用户REST服务初始化
	var userService service.UserService
	if value, err := factory.CreateUserService(map[string]interface{}{}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		userService = value
	}

	// 获取操作人
	var operator *domain.User
	if data, err := userService.OperatorFrom(createCreditAccountCommand.CompanyId, createCreditAccountCommand.OrgId, createCreditAccountCommand.UserId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		operator = data
	}

	newCreditAccount := &domain.CreditAccount{
		ActuallyPaidAmount:        0,
		CreditAccountOrderNum:     "",
		PaymentStatus:             0,
		PaymentTime:               time.Time{},
		SettlementAmount:          0,
		SettlementTime:            time.Time{},
		CooperationContractNumber: "",
		Participator:              nil,
		PaymentDocumentAttachment: nil,
		Org:                       organization,
		Company:                   company,
		Operator:                  operator,
		OperateTime:               time.Now(),
		CreatedAt:                 time.Now(),
		DeletedAt:                 time.Time{},
		UpdatedAt:                 time.Time{},
	}

	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
	}
}

// CreditAccountRanking 账期结算单排名
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
}

// GetCreditAccount 返回账期结算单服务
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", strconv.FormatInt(getCreditAccountQuery.CreditAccountId, 10)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return creditAccount, nil
	}
}

// ListCreditAccount 返回账期结算单服务列表
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{}{
			"grid": map[string]interface{}{
				"total": count,
				"list":  creditAccounts,
			},
		}, nil
	}
}

// PayCreditAccount 支付账期结算(支付分红)
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
}

// RemoveCreditAccount 移除账期结算单服务
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", strconv.FormatInt(removeCreditAccountCommand.CreditAccountId, 10)))
	}
	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
	}
}

// SearchCreditAccount 查询账期结算单
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
}

// UpdateCreditAccount 更新账期结算单服务
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", strconv.FormatInt(updateCreditAccountCommand.CreditAccountId, 10)))
	}
	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
}