package service

import (
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/query"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)

//OrderService 管理员相关服务
type OrderService struct {
}

func NewOrderService(option map[string]interface{}) *OrderService {
	newAdminUserService := new(OrderService)
	return newAdminUserService
}

func (service OrderService) PageListOrder(listOrderQuery query.ListOrderQuery) ([]domain.Order, int, error) {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	var (
		orderRepository domain.OrderRepository
		orders          []domain.Order
		cnt             int
	)
	if value, err := factory.CreateOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		orderRepository = value
	}
	query := domain.OrderFindQuery{
		PartnerId: listOrderQuery.PartnerId,
		OrderCode: listOrderQuery.OrderCode,
		Offset:    listOrderQuery.Offset,
		Limit:     listOrderQuery.Limit,
	}
	orders, err = orderRepository.Find(query)
	if err != nil {
		return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	cnt, err = orderRepository.CountAll(query)
	if err != nil {
		return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	return orders, cnt, nil
}

func (service OrderService) GetOrder(getOrderQuery query.GetOrderQuery) (*domain.Order, error) {
	//实际业务
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	var (
		orderRepository domain.OrderRepository
		order           *domain.Order
	)
	if value, err := factory.CreateOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		orderRepository = value
	}
	order, err = orderRepository.FindOne(domain.OrderFindOneQuery{
		OrderId: getOrderQuery.OrderId,
	})
	if err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}

	return order, nil
}

func (service OrderService) CreateOrde() error {
	return nil
}