dosage_form.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 DosageForm struct {
  14. }
  15. // GetPage 获取DosageForm列表
  16. func (e *DosageForm) GetPage(c *dto.DosageFormPageReq, list *[]models.DosageForm, count *int64, p *actions.DataPermission) error {
  17. var err error
  18. var data models.DosageForm
  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 获取DosageForm对象
  34. func (e *DosageForm) Get(d *dto.DosageFormGetReq, productModel *models.DosageForm, 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 创建DosageForm对象
  48. func (e *DosageForm) Insert(c *dto.DosageFormInsertReq) error {
  49. var err error
  50. var data models.DosageForm
  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 修改DosageForm对象
  71. func (e *DosageForm) Update(c *dto.DosageFormUpdateReq) error {
  72. var productModel = models.DosageForm{}
  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. c.Generate(&productModel)
  82. err = db.DB.Save(&productModel).Error
  83. if err != nil {
  84. logs.Error("db error: %s", err)
  85. return global.UpdateFailedErr
  86. }
  87. return nil
  88. }
  89. // Remove 删除DosageForm
  90. func (e *DosageForm) Remove(d *dto.DosageFormDeleteReq) error {
  91. var err error
  92. var data models.DosageForm
  93. var Count int64
  94. err = db.DB.Table(models.GetMedicineInfoTableName(d.DeptId)).Where("unit_id = ?", d.GetId()).Count(&Count).Error
  95. if err != nil {
  96. logs.Error("Delete error: %s", err)
  97. return global.DeleteFailedErr
  98. }
  99. if Count > 0 {
  100. return errors.New("该剂型已被关联,禁止删除")
  101. }
  102. result := db.DB.Model(&data).Delete(&data, d.GetId())
  103. if err = result.Error; err != nil {
  104. logs.Error("Delete error: %s", err)
  105. return global.DeleteFailedErr
  106. }
  107. if result.RowsAffected == 0 {
  108. return global.DeleteNotFoundOrNoPermissionErr
  109. }
  110. return nil
  111. }