package service import ( cDto "Medical_OAuth/common/dto" "Medical_OAuth/common/global" "errors" "gogs.baozhida.cn/zoie/OAuth-core/service" "gorm.io/gorm" "Medical_OAuth/app/admin/model" "Medical_OAuth/app/admin/service/dto" ) type SysConfig struct { service.Service } // GetPage 获取SysConfig列表 func (e *SysConfig) GetPage(c *dto.SysConfigGetPageReq, list *[]model.SysConfig, count *int64) error { err := e.Orm. Scopes( cDto.MakeCondition(c.GetNeedSearch()), cDto.Paginate(c.GetPageSize(), c.GetPageIndex()), ). Find(list).Limit(-1).Offset(-1). Count(count).Error if err != nil { e.Log.Errorf("Service GetSysConfigPage error: %s", err) return err } return nil } // Get 获取SysConfig对象 func (e *SysConfig) Get(d *dto.SysConfigGetReq, model *model.SysConfig) error { err := e.Orm.First(model, d.GetId()).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.GetNotFoundOrNoPermissionErr } return global.GetFailedErr } return nil } // Insert 创建SysConfig对象 func (e *SysConfig) Insert(c *dto.SysConfigControl) error { var err error var data model.SysConfig c.Generate(&data) err = e.Orm.Create(&data).Error if err != nil { e.Log.Errorf("Service InsertSysConfig error: %s", err) return global.CreateFailedErr } return nil } // Update 修改SysConfig对象 func (e *SysConfig) Update(c *dto.SysConfigControl) error { var err error var confModel = model.SysConfig{} err = e.Orm.First(&confModel, c.GetId()).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return global.UpdateFailedErr } c.Generate(&confModel) err = e.Orm.Save(&confModel).Error if err != nil { e.Log.Errorf("Service UpdateSysConfig error: %s", err) return global.UpdateFailedErr } return nil } // SetSysConfig 修改SysConfig对象 func (e *SysConfig) SetSysConfig(c *[]dto.GetSetSysConfigReq) error { for _, req := range *c { var confModel = model.SysConfig{} err := e.Orm.Where("config_key = ?", req.ConfigKey).First(&confModel).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return global.UpdateFailedErr } } return nil } func (e *SysConfig) GetForSet(c *[]dto.GetSetSysConfigReq) error { var err error var data model.SysConfig err = e.Orm.Model(&data).Find(c).Error if err != nil { e.Log.Errorf("Service GetSysConfigPage error: %s", err) return global.GetFailedErr } return nil } func (e *SysConfig) UpdateForSet(c *[]dto.GetSetSysConfigReq) error { m := *c for _, req := range m { var data model.SysConfig if err := e.Orm.Where("config_key = ?", req.ConfigKey). First(&data).Error; err != nil { e.Log.Errorf("Service GetSysConfigPage error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return global.UpdateFailedErr } if data.ConfigValue != req.ConfigValue { data.ConfigValue = req.ConfigValue if err := e.Orm.Save(&data).Error; err != nil { e.Log.Errorf("Service GetSysConfigPage error: %s", err) return global.UpdateFailedErr } } } return nil } // Remove 删除SysConfig func (e *SysConfig) Remove(d *dto.SysConfigDeleteReq) error { var err error var data model.SysConfig db := e.Orm.Delete(&data, d.Ids) if err = db.Error; err != nil { err = db.Error e.Log.Errorf("Service RemoveSysConfig error: %s", err) return global.DeleteFailedErr } if db.RowsAffected == 0 { return global.DeleteNotFoundOrNoPermissionErr } return nil } // GetWithKey 根据Key获取SysConfig func (e *SysConfig) GetWithKey(c *dto.SysConfigByKeyReq, resp *dto.GetSysConfigByKEYForServiceResp) error { var err error var data model.SysConfig err = e.Orm.Table(data.TableName()).Where("config_key = ?", c.ConfigKey).First(resp).Error if err != nil { e.Log.Errorf("At Service GetSysConfigByKEY error: %s", err) return global.GetFailedErr } return nil } func (e *SysConfig) GetWithKeyList(c *dto.SysConfigGetToSysAppReq, list *[]model.SysConfig) error { var err error err = e.Orm. Scopes( cDto.MakeCondition(c.GetNeedSearch()), ). Find(list).Error if err != nil { e.Log.Errorf("Service GetSysConfigByKey error: %s", err) return global.GetFailedErr } return nil }