spec.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package services
  2. import (
  3. "Medical_ERP/common/actions"
  4. cDto "Medical_ERP/common/dto"
  5. "Medical_ERP/common/global"
  6. db "Medical_ERP/common/initialize"
  7. "errors"
  8. "github.com/beego/beego/v2/core/logs"
  9. "gorm.io/gorm"
  10. "Medical_ERP/dto"
  11. "Medical_ERP/models"
  12. )
  13. type Spec struct {
  14. }
  15. // GetPage 获取Spec列表
  16. func (e *Spec) GetPage(c *dto.SpecPageReq, list *[]models.Spec, count *int64, p *actions.DataPermission) error {
  17. var err error
  18. var data models.Spec
  19. err = db.DB.Model(&data).
  20. Scopes(
  21. cDto.MakeCondition(c.GetNeedSearch()),
  22. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  23. actions.Permission(data.TableName(), p),
  24. ).
  25. Find(list).Limit(-1).Offset(-1).
  26. Count(count).Error
  27. if err != nil {
  28. logs.Error("db error: %s ", err)
  29. return global.GetFailedErr
  30. }
  31. return nil
  32. }
  33. // Get 获取Spec对象
  34. func (e *Spec) Get(d *dto.SpecGetReq, productModel *models.Spec, p *actions.DataPermission) error {
  35. err := db.DB.
  36. Scopes(actions.Permission(productModel.TableName(), p)).
  37. First(productModel, d.GetId()).Error
  38. if err != nil {
  39. logs.Error("db error: %s", err)
  40. if errors.Is(err, gorm.ErrRecordNotFound) {
  41. return global.GetNotFoundOrNoPermissionErr
  42. }
  43. return global.GetFailedErr
  44. }
  45. return nil
  46. }
  47. // Insert 创建Spec对象
  48. func (e *Spec) Insert(c *dto.SpecInsertReq) error {
  49. var err error
  50. var data models.Spec
  51. err = db.DB.Where("name = ? AND dept_id = ?", c.Name, c.DeptId).First(&data).Error
  52. if err != nil {
  53. // 没有则创建
  54. if errors.Is(err, gorm.ErrRecordNotFound) {
  55. c.Generate(&data)
  56. err = db.DB.Create(&data).Error
  57. if err != nil {
  58. logs.Error("db error: %s", err)
  59. return global.CreateFailedErr
  60. }
  61. c.Id = data.Id
  62. return nil
  63. }
  64. logs.Error("db error: %s", err)
  65. return global.CreateFailedErr
  66. }
  67. c.Id = data.Id
  68. return nil
  69. }
  70. // Update 修改Spec对象
  71. func (e *Spec) Update(c *dto.SpecUpdateReq) error {
  72. var productModel = models.Spec{}
  73. err := db.DB.First(&productModel, c.GetId()).Error
  74. if err != nil {
  75. logs.Error("db error: %s", err)
  76. if errors.Is(err, gorm.ErrRecordNotFound) {
  77. return global.UpdateNotFoundOrNoPermissionErr
  78. }
  79. return global.UpdateFailedErr
  80. }
  81. if c.DeptId != productModel.DeptId {
  82. return global.UpdateNotFoundOrNoPermissionErr
  83. }
  84. c.Generate(&productModel)
  85. err = db.DB.Save(&productModel).Error
  86. if err != nil {
  87. logs.Error("db error: %s", err)
  88. return global.UpdateFailedErr
  89. }
  90. return nil
  91. }
  92. // Remove 删除Spec
  93. func (e *Spec) Remove(d *dto.SpecDeleteReq) error {
  94. var err error
  95. var data models.Spec
  96. var Count int64
  97. err = db.DB.Table(models.GetMedicineInfoTableName(d.DeptId)).Where("unit_id = ?", d.GetId()).Count(&Count).Error
  98. if err != nil {
  99. logs.Error("Delete error: %s", err)
  100. return global.DeleteFailedErr
  101. }
  102. if Count > 0 {
  103. return errors.New("该规格已被关联,禁止删除")
  104. }
  105. result := db.DB.Model(&data).Delete(&data, d.GetId())
  106. if err = result.Error; err != nil {
  107. logs.Error("Delete error: %s", err)
  108. return global.DeleteFailedErr
  109. }
  110. if result.RowsAffected == 0 {
  111. return global.DeleteNotFoundOrNoPermissionErr
  112. }
  113. return nil
  114. }