118 lines
3.0 KiB
Go
118 lines
3.0 KiB
Go
package dao
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"hepa-calc-api/api/model"
|
|
"hepa-calc-api/global"
|
|
)
|
|
|
|
type BaseNationDao struct {
|
|
}
|
|
|
|
// GetBaseNationById 获取数据-id
|
|
func (r *BaseNationDao) GetBaseNationById(id int64) (m *model.BaseNation, err error) {
|
|
err = global.Db.First(&m, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetBaseNationPreloadById 获取数据-加载全部关联-id
|
|
func (r *BaseNationDao) GetBaseNationPreloadById(id int64) (m *model.BaseNation, err error) {
|
|
err = global.Db.Preload(clause.Associations).First(&m, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetBaseNationByAppIden 获取数据-app_iden
|
|
func (r *BaseNationDao) GetBaseNationByAppIden(appIden string) (m *model.BaseNation, err error) {
|
|
err = global.Db.Where("app_iden = ?", appIden).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteBaseNation 删除
|
|
func (r *BaseNationDao) DeleteBaseNation(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.BaseNation{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteBaseNationById 删除-id
|
|
func (r *BaseNationDao) DeleteBaseNationById(tx *gorm.DB, id int64) error {
|
|
if err := tx.Delete(&model.BaseNation{}, id).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditBaseNation 修改
|
|
func (r *BaseNationDao) EditBaseNation(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.BaseNation{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditBaseNationById 修改-id
|
|
func (r *BaseNationDao) EditBaseNationById(tx *gorm.DB, id int64, data interface{}) error {
|
|
err := tx.Model(&model.BaseNation{}).Where("id = ?", id).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetBaseNationList 获取列表
|
|
func (r *BaseNationDao) GetBaseNationList(maps interface{}) (m []*model.BaseNation, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetBaseNationCount 获取数量
|
|
func (r *BaseNationDao) GetBaseNationCount(maps interface{}) (total int64, err error) {
|
|
err = global.Db.Model(&model.BaseNation{}).Where(maps).Count(&total).Error
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
// GetBaseNationListRand 获取列表-随机
|
|
func (r *BaseNationDao) GetBaseNationListRand(maps interface{}, limit int) (m []*model.BaseNation, err error) {
|
|
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddBaseNation 新增
|
|
func (r *BaseNationDao) AddBaseNation(tx *gorm.DB, model *model.BaseNation) (*model.BaseNation, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// GetBaseNation 获取
|
|
func (r *BaseNationDao) GetBaseNation(maps interface{}) (m *model.BaseNation, err error) {
|
|
err = global.Db.Where(maps).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|