capabilities.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package model
  2. import (
  3. "project_management/app/e"
  4. "project_management/global"
  5. "project_management/unity"
  6. "project_management/utils"
  7. "strings"
  8. )
  9. type Capabilities struct {
  10. utils.BaseModel
  11. CapName string `gorm:"type:varchar(255);index:idx_name,unique" json:"cap_name"` // 应用名
  12. CpaType string `gorm:"type:varchar(50);" json:"cap_type"` // 应用类型
  13. Image string `gorm:"type:varchar(255);" json:"image"` // 应用图片
  14. ICON string `gorm:"type:varchar(255);" json:"icon"` // 应用图标
  15. CapId string `gorm:"type:varchar(50);unique" json:"cap_id"` // 应用id
  16. CapDescription string `gorm:"type:varchar(255);" json:"cap_description"` // 应用描述
  17. State int `gorm:"type:int;" json:"state"` // 状态 1启用 2禁用
  18. CapUrl string `gorm:"type:varchar(255);" json:"cap_url"` // 应用地址
  19. Price float64 `gorm:"type:decimal(10,5);" json:"price"` //价格
  20. Count int64 `gorm:"type:int;" json:"count"` // 使用数量
  21. IsAdd int `gorm:"type:int;" json:"is_add"` // 是否添加 1已添加
  22. }
  23. type CapabilitiesApply struct {
  24. utils.BaseModel
  25. CapId string `gorm:"type:varchar(50);" json:"cap_id"` // 功能id
  26. AppId string `gorm:"type:varchar(50);" json:"app_id"` // 应用id
  27. }
  28. type CapabilitiesDto struct {
  29. Capabilities
  30. Count int64 `json:"count"` // 使用数量
  31. }
  32. func (c Capabilities) TableName() string {
  33. return "capabilities"
  34. }
  35. // AddCapabilities 添加应用
  36. func (c Capabilities) AddCapabilities(capabilities Capabilities) e.Rescode {
  37. //TODO implement me
  38. capabilities.State = 1
  39. tx := global.DBLink.Table(c.TableName()).Create(&capabilities)
  40. if tx.Error != nil {
  41. if strings.Contains(tx.Error.Error(), "Duplicate entry") {
  42. return e.TheParametersAreRepeated
  43. }
  44. return e.INSERTFAIL
  45. }
  46. if tx.RowsAffected > 0 {
  47. return e.SUCCESS
  48. }
  49. return e.INSERTFAIL
  50. }
  51. // GetCapabilitiesList 获取应用列表
  52. func (c Capabilities) GetCapabilitiesList(params unity.CapQueryPageParams, capabilities Capabilities, queryCond interface{}) (result []Capabilities, total int64, err error) {
  53. //TODO implement me
  54. query := global.DBLink.Table(capabilities.TableName())
  55. if params.Query != "%%" {
  56. query = query.Where(queryCond, params.Query)
  57. }
  58. if params.State != "" {
  59. query = query.Where("state=?", params.State)
  60. }
  61. if err = query.Where("state=?", 1).Count(&total).Error; err != nil {
  62. return nil, 0, err
  63. }
  64. offset := (params.Page - 1) * params.Size
  65. if err = query.Where("state=?", 1).Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  66. return nil, 0, err
  67. }
  68. tablename := "appcap_" + params.AppId
  69. var appcap []ApplyCap
  70. tx := global.DBLink.Table(tablename).Select("cap_id").Find(&appcap)
  71. if tx.Error != nil {
  72. if strings.Contains(tx.Error.Error(), "Error 1146 (42S02)") {
  73. return result, total, nil
  74. }
  75. return nil, 0, tx.Error
  76. }
  77. capIdsMap := make(map[string]bool) // 假设cap_id类型为uint
  78. for _, app := range appcap {
  79. capIdsMap[app.CapId] = true
  80. }
  81. for i, _ := range result {
  82. if _, exists := capIdsMap[result[i].CapId]; exists {
  83. result[i].IsAdd = 1
  84. }
  85. }
  86. return result, total, nil
  87. }
  88. // AdminGetCapabilitiesList 管理员获取应用列表
  89. func (c Capabilities) AdminGetCapabilitiesList(params unity.QueryPageParams, capabilities Capabilities, queryCond interface{}) (result []CapabilitiesDto, total int64, err error) {
  90. //TODO implement me
  91. query := global.DBLink.Table(capabilities.TableName())
  92. if params.Query != "%%" {
  93. query = query.Where(queryCond, params.Query)
  94. }
  95. if params.State != "" {
  96. query = query.Where("state=?", params.State)
  97. }
  98. if err = query.Count(&total).Error; err != nil {
  99. return nil, 0, err
  100. }
  101. offset := (params.Page - 1) * params.Size
  102. if err = query.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  103. return nil, 0, err
  104. }
  105. return result, total, nil
  106. }
  107. // GetCapabilitiesById 根据id获取应用信息
  108. func (c Capabilities) GetCapabilitiesById(id string) (Capabilities, e.Rescode) {
  109. //TODO implement me
  110. tx := global.DBLink.Table(c.TableName()).Select("cap_name", "image", "cap_id", "cap_description", "state", "cap_url", "price").Where("cap_id = ?", id).First(&c)
  111. if tx.Error != nil {
  112. return c, e.FINDFAIL
  113. }
  114. if tx.RowsAffected > 0 {
  115. return c, e.SUCCESS
  116. }
  117. return c, e.FINDFAIL
  118. }
  119. // UpdateCapabilities 更新应用信息
  120. func (c Capabilities) UpdateCapabilities(capabilities Capabilities) e.Rescode {
  121. //TODO implement me
  122. tx := global.DBLink.Table(c.TableName()).Where("cap_id = ?", capabilities.CapId).Updates(&capabilities)
  123. if tx.Error != nil {
  124. return e.UPDATEFAIL
  125. }
  126. if tx.RowsAffected > 0 {
  127. return e.SUCCESS
  128. }
  129. return e.UPDATEFAIL
  130. }
  131. func (c Capabilities) DeleteCapabilities(capid string) e.Rescode {
  132. //TODO implement me
  133. tx := global.DBLink.Table(c.TableName()).Where("cap_id = ?", capid).Delete(&Capabilities{})
  134. if tx.Error != nil {
  135. return e.DELETEFAIL
  136. }
  137. if tx.RowsAffected > 0 {
  138. return e.SUCCESS
  139. }
  140. return e.DELETEFAIL
  141. }
  142. // CapIdISRepeat 检查CapId是否重复
  143. func CapIdISRepeat(id string) bool {
  144. tx := global.DBLink.Where("cap_id = ?", id).First(&Capabilities{})
  145. if tx.RowsAffected > 0 {
  146. return true
  147. }
  148. return false
  149. }