cloud_disk_menu.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package models
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "eta/eta_api/global"
  6. "strings"
  7. "time"
  8. )
  9. type CloudDiskMenu struct {
  10. MenuId int `gorm:"column:menu_id;primaryKey;autoIncrement" description:"目录ID"`
  11. MenuName string `description:"目录名称"`
  12. ParentId int `description:"父级ID"`
  13. Size int64 `description:"目录大小"`
  14. AdminId int `description:"创建人ID"`
  15. AdminName string `description:"创建人名称"`
  16. CreateTime time.Time `description:"创建时间"`
  17. ModifyTime time.Time `description:"修改时间"`
  18. }
  19. type MenuNode struct {
  20. MenuId int `json:"menuId"`
  21. ParentId int `json:"parentId"`
  22. MenuName string
  23. Level int
  24. Children []*MenuNode
  25. }
  26. func (m *CloudDiskMenu) TableName() string {
  27. return "cloud_disk_menu"
  28. }
  29. func (m *CloudDiskMenu) Create() (err error) {
  30. err = global.DEFAULT_DB.Create(m).Error
  31. return
  32. }
  33. func (m *CloudDiskMenu) Update(cols []string) (err error) {
  34. err = global.DEFAULT_DB.Select(cols).Updates(m).Error
  35. return
  36. }
  37. func (m *CloudDiskMenu) GetItemById(id int) (err error) {
  38. sql := `SELECT * FROM cloud_disk_menu WHERE menu_id = ? LIMIT 1`
  39. err = global.DEFAULT_DB.Raw(sql, id).Find(&m).Error
  40. return
  41. }
  42. func (m *CloudDiskMenu) GetItemByCondition(condition string, pars []interface{}) (err error) {
  43. sql := `SELECT * FROM cloud_disk_menu WHERE 1=1 `
  44. sql += condition
  45. sql += ` LIMIT 1`
  46. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&m).Error
  47. return
  48. }
  49. func (m *CloudDiskMenu) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*CloudDiskMenu, err error) {
  50. fields := strings.Join(fieldArr, ",")
  51. if len(fieldArr) == 0 {
  52. fields = `*`
  53. }
  54. order := ``
  55. if orderRule != "" {
  56. order = ` ORDER BY ` + orderRule
  57. }
  58. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  59. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  60. return
  61. }
  62. type CloudDiskMenuCreateReq struct {
  63. MenuName string `description:"目录名称"`
  64. ParentId int `description:"父级ID"`
  65. }
  66. type CloudDiskMenuRenameReq struct {
  67. MenuId int `description:"目录ID"`
  68. MenuName string `description:"目录名称"`
  69. }
  70. type CloudDiskMenuModifyReq struct {
  71. MenuId int `description:"目录ID"`
  72. ParentId int `description:"父级ID"`
  73. MenuName string `description:"目录名称"`
  74. }
  75. type CloudDiskMenuDeleteReq struct {
  76. MenuId int `description:"目录ID"`
  77. }
  78. // DeleteCloudDiskMenuAndResourcesByMenuIds 根据目录IDs删除目录及文件
  79. func DeleteCloudDiskMenuAndResourcesByMenuIds(menuIds []int) (err error) {
  80. menuLens := len(menuIds)
  81. if menuLens == 0 {
  82. return
  83. }
  84. tx := global.DEFAULT_DB.Begin()
  85. defer func() {
  86. if err != nil {
  87. _ = tx.Rollback()
  88. } else {
  89. _ = tx.Commit()
  90. }
  91. }()
  92. sql := `DELETE FROM cloud_disk_menu WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  93. err = tx.Exec(sql, menuIds).Error
  94. if err != nil {
  95. return
  96. }
  97. sql = `DELETE FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  98. err = tx.Exec(sql, menuIds).Error
  99. if err != nil {
  100. return
  101. }
  102. return
  103. }
  104. type CloudDiskMenuTree struct {
  105. *CloudDiskMenu
  106. Children []*CloudDiskMenuTree
  107. }