order.go 11.6 KB
package service

import (
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/command"
	"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,
		OrderType: listOrderQuery.OrderType,
	}
	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())
	}
	var PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	for i := range orders {
		var partnerData *domain.PartnerInfo
		partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: orders[i].PartnerInfo.Id})
		if err != nil {
			return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
		}
		orders[i].PartnerInfo = partnerData.Partner
	}
	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 err = transactionContext.StartTransaction(); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	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())
	}

	var PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	var partnerData *domain.PartnerInfo
	partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: order.PartnerInfo.Id})
	if err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	order.PartnerInfo = partnerData.Partner
	err = transactionContext.CommitTransaction()
	if err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	return order, nil
}

//CreateOrder 创建意向单
func (service OrderService) CreateOrder(command command.CreateOrderCommand) error {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		err                   error
	)
	if err = command.ValidateCommand(); err != nil {
		return lib.ThrowError(lib.ARG_ERROR, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	var partnerData *domain.PartnerInfo
	partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: command.PartnerId})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	var orderRepository domain.OrderRepository
	if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	newOrder := &domain.Order{
		//订单类型
		OrderType: command.OrderType,
		//订单编号
		OrderCode: command.OrderCode,
		//订单名称
		OrderName: command.OrderName,
		//订单状态
		OrderStatus: domain.OrderStatusReserve,
		//数量
		OrderCount: command.OrderCount,
		//实际数量
		OrderActualCount: command.OrderActualCount,
		//订单金额
		OrderAmount: command.OrderAmount,
		//实际订单金额
		OrderActualAmount: command.OrderActualAmount,
		//订单已支付分红金额(货款)
		OrderPaymentAmount: 0,
		//订单区域信息
		OrderRegionInfo: domain.RegionInfo{
			RegionName: command.OrderRegion,
		},
		//买家
		Buyer: domain.Buyer{
			BuyerName:       command.BuyerName,
			ShippingAddress: command.BuyerAddress,
			ContactInfo:     command.BuyerPhone,
		},
		PartnerInfo: partnerData.Partner,
		//合伙人分红百分比
		PartnerBonusPercent: command.PartnerBonusPercent,
		//业务员分红百分比
		SalesmanBonusPercent: command.SalesmanBonusPercent,
	}
	err = orderRepository.Save(newOrder)
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	err = transactionContext.CommitTransaction()
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}

//UpdateOrderPurpose 更新意向单
func (service OrderService) UpdateOrderPurpose(command command.UpdateOrderCommand) error {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		err                   error
	)
	if err = command.ValidateCommand(); err != nil {
		return lib.ThrowError(lib.ARG_ERROR, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var (
		orderRepository domain.OrderRepository
		orderData       *domain.Order
	)
	if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	orderData, err = orderRepository.FindOne(domain.OrderFindOneQuery{
		OrderId: command.Id,
	})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if orderData.OrderType != domain.OrderIntention {
		return lib.ThrowError(lib.BUSINESS_ERROR, "订单不是意向单")
	}
	err = orderData.Update(map[string]interface{}{
		//订单编号
		"orderCode":   command.OrderCode,
		"oderName":    command.OrderName,
		"orderCount":  command.OrderCount,
		"orderAmount": command.OrderAmount,
		"buyer": domain.Buyer{
			BuyerName:       orderData.Buyer.BuyerName,
			ContactInfo:     command.BuyerPhone,
			ShippingAddress: command.BuyerAddress,
		},
		"orderRegion": domain.RegionInfo{
			RegionName: command.OrderRegion,
		},
		"partnerBonusPercent":  command.PartnerBonusPercent,
		"salesmanBonusPercent": command.SalesmanBonusPercent,
	})
	if err != nil {
		return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
	}
	err = orderRepository.Save(orderData)
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	err = transactionContext.CommitTransaction()
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}

//RemoveOrder 删除意向单
func (service OrderService) RemoveOrder(id int64) error {
	//实际业务
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var (
		orderRepository domain.OrderRepository
		order           *domain.Order
	)
	if value, err := factory.CreateOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		orderRepository = value
	}
	order, err = orderRepository.FindOne(domain.OrderFindOneQuery{
		OrderId: id,
	})
	if err != nil {
		return lib.ThrowError(lib.RES_NO_FIND_ERROR, err.Error())
	}
	if order.OrderType != domain.OrderIntention {
		return lib.ThrowError(lib.BUSINESS_ERROR, "订单不是意向单")
	}
	err = orderRepository.Remove(order.Id)
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	err = transactionContext.CommitTransaction()
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}

//UpdateOrderReal 更新为实发单
func (service OrderService) UpdateOrderReal(command command.UpdateOrderRealCommand) error {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		err                   error
	)
	if err = command.ValidateCommand(); err != nil {
		return lib.ThrowError(lib.ARG_ERROR, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var (
		orderRepository domain.OrderRepository
		orderData       *domain.Order
	)
	if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	orderData, err = orderRepository.FindOne(domain.OrderFindOneQuery{
		OrderId: command.Id,
	})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	err = orderData.Update(map[string]interface{}{
		//订单编号
		"orderCode":         command.OrderCode,
		"oderName":          command.OrderName,
		"orderActualCount":  command.OrderActualCount,
		"orderActualAmount": command.OrderActualAmount,
		"buyer": domain.Buyer{
			BuyerName:       orderData.Buyer.BuyerName,
			ContactInfo:     command.BuyerPhone,
			ShippingAddress: command.BuyerAddress,
		},
		"orderRegion": domain.RegionInfo{
			RegionName: command.OrderRegion,
		},
		"partnerBonusPercent":  command.PartnerBonusPercent,
		"salesmanBonusPercent": command.SalesmanBonusPercent,
		"orderStatus":          command.OrderStatus,
		"orderType":            domain.OrderReal,
	})
	if err != nil {
		return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
	}
	err = orderRepository.Save(orderData)
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	err = transactionContext.CommitTransaction()
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}