package jxutils

import (
	"git.rosy.net.cn/jx-callback/business/model"
)

type ActStoreSkuMap struct {
	actStoreSkuMap map[int64]map[int]*model.ActStoreSku2
}

// isActPrice为true表示是活动，false表示是结算
func NewActStoreSkuMap(actStoreSkuList []*model.ActStoreSku2, isActPrice bool) (actMap *ActStoreSkuMap) {
	actMap = &ActStoreSkuMap{}
	actStoreSkuMap := make(map[int64]map[int]*model.ActStoreSku2)
	for _, v := range actStoreSkuList {
		index := Combine2Int(v.StoreID, v.SkuID)
		if actStoreSkuMap[index] == nil {
			actStoreSkuMap[index] = make(map[int]*model.ActStoreSku2)
		}
		if (isActPrice && v.ActualActPrice > 0 && (actStoreSkuMap[index][v.VendorID] == nil || actStoreSkuMap[index][v.VendorID].ActualActPrice > v.ActualActPrice)) ||
			(!isActPrice && v.EarningPrice > 0 && (actStoreSkuMap[index][v.VendorID] == nil || actStoreSkuMap[index][v.VendorID].EarningPrice > v.EarningPrice)) {
			actStoreSkuMap[index][v.VendorID] = v
		}
	}
	actMap.actStoreSkuMap = actStoreSkuMap
	return actMap
}

func (a *ActStoreSkuMap) GetActStoreSku(storeID, skuID, vendorID int) (storeSku *model.ActStoreSku2) {
	index := Combine2Int(storeID, skuID)
	if a.actStoreSkuMap[index] != nil {
		if vendorID < 0 {
			for k := range a.actStoreSkuMap[index] {
				vendorID = k
				break
			}
		}
		storeSku = a.actStoreSkuMap[index][vendorID]
	}
	return storeSku
}
