package service import ( "Medical_OAuth/common/actions" cDto "Medical_OAuth/common/dto" "Medical_OAuth/common/global" "errors" "gogs.baozhida.cn/zoie/OAuth-core/pkg/utils" "gogs.baozhida.cn/zoie/OAuth-core/service" "gorm.io/gorm" "Medical_OAuth/app/admin/model" "Medical_OAuth/app/admin/service/dto" ) type SysService struct { service.Service } // GetPage 获取SysService列表 func (e *SysService) GetPage(c *dto.SysServicePageReq, list *[]model.SysService, count *int64, p *actions.DataPermission) error { var err error var data model.SysService c.No = c.Name err = e.Orm.Model(&data). Scopes( cDto.MakeCondition(c.GetNeedSearch()), cDto.Paginate(c.GetPageSize(), c.GetPageIndex()), actions.Permission(data.TableName(), p), ). Find(list).Limit(-1).Offset(-1). Count(count).Error if err != nil { e.Log.Errorf("db error: %s \r", err) return global.GetFailedErr } return nil } // Get 获取SysService对象 func (e *SysService) Get(d *dto.SysServiceGetReq, postModel *model.SysService, p *actions.DataPermission) error { err := e.Orm. Scopes(actions.Permission(postModel.TableName(), p)). First(postModel, 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 创建SysService对象 func (e *SysService) Insert(c *dto.SysServiceInsertReq) error { var err error var data model.SysService err = e.Orm.Model(&data).Where("name = ?", c.Name).First(&data).Error if err == nil { e.Log.Errorf("db error: %s", err) return errors.New("服务名称已存在") } var no string for { no = utils.GetRandString(8, "", 0) var i int64 err = e.Orm.Model(&data).Where("no = ?", no).Count(&i).Error if err != nil { continue } if i == 0 { break } } c.Generate(&data) data.No = no err = e.Orm.Create(&data).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.CreateFailedErr } c.Id = data.Id return nil } // Update 修改SysService对象 func (e *SysService) Update(c *dto.SysServiceUpdateReq) error { var postModel = model.SysService{} var data = model.SysService{} err := e.Orm.First(&postModel, 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 } err = e.Orm.Model(&data).Where("name = ?", c.Name).First(&data).Error if err == nil && postModel.Name != c.Name { e.Log.Errorf("db error: %s", err) return errors.New("服务名称已存在") } c.Generate(&postModel) err = e.Orm.Save(&postModel).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.UpdateFailedErr } return nil } // Remove 删除SysService func (e *SysService) Remove(d *dto.SysServiceDeleteReq) error { var err error var data model.SysService db := e.Orm.Model(&data).Delete(&data, d.GetId()) if err = db.Error; err != nil { e.Log.Errorf("Delete error: %s", err) return global.DeleteFailedErr } if db.RowsAffected == 0 { return global.DeleteNotFoundOrNoPermissionErr } return nil } // Remove 删除SysService func (e *SysService) Discovery(d *dto.SysServiceDiscoveryReq) error { var err error var data model.SysService err = e.Orm.Model(&data).Where("name = ?", d.Name).First(&data).Error if err != nil { if err == gorm.ErrRecordNotFound { var no string for { no = utils.GetRandString(8, "", 0) var i int64 err = e.Orm.Model(&data).Where("no = ?", no).Count(&i).Error if err != nil { continue } if i == 0 { break } } d.Generate(&data) data.No = no data.Status = 1 err = e.Orm.Create(&data).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.CreateFailedErr } d.Id = data.Id return nil } e.Log.Errorf("Delete error: %s", err) return global.CreateFailedErr } // 有则更新 d.Generate(&data) err = e.Orm.Save(&data).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.UpdateFailedErr } d.Id = data.Id return nil }