65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
// Package mapper 数据访问层
|
|
package mapper
|
|
|
|
import (
|
|
"server/config"
|
|
"server/modules/yx/entity"
|
|
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type YxSchoolMajorMapper struct{}
|
|
|
|
func NewYxSchoolMajorMapper() *YxSchoolMajorMapper {
|
|
return &YxSchoolMajorMapper{}
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) FindAll(page, size int) ([]entity.YxSchoolMajor, int64, error) {
|
|
var items []entity.YxSchoolMajor
|
|
var total int64
|
|
config.DB.Model(&entity.YxSchoolMajor{}).Count(&total)
|
|
err := config.DB.Offset((page - 1) * size).Limit(size).Find(&items).Error
|
|
return items, total, err
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) FindByID(id string) (*entity.YxSchoolMajor, error) {
|
|
var item entity.YxSchoolMajor
|
|
err := config.DB.First(&item, "id = ?", id).Error
|
|
return &item, err
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) Create(item *entity.YxSchoolMajor) error {
|
|
return config.DB.Create(item).Error
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) Update(item *entity.YxSchoolMajor) error {
|
|
return config.DB.Save(item).Error
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) UpdateFields(id string, fields map[string]interface{}) error {
|
|
return config.DB.Model(&entity.YxSchoolMajor{}).Where("id = ?", id).Updates(fields).Error
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) Delete(id string) error {
|
|
return config.DB.Delete(&entity.YxSchoolMajor{}, "id = ?", id).Error
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) BatchCreate(items []entity.YxSchoolMajor, batchSize int) error {
|
|
return config.DB.CreateInBatches(items, batchSize).Error
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) BatchUpdate(items []entity.YxSchoolMajor) error {
|
|
return config.DB.Save(items).Error
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) BatchUpsert(items []entity.YxSchoolMajor, updateColumns []string) error {
|
|
return config.DB.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "id"}},
|
|
DoUpdates: clause.AssignmentColumns(updateColumns),
|
|
}).CreateInBatches(items, 100).Error
|
|
}
|
|
|
|
func (m *YxSchoolMajorMapper) BatchDelete(ids []string) error {
|
|
return config.DB.Delete(&entity.YxSchoolMajor{}, "id IN ?", ids).Error
|
|
}
|