package domain

import "time"

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

// 订单状态
const (
	ORDER_STATUS_SHIPPED  = iota + 1 // 已发货
	ORDER_STATUS_TRANSIT             // 待收货
	ORDER_STATUS_RECEIVED            // 已收货
	ORDER_STATUS_RETURNED            // 已退货
	ORDER_STATUS_CANCELED            //	已取消
	ORDER_STATUS_CLOSED              // 已关闭
)

// 订单实体
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"`
}

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 {
	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
}