order.go 10.3 KB
package domain

import (
	"fmt"
	"time"
)

// 订单类型
const (
	ORDER_TYPE_ACTUAL    = iota + 1 //实际订单
	ORDER_TYPE_INTENTION            // 意向订单
)

// 订单状态
const (
	ORDER_STATUS_UNSHIPPED = iota + 1 // 待发货
	ORDER_STATUS_TRANSIT              // 待收货(运输中)
	ORDER_STATUS_RECEIVED             // 已收货
	ORDER_STATUS_RETURN               // 退换货
	ORDER_STATUS_CANCELED             // 已取消(关闭)
	ORDER_STATUS_COMPLETED            // 已完成
)

// 订单实体
type Order struct {
	// 订单id
	OrderId int64 `json:"orderId"`
	// 买家信息
	Buyer *BuyerInfo `json:"buyer"`
	// 公司Id
	CompanyId int64 `json:"companyId"`
	// 合伙人id
	PartnerId int64 `json:"partnerId"`
	// 发货单号
	DeliveryCode string `json:"deliveryCode"`
	// 是否关闭订单标志
	IsDisable int `json:"isDisable"`
	// 订单编号
	OrderNo string `json:"orderNo"`
	// 订单详情
	OrderDetail *OrderDetail `json:"orderDetail"`
	// 订单分红信息
	OrderDividend *Dividend `json:"orderDividend"`
	// 订单分红支付状态
	OrderDividendStatus int `json:"orderDividendStatus"`
	// 订单货品
	OrderGoods []*Goods `json:"orderGoods"`
	// 订单数据来源
	OrderSource *OrderSource `json:"orderSource"`
	// 订单类型(1.实际订单;2.意向订单)
	OrderType int `json:"orderType"`
	// 订单状态(1.发货;2.收货;3.退货;4.取消;5.完成)
	OrderStatus int `json:"orderStatus"`
	// 订单总价
	TotalPrice float64 `json:"totalPrice"`
	// 订单区域信息
	RegionInfo *RegionInfo `json:"regionInfo"`
	// 备注
	Remarks string `json:"remarks"`
	// 创建时间
	CreateAt time.Time `json:"createAt"`
	// 更新时间
	UpdateAt time.Time `json:"updateAt"`
	// 软删
	DeleteAt time.Time `json:"deleteAt"`
	// 当前状态
	CurrentStatus OrderStatus `json:"-"`
}

// 订单状态机
type OrderStatus interface {
	Update(order *Order, data map[string]interface{}) error // 更新订单
	Checkout(order *Order) error                            // 下单
	Return(order *Order) error                              // 退换货
	Cancel(order *Order) error                              // 取消订单
	Receive(order *Order) error                             // 收货
}

// 代发货状态
type UnShippedStatus struct{}

// 编辑待发货订单
func (status *UnShippedStatus) Update(order *Order, data map[string]interface{}) error {
	if orderId, ok := data["orderId"]; ok {
		order.OrderId = orderId.(int64)
	}
	if buyerName, ok := data["buyerName"]; ok {
		order.Buyer.BuyerName = buyerName.(string)
	}
	if contactInfo, ok := data["contactInfo"]; ok {
		order.Buyer.ContactInfo = contactInfo.(string)
	}
	if shippingAddress, ok := data["shippingAddress"]; ok {
		order.Buyer.ShippingAddress = shippingAddress.(string)
	}
	if companyId, ok := data["companyId"]; ok {
		order.CompanyId = companyId.(int64)
	}
	if partnerId, ok := data["partnerId"]; ok {
		order.PartnerId = partnerId.(int64)
	}
	if deliveryCode, ok := data["deliveryCode"]; ok {
		order.DeliveryCode = deliveryCode.(string)
	}
	if isDisable, ok := data["isDisable"]; ok {
		order.IsDisable = isDisable.(int)
	}
	if orderNo, ok := data["orderNo"]; ok {
		order.OrderNo = orderNo.(string)
	}
	if orderDetailId, ok := data["orderDetailId"]; ok {
		order.OrderDetail.OrderDetailId = orderDetailId.(int64)
	}
	if orderNo, ok := data["orderNo"]; ok {
		order.OrderDetail.OrderNo = orderNo.(string)
	}
	if orderNum, ok := data["orderNum"]; ok {
		order.OrderDetail.OrderNum = orderNum.(int)
	}
	if totalPrice, ok := data["totalPrice"]; ok {
		order.OrderDetail.TotalPrice = totalPrice.(float64)
	}
	if adjustedNum, ok := data["adjustedNum"]; ok {
		order.OrderDetail.AdjustedNum = adjustedNum.(int)
	}
	if adjustedTotalPrice, ok := data["adjustedTotalPrice"]; ok {
		order.OrderDetail.AdjustedTotalPrice = adjustedTotalPrice.(float64)
	}
	if planBonus, ok := data["planBonus"]; ok {
		order.OrderDividend.PlanBonus = planBonus.(float64)
	}
	if useBonus, ok := data["useBonus"]; ok {
		order.OrderDividend.UseBonus = useBonus.(float64)
	}
	if bonusHas, ok := data["bonusHas"]; ok {
		order.OrderDividend.BonusHas = bonusHas.(float64)
	}
	if bonusNot, ok := data["bonusNot"]; ok {
		order.OrderDividend.BonusNot = bonusNot.(float64)
	}
	if bonusExpense, ok := data["bonusExpense"]; ok {
		order.OrderDividend.BonusExpense = bonusExpense.(float64)
	}
	if bonusStatus, ok := data["bonusStatus"]; ok {
		order.OrderDividend.BonusStatus = bonusStatus.(int)
	}
	if partnerBonusPercent, ok := data["partnerBonusPercent"]; ok {
		order.OrderDividend.PartnerBonusPercent = partnerBonusPercent.(float64)
	}
	if salesmanBonusPercent, ok := data["salesmanBonusPercent"]; ok {
		order.OrderDividend.SalesmanBonusPercent = salesmanBonusPercent.(float64)
	}
	if salesmanBonus, ok := data["salesmanBonus"]; ok {
		order.OrderDividend.SalesmanBonus = salesmanBonus.(float64)
	}
	if orderDividendStatus, ok := data["orderDividendStatus"]; ok {
		order.OrderDividendStatus = orderDividendStatus.(int)
	}
	if orderGoods, ok := data["orderGoods"]; ok {
		order.OrderGoods = orderGoods.([]*Goods)
	}
	if dataSourceId, ok := data["dataSourceId"]; ok {
		order.OrderSource.DataSourceId = dataSourceId.(int64)
	}
	if platform, ok := data["platform"]; ok {
		order.OrderSource.Platform = platform.(string)
	}
	if createAt, ok := data["createAt"]; ok {
		order.OrderSource.CreateAt = createAt.(time.Time)
	}
	if orderType, ok := data["orderType"]; ok {
		order.OrderType = orderType.(int)
	}
	if orderStatus, ok := data["orderStatus"]; ok {
		order.OrderStatus = orderStatus.(int)
	}
	if totalPrice, ok := data["totalPrice"]; ok {
		order.TotalPrice = totalPrice.(float64)
	}
	if regionName, ok := data["regionName"]; ok {
		order.RegionInfo.RegionName = regionName.(string)
	}
	if remarks, ok := data["remarks"]; ok {
		order.Remarks = remarks.(string)
	}
	if createAt, ok := data["createAt"]; ok {
		order.CreateAt = createAt.(time.Time)
	}
	if updateAt, ok := data["updateAt"]; ok {
		order.UpdateAt = updateAt.(time.Time)
	}
	if deleteAt, ok := data["deleteAt"]; ok {
		order.DeleteAt = deleteAt.(time.Time)
	}
	return nil
}

func (status *UnShippedStatus) Checkout(order *Order) error {
	return nil
}

func (status *UnShippedStatus) Return(order *Order) error {
	return fmt.Errorf("待发货订单不能退货")
}

func (status *UnShippedStatus) Cancel(order *Order) error {
	return nil
}

func (status *UnShippedStatus) Receive(order *Order) error {
	return fmt.Errorf("待发货订单不能收货")
}

// 待收货状态
type TransitStatus struct{}

// 待收货订单不允许编辑
func (status *TransitStatus) Update(order *Order, data map[string]interface{}) error {
	return fmt.Errorf("已发货订单不允许编辑")
}

func (status *TransitStatus) Checkout(order *Order) error {
	return fmt.Errorf("待收货订单不能重复发货")
}

func (status *TransitStatus) Return(order *Order) error {
	return nil
}

func (status *TransitStatus) Cancel(order *Order) error {
	return fmt.Errorf("待收货订单不能取消")
}

func (status *TransitStatus) Receive(order *Order) error {
	return nil
}

// 收货状态
type ReceivedStatus struct{}

func (status *ReceivedStatus) Update(order *Order, data map[string]interface{}) error {
	return fmt.Errorf("已收货订单不能编辑")
}

func (status *ReceivedStatus) Checkout(order *Order) error {
	return fmt.Errorf("已收货订单不能重复发货")
}

func (status *ReceivedStatus) Return(order *Order) error {
	return nil
}

func (status *ReceivedStatus) Cancel(order *Order) error {
	return fmt.Errorf("已收货订单不能取消")
}

func (status *ReceivedStatus) Receive(order *Order) error {
	return fmt.Errorf("已收货订单不能重复收货")
}

// 退货状态
type ReturnedStatus struct{}

func (status *ReturnedStatus) Update(order *Order, data map[string]interface{}) error {
	return fmt.Errorf("已退货订单不允许编辑")
}

func (status *ReturnedStatus) Checkout(order *Order) error {
	return fmt.Errorf("已退货订单不允许发货")
}

func (status *ReturnedStatus) Return(order *Order) error {
	return fmt.Errorf("已退货订单不允许再次退货")
}

func (status *ReturnedStatus) Cancel(order *Order) error {
	return fmt.Errorf("已退货订单不能取消")
}

func (status *ReturnedStatus) Receive(order *Order) error {
	return fmt.Errorf("已退货订单不能收货")
}

// 取消状态
type CanceledStatus struct{}

func (status *CanceledStatus) Update(order *Order, dat map[string]interface{}) error {
	return fmt.Errorf("已取消订单不能编辑")
}

func (status *CanceledStatus) Checkout(order *Order) error {
	return fmt.Errorf("已取消订单不能发货")
}

func (status *CanceledStatus) Return(order *Order) error {
	return fmt.Errorf("已取消订单不能退货")
}

func (status *CanceledStatus) Cancel(order *Order) error {
	return fmt.Errorf("已取消订单不能再次取消")
}

func (status *CanceledStatus) Receive(order *Order) error {
	return fmt.Errorf("已取消订单不能进行收货")
}

// 已完成状态
type CompletedStatus struct{}

func (status *CompletedStatus) Update(order *Order, data map[string]interface{}) error {
	return nil
}

func (status *CompletedStatus) Checkout(order *Order) error {
	return fmt.Errorf("已完成订单不能发货")
}

func (status *CompletedStatus) Return(order *Order) error {
	return fmt.Errorf("已完成订单不能退货")
}

func (status *CompletedStatus) Cancel(order *Order) error {
	return fmt.Errorf("已完成订单不能取消")
}

func (status *CompletedStatus) Receive(order *Order) error {
	return fmt.Errorf("已完成订单不能收货")
}

// 订单仓储
type OrderRepository interface {
	Save(order *Order) (*Order, error)
	Remove(order *Order) (*Order, error)
	FindOne(queryOptions map[string]interface{}) (*Order, error)
	Find(queryOptions map[string]interface{}) (int64, []*Order, error)
}

func (order *Order) Identify() interface{} {
	if order.OrderId == 0 {
		return nil
	}
	return order.OrderId
}

// 更新订单
func (order *Order) Update(data map[string]interface{}) error {
	return order.CurrentStatus.Update(order, data)
}

// 发货
func (order *Order) Checkout() error {
	return order.CurrentStatus.Checkout(order)
}

// 退货
func (order *Order) Return() error {
	return order.CurrentStatus.Return(order)
}

// 收货
func (order *Order) Receive() error {
	return order.CurrentStatus.Receive(order)
}

// 取消订单
func (order *Order) Cancel() error {
	return order.CurrentStatus.Cancel(order)
}