78 lines
2.3 KiB
Go
78 lines
2.3 KiB
Go
// Package mapper 数据访问层
|
|
package mapper
|
|
|
|
import (
|
|
"server/config"
|
|
"server/modules/user/entity"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type PlatformUserMapper struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewPlatformUserMapper() *PlatformUserMapper {
|
|
return &PlatformUserMapper{db: config.DB}
|
|
}
|
|
|
|
func (m *PlatformUserMapper) baseDB() *gorm.DB {
|
|
return m.db
|
|
}
|
|
|
|
// GetDB 获取数据库实例,默认过滤软删除
|
|
func (m *PlatformUserMapper) GetDB() *gorm.DB {
|
|
return m.baseDB().Where("deleted = 0")
|
|
}
|
|
|
|
// FindAll 分页查询
|
|
func (m *PlatformUserMapper) FindAll(page, size int) ([]entity.PlatformUser, int64, error) {
|
|
var items []entity.PlatformUser
|
|
var total int64
|
|
query := m.GetDB().Model(&entity.PlatformUser{})
|
|
query.Count(&total)
|
|
err := query.Offset((page - 1) * size).Limit(size).Find(&items).Error
|
|
return items, total, err
|
|
}
|
|
|
|
// FindByID 根据 ID 查询
|
|
func (m *PlatformUserMapper) FindByID(id int64) (*entity.PlatformUser, error) {
|
|
var item entity.PlatformUser
|
|
err := m.GetDB().First(&item, "id = ?", id).Error
|
|
return &item, err
|
|
}
|
|
|
|
// FindByPlatformOpenID 根据平台类型与 openid 查询
|
|
func (m *PlatformUserMapper) FindByPlatformOpenID(platformType int8, openid string) (*entity.PlatformUser, error) {
|
|
var item entity.PlatformUser
|
|
err := m.GetDB().First(&item, "platform_type = ? AND platform_openid = ?", platformType, openid).Error
|
|
return &item, err
|
|
}
|
|
|
|
// FindByUserIDAndPlatformType 根据用户ID与平台类型查询
|
|
func (m *PlatformUserMapper) FindByUserIDAndPlatformType(userID int64, platformType int8) (*entity.PlatformUser, error) {
|
|
var item entity.PlatformUser
|
|
err := m.GetDB().First(&item, "user_id = ? AND platform_type = ?", userID, platformType).Error
|
|
return &item, err
|
|
}
|
|
|
|
// Create 创建记录
|
|
func (m *PlatformUserMapper) Create(item *entity.PlatformUser) error {
|
|
return m.baseDB().Create(item).Error
|
|
}
|
|
|
|
// Update 更新记录
|
|
func (m *PlatformUserMapper) Update(item *entity.PlatformUser) error {
|
|
return m.baseDB().Save(item).Error
|
|
}
|
|
|
|
// UpdateFields 更新指定字段
|
|
func (m *PlatformUserMapper) UpdateFields(id int64, fields map[string]interface{}) error {
|
|
return m.baseDB().Model(&entity.PlatformUser{}).Where("id = ?", id).Updates(fields).Error
|
|
}
|
|
|
|
// Delete 逻辑删除
|
|
func (m *PlatformUserMapper) Delete(id int64) error {
|
|
return m.baseDB().Model(&entity.PlatformUser{}).Where("id = ?", id).Update("deleted", 1).Error
|
|
}
|