package services

import (
	"github.com/gogf/gf/util/guid"
	"sparrow/pkg/generator"
	"sparrow/pkg/models"
	"sparrow/services/knowoapi/model"
)

//ProductService 业务接口
type ProductService interface {
	Create(*models.Product) error
	Delete(*models.Product) error
	Update(*models.Product) (models.Product, error)
	// 查询厂商所有产品列表
	GetVendorProducts(vendorid string, pi, ps int, name string) ([]models.Product, int, error)
	// 查询单个产品信息
	//参数:厂商ID,产品KEY
	QueryOne(string, string) (models.Product, error)
}

type productService struct {
	model  *model.All
	keyGen *generator.KeyGenerator
}

// NewProductService ·
func NewProductService(pm *model.All, gen *generator.KeyGenerator) ProductService {
	return &productService{
		model:  pm,
		keyGen: gen,
	}
}
func (p *productService) Create(product *models.Product) error {
	product.RecordId = guid.S()
	err := p.model.Product.Create(product)
	if err != nil {
		return err
	}
	product.ProductKey, _ = p.keyGen.GenRandomKey(product.RecordId)
	return p.model.Product.Create(product)
}

func (p *productService) Delete(pro *models.Product) error {
	return p.model.Product.Delete(pro)
}

func (p *productService) Update(pro *models.Product) (models.Product, error) {
	return p.model.Product.Update(pro)
}

func (p *productService) GetVendorProducts(vendorid string, pi, ps int, name string) ([]models.Product, int, error) {
	return p.model.Product.GetVendorProducts(vendorid, pi, ps, name)
}

func (p *productService) QueryOne(vendorid string, key string) (models.Product, error) {
	return p.model.Product.QueryOne(key, vendorid)
}
