135 lines
3.7 KiB
Go
135 lines
3.7 KiB
Go
package dao
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"hepa-calc-api/api/model"
|
|
"hepa-calc-api/api/requests"
|
|
"hepa-calc-api/global"
|
|
)
|
|
|
|
type UserCollectionDao struct {
|
|
}
|
|
|
|
// GetUserCollectionById 获取数据-id
|
|
func (r *UserCollectionDao) GetUserCollectionById(UserCollectionId int64) (m *model.UserCollection, err error) {
|
|
err = global.Db.First(&m, UserCollectionId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetUserCollectionPreloadById 获取数据-加载全部关联-id
|
|
func (r *UserCollectionDao) GetUserCollectionPreloadById(UserCollectionId int64) (m *model.UserCollection, err error) {
|
|
err = global.Db.Preload(clause.Associations).First(&m, UserCollectionId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteUserCollection 删除
|
|
func (r *UserCollectionDao) DeleteUserCollection(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.UserCollection{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteUserCollectionById 删除-id
|
|
func (r *UserCollectionDao) DeleteUserCollectionById(tx *gorm.DB, UserCollectionId int64) error {
|
|
if err := tx.Delete(&model.UserCollection{}, UserCollectionId).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditUserCollection 修改
|
|
func (r *UserCollectionDao) EditUserCollection(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.UserCollection{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditUserCollectionById 修改-id
|
|
func (r *UserCollectionDao) EditUserCollectionById(tx *gorm.DB, UserCollectionId int64, data interface{}) error {
|
|
err := tx.Model(&model.UserCollection{}).Where("user_coupon_id = ?", UserCollectionId).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetUserCollectionList 获取列表
|
|
func (r *UserCollectionDao) GetUserCollectionList(maps interface{}) (m []*model.UserCollection, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetUserCollectionCount 获取数量
|
|
func (r *UserCollectionDao) GetUserCollectionCount(maps interface{}) (total int64, err error) {
|
|
err = global.Db.Model(&model.UserCollection{}).Where(maps).Count(&total).Error
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
// GetUserCollectionListRand 获取列表-随机
|
|
func (r *UserCollectionDao) GetUserCollectionListRand(maps interface{}, limit int) (m []*model.UserCollection, err error) {
|
|
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddUserCollection 新增
|
|
func (r *UserCollectionDao) AddUserCollection(tx *gorm.DB, model *model.UserCollection) (*model.UserCollection, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// GetUserCollection 获取
|
|
func (r *UserCollectionDao) GetUserCollection(maps interface{}) (m *model.UserCollection, err error) {
|
|
err = global.Db.Where(maps).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetUserCollectionPageSearch 获取列表-分页
|
|
func (r *UserCollectionDao) GetUserCollectionPageSearch(req requests.GetUserCollectionPage, page, pageSize int) (m []*model.UserCollection, total int64, err error) {
|
|
var totalRecords int64
|
|
|
|
// 构建查询条件
|
|
query := global.Db.Model(&model.UserCollection{})
|
|
query = query.Where("user_id = ?", req.UserId)
|
|
|
|
query = query.Preload("Question")
|
|
|
|
// 排序
|
|
query = query.Order("created_at desc")
|
|
|
|
// 查询总数量
|
|
if err := query.Count(&totalRecords).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return m, totalRecords, nil
|
|
}
|