applyCap.go 4.9 KB

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