applyCap.go 4.8 KB

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