applyCap.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package model
  2. import (
  3. "errors"
  4. "gorm.io/gorm"
  5. "project_management/app/e"
  6. "project_management/global"
  7. "project_management/utils"
  8. "strings"
  9. )
  10. type ApplyCap struct {
  11. utils.BaseModel
  12. CapId string `gorm:"type:varchar(50);" json:"cap_id" validate:"required"`
  13. CapName string `gorm:"type:varchar(50);" json:"cap_name" `
  14. AppId string `gorm:"type:varchar(50);" json:"app_id" validate:"required"`
  15. PcIsShow int `gorm:"type:int;" json:"pc_is_show"` // 是否pc端显示 1 显示 2 不显示
  16. MobileIsShow int `gorm:"type:int;" json:"mobile_is_show"` // 是否移动端显示 1 显示 2 不显示
  17. Sort int `gorm:"type:int;" json:"sort"` // 排序
  18. }
  19. // DeleteApplyCAp 删除应用能力
  20. func (a ApplyCap) DeleteApplyCAp(applycap ApplyCapabilities) e.Rescode {
  21. //TODO implement me
  22. tableName := "appcap_" + applycap.AppID
  23. tx := global.DBLink.Table(tableName).Where("cap_id = ?", applycap.CapID).Delete(ApplyCap{})
  24. update := global.DBLink.Table(Capabilities{}.TableName()).Where("cap_id = ?", applycap.CapID).Update("count", gorm.Expr("count - ?", 1))
  25. if tx.Error != nil || update.Error != nil {
  26. return e.DELETEFAIL
  27. }
  28. if tx.RowsAffected > 0 {
  29. return e.SUCCESS
  30. }
  31. return e.DELETEFAIL
  32. }
  33. // UpDateApplyCap 更新应用能力
  34. func (a ApplyCap) UpDateApplyCap(applycap ApplyCap) e.Rescode {
  35. tableName := "appcap_" + applycap.AppId
  36. if applycap.CapName != "" {
  37. first := global.DBLink.Table(tableName).Where("cap_name = ?", applycap.CapName).First(&a)
  38. if first.RowsAffected > 0 {
  39. return e.Repeat
  40. }
  41. }
  42. tx := global.DBLink.Table(tableName).Where("cap_id = ?", applycap.CapId).Updates(applycap)
  43. if tx.Error != nil {
  44. return e.UPDATEFAIL
  45. }
  46. if tx.RowsAffected > 0 {
  47. return e.SUCCESS
  48. }
  49. return e.UPDATEFAIL
  50. }
  51. // UpdateApplyCapSort 拖拽排序功能
  52. func (a ApplyCap) UpdateApplyCapSort(updatedApplyCap ApplyCap) e.Rescode {
  53. tableName := "appcap_" + updatedApplyCap.AppId
  54. // 开始事务
  55. tx := global.DBLink.Begin()
  56. defer func() {
  57. if r := recover(); r != nil {
  58. tx.Rollback()
  59. }
  60. }()
  61. // 尝试在事务中执行更新
  62. if err := tx.Table(tableName).Where("cap_id = ?", updatedApplyCap.CapId).First(&a).Error; err != nil {
  63. tx.Rollback()
  64. return e.UPDATEFAIL
  65. }
  66. // 获取旧的排序位置
  67. oldSort := a.Sort
  68. // 如果新位置小于旧位置,意味着元素向上移动
  69. if updatedApplyCap.Sort < oldSort {
  70. if err := tx.Table(tableName).Where("sort >= ? AND sort < ?", updatedApplyCap.Sort, oldSort).
  71. Update("sort", gorm.Expr("sort + 1")).Error; err != nil {
  72. tx.Rollback()
  73. return e.UPDATEFAIL
  74. }
  75. } else if updatedApplyCap.Sort > oldSort { // 否则元素向下移动
  76. if err := tx.Table(tableName).Where("sort <= ? AND sort > ?", updatedApplyCap.Sort, oldSort).
  77. Update("sort", gorm.Expr("sort - 1")).Error; err != nil {
  78. tx.Rollback()
  79. return e.UPDATEFAIL
  80. }
  81. }
  82. // 更新被拖动元素的排序位置
  83. a.Sort = updatedApplyCap.Sort
  84. // 保存更改
  85. if err := tx.Table(tableName).Save(a).Error; err != nil {
  86. tx.Rollback()
  87. return e.UPDATEFAIL
  88. }
  89. // 提交事务
  90. if err := tx.Commit().Error; err != nil {
  91. return e.UPDATEFAIL
  92. }
  93. return e.SUCCESS
  94. }
  95. // ApplyCapList 获取应用能力列表
  96. func (a ApplyCap) ApplyCapList(appid string) ([]ApplyCap, error) {
  97. //TODO implement me
  98. tableName := "appcap_" + appid
  99. var applycap []ApplyCap
  100. tx := global.DBLink.Table(tableName).Order("sort asc").Find(&applycap)
  101. if tx.Error != nil {
  102. if strings.Contains(tx.Error.Error(), "Error 1146 (42S02)") {
  103. return applycap, errors.New(e.ThereAreNoFeatures.GetMsg())
  104. }
  105. }
  106. if tx.RowsAffected > 0 {
  107. return applycap, nil
  108. }
  109. return applycap, errors.New(e.FINDFAIL.GetMsg())
  110. }
  111. // ApplyAddCap 添加应用能力
  112. func (a ApplyCap) ApplyAddCap(applycap ApplyCapabilities) e.Rescode {
  113. //TODO implement me
  114. var caps Capabilities
  115. tableName := "appcap_" + applycap.AppID
  116. table := global.DBLink.Migrator().HasTable(tableName)
  117. if !table {
  118. global.DBLink.Table(tableName).AutoMigrate(&ApplyCap{})
  119. }
  120. tx := global.DBLink.Where("cap_id = ?", applycap.CapID).First(&caps)
  121. if tx.Error != nil {
  122. return e.CAPISNOT
  123. }
  124. a.CapId = applycap.CapID
  125. a.CapName = caps.CapName
  126. a.AppId = applycap.AppID
  127. a.PcIsShow = 1
  128. a.MobileIsShow = 1
  129. var maxsort int
  130. global.DBLink.Table(tableName).Select("MAX(sort)").Scan(&maxsort)
  131. a.Sort = maxsort + 1
  132. create := global.DBLink.Table(tableName).Create(&a)
  133. update := global.DBLink.Table(Capabilities{}.TableName()).Where("cap_id = ?", applycap.CapID).Update("count", gorm.Expr("count + ?", 1))
  134. if create.Error != nil || update.Error != nil {
  135. return e.INSERTFAIL
  136. }
  137. return e.SUCCESS
  138. }