package yb_user_collection

import "hongze/hongze_yb/global"

func (item *YbUserCollection) Create() (err error) {
	err = global.DEFAULT_MYSQL.Create(item).Error
	return
}

func (item *YbUserCollection) Update(updateCols []string) (err error) {
	err = global.DEFAULT_MYSQL.Model(item).Select(updateCols).Updates(*item).Error
	return
}

// GetPageListByCondition 获取收藏列表-分页
func GetPageListByCondition(condition string, pars []interface{}, pageIndex, pageSize int) (list []*YbUserCollection, err error) {
	offset := (pageIndex - 1) * pageSize
	err = global.DEFAULT_MYSQL.Model(YbUserCollection{}).
		Where(condition, pars...).
		Offset(offset).Limit(pageSize).
		Order("create_time DESC").
		Scan(&list).Error
	return
}

// GetPageListTotalByCondition 获取收藏列表总数-分页
func GetPageListTotalByCondition(condition string, pars []interface{}) (total int64, err error) {
	err = global.DEFAULT_MYSQL.Model(YbUserCollection{}).
		Where(condition, pars...).
		Count(&total).Error
	return
}

// GetItemById 主键获取收藏
func GetItemById(collectionId int) (item *YbUserCollection, err error) {
	err = global.DEFAULT_MYSQL.Model(YbUserCollection{}).
		Where("collection_id = ? AND state = 1", collectionId).
		First(&item).Error
	return
}

// GetItemByCondition 条件获取收藏
func GetItemByCondition(condition string, pars []interface{}) (item *YbUserCollection, err error) {
	err = global.DEFAULT_MYSQL.Model(YbUserCollection{}).
		Where(condition, pars...).
		First(&item).Error
	return
}

// GetListByCondition 条件获取收藏列表
func GetListByCondition(condition string, pars []interface{}) (list []*YbUserCollection, err error) {
	err = global.DEFAULT_MYSQL.Model(YbUserCollection{}).
		Where(condition, pars...).
		Scan(&list).Error
	return
}
