package v2

import (
	"time"
)

type ProviderStatus int

const (
	ProviderStatus_Normal = iota
	// 正在拉取供应商商品信息
	ProviderStatus_Pulling
	// 正在计算价格
	ProviderStatus_Calculating
	// 出错
	ProviderStatus_Error
)

type ProviderId string

type Provider struct {
	ID        uint      `gorm:"primary_key" json:"id"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`

	ProviderId ProviderId `gorm:"unique" json:"providerId"`
	// 供应商的名称
	Name string `json:"name"`
	// 供应商状态
	Status ProviderStatus `json:"status"`
	// 错误信息
	Msg string `json:"msg"`
	// 拉取时间
	PullAt time.Time `json:"pullAt"`
	// 供应商配置
	Config ProviderOption `gorm:"type:json;serializer:json" json:"config"`
	//计算过程
	CalculateProcess []CalculateProcess `json:"calculateProcess" gorm:"polymorphicType:Kind;polymorphicId:OwnerID;polymorphicValue:provider"`
	// 备注
	Remark string `json:"remark,omitempty"`
}

type ProviderOption struct {
	NotCrawl     bool    `json:"notCrawl"`                         // 要不要抓取
	ExchangeRate float64 `yaml:"exchangeRate" json:"exchangeRate"` //汇率
	Freight      float64 `yaml:"freight" json:"freight"`           //运费
	Ticker       string  `yaml:"ticker" json:"ticker"`             //每天几点抓取
}

// ProviderArticle 供应商商品信息
type ProviderArticle struct {
	ID uint `gorm:"primary_key" json:"id"`

	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
	// 对应的商品ID
	ArticleID uint
	// 货号
	Pid string `gorm:"index" json:"pid"`
	// 品牌
	Brand Brand `gorm:"index" json:"brand"`
	// 供应商商品链接链接
	Link string `json:"link"`
	// 图片
	Image string `json:"image"`
	// 供应商id
	ProviderId ProviderId `gorm:"index:providerId_sku,unique" json:"providerId"`
	// 供应商的sku
	SkuID string `gorm:"index:providerId_sku,unique"  json:"skuID"`
	// 当前成本价
	Cost ProviderPrice `json:"cost" gorm:"type:json;serializer:json"`
	// 库存数量
	Ats int `json:"ats"`
	// 监控库存
	TraceAts *bool `json:"traceAts"`
	// 能否购买
	Available bool `json:"available"`
	// 是否蹲货
	Watch *bool `json:"watch"`
	// 状态
	Status ProviderStatus `json:"status"`
	// 是否排除,如果排除,就不用拉取价格
	Exclude bool `json:"exclude"`
	// 历史成本价格
	HistoryPrice []ProviderPrice `json:"historyPrice"`
	// 历史库存
	HistoryAts []ProviderAts `json:"historyAts"`
	// 计算过程
	CalculateProcess []CalculateProcess `json:"calculateProcess" gorm:"polymorphicType:Kind;polymorphicId:OwnerID;polymorphicValue:providerArticle"`
}

func (p *ProviderArticle) SetWatch(watch bool) *ProviderArticle {
	p.Watch = &watch
	return p
}

// 设置是否追踪库存
func (p *ProviderArticle) SetTraceAts(traceAts bool) *ProviderArticle {
	p.TraceAts = &traceAts
	return p
}

// ProviderPrice 供应商成本价格
type ProviderPrice struct {
	ID        uint      `gorm:"primary_key" json:"id"`
	CreatedAt time.Time `json:"createdAt"`

	ProviderArticleID uint `gorm:"index"`
	// 供应商原始价格,美元、人名币、加元
	OriginalPrice float64 `json:"originalPrice"`
	// 计算优惠后最终价格,人名币,单位为分
	FinalPrice float64 `json:"finalPrice"` // 计算完优惠的最终的价格
	CalMark    string  `json:"calMark"`    //计算优惠的过程
}

// ProviderAts 供应商库存
type ProviderAts struct {
	ID        uint      `gorm:"primary_key" json:"id"`
	CreatedAt time.Time `json:"createdAt"`

	ProviderArticleID uint `gorm:"index"`
	// 供应商库存
	Ats int `json:"ats"`
}