package domain

import (
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/utils"
	"time"
)

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

const (
	OrderReal      = iota + 1 //实发订单
	OrderIntention            //意向订单
)

//买家
type Buyer struct {
	//买家姓名
	BuyerName string `json:"buyerName"`
	//联系方式
	ContactInfo string `json:"contactInfo"`
	//收获地址
	ShippingAddress string `json:"shippingAddress"`
}

type Order struct {
	Id int64 `json:"id"`
	//订单类型
	OrderType int `json:"orderType"`
	//订单编号
	OrderCode string `json:"orderCode"`
	//订单名称
	OrderName string `json:"oderName"`
	//订单状态
	OrderStatus int `json:"orderStatus"`
	//数量
	OrderCount int `json:"orderCount"`
	//实际数量
	OrderActualCount int `json:"orderActualCount"`
	//订单金额
	OrderAmount float64 `json:"orderAmount"`
	//实际订单金额
	OrderActualAmount float64 `json:"orderActualAmount"`
	//订单已支付分红金额(货款)
	OrderPaymentAmount float64 `json:"orderPaymentAmount"`
	//订单区域信息
	OrderRegionInfo RegionInfo `json:"orderRegionInfo"`
	//买家
	Buyer Buyer `json:"buyer"`
	//合伙人数据
	PartnerInfo Partner `json:"partnerInfo"`
	//合伙人分红百分比
	PartnerBonusPercent float64 `json:"partnerBonusPercent"`
	//业务员分红百分比
	SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
	//最后查看得时间
	LastViewTime time.Time `json:"lastViewTime"`
	//更新时间
	UpdateAt time.Time `json:"updateAt"`
	CreateAt time.Time `json:"createAt"`
}

//TODO
func (order *Order) Update(data map[string]interface{}) error {
	if v, ok := data["orderType"]; ok {
		order.OrderType = v.(int)
	}
	if v, ok := data["orderCode"]; ok {
		order.OrderCode = v.(string)
	}
	if v, ok := data["oderName"]; ok {
		order.OrderName = v.(string)
	}
	if v, ok := data["orderStatus"]; ok {
		order.OrderStatus = v.(int)
	}
	if v, ok := data["orderCount"]; ok {
		order.OrderCount = v.(int)
	}
	if v, ok := data["orderActualCount"]; ok {
		order.OrderActualCount = v.(int)
	}
	if v, ok := data["orderAmount"]; ok {
		order.OrderAmount = v.(float64)
	}
	if v, ok := data["orderActualAmount"]; ok {
		order.OrderActualAmount = v.(float64)
	}
	if v, ok := data["orderPaymentAmount"]; ok {
		order.OrderPaymentAmount = v.(float64)
	}
	if v, ok := data["orderRegion"]; ok {
		order.OrderRegionInfo = v.(RegionInfo)
	}
	if v, ok := data["buyer"]; ok {
		order.Buyer = v.(Buyer)
	}
	if v, ok := data["partnerInfo"]; ok {
		order.PartnerInfo = v.(Partner)
	}
	if v, ok := data["partnerBonusPercent"]; ok {
		order.PartnerBonusPercent = v.(float64)
	}
	if v, ok := data["salesmanBonusPercent"]; ok {
		order.SalesmanBonusPercent = v.(float64)
	}
	return nil
}

//订单累计分红
func (m *Order) OrderTotalBonus() float64 {
	return utils.Decimal(m.OrderActualAmount * (m.PartnerBonusPercent / 100.0))
}

//订单已收分红
func (m *Order) OrderBonusReceive() float64 {
	return utils.Decimal(m.OrderPaymentAmount * (m.PartnerBonusPercent / 100.0))
}

//订单未收分红
func (m *Order) OrderBonusWait() float64 {
	bonusWait := m.OrderTotalBonus() - m.OrderBonusReceive()
	if bonusWait < 0 {
		return 0
	}
	return bonusWait
}

//分红支出
func (m *Order) OrderBonusOutstanding() float64 {
	if m.OrderAmount <= m.OrderActualAmount {
		return 0
	}
	return utils.Decimal((m.OrderAmount - m.OrderActualAmount) * (m.PartnerBonusPercent / 100.0))
}

//订单被取消金额
func (m *Order) OrderAmountCancel() float64 {
	if m.OrderAmount <= m.OrderActualAmount {
		return 0
	}
	return utils.Decimal((m.OrderAmount - m.OrderActualAmount))
}

type OrderFindOneQuery struct {
	OrderId int64
}

type OrderFindQuery struct {
	PartnerId int64
	OrderCode string
	Offset    int
	Limit     int
	OrderType int
}

type OrderRepository interface {
	Save(order *Order) error
	FindOne(qureyOptions OrderFindOneQuery) (*Order, error)
	Find(queryOptions OrderFindQuery) ([]Order, error)
	CountAll(queryOption OrderFindQuery) (int, error)
	Remove(id int64) error
}