cloud_disk_menu.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package models
  2. import (
  3. "eta_gn/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. type CloudDiskMenu struct {
  10. MenuId int `orm:"column(menu_id);pk" 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. func (m *CloudDiskMenu) TableName() string {
  20. return "cloud_disk_menu"
  21. }
  22. func (m *CloudDiskMenu) Create() (err error) {
  23. o := orm.NewOrm()
  24. id, err := o.Insert(m)
  25. if err != nil {
  26. return
  27. }
  28. m.MenuId = int(id)
  29. return
  30. }
  31. func (m *CloudDiskMenu) Update(cols []string) (err error) {
  32. o := orm.NewOrm()
  33. _, err = o.Update(m, cols...)
  34. return
  35. }
  36. func (m *CloudDiskMenu) GetItemById(id int) (err error) {
  37. o := orm.NewOrm()
  38. sql := `SELECT * FROM cloud_disk_menu WHERE menu_id = ? LIMIT 1`
  39. err = o.Raw(sql, id).QueryRow(&m)
  40. return
  41. }
  42. func (m *CloudDiskMenu) GetItemByCondition(condition string, pars []interface{}) (err error) {
  43. o := orm.NewOrm()
  44. sql := `SELECT * FROM cloud_disk_menu WHERE 1=1 `
  45. sql += condition
  46. sql += ` LIMIT 1`
  47. err = o.Raw(sql, pars).QueryRow(&m)
  48. return
  49. }
  50. func (m *CloudDiskMenu) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*CloudDiskMenu, err error) {
  51. o := orm.NewOrm()
  52. fields := strings.Join(fieldArr, ",")
  53. if len(fieldArr) == 0 {
  54. fields = `*`
  55. }
  56. order := ``
  57. if orderRule != "" {
  58. order = ` ORDER BY ` + orderRule
  59. }
  60. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  61. _, err = o.Raw(sql, pars).QueryRows(&items)
  62. return
  63. }
  64. type CloudDiskMenuCreateReq struct {
  65. MenuName string `description:"目录名称"`
  66. ParentId int `description:"父级ID"`
  67. }
  68. type CloudDiskMenuRenameReq struct {
  69. MenuId int `description:"目录ID"`
  70. MenuName string `description:"目录名称"`
  71. }
  72. type CloudDiskMenuDeleteReq struct {
  73. MenuId int `description:"目录ID"`
  74. }
  75. // DeleteCloudDiskMenuAndResourcesByMenuIds 根据目录IDs删除目录及文件
  76. func DeleteCloudDiskMenuAndResourcesByMenuIds(menuIds []int) (err error) {
  77. menuLens := len(menuIds)
  78. if menuLens == 0 {
  79. return
  80. }
  81. o := orm.NewOrm()
  82. tx, err := o.Begin()
  83. if err != nil {
  84. return
  85. }
  86. defer func() {
  87. if err != nil {
  88. _ = tx.Rollback()
  89. } else {
  90. _ = tx.Commit()
  91. }
  92. }()
  93. sql := `DELETE FROM cloud_disk_menu WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  94. if _, err = tx.Raw(sql, menuIds).Exec(); err != nil {
  95. return
  96. }
  97. sql = `DELETE FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  98. if _, err = tx.Raw(sql, menuIds).Exec(); err != nil {
  99. return
  100. }
  101. return
  102. }
  103. type CloudDiskMenuTree struct {
  104. *CloudDiskMenu
  105. Children []*CloudDiskMenuTree
  106. }