package order

import (
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
)

func Template(header *protocol.RequestHeader, request *protocol.OrderDetailRequest) (rsp *protocol.OrderDetailResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		//OrderResponsitory,_ =factory.CreateOrderRepository(transactionContext)

	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			transactionContext.RollbackTransaction()
		}
	}()
	err = transactionContext.CommitTransaction()
	return
}

func OrderDetail(header *protocol.RequestHeader, request *protocol.OrderDetailRequest) (rsp *protocol.OrderDetailResponse, err error) {
	var (
		transactionContext, _    = factory.CreateTransactionContext(nil)
		OrderResponsitory, _     = factory.CreateOrderRepository(transactionContext)
		PartnerInfoRepository, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
		order                    *domain.Order
		partner                  *domain.PartnerInfo
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			transactionContext.RollbackTransaction()
		}
	}()

	rsp = &protocol.OrderDetailResponse{}
	if order, err = OrderResponsitory.FindOne(utils.ObjectJsonToMap(request)); err != nil {
		return
	}
	if partner, err = PartnerInfoRepository.FindOne(map[string]interface{}{"partnerId": order.PartnerId}); err != nil {
		return
	}
	rsp.Order = order
	rsp.Partner = partner
	err = transactionContext.CommitTransaction()
	return
}

func OrderStatistics(header *protocol.RequestHeader, request *protocol.OrderStatisticsRequest) (rsp *protocol.OrderStatisticsResponse, err error) {
	var (
	//beginTime =time.Unix(request)
	)
	rsp = &protocol.OrderStatisticsResponse{}
	return
}

func OrderList(header *protocol.RequestHeader, request *protocol.OrderListRequest) (rsp *protocol.OrderListResponse, err error) {
	var ()
	rsp = &protocol.OrderListResponse{}
	return
}