|
|
package service
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
"github.com/linmadan/egglib-go/utils/tool_funs"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/command"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/query"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
|
|
|
)
|
|
|
|
|
|
// 订单基础服务
|
|
|
type OrderService struct {
|
|
|
}
|
|
|
|
|
|
// 取消订单服务
|
|
|
func (orderService *OrderService) Cancel(cancelCommand *command.CancelCommand) (interface{}, error) {
|
|
|
if err := cancelCommand.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 (orderService *OrderService) CreateOrder(createOrderCommand *command.CreateOrderCommand) (interface{}, error) {
|
|
|
if err := createOrderCommand.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()
|
|
|
}()
|
|
|
newOrder := &domain.Order{
|
|
|
OrderNo: createOrderCommand.OrderNo,
|
|
|
CompanyId: createOrderCommand.CompanyId,
|
|
|
Buyer: &domain.BuyerInfo{
|
|
|
BuyerName: createOrderCommand.BuyerName,
|
|
|
},
|
|
|
TotalPrice: createOrderCommand.TotalPrice,
|
|
|
}
|
|
|
var orderRepository domain.OrderRepository
|
|
|
if value, err := factory.CreateOrderRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
orderRepository = value
|
|
|
}
|
|
|
if order, err := orderRepository.Save(newOrder); 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 order, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 订单分红服务
|
|
|
func (orderService *OrderService) DarwDividends(darwDividendsCommand *command.DrawDividendsCommand) (interface{}, error) {
|
|
|
if err := darwDividendsCommand.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 (orderService *OrderService) GetOrder(getOrderQuery *query.GetOrderQuery) (interface{}, error) {
|
|
|
if err := getOrderQuery.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 orderRepository domain.OrderRepository
|
|
|
if value, err := factory.CreateOrderRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
orderRepository = value
|
|
|
}
|
|
|
order, err := orderRepository.FindOne(map[string]interface{}{"orderId": getOrderQuery.OrderId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if order == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getOrderQuery.OrderId)))
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return order, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回订单增删改查列表
|
|
|
func (orderService *OrderService) ListOrder(listOrderQuery *query.ListOrderQuery) (interface{}, error) {
|
|
|
if err := listOrderQuery.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 orderRepository domain.OrderRepository
|
|
|
if value, err := factory.CreateOrderRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
orderRepository = value
|
|
|
}
|
|
|
if count, orders, err := orderRepository.Find(tool_funs.SimpleStructToMap(listOrderQuery)); 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,
|
|
|
"orders": orders,
|
|
|
}, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 订单收货服务
|
|
|
func (orderService *OrderService) Receiving(receivingCommand *command.ReceivingCommand) (interface{}, error) {
|
|
|
if err := receivingCommand.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 (orderService *OrderService) RemoveOrder(removeOrderCommand *command.RemoveOrderCommand) (interface{}, error) {
|
|
|
if err := removeOrderCommand.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 orderRepository domain.OrderRepository
|
|
|
if value, err := factory.CreateOrderRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
orderRepository = value
|
|
|
}
|
|
|
order, err := orderRepository.FindOne(map[string]interface{}{"orderId": removeOrderCommand.OrderId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if order == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeOrderCommand.OrderId)))
|
|
|
}
|
|
|
if order, err := orderRepository.Remove(order); 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 order, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 订单退货服务
|
|
|
func (orderService *OrderService) Return(returnCommand *command.ReturnCommand) (interface{}, error) {
|
|
|
if err := returnCommand.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 (orderService *OrderService) Shipping(shippingCommand *command.ShippingCommand) (interface{}, error) {
|
|
|
if err := shippingCommand.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 (orderService *OrderService) UpdateOrder(updateOrderCommand *command.UpdateOrderCommand) (interface{}, error) {
|
|
|
if err := updateOrderCommand.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 orderRepository domain.OrderRepository
|
|
|
if value, err := factory.CreateOrderRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
orderRepository = value
|
|
|
}
|
|
|
order, err := orderRepository.FindOne(map[string]interface{}{"orderId": updateOrderCommand.OrderId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if order == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateOrderCommand.OrderId)))
|
|
|
}
|
|
|
if err := order.Update(tool_funs.SimpleStructToMap(updateOrderCommand)); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
if order, err := orderRepository.Save(order); 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 order, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func NewOrderService(options map[string]interface{}) *OrderService {
|
|
|
newOrderService := &OrderService{}
|
|
|
return newOrderService
|
|
|
} |
...
|
...
|
|