order_payment.go 6.5 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/command"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"
	"time"

	//"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)

// 客户价值服务
type OrderPaymentService struct {
}

func NewOrderPaymentService(options map[string]interface{}) *OrderPaymentService {
	newOrderPaymentService := &OrderPaymentService{}
	return newOrderPaymentService
}

// 创建订单支付数据
func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *command.CreateOrderPaymentCommand) (data interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = command.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	//检查订单是否存在

	var OrderPaymentRepository domain.OrderPaymentRepository
	if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}

	dm := &domain.OrderPayment{
		OrderId:       command.OrderId,
		PartnerId:     0,
		PaymentAmount: 0,
		BonusAmount:   0, //计算分红金额
		BonusStatus:   0,
		CreateAt:      time.Now(),
		PaymentSn:     command.PaymentSn,
		UpdateAt:      time.Now(),
	}

	//检查货款 已存在 / 未存在
	if findDm, e := OrderPaymentRepository.FindOne(domain.OrderPaymentFindOneQuery{OrderId: command.OrderId, PaymentSn: command.PaymentSn}); e == nil {
		if dm.BonusStatus == domain.BonusPaid {
			return
		}
		dm = findDm
	}

	dm.PaymentAmount = command.PaymentForGoods
	dm.BonusStatus = command.StateOfPayment
	dm.BonusAmount = 0

	if data, err = OrderPaymentRepository.Save(dm); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	err = transactionContext.CommitTransaction()
	return
}

//// GetOrderPayment 返回合伙人
//func (OrderPaymentService *OrderPaymentService) GetOrderPayment(command query.GetOrderPaymentQuery) (data *domain.OrderPayment, err error) {
//	var (
//		transactionContext, _ = factory.CreateTransactionContext(nil)
//	)
//	if err = command.ValidateQuery(); err != nil {
//		return nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
//	}
//	if err := transactionContext.StartTransaction(); err != nil {
//		return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
//	}
//	defer func() {
//		transactionContext.RollbackTransaction()
//	}()
//	var OrderPaymentRepository domain.OrderPaymentRepository
//	if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
//		"transactionContext": transactionContext,
//	}); err != nil {
//		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
//	}
//	data, err = OrderPaymentRepository.FindOne(domain.PartnerFindOneQuery{UserId: command.Id})
//	if err != nil {
//		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
//	}
//	err = transactionContext.CommitTransaction()
//	return
//}
//
//// 更新客户价值
//func (OrderPaymentService *OrderPaymentService) UpdateOrderPayment(updateOrderPaymentCommand *command.UpdateOrderPaymentCommand) (err error) {
//	var (
//		transactionContext, _ = factory.CreateTransactionContext(nil)
//	)
//	if err = updateOrderPaymentCommand.ValidateCommand(); err != nil {
//		return application.ThrowError(application.ARG_ERROR, err.Error())
//	}
//	if err := transactionContext.StartTransaction(); err != nil {
//		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
//	}
//	defer func() {
//		transactionContext.RollbackTransaction()
//	}()
//	var OrderPaymentRepository domain.OrderPaymentRepository
//	if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
//		"transactionContext": transactionContext,
//	}); err != nil {
//		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
//	}
//	OrderPayment, err := OrderPaymentRepository.FindOne(domain.PartnerFindOneQuery{
//		UserId: updateOrderPaymentCommand.Id,
//	})
//	if err != nil {
//		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
//	}
//	OrderPayment.PartnerCategory = updateOrderPaymentCommand.PartnerCategory
//	OrderPayment.Salesman = updateOrderPaymentCommand.Salesman
//	OrderPayment.Status = updateOrderPaymentCommand.Status
//	OrderPayment.RegionInfo = updateOrderPaymentCommand.RegionInfo
//	if _, err = OrderPaymentRepository.Save(OrderPayment); err != nil {
//		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
//	}
//	transactionContext.CommitTransaction()
//	return
//}
//
// 返回订单支付列表
func (OrderPaymentService *OrderPaymentService) ListOrderPayment(listOrderPaymentQuery *query.ListOrderPaymentQuery) (int, []*domain.OrderPayment, error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		OrderPayments         []*domain.OrderPayment
		count                 int
		err                   error
	)
	if err = listOrderPaymentQuery.ValidateQuery(); err != nil {
		return 0, nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		if err != nil {
			transactionContext.RollbackTransaction()
		}
	}()
	var OrderPaymentRepository domain.OrderPaymentRepository
	if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	queryOption := domain.OrderPaymentQuery{
		OrderId: listOrderPaymentQuery.OrderId,
	}
	if OrderPayments, err = OrderPaymentRepository.Find(queryOption); err != nil {
		return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if count, err = OrderPaymentRepository.CountAll(queryOption); err != nil {
		return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err = transactionContext.CommitTransaction(); err != nil {
		return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	return count, OrderPayments, nil
}