package gmodel

import "context"

// 查询
func (c *FsProductCollectionModel) FindOne(ctx context.Context, userId, guestId, productId int64) (resp *FsProductCollection, err error) {
	err = c.db.WithContext(ctx).Model(&FsProductCollection{}).
		Where("user_id = ? and guest_id = ? and product_id = ?", userId, guestId, productId).
		Take(&resp).Error
	return resp, err
}

// 创建
func (c *FsProductCollectionModel) Create(ctx context.Context, data *FsProductCollection) error {
	return c.db.WithContext(ctx).Model(&FsProductCollection{}).Create(&data).Error
}

// 更新
func (c *FsProductCollectionModel) Update(ctx context.Context, userId, guestId, productId int64, data *FsProductCollection) error {
	return c.db.WithContext(ctx).Model(&FsProductCollection{}).
		Where("user_id = ? and guest_id = ? and product_id = ?", userId, guestId, productId).
		Updates(&data).Error
}

// 删除
func (c *FsProductCollectionModel) Delete(ctx context.Context, userId, guestId, productId int64) error {
	return c.db.WithContext(ctx).Model(&FsProductCollection{}).
		Where("user_id = ? and guest_id = ? and product_id = ?", userId, guestId, productId).
		Delete(&FsProductCollection{}).Error
}

// 删除
func (c *FsProductCollectionModel) Delete2(ctx context.Context, userId, guestId, id int64) error {
	return c.db.WithContext(ctx).Model(&FsProductCollection{}).
		Where("user_id = ? and guest_id = ? and id = ?", userId, guestId, id).
		Delete(&FsProductCollection{}).Error
}

// 获取列表
func (c *FsProductCollectionModel) GetList(ctx context.Context, userId, guestId int64, page, limit int, sort string) (resp []FsProductCollection, total int64, err error) {
	db := c.db.WithContext(ctx).Model(&FsProductCollection{}).
		Where("user_id = ? and guest_id = ?", userId, guestId)
	if sort != "" {
		db = db.Order(sort)
	}
	if err = db.Count(&total).Error; err != nil {
		return nil, 0, err
	}
	offset := (page - 1) * limit
	err = db.Offset(offset).Limit(limit).Find(&resp).Error
	return resp, total, err
}