package domain

import "time"

const (
	OrderStatusReserve     = iota + 1 //预定中
	OrderStatusDeliverSome            //部分发货
	OrderStatusDeliverAll             //全部发货
)

type OrderRepository interface {
	Save(dm *Order) (*Order, error)
	Remove(dm *Order) (*Order, error)
	FindOne(queryOptions map[string]interface{}) (*Order, error)
	Find(queryOptions map[string]interface{}) (int64, []*Order, error)
}

//Order 订单信息
type Order struct {
	//id
	Id int64 `json:"Id,omitempty"`
	//订单类型
	OrderType int `json:"orderType,omitempty"`
	//订单编号
	OrderCode string `json:"orderCode,omitempty"`
	//订单名称
	OrderName string `json:"orderName,omitempty"`
	//订单状态
	OrderStatus int `json:"orderStatus,omitempty"`
	//订单数量
	OrderCount int `json:"orderCount,omitempty"`
	//实际订单数量
	OrderActualCount int `json:"orderActualCount,omitempty"`
	//订单金额
	OrderAmount float64 `json:"admin_name,omitempty"`
	//订单实际金额
	OrderActualAmount float64 `json:"orderActualAmount,omitempty"`
	//订单已支付金额(货款)
	OrderPaymentAmount float64 `json:"orderPaymentAmount,omitempty"`
	//订单区域信息
	OrderRegionInfo *RegionInfo `json:"orderRegionInfo,omitempty"`

	Buyer *Buyer `json:"buyer,omitempty"`
	//合伙人编号
	PartnerId int64 `json:"partnerId,omitempty"`
	//合伙人分红百分比
	PartnerBonusPercent float64 `json:"partnerBonusPercent,omitempty"`
	//业务员分红百分比
	SalesmanBonusPercent float64 `json:"salesmanBonusPercent,omitempty"`

	//创建时间
	CreateAt time.Time `json:"createAt,omitempty"`
	//更新时间
	UpdateAt time.Time `json:"updateAt,omitempty"`

	//上一次查看时间 已读情况
	LastViewTime time.Time `json:"lastViewTime,omitempty"`
}

func (m *Order) Identify() interface{} {
	if m.Id == 0 {
		return nil
	}
	return m.Id
}

func (m *Order) Update(data map[string]interface{}) error {
	if orderType, ok := data["orderType"]; ok && orderType != 0 {
		m.OrderType = orderType.(int)
	}
	if orderCode, ok := data["orderCode"]; ok && orderCode != "" {
		m.OrderCode = orderCode.(string)
	}
	if orderName, ok := data["orderName"]; ok && orderName != "" {
		m.OrderName = orderName.(string)
	}
	if orderStatus, ok := data["orderStatus"]; ok && orderStatus != 0 {
		m.OrderStatus = orderStatus.(int)
	}
	if lastViewTime, ok := data["lastViewTime"]; ok && lastViewTime != 0 {
		m.LastViewTime = lastViewTime.(time.Time)
	}
	m.UpdateAt = time.Now()
	return nil
}