56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"dianshang/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type PlatformRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewPlatformRepository(db *gorm.DB) *PlatformRepository {
|
|
return &PlatformRepository{db: db}
|
|
}
|
|
|
|
// GetAll 获取所有平台
|
|
func (r *PlatformRepository) GetAll() ([]model.Platform, error) {
|
|
var platforms []model.Platform
|
|
err := r.db.Where("status = ?", 1).Order("sort DESC, created_at ASC").Find(&platforms).Error
|
|
return platforms, err
|
|
}
|
|
|
|
// GetByID 根据ID获取平台
|
|
func (r *PlatformRepository) GetByID(id uint) (*model.Platform, error) {
|
|
var platform model.Platform
|
|
err := r.db.Where("id = ?", id).First(&platform).Error
|
|
return &platform, err
|
|
}
|
|
|
|
// GetByCode 根据代码获取平台
|
|
func (r *PlatformRepository) GetByCode(code string) (*model.Platform, error) {
|
|
var platform model.Platform
|
|
err := r.db.Where("code = ? AND status = ?", code, 1).First(&platform).Error
|
|
return &platform, err
|
|
}
|
|
|
|
// Create 创建平台
|
|
func (r *PlatformRepository) Create(platform *model.Platform) error {
|
|
return r.db.Create(platform).Error
|
|
}
|
|
|
|
// Update 更新平台
|
|
func (r *PlatformRepository) Update(id uint, updates map[string]interface{}) error {
|
|
return r.db.Model(&model.Platform{}).Where("id = ?", id).Updates(updates).Error
|
|
}
|
|
|
|
// Delete 删除平台
|
|
func (r *PlatformRepository) Delete(id uint) error {
|
|
return r.db.Delete(&model.Platform{}, id).Error
|
|
}
|
|
|
|
// GetDB 获取数据库连接
|
|
func (r *PlatformRepository) GetDB() *gorm.DB {
|
|
return r.db
|
|
}
|