123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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
- }
|