dividends_order.go 11.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/dividendsOrder/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/dividendsOrder/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
)

// DividendsOrderService 分红订单实体对象
type DividendsOrderService struct {
}

// CreateDividendsOrder 创建分红订单实体对象
func (dividendsOrderService *DividendsOrderService) CreateDividendsOrder(createDividendsOrderCommand *command.CreateDividendsOrderCommand) (interface{}, error) {
	if err := createDividendsOrderCommand.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()
	}()
	newDividendsOrder := &domain.DividendsOrder{
		CustomerName:              createDividendsOrderCommand.CustomerName,
		DividendsOrderAmount:      createDividendsOrderCommand.DividendsOrderAmount,
		DividendsOrderNumber:      createDividendsOrderCommand.DividendsOrderNumber,
		DividendsOriginalOrderNum: createDividendsOrderCommand.DividendsOriginalOrderNum,
		OrderTime:                 createDividendsOrderCommand.OrderTime,
		//Remarks:                   createDividendsOrderCommand.Remarks,
		//SalesmanUid:               createDividendsOrderCommand.SalesmanUid,
		//OperatorUid:               createDividendsOrderCommand.OperatorUid,
		//OrderGoods:                createDividendsOrderCommand.OrderGoods,
		//CompanyId:                 createDividendsOrderCommand.CompanyId,
		//OrgId:                     createDividendsOrderCommand.OrgId,
		//UserId:                    createDividendsOrderCommand.UserId,
	}
	var dividendsOrderRepository domain.DividendsOrderRepository
	if value, err := factory.CreateDividendsOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dividendsOrderRepository = value
	}
	if dividendsOrder, err := dividendsOrderRepository.Save(newDividendsOrder); 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 dividendsOrder, nil
	}
}

// GetDividendsOrder 返回分红订单实体对象
func (dividendsOrderService *DividendsOrderService) GetDividendsOrder(getDividendsOrderQuery *query.GetDividendsOrderQuery) (interface{}, error) {
	if err := getDividendsOrderQuery.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 dividendsOrderRepository domain.DividendsOrderRepository
	if value, err := factory.CreateDividendsOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dividendsOrderRepository = value
	}
	dividendsOrder, err := dividendsOrderRepository.FindOne(map[string]interface{}{"dividendsOrderId": getDividendsOrderQuery.DividendsOrderId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if dividendsOrder == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getDividendsOrderQuery.DividendsOrderId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return dividendsOrder, nil
	}
}

// ListDividendsOrders 返回分红订单实体对象列表
func (dividendsOrderService *DividendsOrderService) ListDividendsOrders(listDividendsOrdersQuery *query.ListDividendsOrdersQuery) (interface{}, error) {
	if err := listDividendsOrdersQuery.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
}

// RemoveDividendsOrder 移除分红订单实体对象
func (dividendsOrderService *DividendsOrderService) RemoveDividendsOrder(removeDividendsOrderCommand *command.RemoveDividendsOrderCommand) (interface{}, error) {
	if err := removeDividendsOrderCommand.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 dividendsOrderRepository domain.DividendsOrderRepository
	if value, err := factory.CreateDividendsOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dividendsOrderRepository = value
	}
	dividendsOrder, err := dividendsOrderRepository.FindOne(map[string]interface{}{"dividendsOrderId": removeDividendsOrderCommand.DividendsOrderId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if dividendsOrder == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeDividendsOrderCommand.DividendsOrderId)))
	}
	if dividendsOrder, err := dividendsOrderRepository.Remove(dividendsOrder); 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 dividendsOrder, nil
	}
}

// SearchDividendsOrder 查询分红订单方法
func (dividendsOrderService *DividendsOrderService) SearchDividendsOrder(searchDividendsOrderQuery *query.SearchDividendsOrderQuery) (interface{}, error) {
	if err := searchDividendsOrderQuery.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
}

// SearchDividendsOrderNumber 查询分红订单方法
func (dividendsOrderService *DividendsOrderService) SearchDividendsOrderNumber(searchDividendsOrderNumberQuery *query.SearchDividendsOrderNumberQuery) (interface{}, error) {
	if err := searchDividendsOrderNumberQuery.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
}

// UpdateDividendsOrder 更新分红订单实体对象
func (dividendsOrderService *DividendsOrderService) UpdateDividendsOrder(updateDividendsOrderCommand *command.UpdateDividendsOrderCommand) (interface{}, error) {
	if err := updateDividendsOrderCommand.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 dividendsOrderRepository domain.DividendsOrderRepository
	if value, err := factory.CreateDividendsOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		dividendsOrderRepository = value
	}
	dividendsOrder, err := dividendsOrderRepository.FindOne(map[string]interface{}{"dividendsOrderId": updateDividendsOrderCommand.DividendsOrderId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if dividendsOrder == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateDividendsOrderCommand.DividendsOrderId)))
	}
	if err := dividendsOrder.Update(tool_funs.SimpleStructToMap(updateDividendsOrderCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if dividendsOrder, err := dividendsOrderRepository.Save(dividendsOrder); 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 dividendsOrder, nil
	}
}

func NewDividendsOrderService(options map[string]interface{}) *DividendsOrderService {
	newDividendsOrderService := &DividendsOrderService{}
	return newDividendsOrderService
}