package vo

import (
	"demo/configs"
	"demo/data/domain"
)

type GoodsSkuQuery struct {
	SkuName               string  `xorm:"sku_name"  json:"skuName"` // 255 0 注释：商品sku名字
	PriceStart            float64 `xorm:"price"  json:"price"`      // 10 2 注释：现在价格区间
	PriceEnd              float64 `xorm:"price"  json:"price"`      // 10 2 注释：现在价格区间
	HistoricalPricesStart float64 `xorm:"historical_prices"  json:"historicalPrices"`
	HistoricalPricesEnd   float64 `xorm:"historical_prices"  json:"historicalPrices"`
	InventoryNumberStart  int64   `xorm:"inventory_number"  json:"inventoryNumber"`
	InventoryNumberEnd    int64   `xorm:"inventory_number"  json:"inventoryNumber"`
	CommodityAreaId       int64   `xorm:"commodity_area_id"  json:"commodityAreaId"`
	GoodsId               int64   `xorm:"goods_id"  json:"goodsId"`
}

func GetGoodsSkuList(query GoodsSkuQuery, pageNum, pageSize int) (BaseListVo, error) {
	var arr []domain.GoodsSku
	session := configs.Engine.Table("goods_sku").Limit(pageSize, (pageNum-1)*pageSize)
	if query.SkuName != "" {
		session.Where("sku_name = ?", query.SkuName)
	}
	if query.PriceStart != 0 && query.PriceEnd != 0 {
		session.And("price >= ? && price <= ?", query.PriceStart, query.PriceEnd)
	}
	if query.HistoricalPricesStart != 0 && query.HistoricalPricesEnd != 0 {
		session.And("historical_prices >= ? && historical_prices <= ?", query.HistoricalPricesStart, query.HistoricalPricesEnd)
	}
	if query.InventoryNumberStart != 0 && query.InventoryNumberEnd != 0 {
		session.And("inventory_number >= ? && inventory_number <= ?", query.InventoryNumberStart, query.InventoryNumberEnd)
	}

	Count, err := session.FindAndCount(&arr, &query)
	var vo BaseListVo
	if err != nil {
		return vo, err
	}
	if err != nil {
		return vo, err
	}
	vo.PageNum = pageNum
	vo.PageSize = pageSize
	vo.List = make([]any, len(arr))
	for i, v := range arr {
		vo.List[i] = v
	}
	vo.Total = int(Count)
	return vo, nil
}
