123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package model
- import (
- "errors"
- "gorm.io/gorm"
- "project_management/app/e"
- "project_management/global"
- "project_management/utils"
- "strings"
- )
- type ApplyCap struct {
- utils.BaseModel
- CapId string `gorm:"type:varchar(50);" json:"cap_id" validate:"required"`
- CapName string `gorm:"type:varchar(50);" json:"cap_name" `
- AppId string `gorm:"type:varchar(50);" json:"app_id" validate:"required"`
- Icon string `gorm:"type:varchar(255);" json:"icon"` // 图标
- PcIsShow int `gorm:"type:int;" json:"pc_is_show"` // 是否pc端显示 1 显示 2 不显示
- MobileIsShow int `gorm:"type:int;" json:"mobile_is_show"` // 是否移动端显示 1 显示 2 不显示
- Sort int `gorm:"type:int;" json:"sort"` // 排序
- CapUrl string `gorm:"-" json:"cap_url"`
- }
- // DeleteApplyCAp 删除应用能力
- func (a ApplyCap) DeleteApplyCAp(applycap ApplyCapabilities) e.Rescode {
- //TODO implement me
- tableName := "appcap_" + applycap.AppID
- tx := global.DBLink.Table(tableName).Where("cap_id = ?", applycap.CapID).Delete(ApplyCap{})
- update := global.DBLink.Table(Capabilities{}.TableName()).Where("cap_id = ?", applycap.CapID).Update("count", gorm.Expr("count - ?", 1))
- if tx.Error != nil || update.Error != nil {
- return e.DELETEFAIL
- }
- if tx.RowsAffected > 0 {
- return e.SUCCESS
- }
- return e.DELETEFAIL
- }
- // UpDateApplyCap 更新应用能力
- func (a ApplyCap) UpDateApplyCap(applycap ApplyCap) e.Rescode {
- tableName := "appcap_" + applycap.AppId
- if applycap.CapName != "" {
- first := global.DBLink.Table(tableName).Where("cap_name = ?", applycap.CapName).First(&a)
- if first.RowsAffected > 0 {
- return e.Repeat
- }
- }
- tx := global.DBLink.Table(tableName).Where("cap_id = ?", applycap.CapId).Updates(applycap)
- if tx.Error != nil {
- return e.UPDATEFAIL
- }
- if tx.RowsAffected > 0 {
- return e.SUCCESS
- }
- return e.UPDATEFAIL
- }
- // UpdateApplyCapSort 拖拽排序功能
- func (a ApplyCap) UpdateApplyCapSort(updatedApplyCap ApplyCap) e.Rescode {
- tableName := "appcap_" + updatedApplyCap.AppId
- // 开始事务
- tx := global.DBLink.Begin()
- defer func() {
- if r := recover(); r != nil {
- tx.Rollback()
- }
- }()
- // 尝试在事务中执行更新
- if err := tx.Table(tableName).Where("cap_id = ?", updatedApplyCap.CapId).First(&a).Error; err != nil {
- tx.Rollback()
- return e.UPDATEFAIL
- }
- // 获取旧的排序位置
- oldSort := a.Sort
- // 如果新位置小于旧位置,意味着元素向上移动
- if updatedApplyCap.Sort < oldSort {
- if err := tx.Table(tableName).Where("sort >= ? AND sort < ?", updatedApplyCap.Sort, oldSort).
- Update("sort", gorm.Expr("sort + 1")).Error; err != nil {
- tx.Rollback()
- return e.UPDATEFAIL
- }
- } else if updatedApplyCap.Sort > oldSort { // 否则元素向下移动
- if err := tx.Table(tableName).Where("sort <= ? AND sort > ?", updatedApplyCap.Sort, oldSort).
- Update("sort", gorm.Expr("sort - 1")).Error; err != nil {
- tx.Rollback()
- return e.UPDATEFAIL
- }
- }
- // 更新被拖动元素的排序位置
- a.Sort = updatedApplyCap.Sort
- // 保存更改
- if err := tx.Table(tableName).Save(a).Error; err != nil {
- tx.Rollback()
- return e.UPDATEFAIL
- }
- // 提交事务
- if err := tx.Commit().Error; err != nil {
- return e.UPDATEFAIL
- }
- return e.SUCCESS
- }
- // ApplyCapList 获取应用能力列表
- func (a ApplyCap) ApplyCapList(appid string) ([]ApplyCap, error) {
- //TODO implement me
- tableName := "appcap_" + appid
- var applycap []ApplyCap
- tx := global.DBLink.Table(tableName).Order("sort asc").Find(&applycap)
- if tx.Error != nil {
- if strings.Contains(tx.Error.Error(), "Error 1146 (42S02)") {
- return applycap, errors.New(e.ThereAreNoFeatures.GetMsg())
- }
- }
- if tx.RowsAffected > 0 {
- for i, _ := range applycap {
- var capability Capabilities
- tx := global.DBLink.Model(&Capabilities{}).Where("cap_id = ?", applycap[i].CapId).First(&capability)
- if tx.Error == nil {
- applycap[i].CapUrl = capability.CapUrl
- }
- }
- return applycap, nil
- }
- return applycap, errors.New(e.FINDFAIL.GetMsg())
- }
- // ApplyAddCap 添加应用能力
- func (a ApplyCap) ApplyAddCap(applycap ApplyCapabilities) e.Rescode {
- //TODO implement me
- var caps Capabilities
- tableName := "appcap_" + applycap.AppID
- table := global.DBLink.Migrator().HasTable(tableName)
- if !table {
- global.DBLink.Table(tableName).AutoMigrate(&ApplyCap{})
- }
- tx := global.DBLink.Where("cap_id = ?", applycap.CapID).First(&caps)
- if tx.Error != nil {
- return e.CAPISNOT
- }
- a.CapId = applycap.CapID
- a.CapName = caps.CapName
- a.AppId = applycap.AppID
- a.Icon = caps.ICON
- a.PcIsShow = 1
- a.MobileIsShow = 1
- var maxsort int
- global.DBLink.Table(tableName).Select("MAX(sort)").Scan(&maxsort)
- a.Sort = maxsort + 1
- create := global.DBLink.Table(tableName).Create(&a)
- update := global.DBLink.Table(Capabilities{}.TableName()).Where("cap_id = ?", applycap.CapID).Update("count", gorm.Expr("count + ?", 1))
- if create.Error != nil || update.Error != nil {
- return e.INSERTFAIL
- }
- return e.SUCCESS
- }
|